Skip to content

Commit 1ab4204

Browse files
committed
update AddCommand
update the compatibility with CommandLine api add CodeGeneratorRegistry property improve the logic of the command remove show help method and use HelpBuilder instead
1 parent 3f81126 commit 1ab4204

File tree

1 file changed

+96
-188
lines changed

1 file changed

+96
-188
lines changed

src/Commands/AddCommand.php

Lines changed: 96 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -9,131 +9,143 @@
99

1010
namespace DevNet\Cli\Commands;
1111

12-
use DevNet\System\Command\CommandArgument;
12+
use DevNet\Cli\Templating\CodeGeneratorProvider;
13+
use DevNet\Cli\Templating\CodeGeneratorRegistry;
14+
use DevNet\Cli\Templating\CodeModel;
15+
use DevNet\Cli\Templating\ICodeGenerator;
1316
use DevNet\System\Command\CommandEventArgs;
1417
use DevNet\System\Command\CommandLine;
15-
use DevNet\System\Command\CommandOption;
16-
use DevNet\System\Command\ICommandHandler;
1718
use DevNet\System\Text\StringBuilder;
1819
use DevNet\System\IO\ConsoleColor;
1920
use DevNet\System\IO\Console;
2021

21-
class AddCommand extends CommandLine implements ICommandHandler
22+
class AddCommand extends CommandLine implements ICodeGenerator
2223
{
24+
private CodeGeneratorRegistry $registry;
25+
2326
public function __construct()
2427
{
25-
$this->setName('add');
26-
$this->setDescription('Add template class to the project.');
27-
$this->addArgument(new CommandArgument('template'));
28-
$this->addOption(new CommandOption('--directory', '-d'));
29-
$this->addOption(new CommandOption('--name', '-n'));
30-
$this->addOption(new CommandOption('--help', '-h'));
31-
$this->addHandler($this);
28+
parent::__construct('add', 'Add template code to the project.');
29+
30+
$this->addArgument('template', 'The template code want to generate.');
31+
$this->addOption('--output', 'Location to place the generated code output.', '-o');
32+
$this->addOption('--name', 'Name of the generated code.', '-n');
33+
34+
$this->registry = CodeGeneratorRegistry::getSingleton();
35+
$this->registry->set('class', new CodeGeneratorProvider('class', 'Generate an empty class file.', $this));
36+
37+
$this->setHelp(function ($builder) {
38+
$builder->useDefaults();
39+
$builder->writeHeading('Templates:');
40+
41+
$rows = [];
42+
foreach ($this->registry as $provider) {
43+
$rows[$provider->getName()] = $provider->getDescription();
44+
}
45+
46+
$builder->writeRows($rows);
47+
});
48+
49+
$this->setHandler($this);
3250
}
3351

34-
public function execute(object $sender, CommandEventArgs $args): void
52+
public function __invoke(object $sender, CommandEventArgs $args): void
3553
{
36-
if ($args->Residual) {
37-
Console::foregroundColor(ConsoleColor::Red);
38-
Console::writeline("The specified argument or option is not valid, try '--help' option for usage information.");
54+
$template = $args->getParameter('template');
55+
$name = $args->getParameter('--name');
56+
$output = $args->getParameter('--output');
57+
58+
if (!$template || !$template->getValue()) {
59+
Console::foreGroundColor(ConsoleColor::Red);
60+
Console::writeLine("Template argument is missing!");
3961
Console::resetColor();
40-
exit;
62+
return;
4163
}
42-
43-
$namespace = 'Application';
44-
$className = null;
45-
$basePath = null;
46-
$template = $args->get('template');
47-
$help = $args->get('--help');
48-
$name = $args->get('--name');
49-
50-
if ($help) {
51-
$this->showHelp();
64+
65+
if (!$name) {
66+
Console::foreGroundColor(ConsoleColor::Red);
67+
Console::writeLine('The option --name is required!');
68+
Console::resetColor();
69+
return;
5270
}
5371

54-
if (!$template || !$template->Value) {
72+
if (!$name->getValue()) {
5573
Console::foreGroundColor(ConsoleColor::Red);
56-
Console::writeline('Template argument is missing!');
74+
Console::writeLine('The option --name is missing an argument!');
5775
Console::resetColor();
58-
exit;
76+
return;
5977
}
6078

61-
if ($name) {
62-
if (!$name->Value) {
79+
$path = '';
80+
$output = $args->getParameter('--output');
81+
if ($output) {
82+
if (!$output->getValue()) {
6383
Console::foreGroundColor(ConsoleColor::Red);
64-
Console::writeline('Name argument is missing!');
84+
Console::writeLine('The option --output is missing an argument!');
6585
Console::resetColor();
66-
exit;
86+
return;
6787
}
68-
$className = $name->Value;
69-
}
70-
71-
$directory = $args->get('--directory');
72-
if ($directory) {
73-
$basePath = $directory->Value;
88+
$path = $output->getValue();
89+
$path = trim($path, '/');
7490
}
91+
7592

76-
$templateName = $template->Value ?? '';
93+
$templateName = $template->getValue();
7794
$templateName = strtolower($templateName);
78-
$result = null;
79-
80-
switch ($templateName) {
81-
case 'class':
82-
$result = self::createClass($namespace, $className, $basePath);
83-
break;
84-
case 'controller':
85-
$result = self::createController($namespace, $className, $basePath);
86-
break;
87-
case 'migration':
88-
$result = self::createMigration($namespace, $className, $basePath);
89-
break;
90-
default:
91-
Console::foreGroundColor(ConsoleColor::Red);
92-
Console::writeline("The template {$templateName} not exist!");
93-
Console::resetColor();
94-
exit;
95-
break;
96-
}
95+
$provider = $this->registry->get($templateName);
96+
$generator = $provider->getGenerator();
9797

98-
if (!$result) {
99-
Console::foregroundColor(ConsoleColor::Red);
100-
Console::writeline("Somthing whent wrong! faild to create {$className} class.");
101-
Console::resetColor();
102-
exit;
98+
$parameters[$name->getName()] = $name->getValue();
99+
$parameters['--output'] = $path;
100+
101+
$models = $generator->generate($parameters);
102+
103+
foreach ($models as $model) {
104+
$result = $this->create($model, $path);
105+
if (!$result) {
106+
Console::foregroundColor(ConsoleColor::Red);
107+
Console::writeLine("Somthing whent wrong! faild to create {$template}.");
108+
Console::resetColor();
109+
return;
110+
}
103111
}
104112

105113
Console::foregroundColor(ConsoleColor::Green);
106-
Console::writeline("The {$templateName} {$className} was created successfully.");
114+
Console::writeLine("The template '{$templateName}' was created successfully.");
107115
Console::resetColor();
116+
}
108117

109-
exit;
118+
public function generate(array $parameters): array
119+
{
120+
$name = $parameters['--name'] ?? 'MyClass';
121+
$output = $parameters['--output'] ?? '';
122+
$output = str_replace('/', '\\', $output);
123+
$namespace = 'Application\\' . $output;
124+
$namespace = trim($namespace, '\\');
125+
$namespace = ucwords($namespace, '\\');
126+
127+
$content = new StringBuilder();
128+
$content->appendLine('<?php');
129+
$content->appendLine();
130+
$content->appendLine("namespace {$namespace};");
131+
$content->appendLine();
132+
$content->appendLine("class {$name}");
133+
$content->appendLine('{');
134+
$content->appendLine('}');
135+
136+
return [new CodeModel($name . '.php', $content)];
110137
}
111138

112-
public static function createClass(string $namespace, ?string $className, ?string $basePath): bool
139+
public function create(CodeModel $model, string $basePath): bool
113140
{
114141
$destination = implode('/', [getcwd(), $basePath]);
115-
$namespace = implode('\\', [$namespace, $basePath]);
116-
$namespace = str_replace('/', '\\', $namespace);
117-
$namespace = rtrim($namespace, '\\');
118-
$namespace = ucwords($namespace, '\\');
119-
$className = $className ?? 'MyClass';
120-
$className = ucfirst($className);
121-
122-
$context = new StringBuilder();
123-
$context->appendLine('<?php');
124-
$context->appendLine();
125-
$context->appendLine("namespace {$namespace};");
126-
$context->appendLine();
127-
$context->appendLine("class {$className}");
128-
$context->appendLine('{');
129-
$context->appendLine('}');
130142

131143
if (!is_dir($destination)) {
132144
mkdir($destination, 0777, true);
133145
}
134146

135-
$myfile = fopen($destination . '/' . $className . '.php', 'w');
136-
$size = fwrite($myfile, $context->__toString());
147+
$myfile = fopen($destination . '/' . $model->getFileName(), 'w');
148+
$size = fwrite($myfile, $model->getContent());
137149
$status = fclose($myfile);
138150

139151
if (!$size || !$status) {
@@ -142,108 +154,4 @@ public static function createClass(string $namespace, ?string $className, ?strin
142154

143155
return true;
144156
}
145-
146-
public static function createController(string $namespace, ?string $className, ?string $basePath): bool
147-
{
148-
$basePath = $basePath ?? 'Controllers';
149-
$namespace = implode('\\', [$namespace, $basePath]);
150-
$namespace = str_replace('/', '\\', $namespace);
151-
$namespace = rtrim($namespace, '\\');
152-
$namespace = ucwords($namespace, '\\');
153-
$className = $className ?? 'MyController';
154-
$className = ucfirst($className);
155-
$destination = implode('/', [getcwd(), $basePath]);
156-
157-
$context = new StringBuilder();
158-
$context->appendLine('<?php');
159-
$context->appendLine();
160-
$context->appendLine("namespace {$namespace};");
161-
$context->appendLine();
162-
$context->appendLine('use DevNet\Web\Controller\AbstractController;');
163-
$context->appendLine('use DevNet\Web\Controller\IActionResult;');
164-
$context->appendLine();
165-
$context->appendLine("class {$className} extends AbstractController");
166-
$context->appendLine('{');
167-
$context->appendLine(' public function index(): IActionResult');
168-
$context->appendLine(' {');
169-
$context->appendLine(' return $this->view();');
170-
$context->appendLine(' }');
171-
$context->appendLine('}');
172-
173-
if (!is_dir($destination)) {
174-
mkdir($destination, 0777, true);
175-
}
176-
177-
$myfile = fopen($destination . '/' . $className . '.php', 'w');
178-
$size = fwrite($myfile, $context->__toString());
179-
$status = fclose($myfile);
180-
181-
if ($size && $status) {
182-
return true;
183-
}
184-
185-
return false;
186-
}
187-
188-
public static function createMigration(string $namespace, ?string $className, ?string $basePath): bool
189-
{
190-
$basePath = $basePath ?? 'Migrations';
191-
$destination = implode('/', [getcwd(), $basePath]);
192-
$namespace = implode('\\', [$namespace, $basePath]);
193-
$namespace = str_replace('/', '\\', $namespace);
194-
$namespace = rtrim($namespace, '\\');
195-
$namespace = ucwords($namespace, '\\');
196-
$className = $className ?? 'MyMigration';
197-
$className = ucfirst($className);
198-
199-
$context = new StringBuilder();
200-
$context->appendLine('<?php');
201-
$context->appendLine();
202-
$context->appendLine("namespace {$namespace};");
203-
$context->appendLine();
204-
$context->appendLine('use DevNet\Entity\Migration\AbstractMigration;');
205-
$context->appendLine('use DevNet\Entity\Migration\MigrationBuilder;');
206-
$context->appendLine();
207-
$context->appendLine("class {$className} extends AbstractMigration");
208-
$context->appendLine('{');
209-
$context->appendLine(' public function up(MigrationBuilder $builder): void');
210-
$context->appendLine(' {');
211-
$context->appendLine(' }');
212-
$context->appendLine();
213-
$context->appendLine(' public function down(MigrationBuilder $builder): void');
214-
$context->appendLine(' {');
215-
$context->appendLine(' }');
216-
$context->appendLine('}');
217-
218-
if (!is_dir($destination)) {
219-
mkdir($destination, 0777, true);
220-
}
221-
222-
$myfile = fopen($destination . '/' . date('Ymdhis') . '_' . $className . '.php', 'w');
223-
$size = fwrite($myfile, $context->__toString());
224-
$status = fclose($myfile);
225-
226-
if ($size && $status) {
227-
return true;
228-
}
229-
230-
return false;
231-
}
232-
233-
public function showHelp()
234-
{
235-
Console::writeline('Usage: devnet new [template] [options]');
236-
Console::writeline();
237-
Console::writeline('Options:');
238-
Console::writeline(' --help, -h Displays help for this command.');
239-
Console::writeline(' --name, -n Naming the generated class.');
240-
Console::writeline(' --directory, -d Location of where to place the generated class.');
241-
Console::writeline();
242-
Console::writeline('templates:');
243-
Console::writeline(' class Simple Class');
244-
Console::writeline(' controller Controller Class');
245-
Console::writeline(' migration Migration Class');
246-
Console::writeline();
247-
exit;
248-
}
249157
}

0 commit comments

Comments
 (0)