winter/modules/system/console/CreateCommand.php

73 lines
2.1 KiB
PHP
Raw Normal View History

<?php namespace System\Console;
use InvalidArgumentException;
use System\Console\BaseScaffoldCommand;
class CreateCommand extends BaseScaffoldCommand
{
/**
2022-02-24 22:02:01 -06:00
* @var string|null The default command name for lazy loading.
*/
protected static $defaultName = 'create:command';
/**
2022-02-24 22:02:01 -06:00
* @var string The name and signature of this command.
*/
protected $signature = 'create:command
{plugin : The name of the plugin. <info>(eg: Winter.Blog)</info>}
{name : The name of the command to generate. <info>(eg: ImportPosts)</info>}
{--command= : The terminal command that should be assigned. <info>(eg: blog:importposts)</info>}
2022-02-24 22:02:01 -06:00
{--f|force : Overwrite existing files with generated files.}';
/**
2022-02-24 22:02:01 -06:00
* @var string The console command description.
*/
protected $description = 'Creates a new console command.';
/**
* @var array List of commands that this command replaces (aliases)
*/
protected $replaces = [
'make:command',
];
/**
2022-02-24 22:02:01 -06:00
* @var string The type of class being generated.
*/
protected $type = 'Command';
/**
2022-02-24 22:02:01 -06:00
* @var array A mapping of stubs to generated files.
*/
protected $stubs = [
'scaffold/command/command.stub' => 'console/{{studly_name}}.php',
];
/**
* Prepare variables for stubs.
*
2022-02-24 22:02:01 -06:00
* @return array
*/
protected function prepareVars()
{
$parts = explode('.', $this->getPluginIdentifier());
$plugin = array_pop($parts);
$author = array_pop($parts);
$name = $this->getNameInput();
$command = trim($this->option('command') ?? strtolower("{$plugin}:{$name}"));
// More strict than the base Symfony validateName()
// method, make a PR if it's a problem for you
if (preg_match('/^[a-z]++(:[a-z]++)*$/', $command) !== 1) {
throw new InvalidArgumentException(sprintf('Command name "%s" is invalid.', $command));
}
return [
'name' => $name,
'command' => $command,
'author' => $author,
'plugin' => $plugin,
];
}
}