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