Augmentation vers version 3.3.0
This commit is contained in:
146
vendor/symfony/twig-bridge/Command/DebugCommand.php
vendored
146
vendor/symfony/twig-bridge/Command/DebugCommand.php
vendored
@@ -12,12 +12,14 @@
|
||||
namespace Symfony\Bridge\Twig\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatter;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Twig\Environment;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
|
||||
/**
|
||||
* Lists twig functions, filters, globals and tests present in the current project.
|
||||
@@ -26,18 +28,35 @@ use Twig\Environment;
|
||||
*/
|
||||
class DebugCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'debug:twig';
|
||||
|
||||
private $twig;
|
||||
private $projectDir;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @param Environment $twig
|
||||
* @param string|null $projectDir
|
||||
*/
|
||||
public function __construct($name = 'debug:twig')
|
||||
public function __construct($twig = null, $projectDir = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
if (!$twig instanceof Environment) {
|
||||
@trigger_error(sprintf('Passing a command name as the first argument of "%s()" is deprecated since Symfony 3.4 and support for it will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED);
|
||||
|
||||
parent::__construct($twig);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
parent::__construct();
|
||||
|
||||
$this->twig = $twig;
|
||||
$this->projectDir = $projectDir;
|
||||
}
|
||||
|
||||
public function setTwigEnvironment(Environment $twig)
|
||||
{
|
||||
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
|
||||
|
||||
$this->twig = $twig;
|
||||
}
|
||||
|
||||
@@ -46,16 +65,18 @@ class DebugCommand extends Command
|
||||
*/
|
||||
protected function getTwigEnvironment()
|
||||
{
|
||||
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
|
||||
|
||||
return $this->twig;
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDefinition(array(
|
||||
->setDefinition([
|
||||
new InputArgument('filter', InputArgument::OPTIONAL, 'Show details for all entries matching this filter'),
|
||||
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (text or json)', 'text'),
|
||||
))
|
||||
])
|
||||
->setDescription('Shows a list of twig functions, filters, globals and tests')
|
||||
->setHelp(<<<'EOF'
|
||||
The <info>%command.name%</info> command outputs a list of twig functions,
|
||||
@@ -80,36 +101,50 @@ EOF
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$twig = $this->getTwigEnvironment();
|
||||
$decorated = $io->isDecorated();
|
||||
|
||||
if (null === $twig) {
|
||||
$io->error('The Twig environment needs to be set.');
|
||||
// BC to be removed in 4.0
|
||||
if (__CLASS__ !== \get_class($this)) {
|
||||
$r = new \ReflectionMethod($this, 'getTwigEnvironment');
|
||||
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
|
||||
@trigger_error(sprintf('Usage of method "%s" is deprecated since Symfony 3.4 and will no longer be supported in 4.0. Construct the command with its required arguments instead.', \get_class($this).'::getTwigEnvironment'), E_USER_DEPRECATED);
|
||||
|
||||
return 1;
|
||||
$this->twig = $this->getTwigEnvironment();
|
||||
}
|
||||
}
|
||||
if (null === $this->twig) {
|
||||
throw new \RuntimeException('The Twig environment needs to be set.');
|
||||
}
|
||||
|
||||
$types = array('functions', 'filters', 'tests', 'globals');
|
||||
$filter = $input->getArgument('filter');
|
||||
$types = ['functions', 'filters', 'tests', 'globals'];
|
||||
|
||||
if ('json' === $input->getOption('format')) {
|
||||
$data = array();
|
||||
$data = [];
|
||||
foreach ($types as $type) {
|
||||
foreach ($twig->{'get'.ucfirst($type)}() as $name => $entity) {
|
||||
$data[$type][$name] = $this->getMetadata($type, $entity);
|
||||
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) {
|
||||
if (!$filter || false !== strpos($name, $filter)) {
|
||||
$data[$type][$name] = $this->getMetadata($type, $entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
$data['tests'] = array_keys($data['tests']);
|
||||
$io->writeln(json_encode($data));
|
||||
|
||||
if (isset($data['tests'])) {
|
||||
$data['tests'] = array_keys($data['tests']);
|
||||
}
|
||||
|
||||
$data['loader_paths'] = $this->getLoaderPaths();
|
||||
$data = json_encode($data, JSON_PRETTY_PRINT);
|
||||
$io->writeln($decorated ? OutputFormatter::escape($data) : $data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
$filter = $input->getArgument('filter');
|
||||
|
||||
foreach ($types as $index => $type) {
|
||||
$items = array();
|
||||
foreach ($twig->{'get'.ucfirst($type)}() as $name => $entity) {
|
||||
$items = [];
|
||||
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) {
|
||||
if (!$filter || false !== strpos($name, $filter)) {
|
||||
$items[$name] = $name.$this->getPrettyMetadata($type, $entity);
|
||||
$items[$name] = $name.$this->getPrettyMetadata($type, $entity, $decorated);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,25 +158,78 @@ EOF
|
||||
$io->listing($items);
|
||||
}
|
||||
|
||||
$rows = [];
|
||||
$firstNamespace = true;
|
||||
$prevHasSeparator = false;
|
||||
foreach ($this->getLoaderPaths() as $namespace => $paths) {
|
||||
if (!$firstNamespace && !$prevHasSeparator && \count($paths) > 1) {
|
||||
$rows[] = ['', ''];
|
||||
}
|
||||
$firstNamespace = false;
|
||||
foreach ($paths as $path) {
|
||||
$rows[] = [$namespace, $path.\DIRECTORY_SEPARATOR];
|
||||
$namespace = '';
|
||||
}
|
||||
if (\count($paths) > 1) {
|
||||
$rows[] = ['', ''];
|
||||
$prevHasSeparator = true;
|
||||
} else {
|
||||
$prevHasSeparator = false;
|
||||
}
|
||||
}
|
||||
if ($prevHasSeparator) {
|
||||
array_pop($rows);
|
||||
}
|
||||
$io->section('Loader Paths');
|
||||
$io->table(['Namespace', 'Paths'], $rows);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function getLoaderPaths()
|
||||
{
|
||||
if (!($loader = $this->twig->getLoader()) instanceof FilesystemLoader) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$loaderPaths = [];
|
||||
foreach ($loader->getNamespaces() as $namespace) {
|
||||
$paths = array_map(function ($path) {
|
||||
if (null !== $this->projectDir && 0 === strpos($path, $this->projectDir)) {
|
||||
$path = ltrim(substr($path, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
|
||||
}
|
||||
|
||||
return $path;
|
||||
}, $loader->getPaths($namespace));
|
||||
|
||||
if (FilesystemLoader::MAIN_NAMESPACE === $namespace) {
|
||||
$namespace = '(None)';
|
||||
} else {
|
||||
$namespace = '@'.$namespace;
|
||||
}
|
||||
|
||||
$loaderPaths[$namespace] = $paths;
|
||||
}
|
||||
|
||||
return $loaderPaths;
|
||||
}
|
||||
|
||||
private function getMetadata($type, $entity)
|
||||
{
|
||||
if ('globals' === $type) {
|
||||
return $entity;
|
||||
}
|
||||
if ('tests' === $type) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
if ('functions' === $type || 'filters' === $type) {
|
||||
$cb = $entity->getCallable();
|
||||
if (null === $cb) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
if (\is_array($cb)) {
|
||||
if (!method_exists($cb[0], $cb[1])) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
$refl = new \ReflectionMethod($cb[0], $cb[1]);
|
||||
} elseif (\is_object($cb) && method_exists($cb, '__invoke')) {
|
||||
@@ -180,9 +268,11 @@ EOF
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getPrettyMetadata($type, $entity)
|
||||
private function getPrettyMetadata($type, $entity, $decorated)
|
||||
{
|
||||
if ('tests' === $type) {
|
||||
return '';
|
||||
@@ -194,7 +284,7 @@ EOF
|
||||
return '(unknown?)';
|
||||
}
|
||||
} catch (\UnexpectedValueException $e) {
|
||||
return ' <error>'.$e->getMessage().'</error>';
|
||||
return sprintf(' <error>%s</error>', $decorated ? OutputFormatter::escape($e->getMessage()) : $e->getMessage());
|
||||
}
|
||||
|
||||
if ('globals' === $type) {
|
||||
@@ -202,7 +292,9 @@ EOF
|
||||
return ' = object('.\get_class($meta).')';
|
||||
}
|
||||
|
||||
return ' = '.substr(@json_encode($meta), 0, 50);
|
||||
$description = substr(@json_encode($meta), 0, 50);
|
||||
|
||||
return sprintf(' = %s', $decorated ? OutputFormatter::escape($description) : $description);
|
||||
}
|
||||
|
||||
if ('functions' === $type) {
|
||||
@@ -212,5 +304,7 @@ EOF
|
||||
if ('filters' === $type) {
|
||||
return $meta ? '('.implode(', ', $meta).')' : '';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
namespace Symfony\Bridge\Twig\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Console\Exception\RuntimeException;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
@@ -31,18 +33,32 @@ use Twig\Source;
|
||||
*/
|
||||
class LintCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'lint:twig';
|
||||
|
||||
private $twig;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @param Environment $twig
|
||||
*/
|
||||
public function __construct($name = 'lint:twig')
|
||||
public function __construct($twig = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
if (!$twig instanceof Environment) {
|
||||
@trigger_error(sprintf('Passing a command name as the first argument of "%s()" is deprecated since Symfony 3.4 and support for it will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED);
|
||||
|
||||
parent::__construct($twig);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
parent::__construct();
|
||||
|
||||
$this->twig = $twig;
|
||||
}
|
||||
|
||||
public function setTwigEnvironment(Environment $twig)
|
||||
{
|
||||
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
|
||||
|
||||
$this->twig = $twig;
|
||||
}
|
||||
|
||||
@@ -51,13 +67,14 @@ class LintCommand extends Command
|
||||
*/
|
||||
protected function getTwigEnvironment()
|
||||
{
|
||||
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
|
||||
|
||||
return $this->twig;
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setAliases(array('twig:lint'))
|
||||
->setDescription('Lints a template and outputs encountered errors')
|
||||
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
|
||||
->addArgument('filename', InputArgument::IS_ARRAY)
|
||||
@@ -87,21 +104,24 @@ EOF
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
if (false !== strpos($input->getFirstArgument(), ':l')) {
|
||||
$io->caution('The use of "twig:lint" command is deprecated since version 2.7 and will be removed in 3.0. Use the "lint:twig" instead.');
|
||||
// BC to be removed in 4.0
|
||||
if (__CLASS__ !== \get_class($this)) {
|
||||
$r = new \ReflectionMethod($this, 'getTwigEnvironment');
|
||||
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
|
||||
@trigger_error(sprintf('Usage of method "%s" is deprecated since Symfony 3.4 and will no longer be supported in 4.0. Construct the command with its required arguments instead.', \get_class($this).'::getTwigEnvironment'), E_USER_DEPRECATED);
|
||||
|
||||
$this->twig = $this->getTwigEnvironment();
|
||||
}
|
||||
}
|
||||
|
||||
if (null === $twig = $this->getTwigEnvironment()) {
|
||||
$io->error('The Twig environment needs to be set.');
|
||||
|
||||
return 1;
|
||||
if (null === $this->twig) {
|
||||
throw new \RuntimeException('The Twig environment needs to be set.');
|
||||
}
|
||||
|
||||
$filenames = $input->getArgument('filename');
|
||||
|
||||
if (0 === \count($filenames)) {
|
||||
if (0 !== ftell(STDIN)) {
|
||||
throw new \RuntimeException('Please provide a filename or pipe template content to STDIN.');
|
||||
throw new RuntimeException('Please provide a filename or pipe template content to STDIN.');
|
||||
}
|
||||
|
||||
$template = '';
|
||||
@@ -109,20 +129,20 @@ EOF
|
||||
$template .= fread(STDIN, 1024);
|
||||
}
|
||||
|
||||
return $this->display($input, $output, $io, array($this->validate($twig, $template, uniqid('sf_', true))));
|
||||
return $this->display($input, $output, $io, [$this->validate($template, uniqid('sf_', true))]);
|
||||
}
|
||||
|
||||
$filesInfo = $this->getFilesInfo($twig, $filenames);
|
||||
$filesInfo = $this->getFilesInfo($filenames);
|
||||
|
||||
return $this->display($input, $output, $io, $filesInfo);
|
||||
}
|
||||
|
||||
private function getFilesInfo(Environment $twig, array $filenames)
|
||||
private function getFilesInfo(array $filenames)
|
||||
{
|
||||
$filesInfo = array();
|
||||
$filesInfo = [];
|
||||
foreach ($filenames as $filename) {
|
||||
foreach ($this->findFiles($filename) as $file) {
|
||||
$filesInfo[] = $this->validate($twig, file_get_contents($file), $file);
|
||||
$filesInfo[] = $this->validate(file_get_contents($file), $file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,30 +152,30 @@ EOF
|
||||
protected function findFiles($filename)
|
||||
{
|
||||
if (is_file($filename)) {
|
||||
return array($filename);
|
||||
return [$filename];
|
||||
} elseif (is_dir($filename)) {
|
||||
return Finder::create()->files()->in($filename)->name('*.twig');
|
||||
}
|
||||
|
||||
throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename));
|
||||
throw new RuntimeException(sprintf('File or directory "%s" is not readable', $filename));
|
||||
}
|
||||
|
||||
private function validate(Environment $twig, $template, $file)
|
||||
private function validate($template, $file)
|
||||
{
|
||||
$realLoader = $twig->getLoader();
|
||||
$realLoader = $this->twig->getLoader();
|
||||
try {
|
||||
$temporaryLoader = new ArrayLoader(array((string) $file => $template));
|
||||
$twig->setLoader($temporaryLoader);
|
||||
$nodeTree = $twig->parse($twig->tokenize(new Source($template, (string) $file)));
|
||||
$twig->compile($nodeTree);
|
||||
$twig->setLoader($realLoader);
|
||||
$temporaryLoader = new ArrayLoader([(string) $file => $template]);
|
||||
$this->twig->setLoader($temporaryLoader);
|
||||
$nodeTree = $this->twig->parse($this->twig->tokenize(new Source($template, (string) $file)));
|
||||
$this->twig->compile($nodeTree);
|
||||
$this->twig->setLoader($realLoader);
|
||||
} catch (Error $e) {
|
||||
$twig->setLoader($realLoader);
|
||||
$this->twig->setLoader($realLoader);
|
||||
|
||||
return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e);
|
||||
return ['template' => $template, 'file' => $file, 'line' => $e->getTemplateLine(), 'valid' => false, 'exception' => $e];
|
||||
}
|
||||
|
||||
return array('template' => $template, 'file' => $file, 'valid' => true);
|
||||
return ['template' => $template, 'file' => $file, 'valid' => true];
|
||||
}
|
||||
|
||||
private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, $files)
|
||||
@@ -166,7 +186,7 @@ EOF
|
||||
case 'json':
|
||||
return $this->displayJson($output, $files);
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
|
||||
throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,7 +226,7 @@ EOF
|
||||
}
|
||||
});
|
||||
|
||||
$output->writeln(json_encode($filesInfo, \defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES : 0));
|
||||
$output->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
||||
|
||||
return min($errors, 1);
|
||||
}
|
||||
@@ -241,7 +261,7 @@ EOF
|
||||
$position = max(0, $line - $context);
|
||||
$max = min(\count($lines), $line - 1 + $context);
|
||||
|
||||
$result = array();
|
||||
$result = [];
|
||||
while ($position < $max) {
|
||||
$result[$position + 1] = $lines[$position];
|
||||
++$position;
|
||||
|
||||
Reference in New Issue
Block a user