Skip to content

Commit 8e22844

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 8e2554e commit 8e22844

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Bootstraps/Symfony.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22

33
namespace PHPPM\Bootstraps;
4+
5+
use Stack\Builder;
46
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Component\HttpKernel\KernelInterface;
58

69
/**
710
* A default bootstrap for the Symfony framework
@@ -47,6 +50,7 @@ public function getApplication()
4750
$request = new Request();
4851
$request->setMethod(Request::METHOD_HEAD);
4952
$app->handle($request);
53+
$this->preloadServices($app);
5054

5155
return $app;
5256
}
@@ -68,4 +72,23 @@ protected function includeAutoload()
6872
require_once $symfonyAutoload;
6973
}
7074
}
75+
76+
/**
77+
* Instantiate all services in the Dependency Injection Container.
78+
*
79+
* Stateless (shared) services in the Dependency Injection Container will be
80+
* instantiated once by the first Request that require them, and then their
81+
* instance will be reused in all other Requests.
82+
* This also allows to "warm up" part of autoloading.
83+
*
84+
* @param KernelInterface $kernel
85+
*/
86+
protected function preloadServices(KernelInterface $kernel)
87+
{
88+
$kernel->boot();
89+
$container = $kernel->getContainer();
90+
foreach ($container->getServiceIds() as $serviceId) {
91+
$container->get($serviceId);
92+
}
93+
}
7194
}

0 commit comments

Comments
 (0)