Skip to content

Commit

Permalink
Converted CompileShell to CompileCommand (#11)
Browse files Browse the repository at this point in the history
* 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
othercorey and ADmad committed Mar 21, 2020
1 parent e20321d commit 59cb571
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 302 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"autoload-dev": {
"psr-4": {
"Cake\\TwigView\\Test\\": "tests/",
"App\\": "tests/test_app/src/"
"TestApp\\": "tests/test_app/src/"
}
},
"scripts": {
Expand Down
143 changes: 143 additions & 0 deletions src/Command/CompileCommand.php
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;
}
}
17 changes: 13 additions & 4 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

namespace Cake\TwigView;

use Cake\Console\CommandCollection;
use Cake\Core\BasePlugin;
use Cake\Core\Configure;
use Cake\Core\Plugin as CorePlugin;
use Cake\Core\PluginApplicationInterface;
use Cake\Event\EventManager;
use Cake\TwigView\Command\CompileCommand;

/**
* Plugin class for Cake\TwigView.
Expand All @@ -37,10 +39,7 @@ class Plugin extends BasePlugin
protected $routesEnabled = false;

/**
* Load all the plugin configuration and bootstrap logic.
*
* @param \Cake\Core\PluginApplicationInterface $app The host application
* @return void
* @inheritDoc
*/
public function bootstrap(PluginApplicationInterface $app): void
{
Expand All @@ -57,4 +56,14 @@ public function bootstrap(PluginApplicationInterface $app): void
EventManager::instance()->on(new Event\ProfilerListener());
}
}

/**
* @inheritDoc
*/
public function console(CommandCollection $commands): CommandCollection
{
$commands->add('twig-view compile', CompileCommand::class);

return $commands;
}
}
162 changes: 0 additions & 162 deletions src/Shell/CompileShell.php

This file was deleted.

Loading

0 comments on commit 59cb571

Please sign in to comment.