-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted CompileShell to CompileCommand (#11)
* Converted CompileShell to CompileCommand * Apply suggestions from code review Co-Authored-By: ADmad <ADmad@users.noreply.github.com> Co-authored-by: ADmad <ADmad@users.noreply.github.com>
- Loading branch information
1 parent
e20321d
commit 59cb571
Showing
14 changed files
with
347 additions
and
302 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cake\TwigView\Command; | ||
|
||
use Cake\Console\Arguments; | ||
use Cake\Console\BaseCommand; | ||
use Cake\Console\ConsoleIo; | ||
use Cake\Console\ConsoleOptionParser; | ||
use Cake\TwigView\Filesystem\Scanner; | ||
use Cake\TwigView\View\TwigView; | ||
use Exception; | ||
|
||
class CompileCommand extends BaseCommand | ||
{ | ||
/** | ||
* @var \Cake\TwigView\View\TwigView | ||
*/ | ||
protected $twigView; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser | ||
{ | ||
$parser->addArgument('type', [ | ||
'required' => true, | ||
'choices' => ['all', 'file', 'plugin'], | ||
'help' => 'The type you want to compile.', | ||
]); | ||
|
||
$parser->addArgument('target', [ | ||
'required' => false, | ||
'help' => 'The file or plugin you want to compile.', | ||
]); | ||
|
||
return $parser; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(Arguments $args, ConsoleIo $io) | ||
{ | ||
$type = $args->getArgumentAt(0); | ||
|
||
// Setup cached TwigView to avoid creating for every file | ||
$this->twigView = new TwigView(); | ||
|
||
// $type is validated by the 'choices' option in buildOptionsParser | ||
return $this->{"execute{$type}"}($args, $io); | ||
} | ||
|
||
/** | ||
* Compile all templates. | ||
* | ||
* @param \Cake\Console\Arguments $args The command arguments | ||
* @param \Cake\Console\ConsoleIo $io The console logger | ||
* @return int | ||
*/ | ||
protected function executeAll(Arguments $args, ConsoleIo $io): int | ||
{ | ||
$io->info('Compiling all templates'); | ||
|
||
foreach (Scanner::all() as $section => $templates) { | ||
$io->info("Compiling section {$section}"); | ||
foreach ($templates as $template) { | ||
if ($this->compileFile($io, $template) === static::CODE_ERROR) { | ||
return static::CODE_ERROR; | ||
} | ||
} | ||
} | ||
|
||
return static::CODE_SUCCESS; | ||
} | ||
|
||
/** | ||
* Compile all templates for a plugin. | ||
* | ||
* @param \Cake\Console\Arguments $args The command arguments | ||
* @param \Cake\Console\ConsoleIo $io The console logger | ||
* @return int | ||
*/ | ||
protected function executePlugin(Arguments $args, ConsoleIo $io): int | ||
{ | ||
$plugin = $args->getArgumentAt(1); | ||
if ($plugin === null) { | ||
$io->error('Plugin name not specified.'); | ||
|
||
return static::CODE_ERROR; | ||
} | ||
|
||
$io->info("Compiling plugin {$plugin}"); | ||
foreach (Scanner::plugin($plugin) as $template) { | ||
if ($this->compileFile($io, $template) === static::CODE_ERROR) { | ||
return static::CODE_ERROR; | ||
} | ||
} | ||
|
||
return static::CODE_SUCCESS; | ||
} | ||
|
||
/** | ||
* Compile a single template file. | ||
* | ||
* @param \Cake\Console\Arguments $args The command arguments | ||
* @param \Cake\Console\ConsoleIo $io The console logger | ||
* @return int | ||
*/ | ||
protected function executeFile(Arguments $args, ConsoleIo $io): int | ||
{ | ||
$filename = $args->getArgumentAt(1); | ||
if ($filename === null) { | ||
$io->error('File name not specified.'); | ||
|
||
return static::CODE_ERROR; | ||
} | ||
|
||
return $this->compileFile($io, $filename); | ||
} | ||
|
||
/** | ||
* Compile a single template file. | ||
* | ||
* @param \Cake\Console\ConsoleIo $io The console logger | ||
* @param string $filename The template filename | ||
* @return int | ||
*/ | ||
protected function compileFile(ConsoleIo $io, string $filename): int | ||
{ | ||
try { | ||
$this->twigView->getTwig()->load($filename); | ||
$io->success("Compiled {$filename}."); | ||
} catch (Exception $exception) { | ||
$io->error("Unable to compile {$filename}."); | ||
$io->error($exception->getMessage()); | ||
|
||
return static::CODE_ERROR; | ||
} | ||
|
||
return static::CODE_SUCCESS; | ||
} | ||
} |
This file contains 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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.