Kernel.php 23 KB

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