|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Scaffolder\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Spiral\Console\Command; |
|
9
|
|
|
use Spiral\Core\FactoryInterface; |
|
10
|
|
|
use Spiral\Files\FilesInterface; |
|
11
|
|
|
use Spiral\Reactor\Writer; |
|
12
|
|
|
use Spiral\Scaffolder\Config\ScaffolderConfig; |
|
13
|
|
|
use Spiral\Scaffolder\Declaration\DeclarationInterface; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractCommand extends Command |
|
16
|
|
|
{ |
|
17
|
44 |
|
public function __construct( |
|
18
|
|
|
protected ScaffolderConfig $config, |
|
19
|
|
|
protected FilesInterface $files, |
|
20
|
|
|
ContainerInterface $container, |
|
21
|
|
|
private readonly FactoryInterface $factory, |
|
22
|
|
|
) { |
|
23
|
44 |
|
$this->setContainer($container); |
|
24
|
|
|
|
|
25
|
44 |
|
parent::__construct(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @template TClass of DeclarationInterface |
|
30
|
|
|
* |
|
31
|
|
|
* @param class-string<TClass> $class |
|
|
|
|
|
|
32
|
|
|
* @return TClass |
|
33
|
|
|
*/ |
|
34
|
21 |
|
protected function createDeclaration(string $class): DeclarationInterface |
|
35
|
|
|
{ |
|
36
|
21 |
|
return $this->factory->make( |
|
37
|
21 |
|
$class, |
|
38
|
21 |
|
[ |
|
39
|
21 |
|
'name' => (string)$this->argument('name'), |
|
40
|
21 |
|
'comment' => $this->getComment(), |
|
41
|
21 |
|
'namespace' => $this->getNamespace(), |
|
42
|
21 |
|
] + $this->config->declarationOptions($class::TYPE), |
|
43
|
21 |
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Write declaration into file. |
|
48
|
|
|
*/ |
|
49
|
21 |
|
protected function writeDeclaration(DeclarationInterface $declaration): void |
|
50
|
|
|
{ |
|
51
|
21 |
|
$filename = $this->config->classFilename( |
|
52
|
21 |
|
$declaration::TYPE, |
|
53
|
21 |
|
(string)$this->argument('name'), |
|
54
|
21 |
|
$this->getNamespace(), |
|
55
|
21 |
|
); |
|
56
|
21 |
|
$filename = $this->files->normalizePath($filename); |
|
57
|
21 |
|
$className = $declaration->getClass()->getName(); |
|
58
|
|
|
|
|
59
|
21 |
|
if ($this->files->exists($filename)) { |
|
60
|
2 |
|
$this->writeln( |
|
61
|
2 |
|
\sprintf("<fg=red>Unable to create '<comment>%s</comment>' declaration, ", $className) |
|
62
|
2 |
|
. \sprintf("file '<comment>%s</comment>' already exists.</fg=red>", $filename), |
|
63
|
2 |
|
); |
|
64
|
|
|
|
|
65
|
2 |
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
//File declaration |
|
69
|
20 |
|
(new Writer($this->files))->write($filename, $declaration->getFile()); |
|
70
|
|
|
|
|
71
|
20 |
|
$this->writeln( |
|
72
|
20 |
|
\sprintf("Declaration of '<info>%s</info>' ", $className) |
|
73
|
20 |
|
. \sprintf("has been successfully written into '<comment>%s</comment>'.", $filename), |
|
74
|
20 |
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function getNamespace(): ?string |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->hasOption('namespace') ? $this->option('namespace') : null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function getComment(): ?string |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->hasOption('comment') ? $this->option('comment') : null; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|