Skip to content

Commit 968c96e

Browse files
committed
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.
1 parent 55f846c commit 968c96e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Bootstraps/Symfony.php

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPPM\Bootstraps;
44

55
use Symfony\Component\HttpFoundation\Request;
6+
use Symfony\Component\HttpKernel\KernelInterface;
67

78
/**
89
* A default bootstrap for the Symfony framework
@@ -59,6 +60,7 @@ public function getApplication()
5960
$request = new Request();
6061
$request->setMethod(Request::METHOD_HEAD);
6162
$app->handle($request);
63+
$this->preloadServices($app);
6264

6365
return $app;
6466
}
@@ -98,4 +100,23 @@ public function postHandle($app)
98100
$app->getContainer()->get('profiler')->enable();
99101
}
100102
}
103+
104+
/**
105+
* Instantiate all services in the Dependency Injection Container.
106+
*
107+
* Stateless (shared) services in the Dependency Injection Container will be
108+
* instantiated once by the first Request that require them, and then their
109+
* instance will be reused in all other Requests.
110+
* This also allows to "warm up" part of autoloading.
111+
*
112+
* @param KernelInterface $kernel
113+
*/
114+
protected function preloadServices(KernelInterface $kernel)
115+
{
116+
$kernel->boot();
117+
$container = $kernel->getContainer();
118+
foreach ($container->getServiceIds() as $serviceId) {
119+
$container->get($serviceId);
120+
}
121+
}
101122
}

0 commit comments

Comments
 (0)