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@0: use Symfony\Component\DependencyInjection\ContainerInterface; Chris@0: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: use Symfony\Component\DependencyInjection\Dumper\PhpDumper; Chris@0: use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; Chris@0: use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; Chris@0: use Symfony\Component\DependencyInjection\Loader\IniFileLoader; Chris@0: use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; Chris@0: use Symfony\Component\DependencyInjection\Loader\DirectoryLoader; Chris@0: use Symfony\Component\DependencyInjection\Loader\ClosureLoader; 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@0: use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass; Chris@0: use Symfony\Component\HttpKernel\DependencyInjection\AddClassesToCachePass; Chris@0: use Symfony\Component\Config\Loader\LoaderResolver; Chris@0: use Symfony\Component\Config\Loader\DelegatingLoader; Chris@0: use Symfony\Component\Config\ConfigCache; Chris@0: use Symfony\Component\ClassLoader\ClassCollectionLoader; 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@0: abstract class Kernel implements KernelInterface, TerminableInterface Chris@0: { Chris@0: /** Chris@0: * @var BundleInterface[] Chris@0: */ Chris@0: protected $bundles = array(); 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@0: const VERSION = '3.2.8'; Chris@0: const VERSION_ID = 30208; Chris@0: const MAJOR_VERSION = 3; Chris@0: const MINOR_VERSION = 2; Chris@0: const RELEASE_VERSION = 8; Chris@0: const EXTRA_VERSION = ''; Chris@0: Chris@0: const END_OF_MAINTENANCE = '07/2017'; Chris@0: const END_OF_LIFE = '01/2018'; Chris@0: Chris@0: /** Chris@0: * Constructor. 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: if ($this->debug) { Chris@0: $this->startTime = microtime(true); Chris@0: } Chris@0: } Chris@0: Chris@0: public function __clone() Chris@0: { Chris@0: if ($this->debug) { Chris@0: $this->startTime = microtime(true); Chris@0: } Chris@0: Chris@0: $this->booted = false; Chris@0: $this->container = null; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Boots the current kernel. Chris@0: */ Chris@0: public function boot() Chris@0: { Chris@0: if (true === $this->booted) { Chris@0: return; Chris@0: } 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@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@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@0: if (false === $this->booted) { Chris@0: $this->boot(); Chris@0: } Chris@0: Chris@0: return $this->getHttpKernel()->handle($request, $type, $catch); 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@0: public function getBundle($name, $first = true) Chris@0: { Chris@0: if (!isset($this->bundleMap[$name])) { Chris@0: 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@0: $bundles = $this->getBundle($bundleName, false); Chris@0: $files = array(); 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@0: 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.', Chris@0: $file, Chris@0: $resourceBundle, Chris@0: $dir.'/'.$bundles[0]->getName().$overridePath Chris@0: )); 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@0: 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@0: $this->rootDir = dirname($r->getFileName()); Chris@0: } Chris@0: Chris@0: return $this->rootDir; Chris@0: } Chris@0: Chris@0: /** 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@0: */ Chris@0: public function loadClassCache($name = 'classes', $extension = '.php') Chris@0: { Chris@0: $this->loadClassCache = array($name, $extension); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @internal Chris@0: */ Chris@0: public function setClassCache(array $classes) Chris@0: { Chris@0: file_put_contents($this->getCacheDir().'/classes.map', sprintf('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@0: protected function doLoadClassCache($name, $extension) Chris@0: { Chris@0: if (!$this->booted && is_file($this->getCacheDir().'/classes.map')) { Chris@0: ClassCollectionLoader::load(include($this->getCacheDir().'/classes.map'), $this->getCacheDir(), $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@0: $this->bundles = array(); Chris@0: $topMostBundles = array(); Chris@0: $directChildren = array(); 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@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@0: 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@0: $this->bundleMap = array(); Chris@0: foreach ($topMostBundles as $name => $bundle) { Chris@0: $bundleMap = array($bundle); Chris@0: $hierarchy = array($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@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@0: $cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug); Chris@0: $fresh = true; Chris@0: if (!$cache->isFresh()) { Chris@0: $container = $this->buildContainer(); Chris@0: $container->compile(); Chris@0: $this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass()); Chris@0: Chris@0: $fresh = false; Chris@0: } Chris@0: Chris@0: require_once $cache->getPath(); Chris@0: Chris@0: $this->container = new $class(); Chris@0: $this->container->set('kernel', $this); Chris@0: Chris@0: if (!$fresh && $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@0: $bundles = array(); Chris@0: $bundlesMetadata = array(); Chris@0: Chris@0: foreach ($this->bundles as $name => $bundle) { Chris@0: $bundles[$name] = get_class($bundle); Chris@0: $bundlesMetadata[$name] = array( Chris@0: 'parent' => $bundle->getParent(), Chris@0: 'path' => $bundle->getPath(), Chris@0: 'namespace' => $bundle->getNamespace(), Chris@0: ); Chris@0: } Chris@0: Chris@0: return array_merge( Chris@0: array( Chris@0: 'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir, Chris@0: 'kernel.environment' => $this->environment, Chris@0: 'kernel.debug' => $this->debug, Chris@0: 'kernel.name' => $this->name, Chris@0: 'kernel.cache_dir' => realpath($this->getCacheDir()) ?: $this->getCacheDir(), 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@0: ), Chris@0: $this->getEnvParameters() 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@0: */ Chris@0: protected function getEnvParameters() Chris@0: { Chris@0: $parameters = array(); Chris@0: foreach ($_SERVER as $key => $value) { Chris@0: if (0 === strpos($key, 'SYMFONY__')) { 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@0: foreach (array('cache' => $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@0: $container->addCompilerPass(new AddClassesToCachePass($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: * @param ContainerBuilder $container A ContainerBuilder instance Chris@0: */ Chris@0: protected function prepareContainer(ContainerBuilder $container) Chris@0: { Chris@0: $extensions = array(); Chris@0: foreach ($this->bundles as $bundle) { Chris@0: if ($extension = $bundle->getContainerExtension()) { Chris@0: $container->registerExtension($extension); Chris@0: $extensions[] = $extension->getAlias(); Chris@0: } Chris@0: Chris@0: if ($this->debug) { Chris@0: $container->addObjectResource($bundle); Chris@0: } Chris@0: } Chris@0: foreach ($this->bundles as $bundle) { Chris@0: $bundle->build($container); Chris@0: } Chris@0: 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@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@0: $dumper->setProxyDumper(new ProxyDumper(md5($cache->getPath()))); Chris@0: } Chris@0: Chris@0: $content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => $cache->getPath(), 'debug' => $this->debug)); Chris@0: Chris@0: $cache->write($content, $container->getResources()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a loader for the container. Chris@0: * Chris@0: * @param ContainerInterface $container The service 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@0: $resolver = new LoaderResolver(array( 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@0: new DirectoryLoader($container, $locator), Chris@0: new ClosureLoader($container), Chris@0: )); 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@0: 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@0: } while ($token[0] !== T_END_HEREDOC); 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@0: $rawChunk .= preg_replace(array('/\n{2,}/S'), "\n", $token[1]); Chris@0: } elseif (in_array($token[0], array(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@0: 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@0: return serialize(array($this->environment, $this->debug)); Chris@0: } Chris@0: Chris@0: public function unserialize($data) Chris@0: { Chris@0: list($environment, $debug) = unserialize($data); Chris@0: Chris@0: $this->__construct($environment, $debug); Chris@0: } Chris@0: }