DeprecationCollector.php (2205B)
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 class Twig_Util_DeprecationCollector 16 { 17 private $twig; 18 private $deprecations; 19 20 public function __construct(Twig_Environment $twig) 21 { 22 $this->twig = $twig; 23 } 24 25 /** 26 * Returns deprecations for templates contained in a directory. 27 * 28 * @param string $dir A directory where templates are stored 29 * @param string $ext Limit the loaded templates by extension 30 * 31 * @return array() An array of deprecations 32 */ 33 public function collectDir($dir, $ext = '.twig') 34 { 35 $iterator = new RegexIterator( 36 new RecursiveIteratorIterator( 37 new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY 38 ), '{'.preg_quote($ext).'$}' 39 ); 40 41 return $this->collect(new Twig_Util_TemplateDirIterator($iterator)); 42 } 43 44 /** 45 * Returns deprecations for passed templates. 46 * 47 * @param Iterator $iterator An iterator of templates (where keys are template names and values the contents of the template) 48 * 49 * @return array() An array of deprecations 50 */ 51 public function collect(Iterator $iterator) 52 { 53 $this->deprecations = array(); 54 55 set_error_handler(array($this, 'errorHandler')); 56 57 foreach ($iterator as $name => $contents) { 58 try { 59 $this->twig->parse($this->twig->tokenize($contents, $name)); 60 } catch (Twig_Error_Syntax $e) { 61 // ignore templates containing syntax errors 62 } 63 } 64 65 restore_error_handler(); 66 67 $deprecations = $this->deprecations; 68 $this->deprecations = array(); 69 70 return $deprecations; 71 } 72 73 /** 74 * @internal 75 */ 76 public function errorHandler($type, $msg) 77 { 78 if (E_USER_DEPRECATED === $type) { 79 $this->deprecations[] = $msg; 80 } 81 } 82 }