8000 [Twig] Decouple Twig commands from the Famework by GromNaN · Pull Request #9855 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Twig] Decouple Twig commands from the Famework #9855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create method LintCommand->findFiles() to make the bridge command ind…
…ependant from the framework
  • Loading branch information
GromNaN committed Dec 30, 2013
commit c244b14d658ffa089e3d2afd096d1fb5f870f968
31 changes: 12 additions & 19 deletions src/Symfony/Bridge/Twig/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Bundle\FrameworkBundle\Console\Application as FrameworkBundleApplication;

/**
* Command that will validate your template syntax and output encountered errors.
Expand Down Expand Up @@ -70,11 +69,6 @@ protected function configure()
The command finds all twig templates in <comment>dirname</comment> and validates the syntax
of each Twig template.

<info>php %command.full_name% @AcmeMyBundle</info>

The command finds all twig templates in the <comment>AcmeMyBundle</comment> bundle and validates
the syntax of each Twig template.

<info>cat filename | php %command.full_name%</info>

The command gets the template contents from stdin and validates its syntax.
Expand All @@ -101,19 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $this->validateTemplate($twig, $output, $template);
}

if (0 !== strpos($filename, '@') && !is_readable($filename)) {
throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename));
}

$files = array();
if (is_file($filename)) {
$files = array($filename);
} elseif (is_dir($filename)) {
$files = Finder::create()->files()->in($filename)->name('*.twig');
} elseif ($this->getApplication() instanceof FrameworkBundleApplication) {
$dir = $this->getApplication()->getKernel()->locateResource($filename);
$files = Finder::create()->files()->in($dir)->name('*.twig');
}
$files = $this->findFiles($filename);

$errors = 0;
foreach ($files as $file) {
Expand All @@ -123,6 +105,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $errors > 0 ? 1 : 0;
}

protected function findFiles($filename)
{
if (is_file($filename)) {
return array($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));
}

protected function validateTemplate(\Twig_Environment $twig, OutputInterface $output, $template, $file = null)
{
try {
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bridge\Twig\Command\LintCommand as BaseLintCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\Finder\Finder;

class LintCommand extends BaseLintCommand implements ContainerAwareInterface
{
Expand All @@ -37,4 +38,33 @@ public function getTwigEnvironment()
{
7BC8 return $this->container->get('twig');
}

protected function configure()
{
parent::configure();

$this
->setHelp(
$this->getHelp().<<<EOF


<info>php %command.full_name% @AcmeMyBundle</info>

The command finds all twig templates in the <comment>AcmeMyBundle</comment> bundle and validates
the syntax of each Twig template.
EOF
)
;
}

protected function findFiles($filename)
{
if (0 === strpos($filename, '@')) {
$dir = $this->getApplication()->getKernel()->locateResource($filename);

return Finder::create()->files()->in($dir)->name('*.twig');
}

return parent::findFiles($filename);
}
}
0