bootstrap.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. namespace Symfony\Foundation\Bundle;
  3. use Symfony\Components\DependencyInjection\ContainerInterface;
  4. use Symfony\Components\Console\Application;
  5. abstract class Bundle implements BundleInterface
  6. {
  7. protected $name;
  8. protected $namespacePrefix;
  9. protected $path;
  10. protected $reflection;
  11. public function buildContainer(ContainerInterface $container)
  12. {
  13. }
  14. public function boot(ContainerInterface $container)
  15. {
  16. }
  17. public function shutdown(ContainerInterface $container)
  18. {
  19. }
  20. public function getName()
  21. {
  22. if (null === $this->name) {
  23. $this->initReflection();
  24. }
  25. return $this->name;
  26. }
  27. public function getNamespacePrefix()
  28. {
  29. if (null === $this->name) {
  30. $this->initReflection();
  31. }
  32. return $this->namespacePrefix;
  33. }
  34. public function getPath()
  35. {
  36. if (null === $this->name) {
  37. $this->initReflection();
  38. }
  39. return $this->path;
  40. }
  41. public function getReflection()
  42. {
  43. if (null === $this->name) {
  44. $this->initReflection();
  45. }
  46. return $this->reflection;
  47. }
  48. public function registerCommands(Application $application)
  49. {
  50. foreach ($application->getKernel()->getBundleDirs() as $dir) {
  51. $bundleBase = dirname(str_replace('\\', '/', get_class($this)));
  52. $commandDir = $dir.'/'.basename($bundleBase).'/Command';
  53. if (!is_dir($commandDir)) {
  54. continue;
  55. }
  56. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($commandDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  57. if ($file->isDir() || substr($file, -4) !== '.php') {
  58. continue;
  59. }
  60. $class = str_replace('/', '\\', $bundleBase).'\\Command\\'.str_replace(realpath($commandDir).'/', '', basename(realpath($file), '.php'));
  61. $r = new \ReflectionClass($class);
  62. if ($r->isSubclassOf('Symfony\\Components\\Console\\Command\\Command') && !$r->isAbstract()) {
  63. $application->addCommand(new $class());
  64. }
  65. }
  66. }
  67. }
  68. protected function initReflection()
  69. {
  70. $tmp = dirname(str_replace('\\', '/', get_class($this)));
  71. $this->namespacePrefix = str_replace('/', '\\', dirname($tmp));
  72. $this->name = basename($tmp);
  73. $this->reflection = new \ReflectionObject($this);
  74. $this->path = dirname($this->reflection->getFilename());
  75. }
  76. }
  77. namespace Symfony\Foundation\Bundle;
  78. use Symfony\Components\DependencyInjection\ContainerInterface;
  79. interface BundleInterface
  80. {
  81. public function buildContainer(ContainerInterface $container);
  82. public function boot(ContainerInterface $container);
  83. public function shutdown(ContainerInterface $container);
  84. }
  85. namespace Symfony\Foundation;
  86. use Symfony\Foundation\Bundle\Bundle;
  87. use Symfony\Foundation\ClassCollectionLoader;
  88. use Symfony\Foundation\DependencyInjection\KernelExtension;
  89. use Symfony\Components\DependencyInjection\ContainerInterface;
  90. use Symfony\Components\DependencyInjection\Loader\Loader;
  91. use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
  92. use Symfony\Components\DependencyInjection\BuilderConfiguration;
  93. class KernelBundle extends Bundle
  94. {
  95. public function buildContainer(ContainerInterface $container)
  96. {
  97. Loader::registerExtension(new KernelExtension());
  98. $configuration = new BuilderConfiguration();
  99. $loader = new XmlFileLoader(array(__DIR__.'/../Resources/config', __DIR__.'/Resources/config'));
  100. $configuration->merge($loader->load('services.xml'));
  101. if ($container->getParameter('kernel.debug')) {
  102. $configuration->merge($loader->load('debug.xml'));
  103. $configuration->setDefinition('event_dispatcher', $configuration->findDefinition('debug.event_dispatcher'));
  104. }
  105. return $configuration;
  106. }
  107. public function boot(ContainerInterface $container)
  108. {
  109. $container->getErrorHandlerService();
  110. if ($container->getParameter('kernel.include_core_classes')) {
  111. ClassCollectionLoader::load($container->getParameter('kernel.compiled_classes'), $container->getParameter('kernel.cache_dir'), 'classes', $container->getParameter('kernel.debug'));
  112. }
  113. }
  114. }
  115. namespace Symfony\Foundation\DependencyInjection;
  116. use Symfony\Components\DependencyInjection\Loader\LoaderExtension;
  117. use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
  118. use Symfony\Components\DependencyInjection\BuilderConfiguration;
  119. class KernelExtension extends LoaderExtension
  120. {
  121. public function testLoad($config)
  122. {
  123. $configuration = new BuilderConfiguration();
  124. $loader = new XmlFileLoader(array(__DIR__.'/../Resources/config', __DIR__.'/Resources/config'));
  125. $configuration->merge($loader->load('test.xml'));
  126. $configuration->setParameter('kernel.include_core_classes', false);
  127. return $configuration;
  128. }
  129. public function configLoad($config)
  130. {
  131. $configuration = new BuilderConfiguration();
  132. if (isset($config['charset'])) {
  133. $configuration->setParameter('kernel.charset', $config['charset']);
  134. }
  135. if (!array_key_exists('compilation', $config)) {
  136. $classes = array(
  137. 'Symfony\\Components\\Routing\\Router',
  138. 'Symfony\\Components\\Routing\\RouterInterface',
  139. 'Symfony\\Components\\EventDispatcher\\Event',
  140. 'Symfony\\Components\\Routing\\Matcher\\UrlMatcherInterface',
  141. 'Symfony\\Components\\Routing\\Matcher\\UrlMatcher',
  142. 'Symfony\\Components\\HttpKernel\\HttpKernel',
  143. 'Symfony\\Components\\HttpKernel\\Request',
  144. 'Symfony\\Components\\HttpKernel\\Response',
  145. 'Symfony\\Components\\HttpKernel\\Listener\\ResponseFilter',
  146. 'Symfony\\Components\\Templating\\Loader\\LoaderInterface',
  147. 'Symfony\\Components\\Templating\\Loader\\Loader',
  148. 'Symfony\\Components\\Templating\\Loader\\FilesystemLoader',
  149. 'Symfony\\Components\\Templating\\Engine',
  150. 'Symfony\\Components\\Templating\\Renderer\\RendererInterface',
  151. 'Symfony\\Components\\Templating\\Renderer\\Renderer',
  152. 'Symfony\\Components\\Templating\\Renderer\\PhpRenderer',
  153. 'Symfony\\Components\\Templating\\Storage\\Storage',
  154. 'Symfony\\Components\\Templating\\Storage\\FileStorage',
  155. 'Symfony\\Framework\\FoundationBundle\\Controller',
  156. 'Symfony\\Framework\\FoundationBundle\\Listener\\RequestParser',
  157. 'Symfony\\Framework\\FoundationBundle\\Listener\\ControllerLoader',
  158. 'Symfony\\Framework\\FoundationBundle\\Templating\\Engine',
  159. );
  160. } else {
  161. $classes = array();
  162. foreach (explode("\n", $config['compilation']) as $class) {
  163. if ($class) {
  164. $classes[] = trim($class);
  165. }
  166. }
  167. }
  168. $configuration->setParameter('kernel.compiled_classes', $classes);
  169. if (array_key_exists('error_handler_level', $config)) {
  170. $configuration->setParameter('error_handler.level', $config['error_handler_level']);
  171. }
  172. return $configuration;
  173. }
  174. public function getXsdValidationBasePath()
  175. {
  176. return false;
  177. }
  178. public function getNamespace()
  179. {
  180. return 'http://www.symfony-project.org/schema/dic/symfony/kernel';
  181. }
  182. public function getAlias()
  183. {
  184. return 'kernel';
  185. }
  186. }
  187. namespace Symfony\Foundation\Debug;
  188. class ErrorHandler
  189. {
  190. protected $levels = array(
  191. E_WARNING => 'Warning',
  192. E_NOTICE => 'Notice',
  193. E_USER_ERROR => 'User Error',
  194. E_USER_WARNING => 'User Warning',
  195. E_USER_NOTICE => 'User Notice',
  196. E_STRICT => 'Runtime Notice',
  197. E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  198. );
  199. protected $level;
  200. public function __construct($level = null)
  201. {
  202. $this->level = null === $level ? error_reporting() : $level;
  203. }
  204. public function register()
  205. {
  206. set_error_handler(array($this, 'handle'));
  207. }
  208. public function handle($level, $message, $file, $line, $context)
  209. {
  210. if (0 === $this->level) {
  211. return false;
  212. }
  213. if (error_reporting() & $level && $this->level & $level) {
  214. throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line));
  215. }
  216. return false;
  217. }
  218. }
  219. namespace Symfony\Foundation;
  220. class ClassCollectionLoader
  221. {
  222. static public function load($classes, $cacheDir, $name, $autoReload)
  223. {
  224. $cache = $cacheDir.'/'.$name.'.php';
  225. $reload = false;
  226. if ($autoReload) {
  227. $metadata = $cacheDir.'/'.$name.'.meta';
  228. if (!file_exists($metadata) || !file_exists($cache)) {
  229. $reload = true;
  230. } else {
  231. $time = filemtime($cache);
  232. $meta = unserialize(file_get_contents($metadata));
  233. if ($meta[1] != $classes) {
  234. $reload = true;
  235. } else {
  236. foreach ($meta[0] as $resource) {
  237. if (!file_exists($resource) || filemtime($resource) > $time) {
  238. $reload = true;
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. }
  245. if (!$reload && file_exists($cache)) {
  246. require_once $cache;
  247. return;
  248. }
  249. $files = array();
  250. $content = '';
  251. foreach ($classes as $class) {
  252. if (!class_exists($class) && !interface_exists($class)) {
  253. throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class));
  254. }
  255. $r = new \ReflectionClass($class);
  256. $files[] = $r->getFileName();
  257. $content .= preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName()));
  258. }
  259. if (!is_dir(dirname($cache))) {
  260. mkdir(dirname($cache), 0777, true);
  261. }
  262. self::writeCacheFile($cache, Kernel::stripComments('<?php '.$content));
  263. if ($autoReload) {
  264. self::writeCacheFile($metadata, serialize(array($files, $classes)));
  265. }
  266. }
  267. static protected function writeCacheFile($file, $content)
  268. {
  269. $tmpFile = tempnam(dirname($file), basename($file));
  270. if (!$fp = @fopen($tmpFile, 'wb')) {
  271. die(sprintf('Failed to write cache file "%s".', $tmpFile));
  272. }
  273. @fwrite($fp, $content);
  274. @fclose($fp);
  275. if ($content != file_get_contents($tmpFile)) {
  276. die(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile));
  277. }
  278. if (!@rename($tmpFile, $file)) {
  279. throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
  280. }
  281. chmod($file, 0644);
  282. }
  283. }
  284. namespace Symfony\Foundation;
  285. use Symfony\Components\EventDispatcher\EventDispatcher as BaseEventDispatcher;
  286. use Symfony\Components\EventDispatcher\Event;
  287. use Symfony\Components\DependencyInjection\ContainerInterface;
  288. class EventDispatcher extends BaseEventDispatcher
  289. {
  290. public function __construct(ContainerInterface $container)
  291. {
  292. foreach ($container->findAnnotatedServiceIds('kernel.listener') as $id => $attributes) {
  293. $container->getService($id)->register($this);
  294. }
  295. }
  296. }