|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Command\Schema; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Schema\Renderer\PhpSchemaRenderer; |
|
8
|
|
|
use Cycle\Schema\Renderer\SchemaToArrayConverter; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use Yiisoft\Aliases\Aliases; |
|
14
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
|
15
|
|
|
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy; |
|
16
|
|
|
|
|
17
|
|
|
final class SchemaPhpCommand extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
protected static $defaultName = 'cycle/schema/php'; |
|
20
|
|
|
protected static $defaultDescription = 'Saves the current schema in a PHP file'; |
|
21
|
|
|
|
|
22
|
|
|
private CycleDependencyProxy $promise; |
|
23
|
|
|
private Aliases $aliases; |
|
24
|
|
|
|
|
25
|
4 |
|
public function __construct(Aliases $aliases, CycleDependencyProxy $promise) |
|
26
|
|
|
{ |
|
27
|
4 |
|
$this->aliases = $aliases; |
|
28
|
4 |
|
$this->promise = $promise; |
|
29
|
4 |
|
parent::__construct(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
4 |
|
public function configure(): void |
|
33
|
|
|
{ |
|
34
|
4 |
|
$this->addArgument('file', InputArgument::OPTIONAL, 'file'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var string|null $file */ |
|
40
|
4 |
|
$file = $input->getArgument('file'); |
|
41
|
|
|
|
|
42
|
4 |
|
$converter = new SchemaToArrayConverter(); |
|
43
|
4 |
|
$schemaArray = $converter->convert($this->promise->getSchema()); |
|
44
|
|
|
|
|
45
|
4 |
|
$content = (new PhpSchemaRenderer()) |
|
46
|
4 |
|
->render($schemaArray); |
|
47
|
|
|
|
|
48
|
4 |
|
if ($file !== null) { |
|
49
|
3 |
|
$file = $this->aliases->get($file); |
|
50
|
3 |
|
$output->writeln("Destination: <fg=cyan>{$file}</>"); |
|
51
|
|
|
// Dir exists |
|
52
|
3 |
|
$dir = dirname($file); |
|
53
|
3 |
|
if (!is_dir($dir)) { |
|
54
|
1 |
|
throw new \RuntimeException("Directory {$dir} not found"); |
|
55
|
|
|
} |
|
56
|
2 |
|
if (file_put_contents($file, $content) === false) { |
|
57
|
2 |
|
return ExitCode::UNSPECIFIED_ERROR; |
|
58
|
|
|
} |
|
59
|
|
|
} else { |
|
60
|
1 |
|
$output->write($content); |
|
61
|
|
|
} |
|
62
|
3 |
|
return ExitCode::OK; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|