8000 Symfony Console · InitPHP/Framework3@b0c3c55 · GitHub
[go: up one dir, main page]

Skip to content

Commit b0c3c55

Browse files
committed
Symfony Console
1 parent 5403065 commit b0c3c55

16 files changed

+197
-218
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
"nesbot/carbon": "^2.72",
4545
"initphp/cookies": "^1.1",
4646
"initphp/logger": "^1.0",
47-
"initphp/console": "^2.0",
4847
"initphp/upload": "^1.0",
4948
"filp/whoops": "^2.15",
50-
"initphp/performance-meter": "^1.0"
49+
"initphp/performance-meter": "^1.0",
50+
"symfony/console": "^6.4"
5151
},
5252
"require-dev": {
5353
"symfony/var-dumper": "^6.4"

system/Console/Command.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
declare(strict_types=1);
1515
namespace InitPHP\Framework\Console;
1616

17-
abstract class Command extends \InitPHP\Console\Command
17+
abstract class Command extends \Symfony\Component\Console\Command\Command
1818
{
19+
20+
public const SUCCESS = 0;
21+
public const FAILURE = 1;
22+
public const INVALID = 2;
23+
1924
}

system/Console/Commands/KeyGenerateCommand.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,27 @@
1414
declare(strict_types=1);
1515
namespace InitPHP\Framework\Console\Commands;
1616

17+
use InitPHP\Framework\Console\Command;
1718
use InitPHP\Framework\Console\Utils\ChangeDotEnv;
18-
use \InitPHP\Console\{Input, Output};
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Output\OutputInterface;
1921

20-
class KeyGenerateCommand extends \InitPHP\Framework\Console\Command
22+
class KeyGenerateCommand extends Command
2123
{
2224

23-
/** @var string Command */
24-
public $command = 'key:generate';
25+
protected static $defaultName = 'key:generate';
2526

26-
public function execute(Input $input, Output $output)
27+
public function execute(InputInterface $input, OutputInterface $output): int
2728
{
2829
$key = '"' . base64_encode(random_bytes(16)) . '"';
29-
if ((new ChangeDotEnv())->change('APP_KEY', $key)->save()) {
30-
$output->success("Ok");
31-
} else {
32-
$output->error("Failed");
33-
}
34-
}
3530

36-
public function definition(): string
37-
{
38-
return 'Generates and replaces a new APP_KEY.';
31+
return ((new ChangeDotEnv())->change('APP_KEY', $key)->save()) ? Command::SUCCESS : Command::FAILURE;
3932
}
4033

41-
public function arguments(): array
34+
protected function configure(): void
4235
{
43-
return [];
36+
$this->setDescription('Creates a command.')
37+
->setHelp('');
4438
}
4539

46-
}
40+
}

system/Console/Commands/MakeCommandCommand.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,26 @@
1414
declare(strict_types=1);
1515
namespace InitPHP\Framework\Console\Commands;
1616

17-
use \InitPHP\Console\{Input, Output};
1817
use InitPHP\Framework\Console\Command;
1918
use InitPHP\Framework\Console\Utils\MakeFile;
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Input\InputOption;
22+
use Symfony\Component\Console\Output\OutputInterface;
2023

2124
class MakeCommandCommand extends Command
2225
{
2326

24-
public $command = 'make:command';
27+
protected static $defaultName = 'make:command';
2528

26-
public function execute(Input $input, Output $output)
29+
public function execute(InputInterface $input, OutputInterface $output): int
2730
{
28-
$name = !$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0);
31+
32+
$name = $input->getArgument('name');
33+
2934
$path = APP_DIR . "Console/Commands/";
3035
$namespace = "App\\Console\\Commands";
31-
if ($input->hasOption('s')) {
36+
if ($input->getOption('system')) {
3237
$path = SYS_DIR . "Console/Commands/";
3338
$namespace = "InitPHP\\Framework\\Console\\Commands";
3439
}
@@ -41,16 +46,15 @@ public function execute(Input $input, Output $output)
4146
$path .= $name . ".php";
4247
$make = new MakeFile(SYS_DIR . "Console/Templates/Command.txt");
4348

44-
if ($make->to($path, ["name" => $name, "namespace" => $namespace])) {
45-
$output->success("Ok");
46-
} else {
47-
$output->error("Error");
48-
}
49+
return $make->to($path, ["name" => $name, "namespace" => $namespace]) ? Command::SUCCESS : Command::FAILURE;
4950
}
5051

51-
public function definition(): string
52+
protected function configure(): void
5253
{
53-
return 'Creates a command.';
54+
$this->setDescription('Creates a command.')
55+
->setHelp('--name=CommandName')
56+
->addArgument('name', InputArgument::REQUIRED, 'The name of the command class.')
57+
->addOption('system', 's', InputOption::VALUE_OPTIONAL, 'Creates a system command.');
5458
}
5559

5660
}

