options = $options; $this->serviceLocator = $serviceLocator; } /** * {@inheritdoc} */ public function attach(EventManagerInterface $events, $priority = 1) { $this->listeners[] = $events->attach( MvcEvent::EVENT_FINISH, [$this, 'onFinish'], Profiler::PRIORITY_PROFILER ); } /** * MvcEvent::EVENT_FINISH event callback * * @param MvcEvent $event * @throws ServiceNotFoundException */ public function onFinish(MvcEvent $event) { $strict = $this->options->isStrict(); $collectors = $this->options->getCollectors(); $report = $this->serviceLocator->get(Report::class); $profiler = $this->serviceLocator->get(Profiler::class); $profiler->setErrorMode($strict); foreach ($collectors as $name => $collector) { if ($this->serviceLocator->has($collector)) { $profiler->addCollector($this->serviceLocator->get($collector)); continue; } $error = sprintf('Unable to fetch or create an instance for %s.', $collector); if ($strict === true) { throw new ServiceNotFoundException($error); } $report->addError($error); } $profiler->collect($event); } }