-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1068 from spiral/feature/issue-1063
- Loading branch information
Showing
4 changed files
with
150 additions
and
22 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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Scaffolder\Command; | ||
|
||
use Spiral\Boot\DirectoriesInterface; | ||
use Spiral\Console\Attribute\Argument; | ||
use Spiral\Console\Attribute\AsCommand; | ||
use Spiral\Console\Command; | ||
use Spiral\Console\Console; | ||
use Spiral\Scaffolder\Config\ScaffolderConfig; | ||
use Symfony\Component\Console\Helper\TableSeparator; | ||
|
||
#[AsCommand(name: 'scaffolder:info', description: 'Show information about available scaffolder commands.')] | ||
final class InfoCommand extends Command | ||
{ | ||
#[Argument(description: 'Class name')] | ||
private string $name = 'Example'; | ||
|
||
public function perform(ScaffolderConfig $config, DirectoriesInterface $dirs, Console $console): int | ||
{ | ||
$this->output->title('Scaffolder commands'); | ||
$this->writeln( | ||
'Scaffolder enables developers to quickly and easily generate application code for various classes, using a set of console commands', | ||
); | ||
|
||
$this->newLine(); | ||
|
||
$table = $this->table(['Command', 'Target']); | ||
$rootDir = $dirs->get('root'); | ||
$available = $config->getDeclarations(); | ||
|
||
$i = 0; | ||
foreach ($available as $name) { | ||
$command = 'create:' . $name; | ||
|
||
if (!$console->getApplication()->has($command)) { | ||
continue; | ||
} | ||
|
||
$command = $console->getApplication()->get($command); | ||
|
||
if ($i > 0) { | ||
$table->addRow(new TableSeparator()); | ||
} | ||
$declaration = $config->getDeclaration($name); | ||
|
||
$options = []; | ||
foreach ($declaration['options'] ?? [] as $key => $value) { | ||
$options[] = $key . ': <fg=yellow>' . \json_encode(\str_replace($rootDir, '', $value)) . '</>'; | ||
} | ||
|
||
$file = \str_replace($rootDir, '', $config->classFilename($name, $this->name)); | ||
$namespace = $config->classNamespace($name, $this->name); | ||
$table->addRow([ | ||
$command->getName() . "\n<fg=gray>{$command->getDescription()}</>", | ||
<<<TARGET | ||
path: <fg=green>/$file</> | ||
namespace: <fg=yellow>$namespace</> | ||
TARGET | ||
. | ||
($options !== [] ? "\n" . \implode("\n", $options) : ''), | ||
]); | ||
|
||
$i++; | ||
} | ||
|
||
$randomName = $available[\array_rand($available)]; | ||
$this->writeln( | ||
"<info>Use `<fg=yellow>php app.php create:{$randomName} {$this->name}</>` command to generate desired class. Below is a list of available commands:</info>", | ||
); | ||
|
||
$table->render(); | ||
|
||
$this->writeln( | ||
'<info>Use `<fg=yellow>php app.php create:*** --help</>` command to see available options.</info>', | ||
); | ||
|
||
$this->writeln('Read more about scaffolder in <fg=yellow>https://spiral.dev/docs/basics-scaffolding</> documentation section.'); | ||
|
||
return self::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 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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Command; | ||
|
||
use Spiral\Tests\Scaffolder\Command\AbstractCommandTestCase; | ||
|
||
final class InfoCommandTest extends AbstractCommandTestCase | ||
{ | ||
public function testInfo(): void | ||
{ | ||
$result = $this->console()->run('scaffolder:info')->getOutput()->fetch(); | ||
|
||
$strings = [ | ||
'Scaffolder commands', | ||
'create:controller', | ||
'create:bootloader', | ||
'create:config', | ||
'create:filter', | ||
'create:command', | ||
'create:middleware', | ||
'create:jobHandler', | ||
]; | ||
|
||
foreach ($strings as $string) { | ||
$this->assertStringContainsString($string, $result); | ||
} | ||
} | ||
} |