| 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 |
namespace ElementorDeps\Twig\Util; |
| 12 |
|
| 13 |
use ElementorDeps\Twig\Environment; |
| 14 |
use ElementorDeps\Twig\Error\SyntaxError; |
| 15 |
use ElementorDeps\Twig\Source; |
| 16 |
/** |
| 17 |
* @author Fabien Potencier <fabien@symfony.com> |
| 18 |
*/ |
| 19 |
final class DeprecationCollector |
| 20 |
{ |
| 21 |
private $twig; |
| 22 |
public function __construct(Environment $twig) |
| 23 |
{ |
| 24 |
$this->twig = $twig; |
| 25 |
} |
| 26 |
/** |
| 27 |
* Returns deprecations for templates contained in a directory. |
| 28 |
* |
| 29 |
* @param string $dir A directory where templates are stored |
| 30 |
* @param string $ext Limit the loaded templates by extension |
| 31 |
* |
| 32 |
* @return array An array of deprecations |
| 33 |
*/ |
| 34 |
public function collectDir(string $dir, string $ext = '.twig') : array |
| 35 |
{ |
| 36 |
$iterator = new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY), '{' . \preg_quote($ext) . '$}'); |
| 37 |
return $this->collect(new TemplateDirIterator($iterator)); |
| 38 |
} |
| 39 |
/** |
| 40 |
* Returns deprecations for passed templates. |
| 41 |
* |
| 42 |
* @param \Traversable $iterator An iterator of templates (where keys are template names and values the contents of the template) |
| 43 |
* |
| 44 |
* @return array An array of deprecations |
| 45 |
*/ |
| 46 |
public function collect(\Traversable $iterator) : array |
| 47 |
{ |
| 48 |
$deprecations = []; |
| 49 |
\set_error_handler(function ($type, $msg) use(&$deprecations) { |
| 50 |
if (\E_USER_DEPRECATED === $type) { |
| 51 |
$deprecations[] = $msg; |
| 52 |
} |
| 53 |
return \false; |
| 54 |
}); |
| 55 |
foreach ($iterator as $name => $contents) { |
| 56 |
try { |
| 57 |
$this->twig->parse($this->twig->tokenize(new Source($contents, $name))); |
| 58 |
} catch (SyntaxError $e) { |
| 59 |
// ignore templates containing syntax errors |
| 60 |
} |
| 61 |
} |
| 62 |
\restore_error_handler(); |
| 63 |
return $deprecations; |
| 64 |
} |
| 65 |
} |
| 66 |
|