Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel; Chris@0: Chris@0: use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator; Chris@0: use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; Chris@17: use Symfony\Component\ClassLoader\ClassCollectionLoader; Chris@17: use Symfony\Component\Config\ConfigCache; Chris@17: use Symfony\Component\Config\Loader\DelegatingLoader; Chris@17: use Symfony\Component\Config\Loader\LoaderResolver; Chris@14: use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; Chris@14: use Symfony\Component\DependencyInjection\Compiler\PassConfig; Chris@17: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: use Symfony\Component\DependencyInjection\ContainerInterface; Chris@0: use Symfony\Component\DependencyInjection\Dumper\PhpDumper; Chris@17: use Symfony\Component\DependencyInjection\Loader\ClosureLoader; Chris@17: use Symfony\Component\DependencyInjection\Loader\DirectoryLoader; Chris@17: use Symfony\Component\DependencyInjection\Loader\GlobFileLoader; Chris@17: use Symfony\Component\DependencyInjection\Loader\IniFileLoader; Chris@17: use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; Chris@0: use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; Chris@0: use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; Chris@14: use Symfony\Component\Filesystem\Filesystem; Chris@0: use Symfony\Component\HttpFoundation\Request; Chris@0: use Symfony\Component\HttpFoundation\Response; Chris@0: use Symfony\Component\HttpKernel\Bundle\BundleInterface; Chris@0: use Symfony\Component\HttpKernel\Config\EnvParametersResource; Chris@0: use Symfony\Component\HttpKernel\Config\FileLocator; Chris@17: use Symfony\Component\HttpKernel\DependencyInjection\AddAnnotatedClassesToCachePass; Chris@0: use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass; Chris@0: Chris@0: /** Chris@0: * The Kernel is the heart of the Symfony system. Chris@0: * Chris@0: * It manages an environment made of bundles. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@14: abstract class Kernel implements KernelInterface, RebootableInterface, TerminableInterface Chris@0: { Chris@0: /** Chris@0: * @var BundleInterface[] Chris@0: */ Chris@17: protected $bundles = []; Chris@0: Chris@0: protected $bundleMap; Chris@0: protected $container; Chris@0: protected $rootDir; Chris@0: protected $environment; Chris@0: protected $debug; Chris@0: protected $booted = false; Chris@0: protected $name; Chris@0: protected $startTime; Chris@0: protected $loadClassCache; Chris@0: Chris@14: private $projectDir; Chris@14: private $warmupDir; Chris@14: private $requestStackSize = 0; Chris@14: private $resetServices = false; Chris@14: Chris@18: const VERSION = '3.4.27'; Chris@18: const VERSION_ID = 30427; Chris@0: const MAJOR_VERSION = 3; Chris@14: const MINOR_VERSION = 4; Chris@18: const RELEASE_VERSION = 27; Chris@0: const EXTRA_VERSION = ''; Chris@0: Chris@14: const END_OF_MAINTENANCE = '11/2020'; Chris@14: const END_OF_LIFE = '11/2021'; Chris@0: Chris@0: /** Chris@0: * @param string $environment The environment Chris@0: * @param bool $debug Whether to enable debugging or not Chris@0: */ Chris@0: public function __construct($environment, $debug) Chris@0: { Chris@0: $this->environment = $environment; Chris@0: $this->debug = (bool) $debug; Chris@0: $this->rootDir = $this->getRootDir(); Chris@0: $this->name = $this->getName(); Chris@0: } Chris@0: Chris@0: public function __clone() Chris@0: { Chris@0: $this->booted = false; Chris@0: $this->container = null; Chris@14: $this->requestStackSize = 0; Chris@14: $this->resetServices = false; Chris@0: } Chris@0: Chris@0: /** Chris@17: * {@inheritdoc} Chris@0: */ Chris@0: public function boot() Chris@0: { Chris@0: if (true === $this->booted) { Chris@14: if (!$this->requestStackSize && $this->resetServices) { Chris@14: if ($this->container->has('services_resetter')) { Chris@14: $this->container->get('services_resetter')->reset(); Chris@14: } Chris@14: $this->resetServices = false; Chris@16: if ($this->debug) { Chris@16: $this->startTime = microtime(true); Chris@16: } Chris@14: } Chris@14: Chris@0: return; Chris@0: } Chris@16: if ($this->debug) { Chris@16: $this->startTime = microtime(true); Chris@16: } Chris@14: if ($this->debug && !isset($_ENV['SHELL_VERBOSITY']) && !isset($_SERVER['SHELL_VERBOSITY'])) { Chris@14: putenv('SHELL_VERBOSITY=3'); Chris@14: $_ENV['SHELL_VERBOSITY'] = 3; Chris@14: $_SERVER['SHELL_VERBOSITY'] = 3; Chris@14: } Chris@0: Chris@0: if ($this->loadClassCache) { Chris@0: $this->doLoadClassCache($this->loadClassCache[0], $this->loadClassCache[1]); Chris@0: } Chris@0: Chris@0: // init bundles Chris@0: $this->initializeBundles(); Chris@0: Chris@0: // init container Chris@0: $this->initializeContainer(); Chris@0: Chris@0: foreach ($this->getBundles() as $bundle) { Chris@0: $bundle->setContainer($this->container); Chris@0: $bundle->boot(); Chris@0: } Chris@0: Chris@0: $this->booted = true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@14: public function reboot($warmupDir) Chris@14: { Chris@14: $this->shutdown(); Chris@14: $this->warmupDir = $warmupDir; Chris@14: $this->boot(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@0: public function terminate(Request $request, Response $response) Chris@0: { Chris@0: if (false === $this->booted) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if ($this->getHttpKernel() instanceof TerminableInterface) { Chris@0: $this->getHttpKernel()->terminate($request, $response); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function shutdown() Chris@0: { Chris@0: if (false === $this->booted) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $this->booted = false; Chris@0: Chris@0: foreach ($this->getBundles() as $bundle) { Chris@0: $bundle->shutdown(); Chris@0: $bundle->setContainer(null); Chris@0: } Chris@0: Chris@0: $this->container = null; Chris@14: $this->requestStackSize = 0; Chris@14: $this->resetServices = false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) Chris@0: { Chris@14: $this->boot(); Chris@14: ++$this->requestStackSize; Chris@14: $this->resetServices = true; Chris@14: Chris@14: try { Chris@14: return $this->getHttpKernel()->handle($request, $type, $catch); Chris@14: } finally { Chris@14: --$this->requestStackSize; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets a HTTP kernel from the container. Chris@0: * Chris@0: * @return HttpKernel Chris@0: */ Chris@0: protected function getHttpKernel() Chris@0: { Chris@0: return $this->container->get('http_kernel'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getBundles() Chris@0: { Chris@0: return $this->bundles; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@14: public function getBundle($name, $first = true/*, $noDeprecation = false */) Chris@0: { Chris@14: $noDeprecation = false; Chris@17: if (\func_num_args() >= 3) { Chris@14: $noDeprecation = func_get_arg(2); Chris@14: } Chris@14: Chris@14: if (!$first && !$noDeprecation) { Chris@17: @trigger_error(sprintf('Passing "false" as the second argument to "%s()" is deprecated as of 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED); Chris@14: } Chris@14: Chris@0: if (!isset($this->bundleMap[$name])) { Chris@17: throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() method of your %s.php file?', $name, \get_class($this))); Chris@0: } Chris@0: Chris@0: if (true === $first) { Chris@0: return $this->bundleMap[$name][0]; Chris@0: } Chris@0: Chris@0: return $this->bundleMap[$name]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle Chris@0: */ Chris@0: public function locateResource($name, $dir = null, $first = true) Chris@0: { Chris@0: if ('@' !== $name[0]) { Chris@0: throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name)); Chris@0: } Chris@0: Chris@0: if (false !== strpos($name, '..')) { Chris@0: throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name)); Chris@0: } Chris@0: Chris@0: $bundleName = substr($name, 1); Chris@0: $path = ''; Chris@0: if (false !== strpos($bundleName, '/')) { Chris@0: list($bundleName, $path) = explode('/', $bundleName, 2); Chris@0: } Chris@0: Chris@0: $isResource = 0 === strpos($path, 'Resources') && null !== $dir; Chris@0: $overridePath = substr($path, 9); Chris@0: $resourceBundle = null; Chris@14: $bundles = $this->getBundle($bundleName, false, true); Chris@17: $files = []; Chris@0: Chris@0: foreach ($bundles as $bundle) { Chris@0: if ($isResource && file_exists($file = $dir.'/'.$bundle->getName().$overridePath)) { Chris@0: if (null !== $resourceBundle) { Chris@17: throw new \RuntimeException(sprintf('"%s" resource is hidden by a resource from the "%s" derived bundle. Create a "%s" file to override the bundle resource.', $file, $resourceBundle, $dir.'/'.$bundles[0]->getName().$overridePath)); Chris@0: } Chris@0: Chris@0: if ($first) { Chris@0: return $file; Chris@0: } Chris@0: $files[] = $file; Chris@0: } Chris@0: Chris@0: if (file_exists($file = $bundle->getPath().'/'.$path)) { Chris@0: if ($first && !$isResource) { Chris@0: return $file; Chris@0: } Chris@0: $files[] = $file; Chris@0: $resourceBundle = $bundle->getName(); Chris@0: } Chris@0: } Chris@0: Chris@17: if (\count($files) > 0) { Chris@0: return $first && $isResource ? $files[0] : $files; Chris@0: } Chris@0: Chris@0: throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: if (null === $this->name) { Chris@0: $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir)); Chris@0: if (ctype_digit($this->name[0])) { Chris@0: $this->name = '_'.$this->name; Chris@0: } Chris@0: } Chris@0: Chris@0: return $this->name; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getEnvironment() Chris@0: { Chris@0: return $this->environment; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isDebug() Chris@0: { Chris@0: return $this->debug; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getRootDir() Chris@0: { Chris@0: if (null === $this->rootDir) { Chris@0: $r = new \ReflectionObject($this); Chris@17: $this->rootDir = \dirname($r->getFileName()); Chris@0: } Chris@0: Chris@0: return $this->rootDir; Chris@0: } Chris@0: Chris@0: /** Chris@14: * Gets the application root dir (path of the project's composer file). Chris@14: * Chris@14: * @return string The project root dir Chris@14: */ Chris@14: public function getProjectDir() Chris@14: { Chris@14: if (null === $this->projectDir) { Chris@14: $r = new \ReflectionObject($this); Chris@17: $dir = $rootDir = \dirname($r->getFileName()); Chris@14: while (!file_exists($dir.'/composer.json')) { Chris@17: if ($dir === \dirname($dir)) { Chris@14: return $this->projectDir = $rootDir; Chris@14: } Chris@17: $dir = \dirname($dir); Chris@14: } Chris@14: $this->projectDir = $dir; Chris@14: } Chris@14: Chris@14: return $this->projectDir; Chris@14: } Chris@14: Chris@14: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getContainer() Chris@0: { Chris@0: return $this->container; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads the PHP class cache. Chris@0: * Chris@0: * This methods only registers the fact that you want to load the cache classes. Chris@0: * The cache will actually only be loaded when the Kernel is booted. Chris@0: * Chris@0: * That optimization is mainly useful when using the HttpCache class in which Chris@0: * case the class cache is not loaded if the Response is in the cache. Chris@0: * Chris@0: * @param string $name The cache name prefix Chris@0: * @param string $extension File extension of the resulting file Chris@14: * Chris@14: * @deprecated since version 3.3, to be removed in 4.0. The class cache is not needed anymore when using PHP 7.0. Chris@0: */ Chris@0: public function loadClassCache($name = 'classes', $extension = '.php') Chris@0: { Chris@14: if (\PHP_VERSION_ID >= 70000) { Chris@14: @trigger_error(__METHOD__.'() is deprecated since Symfony 3.3, to be removed in 4.0.', E_USER_DEPRECATED); Chris@14: } Chris@14: Chris@17: $this->loadClassCache = [$name, $extension]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @internal Chris@14: * Chris@14: * @deprecated since version 3.3, to be removed in 4.0. Chris@0: */ Chris@0: public function setClassCache(array $classes) Chris@0: { Chris@14: if (\PHP_VERSION_ID >= 70000) { Chris@14: @trigger_error(__METHOD__.'() is deprecated since Symfony 3.3, to be removed in 4.0.', E_USER_DEPRECATED); Chris@14: } Chris@14: Chris@14: file_put_contents(($this->warmupDir ?: $this->getCacheDir()).'/classes.map', sprintf('warmupDir ?: $this->getCacheDir()).'/annotations.map', sprintf('debug ? $this->startTime : -INF; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCacheDir() Chris@0: { Chris@0: return $this->rootDir.'/cache/'.$this->environment; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getLogDir() Chris@0: { Chris@0: return $this->rootDir.'/logs'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCharset() Chris@0: { Chris@0: return 'UTF-8'; Chris@0: } Chris@0: Chris@14: /** Chris@14: * @deprecated since version 3.3, to be removed in 4.0. Chris@14: */ Chris@0: protected function doLoadClassCache($name, $extension) Chris@0: { Chris@14: if (\PHP_VERSION_ID >= 70000) { Chris@14: @trigger_error(__METHOD__.'() is deprecated since Symfony 3.3, to be removed in 4.0.', E_USER_DEPRECATED); Chris@14: } Chris@14: $cacheDir = $this->warmupDir ?: $this->getCacheDir(); Chris@14: Chris@14: if (!$this->booted && is_file($cacheDir.'/classes.map')) { Chris@14: ClassCollectionLoader::load(include($cacheDir.'/classes.map'), $cacheDir, $name, $this->debug, false, $extension); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes the data structures related to the bundle management. Chris@0: * Chris@0: * - the bundles property maps a bundle name to the bundle instance, Chris@0: * - the bundleMap property maps a bundle name to the bundle inheritance hierarchy (most derived bundle first). Chris@0: * Chris@0: * @throws \LogicException if two bundles share a common name Chris@0: * @throws \LogicException if a bundle tries to extend a non-registered bundle Chris@0: * @throws \LogicException if a bundle tries to extend itself Chris@0: * @throws \LogicException if two bundles extend the same ancestor Chris@0: */ Chris@0: protected function initializeBundles() Chris@0: { Chris@0: // init bundles Chris@17: $this->bundles = []; Chris@17: $topMostBundles = []; Chris@17: $directChildren = []; Chris@0: Chris@0: foreach ($this->registerBundles() as $bundle) { Chris@0: $name = $bundle->getName(); Chris@0: if (isset($this->bundles[$name])) { Chris@0: throw new \LogicException(sprintf('Trying to register two bundles with the same name "%s"', $name)); Chris@0: } Chris@0: $this->bundles[$name] = $bundle; Chris@0: Chris@0: if ($parentName = $bundle->getParent()) { Chris@14: @trigger_error('Bundle inheritance is deprecated as of 3.4 and will be removed in 4.0.', E_USER_DEPRECATED); Chris@14: Chris@0: if (isset($directChildren[$parentName])) { Chris@0: throw new \LogicException(sprintf('Bundle "%s" is directly extended by two bundles "%s" and "%s".', $parentName, $name, $directChildren[$parentName])); Chris@0: } Chris@0: if ($parentName == $name) { Chris@0: throw new \LogicException(sprintf('Bundle "%s" can not extend itself.', $name)); Chris@0: } Chris@0: $directChildren[$parentName] = $name; Chris@0: } else { Chris@0: $topMostBundles[$name] = $bundle; Chris@0: } Chris@0: } Chris@0: Chris@0: // look for orphans Chris@17: if (!empty($directChildren) && \count($diff = array_diff_key($directChildren, $this->bundles))) { Chris@0: $diff = array_keys($diff); Chris@0: Chris@0: throw new \LogicException(sprintf('Bundle "%s" extends bundle "%s", which is not registered.', $directChildren[$diff[0]], $diff[0])); Chris@0: } Chris@0: Chris@0: // inheritance Chris@17: $this->bundleMap = []; Chris@0: foreach ($topMostBundles as $name => $bundle) { Chris@17: $bundleMap = [$bundle]; Chris@17: $hierarchy = [$name]; Chris@0: Chris@0: while (isset($directChildren[$name])) { Chris@0: $name = $directChildren[$name]; Chris@0: array_unshift($bundleMap, $this->bundles[$name]); Chris@0: $hierarchy[] = $name; Chris@0: } Chris@0: Chris@0: foreach ($hierarchy as $hierarchyBundle) { Chris@0: $this->bundleMap[$hierarchyBundle] = $bundleMap; Chris@0: array_pop($bundleMap); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@14: * The extension point similar to the Bundle::build() method. Chris@14: * Chris@14: * Use this method to register compiler passes and manipulate the container during the building process. Chris@14: */ Chris@14: protected function build(ContainerBuilder $container) Chris@14: { Chris@14: } Chris@14: Chris@14: /** Chris@0: * Gets the container class. Chris@0: * Chris@0: * @return string The container class Chris@0: */ Chris@0: protected function getContainerClass() Chris@0: { Chris@0: return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the container's base class. Chris@0: * Chris@0: * All names except Container must be fully qualified. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: protected function getContainerBaseClass() Chris@0: { Chris@0: return 'Container'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes the service container. Chris@0: * Chris@0: * The cached version of the service container is used when fresh, otherwise the Chris@0: * container is built. Chris@0: */ Chris@0: protected function initializeContainer() Chris@0: { Chris@0: $class = $this->getContainerClass(); Chris@14: $cacheDir = $this->warmupDir ?: $this->getCacheDir(); Chris@14: $cache = new ConfigCache($cacheDir.'/'.$class.'.php', $this->debug); Chris@14: $oldContainer = null; Chris@14: if ($fresh = $cache->isFresh()) { Chris@14: // Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors Chris@14: $errorLevel = error_reporting(\E_ALL ^ \E_WARNING); Chris@14: $fresh = $oldContainer = false; Chris@14: try { Chris@16: if (file_exists($cache->getPath()) && \is_object($this->container = include $cache->getPath())) { Chris@14: $this->container->set('kernel', $this); Chris@14: $oldContainer = $this->container; Chris@14: $fresh = true; Chris@14: } Chris@14: } catch (\Throwable $e) { Chris@14: } catch (\Exception $e) { Chris@14: } finally { Chris@14: error_reporting($errorLevel); Chris@14: } Chris@14: } Chris@14: Chris@14: if ($fresh) { Chris@14: return; Chris@14: } Chris@14: Chris@14: if ($this->debug) { Chris@17: $collectedLogs = []; Chris@17: $previousHandler = \defined('PHPUNIT_COMPOSER_INSTALL'); Chris@14: $previousHandler = $previousHandler ?: set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) { Chris@14: if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) { Chris@14: return $previousHandler ? $previousHandler($type, $message, $file, $line) : false; Chris@14: } Chris@14: Chris@14: if (isset($collectedLogs[$message])) { Chris@14: ++$collectedLogs[$message]['count']; Chris@14: Chris@14: return; Chris@14: } Chris@14: Chris@14: $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); Chris@14: // Clean the trace by removing first frames added by the error handler itself. Chris@14: for ($i = 0; isset($backtrace[$i]); ++$i) { Chris@14: if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) { Chris@17: $backtrace = \array_slice($backtrace, 1 + $i); Chris@14: break; Chris@14: } Chris@14: } Chris@14: Chris@17: $collectedLogs[$message] = [ Chris@14: 'type' => $type, Chris@14: 'message' => $message, Chris@14: 'file' => $file, Chris@14: 'line' => $line, Chris@14: 'trace' => $backtrace, Chris@14: 'count' => 1, Chris@17: ]; Chris@14: }); Chris@14: } Chris@14: Chris@14: try { Chris@14: $container = null; Chris@0: $container = $this->buildContainer(); Chris@0: $container->compile(); Chris@14: } finally { Chris@14: if ($this->debug && true !== $previousHandler) { Chris@14: restore_error_handler(); Chris@0: Chris@14: file_put_contents($cacheDir.'/'.$class.'Deprecations.log', serialize(array_values($collectedLogs))); Chris@14: file_put_contents($cacheDir.'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : ''); Chris@14: } Chris@0: } Chris@0: Chris@16: if (null === $oldContainer && file_exists($cache->getPath())) { Chris@14: $errorLevel = error_reporting(\E_ALL ^ \E_WARNING); Chris@14: try { Chris@14: $oldContainer = include $cache->getPath(); Chris@14: } catch (\Throwable $e) { Chris@14: } catch (\Exception $e) { Chris@14: } finally { Chris@14: error_reporting($errorLevel); Chris@14: } Chris@14: } Chris@17: $oldContainer = \is_object($oldContainer) ? new \ReflectionClass($oldContainer) : false; Chris@0: Chris@14: $this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass()); Chris@14: $this->container = require $cache->getPath(); Chris@0: $this->container->set('kernel', $this); Chris@0: Chris@17: if ($oldContainer && \get_class($this->container) !== $oldContainer->name) { Chris@14: // Because concurrent requests might still be using them, Chris@14: // old container files are not removed immediately, Chris@14: // but on a next dump of the container. Chris@17: static $legacyContainers = []; Chris@17: $oldContainerDir = \dirname($oldContainer->getFileName()); Chris@16: $legacyContainers[$oldContainerDir.'.legacy'] = true; Chris@17: foreach (glob(\dirname($oldContainerDir).\DIRECTORY_SEPARATOR.'*.legacy') as $legacyContainer) { Chris@16: if (!isset($legacyContainers[$legacyContainer]) && @unlink($legacyContainer)) { Chris@14: (new Filesystem())->remove(substr($legacyContainer, 0, -7)); Chris@14: } Chris@14: } Chris@14: Chris@14: touch($oldContainerDir.'.legacy'); Chris@14: } Chris@14: Chris@14: if ($this->container->has('cache_warmer')) { Chris@0: $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir')); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the kernel parameters. Chris@0: * Chris@0: * @return array An array of kernel parameters Chris@0: */ Chris@0: protected function getKernelParameters() Chris@0: { Chris@17: $bundles = []; Chris@17: $bundlesMetadata = []; Chris@0: Chris@0: foreach ($this->bundles as $name => $bundle) { Chris@17: $bundles[$name] = \get_class($bundle); Chris@17: $bundlesMetadata[$name] = [ Chris@0: 'parent' => $bundle->getParent(), Chris@0: 'path' => $bundle->getPath(), Chris@0: 'namespace' => $bundle->getNamespace(), Chris@17: ]; Chris@0: } Chris@0: Chris@0: return array_merge( Chris@17: [ Chris@0: 'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir, Chris@14: 'kernel.project_dir' => realpath($this->getProjectDir()) ?: $this->getProjectDir(), Chris@0: 'kernel.environment' => $this->environment, Chris@0: 'kernel.debug' => $this->debug, Chris@0: 'kernel.name' => $this->name, Chris@14: 'kernel.cache_dir' => realpath($cacheDir = $this->warmupDir ?: $this->getCacheDir()) ?: $cacheDir, Chris@0: 'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(), Chris@0: 'kernel.bundles' => $bundles, Chris@0: 'kernel.bundles_metadata' => $bundlesMetadata, Chris@0: 'kernel.charset' => $this->getCharset(), Chris@0: 'kernel.container_class' => $this->getContainerClass(), Chris@17: ], Chris@14: $this->getEnvParameters(false) Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the environment parameters. Chris@0: * Chris@0: * Only the parameters starting with "SYMFONY__" are considered. Chris@0: * Chris@0: * @return array An array of parameters Chris@14: * Chris@14: * @deprecated since version 3.3, to be removed in 4.0 Chris@0: */ Chris@0: protected function getEnvParameters() Chris@0: { Chris@17: if (0 === \func_num_args() || func_get_arg(0)) { Chris@17: @trigger_error(sprintf('The "%s()" method is deprecated as of 3.3 and will be removed in 4.0. Use the %%env()%% syntax to get the value of any environment variable from configuration files instead.', __METHOD__), E_USER_DEPRECATED); Chris@14: } Chris@14: Chris@17: $parameters = []; Chris@0: foreach ($_SERVER as $key => $value) { Chris@0: if (0 === strpos($key, 'SYMFONY__')) { Chris@14: @trigger_error(sprintf('The support of special environment variables that start with SYMFONY__ (such as "%s") is deprecated as of 3.3 and will be removed in 4.0. Use the %%env()%% syntax instead to get the value of environment variables in configuration files.', $key), E_USER_DEPRECATED); Chris@0: $parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value; Chris@0: } Chris@0: } Chris@0: Chris@0: return $parameters; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds the service container. Chris@0: * Chris@0: * @return ContainerBuilder The compiled service container Chris@0: * Chris@0: * @throws \RuntimeException Chris@0: */ Chris@0: protected function buildContainer() Chris@0: { Chris@17: foreach (['cache' => $this->warmupDir ?: $this->getCacheDir(), 'logs' => $this->getLogDir()] as $name => $dir) { Chris@0: if (!is_dir($dir)) { Chris@0: if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) { Chris@0: throw new \RuntimeException(sprintf("Unable to create the %s directory (%s)\n", $name, $dir)); Chris@0: } Chris@0: } elseif (!is_writable($dir)) { Chris@0: throw new \RuntimeException(sprintf("Unable to write in the %s directory (%s)\n", $name, $dir)); Chris@0: } Chris@0: } Chris@0: Chris@0: $container = $this->getContainerBuilder(); Chris@0: $container->addObjectResource($this); Chris@0: $this->prepareContainer($container); Chris@0: Chris@0: if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) { Chris@0: $container->merge($cont); Chris@0: } Chris@0: Chris@14: $container->addCompilerPass(new AddAnnotatedClassesToCachePass($this)); Chris@0: $container->addResource(new EnvParametersResource('SYMFONY__')); Chris@0: Chris@0: return $container; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares the ContainerBuilder before it is compiled. Chris@0: */ Chris@0: protected function prepareContainer(ContainerBuilder $container) Chris@0: { Chris@17: $extensions = []; Chris@0: foreach ($this->bundles as $bundle) { Chris@0: if ($extension = $bundle->getContainerExtension()) { Chris@0: $container->registerExtension($extension); Chris@0: } Chris@0: Chris@0: if ($this->debug) { Chris@0: $container->addObjectResource($bundle); Chris@0: } Chris@0: } Chris@14: Chris@0: foreach ($this->bundles as $bundle) { Chris@0: $bundle->build($container); Chris@0: } Chris@0: Chris@14: $this->build($container); Chris@14: Chris@14: foreach ($container->getExtensions() as $extension) { Chris@14: $extensions[] = $extension->getAlias(); Chris@14: } Chris@14: Chris@0: // ensure these extensions are implicitly loaded Chris@0: $container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets a new ContainerBuilder instance used to build the service container. Chris@0: * Chris@0: * @return ContainerBuilder Chris@0: */ Chris@0: protected function getContainerBuilder() Chris@0: { Chris@0: $container = new ContainerBuilder(); Chris@0: $container->getParameterBag()->add($this->getKernelParameters()); Chris@0: Chris@14: if ($this instanceof CompilerPassInterface) { Chris@14: $container->addCompilerPass($this, PassConfig::TYPE_BEFORE_OPTIMIZATION, -10000); Chris@14: } Chris@0: if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) { Chris@0: $container->setProxyInstantiator(new RuntimeInstantiator()); Chris@0: } Chris@0: Chris@0: return $container; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Dumps the service container to PHP code in the cache. Chris@0: * Chris@0: * @param ConfigCache $cache The config cache Chris@0: * @param ContainerBuilder $container The service container Chris@0: * @param string $class The name of the class to generate Chris@0: * @param string $baseClass The name of the container's base class Chris@0: */ Chris@0: protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class, $baseClass) Chris@0: { Chris@0: // cache the container Chris@0: $dumper = new PhpDumper($container); Chris@0: Chris@0: if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper')) { Chris@14: $dumper->setProxyDumper(new ProxyDumper()); Chris@0: } Chris@0: Chris@17: $content = $dumper->dump([ Chris@14: 'class' => $class, Chris@14: 'base_class' => $baseClass, Chris@14: 'file' => $cache->getPath(), Chris@14: 'as_files' => true, Chris@14: 'debug' => $this->debug, Chris@14: 'inline_class_loader_parameter' => \PHP_VERSION_ID >= 70000 && !$this->loadClassCache && !class_exists(ClassCollectionLoader::class, false) ? 'container.dumper.inline_class_loader' : null, Chris@14: 'build_time' => $container->hasParameter('kernel.container_build_time') ? $container->getParameter('kernel.container_build_time') : time(), Chris@17: ]); Chris@0: Chris@14: $rootCode = array_pop($content); Chris@17: $dir = \dirname($cache->getPath()).'/'; Chris@14: $fs = new Filesystem(); Chris@14: Chris@14: foreach ($content as $file => $code) { Chris@14: $fs->dumpFile($dir.$file, $code); Chris@14: @chmod($dir.$file, 0666 & ~umask()); Chris@14: } Chris@17: $legacyFile = \dirname($dir.$file).'.legacy'; Chris@17: if (file_exists($legacyFile)) { Chris@17: @unlink($legacyFile); Chris@17: } Chris@14: Chris@14: $cache->write($rootCode, $container->getResources()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a loader for the container. Chris@0: * Chris@0: * @return DelegatingLoader The loader Chris@0: */ Chris@0: protected function getContainerLoader(ContainerInterface $container) Chris@0: { Chris@0: $locator = new FileLocator($this); Chris@17: $resolver = new LoaderResolver([ Chris@0: new XmlFileLoader($container, $locator), Chris@0: new YamlFileLoader($container, $locator), Chris@0: new IniFileLoader($container, $locator), Chris@0: new PhpFileLoader($container, $locator), Chris@14: new GlobFileLoader($container, $locator), Chris@0: new DirectoryLoader($container, $locator), Chris@0: new ClosureLoader($container), Chris@17: ]); Chris@0: Chris@0: return new DelegatingLoader($resolver); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes comments from a PHP source string. Chris@0: * Chris@0: * We don't use the PHP php_strip_whitespace() function Chris@0: * as we want the content to be readable and well-formatted. Chris@0: * Chris@0: * @param string $source A PHP string Chris@0: * Chris@0: * @return string The PHP string with the comments removed Chris@0: */ Chris@0: public static function stripComments($source) Chris@0: { Chris@17: if (!\function_exists('token_get_all')) { Chris@0: return $source; Chris@0: } Chris@0: Chris@0: $rawChunk = ''; Chris@0: $output = ''; Chris@0: $tokens = token_get_all($source); Chris@0: $ignoreSpace = false; Chris@0: for ($i = 0; isset($tokens[$i]); ++$i) { Chris@0: $token = $tokens[$i]; Chris@0: if (!isset($token[1]) || 'b"' === $token) { Chris@0: $rawChunk .= $token; Chris@0: } elseif (T_START_HEREDOC === $token[0]) { Chris@0: $output .= $rawChunk.$token[1]; Chris@0: do { Chris@0: $token = $tokens[++$i]; Chris@0: $output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token; Chris@14: } while (T_END_HEREDOC !== $token[0]); Chris@0: $rawChunk = ''; Chris@0: } elseif (T_WHITESPACE === $token[0]) { Chris@0: if ($ignoreSpace) { Chris@0: $ignoreSpace = false; Chris@0: Chris@0: continue; Chris@0: } Chris@0: Chris@0: // replace multiple new lines with a single newline Chris@17: $rawChunk .= preg_replace(['/\n{2,}/S'], "\n", $token[1]); Chris@17: } elseif (\in_array($token[0], [T_COMMENT, T_DOC_COMMENT])) { Chris@0: $ignoreSpace = true; Chris@0: } else { Chris@0: $rawChunk .= $token[1]; Chris@0: Chris@0: // The PHP-open tag already has a new-line Chris@0: if (T_OPEN_TAG === $token[0]) { Chris@0: $ignoreSpace = true; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $output .= $rawChunk; Chris@0: Chris@12: if (\PHP_VERSION_ID >= 70000) { Chris@0: // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 Chris@0: unset($tokens, $rawChunk); Chris@0: gc_mem_caches(); Chris@0: } Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: public function serialize() Chris@0: { Chris@17: return serialize([$this->environment, $this->debug]); Chris@0: } Chris@0: Chris@0: public function unserialize($data) Chris@0: { Chris@14: if (\PHP_VERSION_ID >= 70000) { Chris@17: list($environment, $debug) = unserialize($data, ['allowed_classes' => false]); Chris@14: } else { Chris@14: list($environment, $debug) = unserialize($data); Chris@14: } Chris@0: Chris@0: $this->__construct($environment, $debug); Chris@0: } Chris@0: }