1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Tools\Console\MetadataFilter; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
9
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class GenerateRelationsCommand extends AbstractCommand |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
public const OPT_FILTER = 'filter'; |
18
|
|
|
public const OPT_FILTER_SHORT = 'f'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var RelationsGenerator |
22
|
|
|
*/ |
23
|
|
|
protected $relationsGenerator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* GenerateRelationsCommand constructor. |
27
|
|
|
* |
28
|
|
|
* @param RelationsGenerator $relationsGenerator |
29
|
|
|
* @param NamespaceHelper $namespaceHelper |
30
|
|
|
* @param null|string $name |
31
|
|
|
* |
32
|
|
|
* @throws DoctrineStaticMetaException |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
RelationsGenerator $relationsGenerator, |
36
|
|
|
NamespaceHelper $namespaceHelper, |
37
|
|
|
?string $name = null |
38
|
|
|
) { |
39
|
|
|
parent::__construct($namespaceHelper, $name); |
40
|
|
|
$this->relationsGenerator = $relationsGenerator; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws DoctrineStaticMetaException |
46
|
|
|
*/ |
47
|
|
|
protected function configure(): void |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
$this |
51
|
|
|
->setName(AbstractCommand::COMMAND_PREFIX.'generate:relations') |
52
|
|
|
->setDefinition( |
53
|
|
|
[ |
54
|
|
|
new InputOption( |
55
|
|
|
self::OPT_FILTER, |
56
|
|
|
self::OPT_FILTER_SHORT, |
57
|
|
|
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, |
58
|
|
|
'A string pattern used to match entities that should be processed.' |
59
|
|
|
), |
60
|
|
|
$this->getProjectRootPathOption(), |
61
|
|
|
$this->getProjectRootNamespaceOption(), |
62
|
|
|
$this->getSrcSubfolderOption(), |
63
|
|
|
] |
64
|
|
|
)->setDescription( |
65
|
|
|
'Generate relations traits for your entities. ' |
66
|
|
|
.'Optionally filter down the list of entities to generate relationship traits for' |
67
|
|
|
); |
68
|
|
|
} catch (\Exception $e) { |
69
|
|
|
throw new DoctrineStaticMetaException('Exception in '.__METHOD__.': '.$e->getMessage(), $e->getCode(), $e); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param InputInterface $input |
76
|
|
|
* @param OutputInterface $output |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
* @throws DoctrineStaticMetaException |
80
|
|
|
* @SuppressWarnings(PHPMD) |
81
|
|
|
*/ |
82
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
$this->checkOptions($input); |
86
|
|
|
$entityManager = $this->getEntityManager(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var ClassMetadata[] $metadatas |
90
|
|
|
*/ |
91
|
|
|
$metadatas = $entityManager->getMetadataFactory()->getAllMetadata(); |
92
|
|
|
$metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); |
93
|
|
|
$this->relationsGenerator |
94
|
|
|
->setPathToProjectRoot($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_PATH)) |
95
|
|
|
->setProjectRootNamespace($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_NAMESPACE)); |
96
|
|
|
|
97
|
|
|
$output->writeln( |
98
|
|
|
'<comment>Starting relations generation for ' |
99
|
|
|
.implode( |
100
|
|
|
' ', |
101
|
|
|
$input->getOption('filter') |
102
|
|
|
).'</comment>' |
103
|
|
|
); |
104
|
|
|
$progress = new ProgressBar($output, count($metadatas)); |
105
|
|
|
$progress::setFormatDefinition('custom', ' %current%/%max% -- %message%'); |
106
|
|
|
$progress->start(); |
107
|
|
|
foreach ($metadatas as $metadata) { |
108
|
|
|
$progress->setMessage('<comment>Generating for '.$metadata->name.'</comment>'); |
109
|
|
|
$this->relationsGenerator->generateRelationCodeForEntity($metadata->name); |
110
|
|
|
$progress->setMessage('<info>done</info>'); |
111
|
|
|
$progress->advance(); |
112
|
|
|
} |
113
|
|
|
$progress->finish(); |
114
|
|
|
$output->writeln('completed'); |
115
|
|
|
} catch (\Exception $e) { |
116
|
|
|
throw new DoctrineStaticMetaException('Exception in '.__METHOD__.': '.$e->getMessage(), $e->getCode(), $e); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|