Created
February 15, 2018 16:32
-
-
Save davidkudera/4f5a01f9e78452d63b2695184f72d14d to your computer and use it in GitHub Desktop.
PrecompileLatteCommand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Commands\Cache; | |
use Nette\Application\UI\ITemplateFactory; | |
use Nette\Utils\FileSystem; | |
use Nette\Utils\Finder; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
final class PrecompileLatteCommand extends Command | |
{ | |
/** @var \Nette\Application\UI\ITemplateFactory */ | |
private $templateFactory; | |
/** | |
* @param \Nette\Application\UI\ITemplateFactory $templateFactory | |
*/ | |
public function __construct(ITemplateFactory $templateFactory) | |
{ | |
parent::__construct(); | |
$this->templateFactory = $templateFactory; | |
} | |
public function configure() | |
{ | |
$this | |
->setName('app:cache:generate:latte') | |
->setDescription('Generate cache for all latte files.') | |
; | |
} | |
/** | |
* @param \Symfony\Component\Console\Input\InputInterface $input | |
* @param \Symfony\Component\Console\Output\OutputInterface $output | |
* @return int | |
*/ | |
public function execute(InputInterface $input, OutputInterface $output) | |
{ | |
/** @var \Nette\Bridges\ApplicationLatte\Template $template */ | |
$template = $this->templateFactory->createTemplate(); | |
/** @var \Latte\Engine $latte */ | |
$latte = $template->getLatte(); | |
$dirs = [ | |
__DIR__. '/../../', | |
__DIR__. '/../../../libs', | |
]; | |
foreach (Finder::findFiles('*.latte')->from($dirs) as $path => $file) { | |
$path = realpath($path); | |
$output->write('Compiling '. $path. '...'); | |
$temp = $latte->getCacheFile($path); | |
$code = $latte->compile($path); | |
FileSystem::createDir(pathinfo($temp, PATHINFO_DIRNAME)); | |
file_put_contents($temp, $code); | |
$output->writeln(' <info>done</info>'); | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment