From f98a7a1d9467344d7b1ac55f082cbda5b68a96c9 Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Thu, 3 Nov 2016 12:54:54 +0100 Subject: [PATCH] Set up kernel file & bootstrap application if needed --- src/ServiceContainer/SymfonyExtension.php | 56 ++++++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index ceaa0db..c51e75c 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -123,9 +123,11 @@ final class SymfonyExtension implements Extension $config['debug'], )); $definition->addMethodCall('boot'); + $definition->setFile($this->getKernelFile($container->getParameter('paths.base'), $config['path'])); + $container->setDefinition(self::KERNEL_ID, $definition); - $container->setParameter(self::KERNEL_ID . '.path', $config['path']); - $container->setParameter(self::KERNEL_ID . '.bootstrap', $config['bootstrap']); + + $this->requireKernelBootstrapFile($container->getParameter('paths.base'), $config['bootstrap']); } /** @@ -199,4 +201,54 @@ final class SymfonyExtension implements Extension new Reference(self::DRIVER_KERNEL_ID) )); } + + /** + * @param string $basePath + * @param string $kernelPath + * + * @return string|null + */ + private function getKernelFile($basePath, $kernelPath) + { + $possibleFiles = [ + sprintf('%s/%s', $basePath, $kernelPath), + $kernelPath, + ]; + + foreach ($possibleFiles as $possibleFile) { + if (file_exists($possibleFile)) { + return $possibleFile; + } + } + + return null; + } + + /** + * @param string $basePath + * @param string|null $bootstrapPath + * + * @throws \DomainException + */ + private function requireKernelBootstrapFile($basePath, $bootstrapPath) + { + if (null === $bootstrapPath) { + return; + } + + $possiblePaths = [ + sprintf('%s/%s', $basePath, $bootstrapPath), + $bootstrapPath, + ]; + + foreach ($possiblePaths as $possiblePath) { + if (file_exists($possiblePath)) { + require_once $possiblePath; + + return; + } + } + + throw new \DomainException('Could not load bootstrap file.'); + } }