system/Console/Commands/MakeControllerCommand.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@
1414
declare(strict_types=1);
1515
namespace InitPHP\Framework\Console\Commands;
1616

17+
use InitPHP\Framework\Console\Command;
1718
use InitPHP\Framework\Console\Utils\MakeFile;
18-
use \InitPHP\Console\{Input, Output};
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Output\OutputInterface;
1922

20-
class MakeControllerCommand extends \InitPHP\Framework\Console\Command
23+
class MakeControllerCommand extends Command
2124
{
2225

23-
/** @var string Command */
24-
public $command = 'make:controller';
26+
protected static $defaultName = 'make:controller';
2527

26-
public function execute(Input $input, Output $output)
28+
public function execute(InputInterface $input, OutputInterface $output): int
2729
{
28-
$name = trim((!$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0)), "/");
30+
$name = $input->getArgument('name');
31+
2932
$path = APP_DIR . "HTTP/Controllers/";
3033
$namespace = "App\\HTTP\\Controllers";
3134

@@ -39,21 +42,14 @@ public function execute(Input $input, Output $output)
3942
$path .= $name . ".php";
4043
$make = new MakeFile(SYS_DIR . "Console/Templates/Controller.txt");
4144

42-
if ($make->to($path, ["name" => $name, "namespace" => $namespace])) {
43-
$output->success("Ok");
44-
} else {
45-
$output->error("Error");
46-
}
47-
}
48-
49-
public function definition(): string
50-
{
51-
return 'Creates a controller.';
45+
return $make->to($path, ["name" => $name, "namespace" => $namespace]) ? Command::SUCCESS : Command::FAILURE;
5246
}
5347

54-
public function arguments(): array
48+
protected function configure(): void
5549
{
56-
return [];
50+
$this->setDescription('Creates a controller.')
51+
->setHelp('--name=ControllerName')
52+
->addArgument('name', InputArgument::REQUIRED, 'The name of the controller class.');
5753
}
5854

5955
}

system/Console/Commands/MakeEntityCommand.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,29 @@
1414
declare(strict_types=1);
1515
namespace InitPHP\Framework\Console\Commands;
1616

17+
use InitPHP\Framework\Console\Command;
1718
use InitPHP\Framework\Console\Utils\MakeFile;
18-
use \InitPHP\Console\{Input, Output};
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Output\OutputInterface;
1922

20-
class MakeEntityCommand extends \InitPHP\Framework\Console\Command
23+
class MakeEntityCommand extends Command
2124
{
2225

23-
/** @var string Command */
24-
public $command = 'make:entity';
26+
protected static $defaultName = 'make:entity';
2527

26-
public function execute(Input $input, Output $output)
28+
public function execute(InputInterface $input, OutputInterface $output): int
2729
{
28-
$name = trim((!$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0)), "/");
30+
$name = trim($input->getArgument('name'), "/");
2931

30-
if (!empty(self::makeEntity($name))) {
31-
$output->success("Ok");
32-
} else {
33-
$output->error("Error");
34-
}
35-
}
36-
37-
public function definition(): string
38-
{
39-
return 'Creates a entity.';
32+
return !empty(self::makeEntity($name)) ? Command::SUCCESS : Command::FAILURE;
4033
}
4134

42-
public function arguments(): array
35+
protected function configure(): void
4336
{
44-
return [];
37+
$this->setDescription('Creates a entity.')
38+
->setHelp('--name=EntityName')
39+
->addArgument('name', InputArgument::REQUIRED, 'The name of the entity class.');
4540
}
4641

4742
public static function makeEntity(string $name): ?string

system/Console/Commands/MakeMiddlewareCommand.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,25 @@
1515
namespace InitPHP\Framework\Console\Commands;
1616

