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