Skip to content

Commit e6f17c6

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 117f244 commit e6f17c6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Bootstraps/Symfony.php

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

55
use Stack\Builder;
6+
use Symfony\Component\HttpKernel\KernelInterface;
67

78
/**
89
* A default bootstrap for the Symfony framework
@@ -36,6 +37,8 @@ public function getApplication()
3637
$app = new \AppKernel($this->appenv, false);
3738
$app->loadClassCache();
3839

40+
$this->preloadServices($app);
41+
3942
return $app;
4043
}
4144

@@ -64,4 +67,23 @@ protected function includeAutoload()
6467
require_once $symfonyAutoload;
6568
}
6669
}
70+
71+
/**
72+
* Instantiate all services in the Dependency Injection Container.
73+
*
74+
* Stateless (shared) services in the Dependency Injection Container will be
75+
* instantiated once by the first Request that require them, and then their
76+
* instance will be reused in all other Requests.
77+
* This also allows to "warm up" part of autoloading.
78+
*
79+
* @param KernelInterface $kernel
80+
*/
81+
protected function preloadServices(KernelInterface $kernel)
82+
{
83+
$kernel->boot();
84+
$container = $kernel->getContainer();
85+
foreach ($container->getServiceIds() as $serviceId) {
86+
$container->get($serviceId);
87+
}
88+
}
6789
}

0 commit comments

Comments
 (0)