[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\HttpKernel; 13 14 use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator; 15 use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; 16 use Symfony\Component\ClassLoader\ClassCollectionLoader; 17 use Symfony\Component\Config\ConfigCache; 18 use Symfony\Component\Config\Loader\DelegatingLoader; 19 use Symfony\Component\Config\Loader\LoaderResolver; 20 use Symfony\Component\DependencyInjection\ContainerBuilder; 21 use Symfony\Component\DependencyInjection\ContainerInterface; 22 use Symfony\Component\DependencyInjection\Dumper\PhpDumper; 23 use Symfony\Component\DependencyInjection\Loader\ClosureLoader; 24 use Symfony\Component\DependencyInjection\Loader\DirectoryLoader; 25 use Symfony\Component\DependencyInjection\Loader\IniFileLoader; 26 use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; 27 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; 28 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; 29 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; 30 use Symfony\Component\HttpFoundation\Request; 31 use Symfony\Component\HttpFoundation\Response; 32 use Symfony\Component\HttpKernel\Bundle\BundleInterface; 33 use Symfony\Component\HttpKernel\Config\EnvParametersResource; 34 use Symfony\Component\HttpKernel\Config\FileLocator; 35 use Symfony\Component\HttpKernel\DependencyInjection\AddClassesToCachePass; 36 use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass; 37 38 /** 39 * The Kernel is the heart of the Symfony system. 40 * 41 * It manages an environment made of bundles. 42 * 43 * @author Fabien Potencier <fabien@symfony.com> 44 */ 45 abstract class Kernel implements KernelInterface, TerminableInterface 46 { 47 /** 48 * @var BundleInterface[] 49 */ 50 protected $bundles = array(); 51 52 protected $bundleMap; 53 protected $container; 54 protected $rootDir; 55 protected $environment; 56 protected $debug; 57 protected $booted = false; 58 protected $name; 59 protected $startTime; 60 protected $loadClassCache; 61 62 const VERSION = '2.8.52'; 63 const VERSION_ID = 20852; 64 const MAJOR_VERSION = 2; 65 const MINOR_VERSION = 8; 66 const RELEASE_VERSION = 52; 67 const EXTRA_VERSION = ''; 68 69 const END_OF_MAINTENANCE = '11/2018'; 70 const END_OF_LIFE = '11/2019'; 71 72 /** 73 * @param string $environment The environment 74 * @param bool $debug Whether to enable debugging or not 75 */ 76 public function __construct($environment, $debug) 77 { 78 $this->environment = $environment; 79 $this->debug = (bool) $debug; 80 $this->rootDir = $this->getRootDir(); 81 $this->name = $this->getName(); 82 83 if ($this->debug) { 84 $this->startTime = microtime(true); 85 } 86 87 $defClass = new \ReflectionMethod($this, 'init'); 88 $defClass = $defClass->getDeclaringClass()->name; 89 90 if (__CLASS__ !== $defClass) { 91 @trigger_error(sprintf('Calling the %s::init() method is deprecated since Symfony 2.3 and will be removed in 3.0. Move your logic to the constructor method instead.', $defClass), E_USER_DEPRECATED); 92 $this->init(); 93 } 94 } 95 96 /** 97 * @deprecated since version 2.3, to be removed in 3.0. Move your logic in the constructor instead. 98 */ 99 public function init() 100 { 101 @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.3 and will be removed in 3.0. Move your logic to the constructor method instead.', E_USER_DEPRECATED); 102 } 103 104 public function __clone() 105 { 106 if ($this->debug) { 107 $this->startTime = microtime(true); 108 } 109 110 $this->booted = false; 111 $this->container = null; 112 } 113 114 /** 115 * {@inheritdoc} 116 */ 117 public function boot() 118 { 119 if (true === $this->booted) { 120 return; 121 } 122 123 if ($this->loadClassCache) { 124 $this->doLoadClassCache($this->loadClassCache[0], $this->loadClassCache[1]); 125 } 126 127 // init bundles 128 $this->initializeBundles(); 129 130 // init container 131 $this->initializeContainer(); 132 133 foreach ($this->getBundles() as $bundle) { 134 $bundle->setContainer($this->container); 135 $bundle->boot(); 136 } 137 138 $this->booted = true; 139 } 140 141 /** 142 * {@inheritdoc} 143 */ 144 public function terminate(Request $request, Response $response) 145 { 146 if (false === $this->booted) { 147 return; 148 } 149 150 if ($this->getHttpKernel() instanceof TerminableInterface) { 151 $this->getHttpKernel()->terminate($request, $response); 152 } 153 } 154 155 /** 156 * {@inheritdoc} 157 */ 158 public function shutdown() 159 { 160 if (false === $this->booted) { 161 return; 162 } 163 164 $this->booted = false; 165 166 foreach ($this->getBundles() as $bundle) { 167 $bundle->shutdown(); 168 $bundle->setContainer(null); 169 } 170 171 $this->container = null; 172 } 173 174 /** 175 * {@inheritdoc} 176 */ 177 public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) 178 { 179 if (false === $this->booted) { 180 $this->boot(); 181 } 182 183 return $this->getHttpKernel()->handle($request, $type, $catch); 184 } 185 186 /** 187 * Gets a HTTP kernel from the container. 188 * 189 * @return HttpKernel 190 */ 191 protected function getHttpKernel() 192 { 193 return $this->container->get('http_kernel'); 194 } 195 196 /** 197 * {@inheritdoc} 198 */ 199 public function getBundles() 200 { 201 return $this->bundles; 202 } 203 204 /** 205 * {@inheritdoc} 206 * 207 * @deprecated since version 2.6, to be removed in 3.0. 208 */ 209 public function isClassInActiveBundle($class) 210 { 211 @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.6 and will be removed in version 3.0.', E_USER_DEPRECATED); 212 213 foreach ($this->getBundles() as $bundle) { 214 if (0 === strpos($class, $bundle->getNamespace())) { 215 return true; 216 } 217 } 218 219 return false; 220 } 221 222 /** 223 * {@inheritdoc} 224 */ 225 public function getBundle($name, $first = true) 226 { 227 if (!isset($this->bundleMap[$name])) { 228 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))); 229 } 230 231 if (true === $first) { 232 return $this->bundleMap[$name][0]; 233 } 234 235 return $this->bundleMap[$name]; 236 } 237 238 /** 239 * {@inheritdoc} 240 * 241 * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle 242 */ 243 public function locateResource($name, $dir = null, $first = true) 244 { 245 if ('@' !== $name[0]) { 246 throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name)); 247 } 248 249 if (false !== strpos($name, '..')) { 250 throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name)); 251 } 252 253 $bundleName = substr($name, 1); 254 $path = ''; 255 if (false !== strpos($bundleName, '/')) { 256 list($bundleName, $path) = explode('/', $bundleName, 2); 257 } 258 259 $isResource = 0 === strpos($path, 'Resources') && null !== $dir; 260 $overridePath = substr($path, 9); 261 $resourceBundle = null; 262 $bundles = $this->getBundle($bundleName, false); 263 $files = array(); 264 265 foreach ($bundles as $bundle) { 266 if ($isResource && file_exists($file = $dir.'/'.$bundle->getName().$overridePath)) { 267 if (null !== $resourceBundle) { 268 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)); 269 } 270 271 if ($first) { 272 return $file; 273 } 274 $files[] = $file; 275 } 276 277 if (file_exists($file = $bundle->getPath().'/'.$path)) { 278 if ($first && !$isResource) { 279 return $file; 280 } 281 $files[] = $file; 282 $resourceBundle = $bundle->getName(); 283 } 284 } 285 286 if (\count($files) > 0) { 287 return $first && $isResource ? $files[0] : $files; 288 } 289 290 throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name)); 291 } 292 293 /** 294 * {@inheritdoc} 295 */ 296 public function getName() 297 { 298 if (null === $this->name) { 299 $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir)); 300 if (ctype_digit($this->name[0])) { 301 $this->name = '_'.$this->name; 302 } 303 } 304 305 return $this->name; 306 } 307 308 /** 309 * {@inheritdoc} 310 */ 311 public function getEnvironment() 312 { 313 return $this->environment; 314 } 315 316 /** 317 * {@inheritdoc} 318 */ 319 public function isDebug() 320 { 321 return $this->debug; 322 } 323 324 /** 325 * {@inheritdoc} 326 */ 327 public function getRootDir() 328 { 329 if (null === $this->rootDir) { 330 $r = new \ReflectionObject($this); 331 $this->rootDir = \dirname($r->getFileName()); 332 } 333 334 return $this->rootDir; 335 } 336 337 /** 338 * {@inheritdoc} 339 */ 340 public function getContainer() 341 { 342 return $this->container; 343 } 344 345 /** 346 * Loads the PHP class cache. 347 * 348 * This methods only registers the fact that you want to load the cache classes. 349 * The cache will actually only be loaded when the Kernel is booted. 350 * 351 * That optimization is mainly useful when using the HttpCache class in which 352 * case the class cache is not loaded if the Response is in the cache. 353 * 354 * @param string $name The cache name prefix 355 * @param string $extension File extension of the resulting file 356 */ 357 public function loadClassCache($name = 'classes', $extension = '.php') 358 { 359 $this->loadClassCache = array($name, $extension); 360 } 361 362 /** 363 * Used internally. 364 */ 365 public function setClassCache(array $classes) 366 { 367 file_put_contents($this->getCacheDir().'/classes.map', sprintf('<?php return %s;', var_export($classes, true))); 368 } 369 370 /** 371 * {@inheritdoc} 372 */ 373 public function getStartTime() 374 { 375 return $this->debug ? $this->startTime : -INF; 376 } 377 378 /** 379 * {@inheritdoc} 380 */ 381 public function getCacheDir() 382 { 383 return $this->rootDir.'/cache/'.$this->environment; 384 } 385 386 /** 387 * {@inheritdoc} 388 */ 389 public function getLogDir() 390 { 391 return $this->rootDir.'/logs'; 392 } 393 394 /** 395 * {@inheritdoc} 396 */ 397 public function getCharset() 398 { 399 return 'UTF-8'; 400 } 401 402 protected function doLoadClassCache($name, $extension) 403 { 404 if (!$this->booted && is_file($this->getCacheDir().'/classes.map')) { 405 ClassCollectionLoader::load(include($this->getCacheDir().'/classes.map'), $this->getCacheDir(), $name, $this->debug, false, $extension); 406 } 407 } 408 409 /** 410 * Initializes the data structures related to the bundle management. 411 * 412 * - the bundles property maps a bundle name to the bundle instance, 413 * - the bundleMap property maps a bundle name to the bundle inheritance hierarchy (most derived bundle first). 414 * 415 * @throws \LogicException if two bundles share a common name 416 * @throws \LogicException if a bundle tries to extend a non-registered bundle 417 * @throws \LogicException if a bundle tries to extend itself 418 * @throws \LogicException if two bundles extend the same ancestor 419 */ 420 protected function initializeBundles() 421 { 422 // init bundles 423 $this->bundles = array(); 424 $topMostBundles = array(); 425 $directChildren = array(); 426 427 foreach ($this->registerBundles() as $bundle) { 428 $name = $bundle->getName(); 429 if (isset($this->bundles[$name])) { 430 throw new \LogicException(sprintf('Trying to register two bundles with the same name "%s"', $name)); 431 } 432 $this->bundles[$name] = $bundle; 433 434 if ($parentName = $bundle->getParent()) { 435 if (isset($directChildren[$parentName])) { 436 throw new \LogicException(sprintf('Bundle "%s" is directly extended by two bundles "%s" and "%s".', $parentName, $name, $directChildren[$parentName])); 437 } 438 if ($parentName == $name) { 439 throw new \LogicException(sprintf('Bundle "%s" can not extend itself.', $name)); 440 } 441 $directChildren[$parentName] = $name; 442 } else { 443 $topMostBundles[$name] = $bundle; 444 } 445 } 446 447 // look for orphans 448 if (!empty($directChildren) && \count($diff = array_diff_key($directChildren, $this->bundles))) { 449 $diff = array_keys($diff); 450 451 throw new \LogicException(sprintf('Bundle "%s" extends bundle "%s", which is not registered.', $directChildren[$diff[0]], $diff[0])); 452 } 453 454 // inheritance 455 $this->bundleMap = array(); 456 foreach ($topMostBundles as $name => $bundle) { 457 $bundleMap = array($bundle); 458 $hierarchy = array($name); 459 460 while (isset($directChildren[$name])) { 461 $name = $directChildren[$name]; 462 array_unshift($bundleMap, $this->bundles[$name]); 463 $hierarchy[] = $name; 464 } 465 466 foreach ($hierarchy as $hierarchyBundle) { 467 $this->bundleMap[$hierarchyBundle] = $bundleMap; 468 array_pop($bundleMap); 469 } 470 } 471 } 472 473 /** 474 * Gets the container class. 475 * 476 * @return string The container class 477 */ 478 protected function getContainerClass() 479 { 480 return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer'; 481 } 482 483 /** 484 * Gets the container's base class. 485 * 486 * All names except Container must be fully qualified. 487 * 488 * @return string 489 */ 490 protected function getContainerBaseClass() 491 { 492 return 'Container'; 493 } 494 495 /** 496 * Initializes the service container. 497 * 498 * The cached version of the service container is used when fresh, otherwise the 499 * container is built. 500 */ 501 protected function initializeContainer() 502 { 503 $class = $this->getContainerClass(); 504 $cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug); 505 $fresh = true; 506 if (!$cache->isFresh()) { 507 $container = $this->buildContainer(); 508 $container->compile(); 509 $this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass()); 510 511 $fresh = false; 512 } 513 514 require_once $cache->getPath(); 515 516 $this->container = new $class(); 517 $this->container->set('kernel', $this); 518 519 if (!$fresh && $this->container->has('cache_warmer')) { 520 $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir')); 521 } 522 } 523 524 /** 525 * Returns the kernel parameters. 526 * 527 * @return array An array of kernel parameters 528 */ 529 protected function getKernelParameters() 530 { 531 $bundles = array(); 532 $bundlesMetadata = array(); 533 534 foreach ($this->bundles as $name => $bundle) { 535 $bundles[$name] = \get_class($bundle); 536 $bundlesMetadata[$name] = array( 537 'parent' => $bundle->getParent(), 538 'path' => $bundle->getPath(), 539 'namespace' => $bundle->getNamespace(), 540 ); 541 } 542 543 return array_merge( 544 array( 545 'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir, 546 'kernel.environment' => $this->environment, 547 'kernel.debug' => $this->debug, 548 'kernel.name' => $this->name, 549 'kernel.cache_dir' => realpath($this->getCacheDir()) ?: $this->getCacheDir(), 550 'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(), 551 'kernel.bundles' => $bundles, 552 'kernel.bundles_metadata' => $bundlesMetadata, 553 'kernel.charset' => $this->getCharset(), 554 'kernel.container_class' => $this->getContainerClass(), 555 ), 556 $this->getEnvParameters() 557 ); 558 } 559 560 /** 561 * Gets the environment parameters. 562 * 563 * Only the parameters starting with "SYMFONY__" are considered. 564 * 565 * @return array An array of parameters 566 */ 567 protected function getEnvParameters() 568 { 569 $parameters = array(); 570 foreach ($_SERVER as $key => $value) { 571 if (0 === strpos($key, 'SYMFONY__')) { 572 $parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value; 573 } 574 } 575 576 return $parameters; 577 } 578 579 /** 580 * Builds the service container. 581 * 582 * @return ContainerBuilder The compiled service container 583 * 584 * @throws \RuntimeException 585 */ 586 protected function buildContainer() 587 { 588 foreach (array('cache' => $this->getCacheDir(), 'logs' => $this->getLogDir()) as $name => $dir) { 589 if (!is_dir($dir)) { 590 if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) { 591 throw new \RuntimeException(sprintf("Unable to create the %s directory (%s)\n", $name, $dir)); 592 } 593 } elseif (!is_writable($dir)) { 594 throw new \RuntimeException(sprintf("Unable to write in the %s directory (%s)\n", $name, $dir)); 595 } 596 } 597 598 $container = $this->getContainerBuilder(); 599 $container->addObjectResource($this); 600 $this->prepareContainer($container); 601 602 if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) { 603 $container->merge($cont); 604 } 605 606 $container->addCompilerPass(new AddClassesToCachePass($this)); 607 $container->addResource(new EnvParametersResource('SYMFONY__')); 608 609 return $container; 610 } 611 612 /** 613 * Prepares the ContainerBuilder before it is compiled. 614 */ 615 protected function prepareContainer(ContainerBuilder $container) 616 { 617 $extensions = array(); 618 foreach ($this->bundles as $bundle) { 619 if ($extension = $bundle->getContainerExtension()) { 620 $container->registerExtension($extension); 621 $extensions[] = $extension->getAlias(); 622 } 623 624 if ($this->debug) { 625 $container->addObjectResource($bundle); 626 } 627 } 628 foreach ($this->bundles as $bundle) { 629 $bundle->build($container); 630 } 631 632 // ensure these extensions are implicitly loaded 633 $container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions)); 634 } 635 636 /** 637 * Gets a new ContainerBuilder instance used to build the service container. 638 * 639 * @return ContainerBuilder 640 */ 641 protected function getContainerBuilder() 642 { 643 $container = new ContainerBuilder(new ParameterBag($this->getKernelParameters())); 644 645 if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) { 646 $container->setProxyInstantiator(new RuntimeInstantiator()); 647 } 648 649 return $container; 650 } 651 652 /** 653 * Dumps the service container to PHP code in the cache. 654 * 655 * @param ConfigCache $cache The config cache 656 * @param ContainerBuilder $container The service container 657 * @param string $class The name of the class to generate 658 * @param string $baseClass The name of the container's base class 659 */ 660 protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class, $baseClass) 661 { 662 // cache the container 663 $dumper = new PhpDumper($container); 664 665 if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper')) { 666 $dumper->setProxyDumper(new ProxyDumper(md5($cache->getPath()))); 667 } 668 669 $content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => $cache->getPath(), 'debug' => $this->debug)); 670 671 $cache->write($content, $container->getResources()); 672 } 673 674 /** 675 * Returns a loader for the container. 676 * 677 * @return DelegatingLoader The loader 678 */ 679 protected function getContainerLoader(ContainerInterface $container) 680 { 681 $locator = new FileLocator($this); 682 $resolver = new LoaderResolver(array( 683 new XmlFileLoader($container, $locator), 684 new YamlFileLoader($container, $locator), 685 new IniFileLoader($container, $locator), 686 new PhpFileLoader($container, $locator), 687 new DirectoryLoader($container, $locator), 688 new ClosureLoader($container), 689 )); 690 691 return new DelegatingLoader($resolver); 692 } 693 694 /** 695 * Removes comments from a PHP source string. 696 * 697 * We don't use the PHP php_strip_whitespace() function 698 * as we want the content to be readable and well-formatted. 699 * 700 * @param string $source A PHP string 701 * 702 * @return string The PHP string with the comments removed 703 */ 704 public static function stripComments($source) 705 { 706 if (!\function_exists('token_get_all')) { 707 return $source; 708 } 709 710 $rawChunk = ''; 711 $output = ''; 712 $tokens = token_get_all($source); 713 $ignoreSpace = false; 714 for ($i = 0; isset($tokens[$i]); ++$i) { 715 $token = $tokens[$i]; 716 if (!isset($token[1]) || 'b"' === $token) { 717 $rawChunk .= $token; 718 } elseif (T_START_HEREDOC === $token[0]) { 719 $output .= $rawChunk.$token[1]; 720 do { 721 $token = $tokens[++$i]; 722 $output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token; 723 } while (T_END_HEREDOC !== $token[0]); 724 $rawChunk = ''; 725 } elseif (T_WHITESPACE === $token[0]) { 726 if ($ignoreSpace) { 727 $ignoreSpace = false; 728 729 continue; 730 } 731 732 // replace multiple new lines with a single newline 733 $rawChunk .= preg_replace(array('/\n{2,}/S'), "\n", $token[1]); 734 } elseif (\in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) { 735 $ignoreSpace = true; 736 } else { 737 $rawChunk .= $token[1]; 738 739 // The PHP-open tag already has a new-line 740 if (T_OPEN_TAG === $token[0]) { 741 $ignoreSpace = true; 742 } 743 } 744 } 745 746 $output .= $rawChunk; 747 748 if (\PHP_VERSION_ID >= 70000) { 749 // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 750 unset($tokens, $rawChunk); 751 gc_mem_caches(); 752 } 753 754 return $output; 755 } 756 757 public function serialize() 758 { 759 return serialize(array($this->environment, $this->debug)); 760 } 761 762 public function unserialize($data) 763 { 764 list($environment, $debug) = unserialize($data); 765 766 $this->__construct($environment, $debug); 767 } 768 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |