Skip to content

Commit

Permalink
Implements PromptsForMissingInput (#4)
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Sep 1, 2023
1 parent 7bf5c46 commit 0260e6c
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Console\Concerns\ConfiguresPrompts;
use Illuminate\Console\Concerns\HasParameters;
use Illuminate\Console\Concerns\InteractsWithIO;
use Illuminate\Console\Concerns\PromptsForMissingInput;
use Illuminate\Console\OutputStyle;
use Illuminate\Console\View\Components\Factory;
use Illuminate\Container\Container;
Expand All @@ -20,7 +21,8 @@ abstract class Command extends \Symfony\Component\Console\Command\Command
use CallsCommands,
ConfiguresPrompts,
HasParameters,
InteractsWithIO;
InteractsWithIO,
PromptsForMissingInput;

/**
* Canvas preset.
Expand Down
115 changes: 114 additions & 1 deletion src/Commands/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Orchestra\Canvas\Core\Commands;

use Illuminate\Console\Concerns\CreatesMatchingTest;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Orchestra\Canvas\Core\CodeGenerator;
use Orchestra\Canvas\Core\Contracts\GeneratesCodeListener;
use Orchestra\Canvas\Core\GeneratesCode;
Expand All @@ -15,7 +16,7 @@
* @property string|null $name
* @property string|null $description
*/
abstract class Generator extends Command implements GeneratesCodeListener
abstract class Generator extends Command implements GeneratesCodeListener, PromptsForMissingInput
{
use CodeGenerator;

Expand Down Expand Up @@ -43,6 +44,96 @@ abstract class Generator extends Command implements GeneratesCodeListener
*/
protected string $processor = GeneratesCode::class;

/**
* Reserved names that cannot be used for generation.
*
* @var array<int, string>
*/
protected array $reservedNames = [
'__halt_compiler',
'abstract',
'and',
'array',
'as',
'break',
'callable',
'case',
'catch',
'class',
'clone',
'const',
'continue',
'declare',
'default',
'die',
'do',
'echo',
'else',
'elseif',
'empty',
'enddeclare',
'endfor',
'endforeach',
'endif',
'endswitch',
'endwhile',
'enum',
'eval',
'exit',
'extends',
'false',
'final',
'finally',
'fn',
'for',
'foreach',
'function',
'global',
'goto',
'if',
'implements',
'include',
'include_once',
'instanceof',
'insteadof',
'interface',
'isset',
'list',
'match',
'namespace',
'new',
'or',
'print',
'private',
'protected',
'public',
'readonly',
'require',
'require_once',
'return',
'self',
'static',
'switch',
'throw',
'trait',
'true',
'try',
'unset',
'use',
'var',
'while',
'xor',
'yield',
'__CLASS__',
'__DIR__',
'__FILE__',
'__FUNCTION__',
'__LINE__',
'__METHOD__',
'__NAMESPACE__',
'__TRAIT__',
];

/**
* Construct a new generator command.
*/
Expand Down Expand Up @@ -81,6 +172,15 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// First we need to ensure that the given name is not a reserved word within the PHP
// language and that the class name will actually be valid. If it is not valid we
// can error now and prevent from polluting the filesystem using invalid files.
if ($this->isReservedName($name = $this->generatorName())) {
$this->components->error('The name "'.$name.'" is reserved by PHP.');

return Command::FAILURE;
}

$force = $this->hasOption('force') && $this->option('force') === true;

return $this->generateCode($force);
Expand Down Expand Up @@ -143,4 +243,17 @@ public function generatorName(): string
return trim($name);
});
}

/**
* Checks whether the given name is reserved.
*
* @param string $name
* @return bool
*/
protected function isReservedName($name)
{
$name = strtolower($name);

return in_array($name, $this->reservedNames);
}
}

0 comments on commit 0260e6c

Please sign in to comment.