From 968c96e428248ed85e8694d64620acf84ca7d25f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Faugeron?= Date: Fri, 18 Mar 2016 09:25:19 +0000 Subject: [PATCH] Preload services Instantiate all services in the Dependency Injection Container. Stateless (shared) services in the Dependency Injection Container will be instantiated once by the first Request that require them, and then their instance will be reused in all other Requests. This also allows to "warm up" part of autoloading. --- Bootstraps/Symfony.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Bootstraps/Symfony.php b/Bootstraps/Symfony.php index 6d53392..e47bb3a 100644 --- a/Bootstraps/Symfony.php +++ b/Bootstraps/Symfony.php @@ -3,6 +3,7 @@ namespace PHPPM\Bootstraps; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\KernelInterface; /** * A default bootstrap for the Symfony framework @@ -59,6 +60,7 @@ public function getApplication() $request = new Request(); $request->setMethod(Request::METHOD_HEAD); $app->handle($request); + $this->preloadServices($app); return $app; } @@ -98,4 +100,23 @@ public function postHandle($app) $app->getContainer()->get('profiler')->enable(); } } + + /** + * Instantiate all services in the Dependency Injection Container. + * + * Stateless (shared) services in the Dependency Injection Container will be + * instantiated once by the first Request that require them, and then their + * instance will be reused in all other Requests. + * This also allows to "warm up" part of autoloading. + * + * @param KernelInterface $kernel + */ + protected function preloadServices(KernelInterface $kernel) + { + $kernel->boot(); + $container = $kernel->getContainer(); + foreach ($container->getServiceIds() as $serviceId) { + $container->get($serviceId); + } + } }