Skip to content

Preload services #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Bootstraps/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPPM\Bootstraps;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* A default bootstrap for the Symfony framework
Expand Down Expand Up @@ -59,6 +60,7 @@ public function getApplication()
$request = new Request();
$request->setMethod(Request::METHOD_HEAD);
$app->handle($request);
$this->preloadServices($app);

return $app;
}
Expand Down Expand Up @@ -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);
}
}
}