4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * @author Fabien Potencier <fabien@symfony.com>
17 class Twig_Util_DeprecationCollector
20 private $deprecations;
22 public function __construct(Twig_Environment $twig)
28 * Returns deprecations for templates contained in a directory.
30 * @param string $dir A directory where templates are stored
31 * @param string $ext Limit the loaded templates by extension
33 * @return array An array of deprecations
35 public function collectDir($dir, $ext = '.twig')
37 $iterator = new RegexIterator(
38 new RecursiveIteratorIterator(
39 new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY
40 ), '{'.preg_quote($ext).'$}'
43 return $this->collect(new Twig_Util_TemplateDirIterator($iterator));
47 * Returns deprecations for passed templates.
49 * @param Traversable $iterator An iterator of templates (where keys are template names and values the contents of the template)
51 * @return array An array of deprecations
53 public function collect(Traversable $iterator)
55 $this->deprecations = array();
57 set_error_handler(array($this, 'errorHandler'));
59 foreach ($iterator as $name => $contents) {
61 $this->twig->parse($this->twig->tokenize(new Twig_Source($contents, $name)));
62 } catch (Twig_Error_Syntax $e) {
63 // ignore templates containing syntax errors
67 restore_error_handler();
69 $deprecations = $this->deprecations;
70 $this->deprecations = array();
78 public function errorHandler($type, $msg)
80 if (E_USER_DEPRECATED === $type) {
81 $this->deprecations[] = $msg;
86 class_alias('Twig_Util_DeprecationCollector', 'Twig\Util\DeprecationCollector', false);