Kernel.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. namespace Symfony\Foundation;
  3. use Symfony\Components\DependencyInjection\ContainerInterface;
  4. use Symfony\Components\DependencyInjection\Builder;
  5. use Symfony\Components\DependencyInjection\BuilderConfiguration;
  6. use Symfony\Components\DependencyInjection\Dumper\PhpDumper;
  7. use Symfony\Components\DependencyInjection\FileResource;
  8. use Symfony\Components\RequestHandler\Request;
  9. use Symfony\Components\RequestHandler\RequestHandlerInterface;
  10. /*
  11. * This file is part of the Symfony package.
  12. *
  13. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  14. *
  15. * For the full copyright and license information, please view the LICENSE
  16. * file that was distributed with this source code.
  17. */
  18. /**
  19. * The Kernel is the heart of the Symfony system. It manages an environment
  20. * that can host bundles.
  21. *
  22. * @package Symfony
  23. * @subpackage Foundation
  24. * @author Fabien Potencier <fabien.potencier@symfony-project.org>
  25. */
  26. abstract class Kernel implements RequestHandlerInterface, \Serializable
  27. {
  28. protected $bundles;
  29. protected $bundleDirs;
  30. protected $container;
  31. protected $rootDir;
  32. protected $environment;
  33. protected $debug;
  34. protected $booted;
  35. protected $name;
  36. protected $startTime;
  37. const VERSION = '2.0.0-DEV';
  38. /**
  39. * Constructor.
  40. *
  41. * @param string $environment The environment
  42. * @param Boolean $debug Whether to enable debugging or not
  43. */
  44. public function __construct($environment, $debug)
  45. {
  46. $this->debug = (Boolean) $debug;
  47. if ($this->debug)
  48. {
  49. ini_set('display_errors', 1);
  50. error_reporting(-1);
  51. }
  52. else
  53. {
  54. ini_set('display_errors', 0);
  55. }
  56. if ($this->debug)
  57. {
  58. $this->startTime = microtime(true);
  59. }
  60. $this->booted = false;
  61. $this->environment = $environment;
  62. $this->bundles = $this->registerBundles();
  63. $this->bundleDirs = $this->registerBundleDirs();
  64. $this->rootDir = realpath($this->registerRootDir());
  65. $this->name = basename($this->rootDir);
  66. }
  67. abstract public function registerRootDir();
  68. abstract public function registerBundles();
  69. abstract public function registerBundleDirs();
  70. abstract public function registerContainerConfiguration();
  71. abstract public function registerRoutes();
  72. /**
  73. * Checks whether the current kernel has been booted or not.
  74. *
  75. * @return boolean $booted
  76. */
  77. public function isBooted()
  78. {
  79. return $this->booted;
  80. }
  81. /**
  82. * Boots the current kernel.
  83. *
  84. * This method boots the bundles, which MUST set
  85. * the DI container.
  86. *
  87. * @return Kernel The current Kernel instance
  88. *
  89. * @throws \LogicException When the Kernel is already booted
  90. */
  91. public function boot()
  92. {
  93. if (true === $this->booted)
  94. {
  95. throw new \LogicException('The kernel is already booted.');
  96. }
  97. // initialize the container
  98. $this->container = $this->initializeContainer();
  99. $this->container->setService('kernel', $this);
  100. // boot bundles
  101. foreach ($this->bundles as $bundle)
  102. {
  103. $bundle->boot($this->container);
  104. }
  105. $this->booted = true;
  106. return $this;
  107. }
  108. /**
  109. * Shutdowns the kernel.
  110. *
  111. * This method is mainly useful when doing functional testing.
  112. */
  113. public function shutdown()
  114. {
  115. $this->booted = false;
  116. foreach ($this->bundles as $bundle)
  117. {
  118. $bundle->shutdown($this->container);
  119. }
  120. $this->container = null;
  121. }
  122. /**
  123. * Reboots the kernel.
  124. *
  125. * This method is mainly useful when doing functional testing.
  126. *
  127. * It is a shortcut for the call to shutdown() and boot().
  128. */
  129. public function reboot()
  130. {
  131. $this->shutdown();
  132. $this->boot();
  133. }
  134. public function handle(Request $request = null, $main = true)
  135. {
  136. if (false === $this->booted)
  137. {
  138. $this->boot();
  139. }
  140. if (null === $request)
  141. {
  142. $request = $this->container->getRequestService();
  143. }
  144. else
  145. {
  146. $this->container->setService('request', $request);
  147. }
  148. return $this->container->getRequestHandlerService()->handle($request);
  149. }
  150. public function getBundleDirs()
  151. {
  152. return $this->bundleDirs;
  153. }
  154. public function getBundles()
  155. {
  156. return $this->bundles;
  157. }
  158. public function getName()
  159. {
  160. return $this->name;
  161. }
  162. public function getEnvironment()
  163. {
  164. return $this->environment;
  165. }
  166. public function isDebug()
  167. {
  168. return $this->debug;
  169. }
  170. public function getRootDir()
  171. {
  172. return $this->rootDir;
  173. }
  174. public function getContainer()
  175. {
  176. return $this->container;
  177. }
  178. public function getStartTime()
  179. {
  180. return $this->debug ? $this->startTime : -INF;
  181. }
  182. public function getCacheDir()
  183. {
  184. return $this->rootDir.'/cache/'.$this->environment;
  185. }
  186. public function getLogDir()
  187. {
  188. return $this->rootDir.'/logs';
  189. }
  190. protected function initializeContainer()
  191. {
  192. $class = $this->name.'ProjectContainer';
  193. $location = $this->getCacheDir().'/'.$class;
  194. $reload = $this->debug ? $this->needsReload($class, $location) : false;
  195. if ($reload || !file_exists($location.'.php'))
  196. {
  197. $this->buildContainer($class, $location.'.php');
  198. }
  199. require_once $location.'.php';
  200. return new $class();
  201. }
  202. public function getKernelParameters()
  203. {
  204. $bundles = array();
  205. foreach ($this->bundles as $bundle)
  206. {
  207. $bundles[] = get_class($bundle);
  208. }
  209. return array_merge(
  210. array(
  211. 'kernel.root_dir' => $this->rootDir,
  212. 'kernel.environment' => $this->environment,
  213. 'kernel.debug' => $this->debug,
  214. 'kernel.name' => $this->name,
  215. 'kernel.cache_dir' => $this->getCacheDir(),
  216. 'kernel.logs_dir' => $this->getLogDir(),
  217. 'kernel.bundle_dirs' => $this->bundleDirs,
  218. 'kernel.bundles' => $bundles,
  219. 'kernel.charset' => 'UTF-8',
  220. ),
  221. $this->getEnvParameters()
  222. );
  223. }
  224. protected function getEnvParameters()
  225. {
  226. $parameters = array();
  227. foreach ($_SERVER as $key => $value)
  228. {
  229. if ('SYMFONY__' === substr($key, 0, 9))
  230. {
  231. $parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
  232. }
  233. }
  234. return $parameters;
  235. }
  236. protected function needsReload($class, $location)
  237. {
  238. if (!file_exists($location.'.meta') || !file_exists($location.'.php'))
  239. {
  240. return true;
  241. }
  242. $meta = unserialize(file_get_contents($location.'.meta'));
  243. $time = filemtime($location.'.php');
  244. foreach ($meta as $resource)
  245. {
  246. if (!$resource->isUptodate($time))
  247. {
  248. return true;
  249. }
  250. }
  251. return false;
  252. }
  253. protected function buildContainer($class, $file)
  254. {
  255. $container = new Builder($this->getKernelParameters());
  256. $configuration = new BuilderConfiguration();
  257. foreach ($this->bundles as $bundle)
  258. {
  259. $configuration->merge($bundle->buildContainer($container));
  260. }
  261. $configuration->merge($this->registerContainerConfiguration());
  262. $container->merge($configuration);
  263. $this->optimizeContainer($container);
  264. foreach (array('cache', 'logs') as $name)
  265. {
  266. $dir = $container->getParameter(sprintf('kernel.%s_dir', $name));
  267. if (!is_dir($dir))
  268. {
  269. if (false === @mkdir($dir, 0777, true))
  270. {
  271. die(sprintf('Unable to create the %s directory (%s)', $name, dirname($dir)));
  272. }
  273. }
  274. elseif (!is_writable($dir))
  275. {
  276. die(sprintf('Unable to write in the %s directory (%s)', $name, $dir));
  277. }
  278. }
  279. // cache the container
  280. $dumper = new PhpDumper($container);
  281. $content = $dumper->dump(array('class' => $class));
  282. if (!$this->debug)
  283. {
  284. $content = self::stripComments($content);
  285. }
  286. $this->writeCacheFile($file, $content);
  287. if ($this->debug)
  288. {
  289. // add the Kernel class hierarchy as resources
  290. $parent = new \ReflectionObject($this);
  291. $configuration->addResource(new FileResource($parent->getFileName()));
  292. while ($parent = $parent->getParentClass())
  293. {
  294. $configuration->addResource(new FileResource($parent->getFileName()));
  295. }
  296. // save the resources
  297. $this->writeCacheFile($this->getCacheDir().'/'.$class.'.meta', serialize($configuration->getResources()));
  298. }
  299. }
  300. public function optimizeContainer(Builder $container)
  301. {
  302. // replace all classes with the real value
  303. foreach ($container->getDefinitions() as $definition)
  304. {
  305. if (false !== strpos($class = $definition->getClass(), '%'))
  306. {
  307. $definition->setClass(Builder::resolveValue($class, $container->getParameters()));
  308. unset($container[substr($class, 1, -1)]);
  309. }
  310. }
  311. }
  312. static public function stripComments($source)
  313. {
  314. if (!function_exists('token_get_all'))
  315. {
  316. return $source;
  317. }
  318. $ignore = array(T_COMMENT => true, T_DOC_COMMENT => true);
  319. $output = '';
  320. foreach (token_get_all($source) as $token)
  321. {
  322. // array
  323. if (isset($token[1]))
  324. {
  325. // no action on comments
  326. if (!isset($ignore[$token[0]]))
  327. {
  328. // anything else -> output "as is"
  329. $output .= $token[1];
  330. }
  331. }
  332. else
  333. {
  334. // simple 1-character token
  335. $output .= $token;
  336. }
  337. }
  338. return $output;
  339. }
  340. protected function writeCacheFile($file, $content)
  341. {
  342. $tmpFile = tempnam(dirname($file), basename($file));
  343. if (!$fp = @fopen($tmpFile, 'wb'))
  344. {
  345. die(sprintf('Failed to write cache file "%s".', $tmpFile));
  346. }
  347. @fwrite($fp, $content);
  348. @fclose($fp);
  349. if ($content != file_get_contents($tmpFile))
  350. {
  351. die(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile));
  352. }
  353. @rename($tmpFile, $file);
  354. chmod($file, 0644);
  355. }
  356. public function serialize()
  357. {
  358. return serialize(array($this->environment, $this->debug));
  359. }
  360. public function unserialize($data)
  361. {
  362. list($environment, $debug) = unserialize($data);
  363. $this->__construct($environment, $debug);
  364. }
  365. }