bootstrap.php 13 KB

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