1717
use InitPHP\Framework\Console\Utils\MakeFile;
18-
use \InitPHP\Console\{Input, Output};
18+
use \InitPHP\Framework\Console\Command;
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Output\OutputInterface;
1922

20-
class MakeMiddlewareCommand extends \InitPHP\Framework\Console\Command
23+
class MakeMiddlewareCommand extends Command
2124
{
2225

23-
/** @var string Command */
24-
public $command = 'make:middleware';
26+
protected static $defaultName = 'make:middleware';
2527

26-
public function execute(Input $input, Output $output)
28+
protected function configure(): void
2729
{
28-
$name = trim((!$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0)), "/");
30+
$this->setDescription('Creates a middleware.')
31+
->addArgument('name', InputArgument::REQUIRED, 'Middleware Name');
32+
}
33+
34+
protected function execute(InputInterface $input, OutputInterface $output): int
35+
{
36+
$name = trim($input->getArgument('name'), "/");
2937

3038
$path = APP_DIR . "HTTP/Middlewares/";
3139
$namespace = "App\\HTTP\\Middlewares";
@@ -39,21 +47,7 @@ public function execute(Input $input, Output $output)
3947
$path .= $name . ".php";
4048
$make = new MakeFile(SYS_DIR . "Console/Templates/Middleware.txt");
4149

42-
if ($make->to($path, ["name" => $name, "namespace" => $namespace])) {
43-
$output->success("Ok");
44-
} else {
45-
$output->error("Error");
46-
}
47-
}
48-
49-
public function definition(): string
50-
{
51-
return 'Creates a middleware.';
52-
}
53-
54-
public function arguments(): array
55-
{
56-
return [];
50+
return $make->to($path, ["name" => $name, "namespace" => $namespace]) ? Command::SUCCESS : Command::FAILURE;
5751
}
5852

5953
}

system/Console/Commands/MakeModelCommand.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,30 @@
1515
namespace InitPHP\Framework\Console\Commands;
1616

1717
use InitPHP\Framework\Console\Utils\MakeFile;
18-
use \InitPHP\Console\{Input, Output};
18+
use \InitPHP\Framework\Console\Command;
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Input\InputOption;
22+
use Symfony\Component\Console\Output\OutputInterface;
1923

20-
class MakeModelCommand extends \InitPHP\Framework\Console\Command
24+
class MakeModelCommand extends Command
2125
{
2226

23-
/** @var string Command */
24-
public $command = 'make:model';
27+
protected static $defaultName = 'make:model';
2528

26-
public function execute(Input $input, Output $output)
29+
protected function configure(): void
2730
{
28-
$name = trim((!$input->hasSegment(0) ? $output->ask("Name ?", false) : $input->getSegment(0)), "/");
31+
$this->setDescription('Creates a model.')
32+
->addArgument('name', InputArgument::REQUIRED, 'Model class name')
33+
->addOption('entity', 'e', InputOption::VALUE_OPTIONAL, 'Create Entity Class.');
34+
}
35+
36+
protected function execute(InputInterface $input, OutputInterface $output): int
37+
{
38+
$name = trim($input->getArgument('name'), "/");
2939

3040
$entity = null;
31-
if ($input->hasOption('e')) {
41+
if ($input->hasOption('entity')) {
3242
$entity = MakeEntityCommand::makeEntity($name);
3343
}
3444
empty($entity) && $entity = "\\InitPHP\\Framework\\Database\\Entity::class";
@@ -45,21 +55,9 @@ public function execute(Input $input, Output $output)
4555
$path .= $name . ".php";
4656
$make = new MakeFile(SYS_DIR . "Console/Templates/Model.txt");
4757

48-
if ($make->to($path, ["name" => $name, "namespace" => $namespace, 'entity' => $entity, 'schema' => camelCase2SnakeCase($name)])) {
49-
$output->success("Ok");
50-
} else {
51-
$output->error("Error");
52-
}
53-
}
54-
55-
public function definition(): string
56-
{
57-
return 'Creates a model.';
58-
}
59-
60-
public function arguments(): array
61-
{
62-
return [];
58+
return $make->to($path, ["name" => $name, "namespace" => $namespace, 'entity' => $entity, 'schema' => camelCase2SnakeCase($name)])
59+
? Command::SUCCESS
60+
: Command::FAILURE;
6361
}
6462

6563
}

0 commit comments

Comments
 (0)
0