bootstrap.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. <?php
  2. namespace Symfony\Foundation\Bundle;
  3. use Symfony\Components\DependencyInjection\ContainerInterface;
  4. abstract class Bundle implements BundleInterface
  5. {
  6. public function buildContainer(ContainerInterface $container)
  7. {
  8. }
  9. public function boot(ContainerInterface $container)
  10. {
  11. }
  12. }
  13. namespace Symfony\Foundation\Bundle;
  14. use Symfony\Components\DependencyInjection\ContainerInterface;
  15. interface BundleInterface
  16. {
  17. public function buildContainer(ContainerInterface $container);
  18. public function boot(ContainerInterface $container);
  19. }
  20. namespace Symfony\Foundation\Bundle;
  21. use Symfony\Foundation\Bundle\Bundle;
  22. use Symfony\Foundation\ClassCollectionLoader;
  23. use Symfony\Components\DependencyInjection\ContainerInterface;
  24. use Symfony\Components\DependencyInjection\Loader\Loader;
  25. use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
  26. use Symfony\Components\DependencyInjection\BuilderConfiguration;
  27. class KernelBundle extends Bundle
  28. {
  29. public function buildContainer(ContainerInterface $container)
  30. {
  31. Loader::registerExtension(new KernelExtension());
  32. $configuration = new BuilderConfiguration();
  33. $loader = new XmlFileLoader(array(__DIR__.'/../Resources/config', __DIR__.'/Resources/config'));
  34. $configuration->merge($loader->load('services.xml'));
  35. if ($container->getParameter('kernel.debug'))
  36. {
  37. $configuration->merge($loader->load('debug.xml'));
  38. $configuration->setDefinition('event_dispatcher', $configuration->findDefinition('debug.event_dispatcher'));
  39. }
  40. return $configuration;
  41. }
  42. public function boot(ContainerInterface $container)
  43. {
  44. $container->getErrorHandlerService();
  45. ClassCollectionLoader::load($container->getParameter('kernel.compiled_classes'), $container->getParameter('kernel.cache_dir'), 'classes', $container->getParameter('kernel.debug'));
  46. }
  47. }
  48. namespace Symfony\Foundation\Bundle;
  49. use Symfony\Components\DependencyInjection\Loader\LoaderExtension;
  50. use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
  51. use Symfony\Components\DependencyInjection\BuilderConfiguration;
  52. class KernelExtension extends LoaderExtension
  53. {
  54. public function configLoad($config)
  55. {
  56. $configuration = new BuilderConfiguration();
  57. if (isset($config['charset']))
  58. {
  59. $configuration->setParameter('kernel.charset', $config['charset']);
  60. }
  61. if (!array_key_exists('compilation', $config))
  62. {
  63. $classes = array(
  64. 'Symfony\\Components\\Routing\\Router',
  65. 'Symfony\\Components\\Routing\\RouterInterface',
  66. 'Symfony\\Components\\EventDispatcher\\Event',
  67. 'Symfony\\Components\\Routing\\Matcher\\UrlMatcherInterface',
  68. 'Symfony\\Components\\Routing\\Matcher\\UrlMatcher',
  69. 'Symfony\\Components\\RequestHandler\\RequestInterface',
  70. 'Symfony\\Components\\RequestHandler\\Request',
  71. 'Symfony\\Components\\RequestHandler\\RequestHandler',
  72. 'Symfony\\Components\\RequestHandler\\ResponseInterface',
  73. 'Symfony\\Components\\RequestHandler\\Response',
  74. 'Symfony\\Components\\Templating\\Loader\\LoaderInterface',
  75. 'Symfony\\Components\\Templating\\Loader\\Loader',
  76. 'Symfony\\Components\\Templating\\Loader\\FilesystemLoader',
  77. 'Symfony\\Components\\Templating\\Engine',
  78. 'Symfony\\Components\\Templating\\Renderer\\RendererInterface',
  79. 'Symfony\\Components\\Templating\\Renderer\\Renderer',
  80. 'Symfony\\Components\\Templating\\Renderer\\PhpRenderer',
  81. 'Symfony\\Components\\Templating\\Storage\\Storage',
  82. 'Symfony\\Components\\Templating\\Storage\\FileStorage',
  83. 'Symfony\\Framework\\WebBundle\\Controller',
  84. 'Symfony\\Framework\\WebBundle\\Listener\\RequestParser',
  85. 'Symfony\\Framework\\WebBundle\\Listener\\ControllerLoader',
  86. 'Symfony\\Framework\\WebBundle\\Listener\\ResponseFilter',
  87. 'Symfony\\Framework\\WebBundle\\Templating\\Engine',
  88. );
  89. }
  90. else
  91. {
  92. $classes = array();
  93. foreach (explode("\n", $config['compilation']) as $class)
  94. {
  95. if ($class)
  96. {
  97. $classes[] = trim($class);
  98. }
  99. }
  100. }
  101. $configuration->setParameter('kernel.compiled_classes', $classes);
  102. if (array_key_exists('error_handler_level', $config))
  103. {
  104. $configuration->setParameter('error_handler.level', $config['error_handler_level']);
  105. }
  106. return $configuration;
  107. }
  108. public function getXsdValidationBasePath()
  109. {
  110. return false;
  111. }
  112. public function getNamespace()
  113. {
  114. return 'http://www.symfony-project.org/schema/dic/symfony/kernel';
  115. }
  116. public function getAlias()
  117. {
  118. return 'kernel';
  119. }
  120. }
  121. namespace Symfony\Foundation\Debug;
  122. class ErrorHandler
  123. {
  124. protected $levels = array(
  125. E_WARNING => 'Warning',
  126. E_NOTICE => 'Notice',
  127. E_USER_ERROR => 'User Error',
  128. E_USER_WARNING => 'User Warning',
  129. E_USER_NOTICE => 'User Notice',
  130. E_STRICT => 'Runtime Notice',
  131. E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  132. );
  133. protected $level;
  134. public function __construct($level = null)
  135. {
  136. $this->level = null === $level ? error_reporting() : $level;
  137. }
  138. public function register()
  139. {
  140. set_error_handler(array($this, 'handle'));
  141. }
  142. public function handle($level, $message, $file, $line, $context)
  143. {
  144. if (0 === $this->level)
  145. {
  146. return false;
  147. }
  148. if (error_reporting() & $level && $this->level & $level)
  149. {
  150. throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line));
  151. }
  152. return false;
  153. }
  154. }
  155. namespace Symfony\Foundation;
  156. class ClassCollectionLoader
  157. {
  158. static public function load($classes, $cacheDir, $name, $autoReload)
  159. {
  160. $cache = $cacheDir.'/'.$name.'.php';
  161. $reload = false;
  162. if ($autoReload)
  163. {
  164. $metadata = $cacheDir.'/'.$name.'.meta';
  165. if (!file_exists($metadata) || !file_exists($cache))
  166. {
  167. $reload = true;
  168. }
  169. else
  170. {
  171. $time = filemtime($cache);
  172. $meta = unserialize(file_get_contents($metadata));
  173. if ($meta[1] != $classes)
  174. {
  175. $reload = true;
  176. }
  177. else
  178. {
  179. foreach ($meta[0] as $resource)
  180. {
  181. if (!file_exists($resource) || filemtime($resource) > $time)
  182. {
  183. $reload = true;
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. if (!$reload && file_exists($cache))
  191. {
  192. require_once $cache;
  193. return;
  194. }
  195. $files = array();
  196. $content = '';
  197. foreach ($classes as $class)
  198. {
  199. if (!class_exists($class) && !interface_exists($class))
  200. {
  201. throw new \InvalidArgumentException(sprintf('Unable to load class "%s"', $class));
  202. }
  203. $r = new \ReflectionClass($class);
  204. $files[] = $r->getFileName();
  205. $content .= preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName()));
  206. }
  207. if (!is_dir(dirname($cache)))
  208. {
  209. mkdir(dirname($cache), 0777, true);
  210. }
  211. self::writeCacheFile($cache, Kernel::stripComments('<?php '.$content));
  212. if ($autoReload)
  213. {
  214. self::writeCacheFile($metadata, serialize(array($files, $classes)));
  215. }
  216. }
  217. static protected function writeCacheFile($file, $content)
  218. {
  219. $tmpFile = tempnam(dirname($file), basename($file));
  220. if (!$fp = @fopen($tmpFile, 'wb'))
  221. {
  222. die(sprintf('Failed to write cache file "%s".', $tmpFile));
  223. }
  224. @fwrite($fp, $content);
  225. @fclose($fp);
  226. if ($content != file_get_contents($tmpFile))
  227. {
  228. die(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile));
  229. }
  230. @rename($tmpFile, $file);
  231. chmod($file, 0644);
  232. }
  233. }
  234. namespace Symfony\Foundation;
  235. use Symfony\Components\DependencyInjection\ContainerInterface;
  236. use Symfony\Components\DependencyInjection\Builder;
  237. use Symfony\Components\DependencyInjection\BuilderConfiguration;
  238. use Symfony\Components\DependencyInjection\Dumper\PhpDumper;
  239. use Symfony\Components\RequestHandler\RequestInterface;
  240. abstract class Kernel
  241. {
  242. protected $bundles;
  243. protected $bundleDirs;
  244. protected $container;
  245. protected $rootDir;
  246. protected $environment;
  247. protected $debug;
  248. protected $booted;
  249. protected $name;
  250. protected $parameters;
  251. protected $startTime;
  252. const VERSION = '2.0.0-DEV';
  253. public function __construct($environment, $debug, $parameters = array())
  254. {
  255. $this->debug = (Boolean) $debug;
  256. if ($this->debug)
  257. {
  258. ini_set('display_errors', 1);
  259. error_reporting(-1);
  260. }
  261. else
  262. {
  263. ini_set('display_errors', 0);
  264. }
  265. if ($this->debug)
  266. {
  267. $this->startTime = microtime(true);
  268. }
  269. $this->booted = false;
  270. $this->environment = $environment;
  271. $this->parameters = $parameters;
  272. $this->bundles = $this->registerBundles();
  273. $this->bundleDirs = $this->registerBundleDirs();
  274. $this->rootDir = realpath($this->registerRootDir());
  275. $this->name = basename($this->rootDir);
  276. }
  277. abstract public function registerRootDir();
  278. abstract public function registerBundles();
  279. abstract public function registerBundleDirs();
  280. abstract public function registerContainerConfiguration();
  281. abstract public function registerRoutes();
  282. public function isBooted()
  283. {
  284. return $this->booted;
  285. }
  286. public function boot()
  287. {
  288. if (true === $this->booted)
  289. {
  290. throw new \LogicException('The kernel is already booted.');
  291. }
  292. $this->container = $this->initializeContainer();
  293. $this->container->setService('kernel', $this);
  294. foreach ($this->bundles as $bundle)
  295. {
  296. $bundle->boot($this->container);
  297. }
  298. $this->booted = true;
  299. return $this;
  300. }
  301. public function run()
  302. {
  303. $this->handle()->send();
  304. }
  305. public function handle(RequestInterface $request = null)
  306. {
  307. if (false === $this->booted)
  308. {
  309. $this->boot();
  310. }
  311. if (null === $request)
  312. {
  313. $request = $this->container->getRequestService();
  314. }
  315. return $this->container->getRequestHandlerService()->handle($request);
  316. }
  317. public function getBundleDirs()
  318. {
  319. return $this->bundleDirs;
  320. }
  321. public function getBundles()
  322. {
  323. return $this->bundles;
  324. }
  325. public function getName()
  326. {
  327. return $this->name;
  328. }
  329. public function getEnvironment()
  330. {
  331. return $this->environment;
  332. }
  333. public function isDebug()
  334. {
  335. return $this->debug;
  336. }
  337. public function getRootDir()
  338. {
  339. return $this->rootDir;
  340. }
  341. public function getContainer()
  342. {
  343. return $this->container;
  344. }
  345. public function getStartTime()
  346. {
  347. return $this->debug ? $this->startTime : -INF;
  348. }
  349. public function getParameters()
  350. {
  351. return $parameters;
  352. }
  353. public function getDefaultParameters()
  354. {
  355. $bundles = array();
  356. foreach ($this->bundles as $bundle)
  357. {
  358. $bundles[] = get_class($bundle);
  359. }
  360. return array_merge(
  361. array(
  362. 'kernel.root_dir' => $this->rootDir,
  363. 'kernel.environment' => $this->environment,
  364. 'kernel.debug' => $this->debug,
  365. 'kernel.name' => $this->name,
  366. 'kernel.cache_dir' => $this->rootDir.'/cache/'.$this->environment,
  367. 'kernel.logs_dir' => $this->rootDir.'/logs',
  368. 'kernel.bundle_dirs' => $this->bundleDirs,
  369. 'kernel.bundles' => $bundles,
  370. 'kernel.charset' => 'UTF-8',
  371. ),
  372. $this->getEnvParameters(),
  373. $this->parameters
  374. );
  375. }
  376. protected function initializeContainer()
  377. {
  378. $parameters = $this->getDefaultParameters();
  379. $class = $this->name.'ProjectContainer';
  380. $file = $parameters['kernel.cache_dir'].'/'.$class.'.php';
  381. $reload = $this->debug ? $this->needsReload($class, $file, $parameters) : false;
  382. if ($reload || !file_exists($file))
  383. {
  384. $this->buildContainer($class, $file, $parameters);
  385. }
  386. require_once $file;
  387. return new $class();
  388. }
  389. protected function getEnvParameters()
  390. {
  391. $parameters = array();
  392. foreach ($_SERVER as $key => $value)
  393. {
  394. if ('SYMFONY__' === substr($key, 0, 9))
  395. {
  396. $parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
  397. }
  398. }
  399. return $parameters;
  400. }
  401. protected function needsReload($class, $file, $parameters)
  402. {
  403. $metadata = $parameters['kernel.cache_dir'].'/'.$class.'.meta';
  404. if (!file_exists($metadata) || !file_exists($file))
  405. {
  406. return true;
  407. }
  408. $meta = unserialize(file_get_contents($metadata));
  409. $time = filemtime($file);
  410. foreach ($meta as $resource)
  411. {
  412. if (!$resource->isUptodate($time))
  413. {
  414. return true;
  415. }
  416. }
  417. return false;
  418. }
  419. protected function buildContainer($class, $file, $parameters)
  420. {
  421. $container = new Builder($parameters);
  422. $configuration = new BuilderConfiguration();
  423. foreach ($this->bundles as $bundle)
  424. {
  425. $configuration->merge($bundle->buildContainer($container));
  426. }
  427. $configuration->merge($this->registerContainerConfiguration());
  428. $container->merge($configuration);
  429. $this->optimizeContainer($container);
  430. foreach (array('cache', 'logs') as $name)
  431. {
  432. $key = sprintf('kernel.%s_dir', $name);
  433. if (!is_dir($parameters[$key]))
  434. {
  435. if (false === @mkdir($parameters[$key], 0777, true))
  436. {
  437. die(sprintf('Unable to create the %s directory (%s)', $name, dirname($parameters['kernel.cache_dir'])));
  438. }
  439. }
  440. elseif (!is_writable($parameters[$key]))
  441. {
  442. die(sprintf('Unable to write in the %s directory (%s)', $name, $parameters['kernel.cache_dir']));
  443. }
  444. }
  445. $dumper = new PhpDumper($container);
  446. $content = $dumper->dump(array('class' => $class));
  447. if (!$this->debug)
  448. {
  449. $content = self::stripComments($content);
  450. }
  451. $this->writeCacheFile($file, $content);
  452. if ($this->debug)
  453. {
  454. $this->writeCacheFile($parameters['kernel.cache_dir'].'/'.$class.'.meta', serialize($configuration->getResources()));
  455. }
  456. }
  457. public function optimizeContainer(Builder $container)
  458. {
  459. foreach ($container->getDefinitions() as $definition)
  460. {
  461. if (false !== strpos($class = $definition->getClass(), '%'))
  462. {
  463. $definition->setClass(Builder::resolveValue($class, $container->getParameters()));
  464. unset($container[substr($class, 1, -1)]);
  465. }
  466. }
  467. }
  468. static public function stripComments($source)
  469. {
  470. if (!function_exists('token_get_all'))
  471. {
  472. return $source;
  473. }
  474. $ignore = array(T_COMMENT => true, T_DOC_COMMENT => true);
  475. $output = '';
  476. foreach (token_get_all($source) as $token)
  477. {
  478. if (isset($token[1]))
  479. {
  480. if (!isset($ignore[$token[0]]))
  481. {
  482. $output .= $token[1];
  483. }
  484. }
  485. else
  486. {
  487. $output .= $token;
  488. }
  489. }
  490. return $output;
  491. }
  492. protected function writeCacheFile($file, $content)
  493. {
  494. $tmpFile = tempnam(dirname($file), basename($file));
  495. if (!$fp = @fopen($tmpFile, 'wb'))
  496. {
  497. die(sprintf('Failed to write cache file "%s".', $tmpFile));
  498. }
  499. @fwrite($fp, $content);
  500. @fclose($fp);
  501. if ($content != file_get_contents($tmpFile))
  502. {
  503. die(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile));
  504. }
  505. @rename($tmpFile, $file);
  506. chmod($file, 0644);
  507. }
  508. }
  509. namespace Symfony\Foundation;
  510. use Symfony\Components\EventDispatcher\EventDispatcher as BaseEventDispatcher;
  511. use Symfony\Components\EventDispatcher\Event;
  512. use Symfony\Components\DependencyInjection\ContainerInterface;
  513. class EventDispatcher extends BaseEventDispatcher
  514. {
  515. protected $container;
  516. public function __construct(ContainerInterface $container)
  517. {
  518. $this->container = $container;
  519. foreach ($container->findAnnotatedServiceIds('kernel.listener') as $id => $attributes)
  520. {
  521. foreach ($attributes as $attribute)
  522. {
  523. if (isset($attribute['event']))
  524. {
  525. $this->connect($attribute['event'], array($id, isset($attribute['method']) ? $attribute['method'] : 'handle'));
  526. }
  527. }
  528. }
  529. }
  530. public function getListeners($name)
  531. {
  532. if (!isset($this->listeners[$name]))
  533. {
  534. return array();
  535. }
  536. foreach ($this->listeners[$name] as $i => $listener)
  537. {
  538. if (is_array($listener) && is_string($listener[0]))
  539. {
  540. $this->listeners[$name][$i] = array($this->container->getService($listener[0]), $listener[1]);
  541. }
  542. }
  543. return $this->listeners[$name];
  544. }
  545. }