c7bf53be2e705c83f0af71803ff9dde0c4716e0c
[yaffs-website] / vendor / twig / twig / lib / Twig / Util / DeprecationCollector.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * @author Fabien Potencier <fabien@symfony.com>
14  *
15  * @final
16  */
17 class Twig_Util_DeprecationCollector
18 {
19     private $twig;
20     private $deprecations;
21
22     public function __construct(Twig_Environment $twig)
23     {
24         $this->twig = $twig;
25     }
26
27     /**
28      * Returns deprecations for templates contained in a directory.
29      *
30      * @param string $dir A directory where templates are stored
31      * @param string $ext Limit the loaded templates by extension
32      *
33      * @return array An array of deprecations
34      */
35     public function collectDir($dir, $ext = '.twig')
36     {
37         $iterator = new RegexIterator(
38             new RecursiveIteratorIterator(
39                 new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY
40             ), '{'.preg_quote($ext).'$}'
41         );
42
43         return $this->collect(new Twig_Util_TemplateDirIterator($iterator));
44     }
45
46     /**
47      * Returns deprecations for passed templates.
48      *
49      * @param Traversable $iterator An iterator of templates (where keys are template names and values the contents of the template)
50      *
51      * @return array An array of deprecations
52      */
53     public function collect(Traversable $iterator)
54     {
55         $this->deprecations = array();
56
57         set_error_handler(array($this, 'errorHandler'));
58
59         foreach ($iterator as $name => $contents) {
60             try {
61                 $this->twig->parse($this->twig->tokenize(new Twig_Source($contents, $name)));
62             } catch (Twig_Error_Syntax $e) {
63                 // ignore templates containing syntax errors
64             }
65         }
66
67         restore_error_handler();
68
69         $deprecations = $this->deprecations;
70         $this->deprecations = array();
71
72         return $deprecations;
73     }
74
75     /**
76      * @internal
77      */
78     public function errorHandler($type, $msg)
79     {
80         if (E_USER_DEPRECATED === $type) {
81             $this->deprecations[] = $msg;
82         }
83     }
84 }
85
86 class_alias('Twig_Util_DeprecationCollector', 'Twig\Util\DeprecationCollector', false);