PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Framework/bootstrap.php

https://github.com/schinzi/symfony
PHP | 189 lines | 189 code | 0 blank | 0 comment | 36 complexity | b441dff9c3cfe25b590f39ff056c53be MD5 | raw file
Possible License(s): ISC
  1. <?php
  2. namespace Symfony\Framework\Bundle;
  3. use Symfony\Component\DependencyInjection\ContainerAware;
  4. use Symfony\Component\DependencyInjection\ContainerBuilder;
  5. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  6. use Symfony\Component\Console\Application;
  7. use Symfony\Component\Finder\Finder;
  8. abstract class Bundle extends ContainerAware implements BundleInterface {
  9. protected $name;
  10. protected $namespacePrefix;
  11. protected $path;
  12. protected $reflection;
  13. public function boot() { }
  14. public function shutdown() { }
  15. public function getName() {
  16. if (null === $this->name) {
  17. $this->initReflection(); }
  18. return $this->name; }
  19. public function getNamespacePrefix() {
  20. if (null === $this->name) {
  21. $this->initReflection(); }
  22. return $this->namespacePrefix; }
  23. public function getPath() {
  24. if (null === $this->name) {
  25. $this->initReflection(); }
  26. return $this->path; }
  27. public function getReflection() {
  28. if (null === $this->name) {
  29. $this->initReflection(); }
  30. return $this->reflection; }
  31. public function registerExtensions(ContainerBuilder $container) {
  32. if (!$dir = realpath($this->getPath().'/DependencyInjection')) {
  33. return array(); }
  34. $finder = new Finder();
  35. $finder->files()->name('*Extension.php')->in($dir);
  36. $prefix = $this->namespacePrefix.'\\'.$this->name.'\\DependencyInjection';
  37. foreach ($finder as $file) {
  38. $class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.basename($file, '.php');
  39. if ('Extension' === substr($class, -9)) {
  40. $container->registerExtension(new $class()); } } }
  41. public function registerCommands(Application $application) {
  42. if (!$dir = realpath($this->getPath().'/Command')) {
  43. return; }
  44. $finder = new Finder();
  45. $finder->files()->name('*Command.php')->in($dir);
  46. $prefix = $this->namespacePrefix.'\\'.$this->name.'\\Command';
  47. foreach ($finder as $file) {
  48. $r = new \ReflectionClass($prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.basename($file, '.php'));
  49. if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract()) {
  50. $application->addCommand($r->newInstance()); } } }
  51. protected function initReflection() {
  52. $tmp = dirname(str_replace('\\', '/', get_class($this)));
  53. $this->namespacePrefix = str_replace('/', '\\', dirname($tmp));
  54. $this->name = basename($tmp);
  55. $this->reflection = new \ReflectionObject($this);
  56. $this->path = dirname($this->reflection->getFilename()); } }
  57. namespace Symfony\Framework\Bundle;
  58. interface BundleInterface {
  59. public function boot();
  60. public function shutdown(); }
  61. namespace Symfony\Framework;
  62. use Symfony\Framework\Bundle\Bundle;
  63. class KernelBundle extends Bundle {
  64. public function boot() {
  65. if ($this->container->has('error_handler')) {
  66. $this->container['error_handler']; } } }
  67. namespace Symfony\Framework\DependencyInjection;
  68. use Symfony\Component\DependencyInjection\Extension\Extension;
  69. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  70. use Symfony\Component\DependencyInjection\ContainerBuilder;
  71. class KernelExtension extends Extension {
  72. public function testLoad($config, ContainerBuilder $container) {
  73. $loader = new XmlFileLoader($container, array(__DIR__.'/../Resources/config', __DIR__.'/Resources/config'));
  74. $loader->load('test.xml'); }
  75. public function sessionLoad($config, ContainerBuilder $container) {
  76. if (!$container->hasDefinition('session')) {
  77. $loader = new XmlFileLoader($container, array(__DIR__.'/../Resources/config', __DIR__.'/Resources/config'));
  78. $loader->load('session.xml'); }
  79. if (isset($config['default_locale'])) {
  80. $container->setParameter('session.default_locale', $config['default_locale']); }
  81. if (isset($config['class'])) {
  82. $container->setParameter('session.class', $config['class']); }
  83. foreach (array('name', 'lifetime', 'path', 'domain', 'secure', 'httponly', 'cache_limiter', 'pdo.db_table') as $name) {
  84. if (isset($config['session'][$name])) {
  85. $container->setParameter('session.options.'.$name, $config['session'][$name]); } }
  86. if (isset($config['session']['class'])) {
  87. $class = $config['session']['class'];
  88. if (in_array($class, array('Native', 'Pdo'))) {
  89. $class = 'Symfony\\Component\\HttpFoundation\\SessionStorage\\'.$class.'SessionStorage'; }
  90. $container->setParameter('session.session', 'session.session.'.strtolower($class)); } }
  91. public function configLoad($config, ContainerBuilder $container) {
  92. if (!$container->hasDefinition('event_dispatcher')) {
  93. $loader = new XmlFileLoader($container, array(__DIR__.'/../Resources/config', __DIR__.'/Resources/config'));
  94. $loader->load('services.xml');
  95. if ($container->getParameter('kernel.debug')) {
  96. $loader->load('debug.xml');
  97. $container->setDefinition('event_dispatcher', $container->findDefinition('debug.event_dispatcher'));
  98. $container->setAlias('debug.event_dispatcher', 'event_dispatcher'); } }
  99. if (isset($config['charset'])) {
  100. $container->setParameter('kernel.charset', $config['charset']); }
  101. if (array_key_exists('error_handler', $config)) {
  102. if (false === $config['error_handler']) {
  103. $container->getDefinition('error_handler')->setMethodCalls(array()); } else {
  104. $container->getDefinition('error_handler')->addMethodCall('register', array());
  105. $container->setParameter('error_handler.level', $config['error_handler']); } } }
  106. public function getXsdValidationBasePath() {
  107. return false; }
  108. public function getNamespace() {
  109. return 'http://www.symfony-project.org/schema/dic/symfony/kernel'; }
  110. public function getAlias() {
  111. return 'kernel'; } }
  112. namespace Symfony\Framework\Debug;
  113. class ErrorHandler {
  114. protected $levels = array(
  115. E_WARNING => 'Warning',
  116. E_NOTICE => 'Notice',
  117. E_USER_ERROR => 'User Error',
  118. E_USER_WARNING => 'User Warning',
  119. E_USER_NOTICE => 'User Notice',
  120. E_STRICT => 'Runtime Notice',
  121. E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  122. );
  123. protected $level;
  124. public function __construct($level = null) {
  125. $this->level = null === $level ? error_reporting() : $level; }
  126. public function register() {
  127. set_error_handler(array($this, 'handle')); }
  128. public function handle($level, $message, $file, $line, $context) {
  129. if (0 === $this->level) {
  130. return false; }
  131. if (error_reporting() & $level && $this->level & $level) {
  132. throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line)); }
  133. return false; } }
  134. namespace Symfony\Framework;
  135. class ClassCollectionLoader {
  136. static protected $loaded;
  137. static public function load($classes, $cacheDir, $name, $autoReload, $adaptive = false) {
  138. if (isset(self::$loaded[$name])) {
  139. return; }
  140. self::$loaded[$name] = true;
  141. $classes = array_unique($classes);
  142. if ($adaptive) {
  143. $classes = array_diff($classes, get_declared_classes(), get_declared_interfaces());
  144. $name = $name.'-'.substr(md5(implode('|', $classes)), 0, 5); }
  145. $cache = $cacheDir.'/'.$name.'.php';
  146. $reload = false;
  147. if ($autoReload) {
  148. $metadata = $cacheDir.'/'.$name.'.meta';
  149. if (!file_exists($metadata) || !file_exists($cache)) {
  150. $reload = true; } else {
  151. $time = filemtime($cache);
  152. $meta = unserialize(file_get_contents($metadata));
  153. if ($meta[1] != $classes) {
  154. $reload = true; } else {
  155. foreach ($meta[0] as $resource) {
  156. if (!file_exists($resource) || filemtime($resource) > $time) {
  157. $reload = true;
  158. break; } } } } }
  159. if (!$reload && file_exists($cache)) {
  160. require_once $cache;
  161. return; }
  162. $files = array();
  163. $content = '';
  164. foreach ($classes as $class) {
  165. if (!class_exists($class) && !interface_exists($class)) {
  166. throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class)); }
  167. $r = new \ReflectionClass($class);
  168. $files[] = $r->getFileName();
  169. $content .= preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName())); }
  170. if (!is_dir(dirname($cache))) {
  171. mkdir(dirname($cache), 0777, true); }
  172. self::writeCacheFile($cache, Kernel::stripComments('<?php '.$content));
  173. if ($autoReload) {
  174. self::writeCacheFile($metadata, serialize(array($files, $classes))); } }
  175. static protected function writeCacheFile($file, $content) {
  176. $tmpFile = tempnam(dirname($file), basename($file));
  177. if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
  178. chmod($file, 0644);
  179. return; }
  180. throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file)); } }
  181. namespace Symfony\Framework;
  182. use Symfony\Component\EventDispatcher\EventDispatcher as BaseEventDispatcher;
  183. use Symfony\Component\EventDispatcher\Event;
  184. use Symfony\Component\DependencyInjection\ContainerInterface;
  185. class EventDispatcher extends BaseEventDispatcher {
  186. public function setContainer(ContainerInterface $container) {
  187. foreach ($container->findTaggedServiceIds('kernel.listener') as $id => $attributes) {
  188. $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
  189. $container->get($id)->register($this, $priority); } } }