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