Kernel.php 10 KB

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