bootstrap.php 13 KB

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