Няма описание

Kernel.php 23KB

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