diff --git a/composer.json b/composer.json index 7f4cac4..a269c00 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ } ], "require": { - "link0/profiler": "~1.0" + "link0/profiler": "^1.0.0" }, "require-dev": { "symfony/framework-bundle": "~2", diff --git a/composer.lock b/composer.lock index 539fc78..7b05158 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "7765098bf8928f76b576ba2dbbd19643", + "hash": "7e8796586559eecdf8f5157dfebc6d0f", "packages": [ { "name": "league/flysystem", diff --git a/src/Link0/ProfilerBundle/EventListener/ProfilingEventListener.php b/src/Link0/ProfilerBundle/EventListener/ProfilingEventListener.php index b20fa2f..27f83f9 100644 --- a/src/Link0/ProfilerBundle/EventListener/ProfilingEventListener.php +++ b/src/Link0/ProfilerBundle/EventListener/ProfilingEventListener.php @@ -8,88 +8,65 @@ namespace Link0\ProfilerBundle\EventListener; use Link0\Profiler\PersistenceHandler; -use Link0\Profiler\Profiler; -use Symfony\Component\Console\Event\ConsoleTerminateEvent; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; -use Symfony\Component\Console\Event\ConsoleCommandEvent; -use Symfony\Component\HttpKernel\TerminableInterface; +use Link0\ProfilerBundle\Service\ProfilingService; /** * Class ProfilingEventListener * * Hooks into the symfony event system, and starts and stops the profiler on certain events * - * Start: - * - console.command - * - kernel.request - * Stop: - * - console.terminate - * - kernel.terminate + * You can configure this event listener in your services.yml in the following format + * + * services: + * kernel.listener.link0profilerlistener: + * class: Link0\ProfilerBundle\EventListener\ProfilingEventListener + * param: [@profiling_service] + * tags: + * - { name: kernel.event_listener, event: console.command, method: start } + * - { name: kernel.event_listener, event: kernel.request, method: start } + * - { name: kernel.event_listener, event: console.terminate, method: stop } + * - { name: kernel.event_listener, event: kernel.terminate, method: stop } * * @package Link0\ProfilerBundle\EventListener */ -class ProfilingEventListener +final class ProfilingEventListener { /** * @var \Link0\Profiler\Profiler */ - private $profiler; - - /** - * A persistence handler should be fed, since null will disable persisting profiles - * - * @param PersistenceHandler $handler - */ - public function __construct(PersistenceHandler $handler = null) - { - $this->profiler = new Profiler($handler); - } - - /** - * @return Profiler - */ - public function getProfiler() - { - return $this->profiler; - } + private $profilingService; /** - * @event console.command - * @param ConsoleCommandEvent $event + * @param ProfilingService $profilingService */ - public function onConsoleCommand(ConsoleCommandEvent $event) + public function __construct(ProfilingService $profilingService) { - $this->getProfiler()->start(); + $this->profilingService = $profilingService; } /** - * @event kernel.request - * @param GetResponseEvent $event + * @return ProfilingService */ - public function onKernelRequest(GetResponseEvent $event) + public function getProfilingService() { - if (!$event->isMasterRequest()) { - return; - } - - $this->getProfiler()->start(); + return $this->profilingService; } /** - * @event console.terminate - * @param ConsoleTerminateEvent $event + * Starts the profiler */ - public function onConsoleTerminate(ConsoleTerminateEvent $event) + public function start() { - $this->getProfiler()->stop(); + $this->getProfilingService()->start(); } /** - * @event kernel.terminate - * @param TerminableInterface $event + * Stops the profiler + * + * @return \Link0\Profiler\Profile */ - public function onKernelTerminate(TerminableInterface $event) + public function stop() { - $this->getProfiler()->stop(); + return $this->getProfilingService()->stop(); } } \ No newline at end of file diff --git a/src/Link0/ProfilerBundle/Resources/config/services.yml b/src/Link0/ProfilerBundle/Resources/config/services.yml index 49eb497..8b0bb6e 100644 --- a/src/Link0/ProfilerBundle/Resources/config/services.yml +++ b/src/Link0/ProfilerBundle/Resources/config/services.yml @@ -1,5 +1,11 @@ services: kernel.listener.link0profilerlistener: class: Link0\ProfilerBundle\EventListener\ProfilingEventListener + param: [@profiling_service] tags: - - { name: kernel.event_listener, event: console.command, method: onConsoleCommand } + - { name: kernel.event_listener, event: console.command, method: start } + - { name: kernel.event_listener, event: kernel.request, method: start } + - { name: kernel.event_listener, event: console.terminate, method: stop } + - { name: kernel.event_listener, event: kernel.terminate, method: stop } + profiling_service: + class: Link0\ProfilerBundle\Service\ProfilingService \ No newline at end of file diff --git a/src/Link0/ProfilerBundle/Service/ProfilingService.php b/src/Link0/ProfilerBundle/Service/ProfilingService.php new file mode 100644 index 0000000..20054cb --- /dev/null +++ b/src/Link0/ProfilerBundle/Service/ProfilingService.php @@ -0,0 +1,52 @@ + + */ +namespace Link0\ProfilerBundle\Service; + +use Link0\Profiler\PersistenceService; + +/** + * Class ProfilingService + * + * @package Link0\ProfilerBundle\Service + */ +final class ProfilingService +{ + /** + * @var \Link0\Profiler\PersistenceService + */ + private $persistenceService; + + /** + * @var \Link0\Profiler\Profiler + */ + private $profiler; + + /** + * @param PersistenceService $persistenceService + */ + public function __construct(PersistenceService $persistenceService = null) + { + $this->persistenceService = $persistenceService; + } + + /** + * Starts the profiler + */ + public function start() + { + $this->profiler->start(); + } + + /** + * @return \Link0\Profiler\Profile + */ + public function stop() + { + return $this->profiler->stop(); + } +}