Kernel.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel;
  11. use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator;
  12. use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  17. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  18. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  19. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  20. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  21. use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  25. use Symfony\Component\HttpKernel\Config\EnvParametersResource;
  26. use Symfony\Component\HttpKernel\Config\FileLocator;
  27. use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
  28. use Symfony\Component\HttpKernel\DependencyInjection\AddClassesToCachePass;
  29. use Symfony\Component\Config\Loader\LoaderResolver;
  30. use Symfony\Component\Config\Loader\DelegatingLoader;
  31. use Symfony\Component\Config\ConfigCache;
  32. use Symfony\Component\ClassLoader\ClassCollectionLoader;
  33. /**
  34. * The Kernel is the heart of the Symfony system.
  35. *
  36. * It manages an environment made of bundles.
  37. *
  38. * @author Fabien Potencier <fabien@symfony.com>
  39. *
  40. * @api
  41. */
  42. abstract class Kernel implements KernelInterface, TerminableInterface
  43. {
  44. /**
  45. * @var BundleInterface[]
  46. */
  47. protected $bundles = array();
  48. protected $bundleMap;
  49. protected $container;
  50. protected $rootDir;
  51. protected $environment;
  52. protected $debug;
  53. protected $booted = false;
  54. protected $name;
  55. protected $startTime;
  56. protected $loadClassCache;
  57. const VERSION = '2.6.12';
  58. const VERSION_ID = '20612';
  59. const MAJOR_VERSION = '2';
  60. const MINOR_VERSION = '6';
  61. const RELEASE_VERSION = '12';
  62. const EXTRA_VERSION = '';
  63. /**
  64. * Constructor.
  65. *
  66. * @param string $environment The environment
  67. * @param bool $debug Whether to enable debugging or not
  68. *
  69. * @api
  70. */
  71. public function __construct($environment, $debug)
  72. {
  73. $this->environment = $environment;
  74. $this->debug = (bool) $debug;
  75. $this->rootDir = $this->getRootDir();
  76. $this->name = $this->getName();
  77. if ($this->debug) {
  78. $this->startTime = microtime(true);
  79. }
  80. $this->init();
  81. }
  82. /**
  83. * @deprecated Deprecated since version 2.3, to be removed in 3.0. Move your logic in the constructor instead.
  84. */
  85. public function init()
  86. {
  87. }
  88. public function __clone()
  89. {
  90. if ($this->debug) {
  91. $this->startTime = microtime(true);
  92. }
  93. $this->booted = false;
  94. $this->container = null;
  95. }
  96. /**
  97. * Boots the current kernel.
  98. *
  99. * @api
  100. */
  101. public function boot()
  102. {
  103. if (true === $this->booted) {
  104. return;
  105. }
  106. if ($this->loadClassCache) {
  107. $this->doLoadClassCache($this->loadClassCache[0], $this->loadClassCache[1]);
  108. }
  109. // init bundles
  110. $this->initializeBundles();
  111. // init container
  112. $this->initializeContainer();
  113. foreach ($this->getBundles() as $bundle) {
  114. $bundle->setContainer($this->container);
  115. $bundle->boot();
  116. }
  117. $this->booted = true;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. *
  122. * @api
  123. */
  124. public function terminate(Request $request, Response $response)
  125. {
  126. if (false === $this->booted) {
  127. return;
  128. }
  129. if ($this->getHttpKernel() instanceof TerminableInterface) {
  130. $this->getHttpKernel()->terminate($request, $response);
  131. }
  132. }
  133. /**
  134. * {@inheritdoc}
  135. *
  136. * @api
  137. */
  138. public function shutdown()
  139. {
  140. if (false === $this->booted) {
  141. return;
  142. }
  143. $this->booted = false;
  144. foreach ($this->getBundles() as $bundle) {
  145. $bundle->shutdown();
  146. $bundle->setContainer(null);
  147. }
  148. $this->container = null;
  149. }
  150. /**
  151. * {@inheritdoc}
  152. *
  153. * @api
  154. */
  155. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  156. {
  157. if (false === $this->booted) {
  158. $this->boot();
  159. }
  160. return $this->getHttpKernel()->handle($request, $type, $catch);
  161. }
  162. /**
  163. * Gets a HTTP kernel from the container.
  164. *
  165. * @return HttpKernel
  166. */
  167. protected function getHttpKernel()
  168. {
  169. return $this->container->get('http_kernel');
  170. }
  171. /**
  172. * {@inheritdoc}
  173. *
  174. * @api
  175. */
  176. public function getBundles()
  177. {
  178. return $this->bundles;
  179. }
  180. /**
  181. * {@inheritdoc}
  182. *
  183. * @api
  184. *
  185. * @deprecated Deprecated since version 2.6, to be removed in 3.0.
  186. */
  187. public function isClassInActiveBundle($class)
  188. {
  189. foreach ($this->getBundles() as $bundle) {
  190. if (0 === strpos($class, $bundle->getNamespace())) {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. /**
  197. * {@inheritdoc}
  198. *
  199. * @api
  200. */
  201. public function getBundle($name, $first = true)
  202. {
  203. if (!isset($this->bundleMap[$name])) {
  204. throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() method of your %s.php file?', $name, get_class($this)));
  205. }
  206. if (true === $first) {
  207. return $this->bundleMap[$name][0];
  208. }
  209. return $this->bundleMap[$name];
  210. }
  211. /**
  212. * {@inheritdoc}
  213. *
  214. * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle
  215. */
  216. public function locateResource($name, $dir = null, $first = true)
  217. {
  218. if ('@' !== $name[0]) {
  219. throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name));
  220. }
  221. if (false !== strpos($name, '..')) {
  222. throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name));
  223. }
  224. $bundleName = substr($name, 1);
  225. $path = '';
  226. if (false !== strpos($bundleName, '/')) {
  227. list($bundleName, $path) = explode('/', $bundleName, 2);
  228. }
  229. $isResource = 0 === strpos($path, 'Resources') && null !== $dir;
  230. $overridePath = substr($path, 9);
  231. $resourceBundle = null;
  232. $bundles = $this->getBundle($bundleName, false);
  233. $files = array();
  234. foreach ($bundles as $bundle) {
  235. if ($isResource && file_exists($file = $dir.'/'.$bundle->getName().$overridePath)) {
  236. if (null !== $resourceBundle) {
  237. throw new \RuntimeException(sprintf('"%s" resource is hidden by a resource from the "%s" derived bundle. Create a "%s" file to override the bundle resource.',
  238. $file,
  239. $resourceBundle,
  240. $dir.'/'.$bundles[0]->getName().$overridePath
  241. ));
  242. }
  243. if ($first) {
  244. return $file;
  245. }
  246. $files[] = $file;
  247. }
  248. if (file_exists($file = $bundle->getPath().'/'.$path)) {
  249. if ($first && !$isResource) {
  250. return $file;
  251. }
  252. $files[] = $file;
  253. $resourceBundle = $bundle->getName();
  254. }
  255. }
  256. if (count($files) > 0) {
  257. return $first && $isResource ? $files[0] : $files;
  258. }
  259. throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name));
  260. }
  261. /**
  262. * {@inheritdoc}
  263. *
  264. * @api
  265. */
  266. public function getName()
  267. {
  268. if (null === $this->name) {
  269. $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
  270. }
  271. return $this->name;
  272. }
  273. /**
  274. * {@inheritdoc}
  275. *
  276. * @api
  277. */
  278. public function getEnvironment()
  279. {
  280. return $this->environment;
  281. }
  282. /**
  283. * {@inheritdoc}
  284. *
  285. * @api
  286. */
  287. public function isDebug()
  288. {
  289. return $this->debug;
  290. }
  291. /**
  292. * {@inheritdoc}
  293. *
  294. * @api
  295. */
  296. public function getRootDir()
  297. {
  298. if (null === $this->rootDir) {
  299. $r = new \ReflectionObject($this);
  300. $this->rootDir = str_replace('\\', '/', dirname($r->getFileName()));
  301. }
  302. return $this->rootDir;
  303. }
  304. /**
  305. * {@inheritdoc}
  306. *
  307. * @api
  308. */
  309. public function getContainer()
  310. {
  311. return $this->container;
  312. }
  313. /**
  314. * Loads the PHP class cache.
  315. *
  316. * This methods only registers the fact that you want to load the cache classes.
  317. * The cache will actually only be loaded when the Kernel is booted.
  318. *
  319. * That optimization is mainly useful when using the HttpCache class in which
  320. * case the class cache is not loaded if the Response is in the cache.
  321. *
  322. * @param string $name The cache name prefix
  323. * @param string $extension File extension of the resulting file
  324. */
  325. public function loadClassCache($name = 'classes', $extension = '.php')
  326. {
  327. $this->loadClassCache = array($name, $extension);
  328. }
  329. /**
  330. * Used internally.
  331. */
  332. public function setClassCache(array $classes)
  333. {
  334. file_put_contents($this->getCacheDir().'/classes.map', sprintf('<?php return %s;', var_export($classes, true)));
  335. }
  336. /**
  337. * {@inheritdoc}
  338. *
  339. * @api
  340. */
  341. public function getStartTime()
  342. {
  343. return $this->debug ? $this->startTime : -INF;
  344. }
  345. /**
  346. * {@inheritdoc}
  347. *
  348. * @api
  349. */
  350. public function getCacheDir()
  351. {
  352. return $this->rootDir.'/cache/'.$this->environment;
  353. }
  354. /**
  355. * {@inheritdoc}
  356. *
  357. * @api
  358. */
  359. public function getLogDir()
  360. {
  361. return $this->rootDir.'/logs';
  362. }
  363. /**
  364. * {@inheritdoc}
  365. *
  366. * @api
  367. */
  368. public function getCharset()
  369. {
  370. return 'UTF-8';
  371. }
  372. protected function doLoadClassCache($name, $extension)
  373. {
  374. if (!$this->booted && is_file($this->getCacheDir().'/classes.map')) {
  375. ClassCollectionLoader::load(include($this->getCacheDir().'/classes.map'), $this->getCacheDir(), $name, $this->debug, false, $extension);
  376. }
  377. }
  378. /**
  379. * Initializes the data structures related to the bundle management.
  380. *
  381. * - the bundles property maps a bundle name to the bundle instance,
  382. * - the bundleMap property maps a bundle name to the bundle inheritance hierarchy (most derived bundle first).
  383. *
  384. * @throws \LogicException if two bundles share a common name
  385. * @throws \LogicException if a bundle tries to extend a non-registered bundle
  386. * @throws \LogicException if a bundle tries to extend itself
  387. * @throws \LogicException if two bundles extend the same ancestor
  388. */
  389. protected function initializeBundles()
  390. {
  391. // init bundles
  392. $this->bundles = array();
  393. $topMostBundles = array();
  394. $directChildren = array();
  395. foreach ($this->registerBundles() as $bundle) {
  396. $name = $bundle->getName();
  397. if (isset($this->bundles[$name])) {
  398. throw new \LogicException(sprintf('Trying to register two bundles with the same name "%s"', $name));
  399. }
  400. $this->bundles[$name] = $bundle;
  401. if ($parentName = $bundle->getParent()) {
  402. if (isset($directChildren[$parentName])) {
  403. throw new \LogicException(sprintf('Bundle "%s" is directly extended by two bundles "%s" and "%s".', $parentName, $name, $directChildren[$parentName]));
  404. }
  405. if ($parentName == $name) {
  406. throw new \LogicException(sprintf('Bundle "%s" can not extend itself.', $name));
  407. }
  408. $directChildren[$parentName] = $name;
  409. } else {
  410. $topMostBundles[$name] = $bundle;
  411. }
  412. }
  413. // look for orphans
  414. if (!empty($directChildren) && count($diff = array_diff_key($directChildren, $this->bundles))) {
  415. $diff = array_keys($diff);
  416. throw new \LogicException(sprintf('Bundle "%s" extends bundle "%s", which is not registered.', $directChildren[$diff[0]], $diff[0]));
  417. }
  418. // inheritance
  419. $this->bundleMap = array();
  420. foreach ($topMostBundles as $name => $bundle) {
  421. $bundleMap = array($bundle);
  422. $hierarchy = array($name);
  423. while (isset($directChildren[$name])) {
  424. $name = $directChildren[$name];
  425. array_unshift($bundleMap, $this->bundles[$name]);
  426. $hierarchy[] = $name;
  427. }
  428. foreach ($hierarchy as $bundle) {
  429. $this->bundleMap[$bundle] = $bundleMap;
  430. array_pop($bundleMap);
  431. }
  432. }
  433. }
  434. /**
  435. * Gets the container class.
  436. *
  437. * @return string The container class
  438. */
  439. protected function getContainerClass()
  440. {
  441. return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
  442. }
  443. /**
  444. * Gets the container's base class.
  445. *
  446. * All names except Container must be fully qualified.
  447. *
  448. * @return string
  449. */
  450. protected function getContainerBaseClass()
  451. {
  452. return 'Container';
  453. }
  454. /**
  455. * Initializes the service container.
  456. *
  457. * The cached version of the service container is used when fresh, otherwise the
  458. * container is built.
  459. */
  460. protected function initializeContainer()
  461. {
  462. $class = $this->getContainerClass();
  463. $cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
  464. $fresh = true;
  465. if (!$cache->isFresh()) {
  466. $container = $this->buildContainer();
  467. $container->compile();
  468. $this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
  469. $fresh = false;
  470. }
  471. require_once $cache;
  472. $this->container = new $class();
  473. $this->container->set('kernel', $this);
  474. if (!$fresh && $this->container->has('cache_warmer')) {
  475. $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
  476. }
  477. }
  478. /**
  479. * Returns the kernel parameters.
  480. *
  481. * @return array An array of kernel parameters
  482. */
  483. protected function getKernelParameters()
  484. {
  485. $bundles = array();
  486. foreach ($this->bundles as $name => $bundle) {
  487. $bundles[$name] = get_class($bundle);
  488. }
  489. return array_merge(
  490. array(
  491. 'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir,
  492. 'kernel.environment' => $this->environment,
  493. 'kernel.debug' => $this->debug,
  494. 'kernel.name' => $this->name,
  495. 'kernel.cache_dir' => realpath($this->getCacheDir()) ?: $this->getCacheDir(),
  496. 'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(),
  497. 'kernel.bundles' => $bundles,
  498. 'kernel.charset' => $this->getCharset(),
  499. 'kernel.container_class' => $this->getContainerClass(),
  500. ),
  501. $this->getEnvParameters()
  502. );
  503. }
  504. /**
  505. * Gets the environment parameters.
  506. *
  507. * Only the parameters starting with "SYMFONY__" are considered.
  508. *
  509. * @return array An array of parameters
  510. */
  511. protected function getEnvParameters()
  512. {
  513. $parameters = array();
  514. foreach ($_SERVER as $key => $value) {
  515. if (0 === strpos($key, 'SYMFONY__')) {
  516. $parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
  517. }
  518. }
  519. return $parameters;
  520. }
  521. /**
  522. * Builds the service container.
  523. *
  524. * @return ContainerBuilder The compiled service container
  525. *
  526. * @throws \RuntimeException
  527. */
  528. protected function buildContainer()
  529. {
  530. foreach (array('cache' => $this->getCacheDir(), 'logs' => $this->getLogDir()) as $name => $dir) {
  531. if (!is_dir($dir)) {
  532. if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
  533. throw new \RuntimeException(sprintf("Unable to create the %s directory (%s)\n", $name, $dir));
  534. }
  535. } elseif (!is_writable($dir)) {
  536. throw new \RuntimeException(sprintf("Unable to write in the %s directory (%s)\n", $name, $dir));
  537. }
  538. }
  539. $container = $this->getContainerBuilder();
  540. $container->addObjectResource($this);
  541. $this->prepareContainer($container);
  542. if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) {
  543. $container->merge($cont);
  544. }
  545. $container->addCompilerPass(new AddClassesToCachePass($this));
  546. $container->addResource(new EnvParametersResource('SYMFONY__'));
  547. return $container;
  548. }
  549. /**
  550. * Prepares the ContainerBuilder before it is compiled.
  551. *
  552. * @param ContainerBuilder $container A ContainerBuilder instance
  553. */
  554. protected function prepareContainer(ContainerBuilder $container)
  555. {
  556. $extensions = array();
  557. foreach ($this->bundles as $bundle) {
  558. if ($extension = $bundle->getContainerExtension()) {
  559. $container->registerExtension($extension);
  560. $extensions[] = $extension->getAlias();
  561. }
  562. if ($this->debug) {
  563. $container->addObjectResource($bundle);
  564. }
  565. }
  566. foreach ($this->bundles as $bundle) {
  567. $bundle->build($container);
  568. }
  569. // ensure these extensions are implicitly loaded
  570. $container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
  571. }
  572. /**
  573. * Gets a new ContainerBuilder instance used to build the service container.
  574. *
  575. * @return ContainerBuilder
  576. */
  577. protected function getContainerBuilder()
  578. {
  579. $container = new ContainerBuilder(new ParameterBag($this->getKernelParameters()));
  580. if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) {
  581. $container->setProxyInstantiator(new RuntimeInstantiator());
  582. }
  583. return $container;
  584. }
  585. /**
  586. * Dumps the service container to PHP code in the cache.
  587. *
  588. * @param ConfigCache $cache The config cache
  589. * @param ContainerBuilder $container The service container
  590. * @param string $class The name of the class to generate
  591. * @param string $baseClass The name of the container's base class
  592. */
  593. protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class, $baseClass)
  594. {
  595. // cache the container
  596. $dumper = new PhpDumper($container);
  597. if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper')) {
  598. $dumper->setProxyDumper(new ProxyDumper(md5((string) $cache)));
  599. }
  600. $content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => (string) $cache));
  601. if (!$this->debug) {
  602. $content = static::stripComments($content);
  603. }
  604. $cache->write($content, $container->getResources());
  605. }
  606. /**
  607. * Returns a loader for the container.
  608. *
  609. * @param ContainerInterface $container The service container
  610. *
  611. * @return DelegatingLoader The loader
  612. */
  613. protected function getContainerLoader(ContainerInterface $container)
  614. {
  615. $locator = new FileLocator($this);
  616. $resolver = new LoaderResolver(array(
  617. new XmlFileLoader($container, $locator),
  618. new YamlFileLoader($container, $locator),
  619. new IniFileLoader($container, $locator),
  620. new PhpFileLoader($container, $locator),
  621. new ClosureLoader($container),
  622. ));
  623. return new DelegatingLoader($resolver);
  624. }
  625. /**
  626. * Removes comments from a PHP source string.
  627. *
  628. * We don't use the PHP php_strip_whitespace() function
  629. * as we want the content to be readable and well-formatted.
  630. *
  631. * @param string $source A PHP string
  632. *
  633. * @return string The PHP string with the comments removed
  634. */
  635. public static function stripComments($source)
  636. {
  637. if (!function_exists('token_get_all')) {
  638. return $source;
  639. }
  640. $rawChunk = '';
  641. $output = '';
  642. $tokens = token_get_all($source);
  643. $ignoreSpace = false;
  644. for (reset($tokens); false !== $token = current($tokens); next($tokens)) {
  645. if (is_string($token)) {
  646. $rawChunk .= $token;
  647. } elseif (T_START_HEREDOC === $token[0]) {
  648. $output .= $rawChunk.$token[1];
  649. do {
  650. $token = next($tokens);
  651. $output .= $token[1];
  652. } while ($token[0] !== T_END_HEREDOC);
  653. $rawChunk = '';
  654. } elseif (T_WHITESPACE === $token[0]) {
  655. if ($ignoreSpace) {
  656. $ignoreSpace = false;
  657. continue;
  658. }
  659. // replace multiple new lines with a single newline
  660. $rawChunk .= preg_replace(array('/\n{2,}/S'), "\n", $token[1]);
  661. } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
  662. $ignoreSpace = true;
  663. } else {
  664. $rawChunk .= $token[1];
  665. // The PHP-open tag already has a new-line
  666. if (T_OPEN_TAG === $token[0]) {
  667. $ignoreSpace = true;
  668. }
  669. }
  670. }
  671. $output .= $rawChunk;
  672. return $output;
  673. }
  674. public function serialize()
  675. {
  676. return serialize(array($this->environment, $this->debug));
  677. }
  678. public function unserialize($data)
  679. {
  680. list($environment, $debug) = unserialize($data);
  681. $this->__construct($environment, $debug);
  682. }
  683. }