1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
||
6 | |||
7 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\EntityGenerator; |
||
8 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\IdTrait; |
||
9 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
||
10 | use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
||
11 | use Exception; |
||
12 | use Symfony\Component\Console\Input\InputInterface; |
||
13 | use Symfony\Component\Console\Input\InputOption; |
||
14 | use Symfony\Component\Console\Output\OutputInterface; |
||
15 | |||
16 | class GenerateEntityCommand extends AbstractCommand |
||
17 | { |
||
18 | |||
19 | public const OPT_FQN = 'entity-fully-qualified-name'; |
||
20 | public const OPT_FQN_SHORT = 'f'; |
||
21 | public const DEFINITION_FQN = 'The fully qualified name of the entity you want to create'; |
||
22 | |||
23 | public const OPT_INT_PRIMARY_KEY = 'int-primary-key'; |
||
24 | public const OPT_INT_PRIMARY_KEY_SHORT = 'd'; |
||
25 | public const DEFINITION_UUID = 'Use an Integer primary key in place of the standard UUID primary key'; |
||
26 | |||
27 | public const OPT_ENTITY_SPECIFIC_SAVER = 'entity-specific-saver'; |
||
28 | public const OPT_ENTITY_SPECIFIC_SAVER_SHORT = 'c'; |
||
29 | public const DEFINITION_ENTITY_SPECIFIC_SAVER = 'Generate an implmentation of SaverInterface just for this entity'; |
||
30 | |||
31 | /** |
||
32 | * @var EntityGenerator |
||
33 | */ |
||
34 | protected $entityGenerator; |
||
35 | |||
36 | /** |
||
37 | * GenerateEntityCommand constructor. |
||
38 | * |
||
39 | * @param EntityGenerator $relationsGenerator |
||
40 | * @param null|string $name |
||
41 | * |
||
42 | * @throws DoctrineStaticMetaException |
||
43 | */ |
||
44 | 1 | public function __construct( |
|
45 | EntityGenerator $relationsGenerator, |
||
46 | ?string $name = null |
||
47 | ) { |
||
48 | 1 | parent::__construct($name); |
|
49 | 1 | $this->entityGenerator = $relationsGenerator; |
|
50 | 1 | } |
|
51 | |||
52 | /** |
||
53 | * @throws DoctrineStaticMetaException |
||
54 | */ |
||
55 | 1 | protected function configure(): void |
|
56 | { |
||
57 | try { |
||
58 | $this |
||
59 | 1 | ->setName(AbstractCommand::COMMAND_PREFIX . 'generate:entity') |
|
60 | 1 | ->setDefinition( |
|
61 | [ |
||
62 | 1 | new InputOption( |
|
63 | 1 | self::OPT_FQN, |
|
64 | 1 | self::OPT_FQN_SHORT, |
|
65 | 1 | InputOption::VALUE_REQUIRED, |
|
66 | 1 | self::DEFINITION_FQN |
|
67 | ), |
||
68 | 1 | new InputOption( |
|
69 | 1 | self::OPT_INT_PRIMARY_KEY, |
|
70 | 1 | self::OPT_INT_PRIMARY_KEY_SHORT, |
|
71 | 1 | InputOption::VALUE_NONE, |
|
72 | 1 | self::DEFINITION_UUID |
|
73 | ), |
||
74 | 1 | new InputOption( |
|
75 | 1 | self::OPT_ENTITY_SPECIFIC_SAVER, |
|
76 | 1 | self::OPT_ENTITY_SPECIFIC_SAVER_SHORT, |
|
77 | 1 | InputOption::VALUE_NONE, |
|
78 | 1 | self::DEFINITION_ENTITY_SPECIFIC_SAVER |
|
79 | ), |
||
80 | 1 | $this->getProjectRootPathOption(), |
|
81 | 1 | $this->getProjectRootNamespaceOption(), |
|
82 | 1 | $this->getSrcSubfolderOption(), |
|
83 | ] |
||
84 | 1 | )->setDescription( |
|
85 | 1 | 'Generate an Entity' |
|
86 | ); |
||
87 | } catch (Exception $e) { |
||
88 | throw new DoctrineStaticMetaException( |
||
89 | 'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
||
90 | $e->getCode(), |
||
91 | $e |
||
92 | ); |
||
93 | } |
||
94 | 1 | } |
|
95 | |||
96 | |||
97 | /** |
||
98 | * @param InputInterface $input |
||
99 | * @param OutputInterface $output |
||
100 | * |
||
101 | * @throws DoctrineStaticMetaException |
||
102 | */ |
||
103 | 1 | protected function execute(InputInterface $input, OutputInterface $output): void |
|
104 | { |
||
105 | try { |
||
106 | 1 | $this->checkOptions($input); |
|
107 | 1 | $output->writeln( |
|
108 | 1 | '<comment>Starting generation for ' . $input->getOption(self::OPT_FQN) . '</comment>' |
|
109 | ); |
||
110 | $idType = |
||
111 | 1 | (true !== $input->getOption(self::OPT_INT_PRIMARY_KEY)) ? |
|
112 | 1 | IdTrait::UUID_FIELD_TRAIT : IdTrait::INTEGER_ID_FIELD_TRAIT; |
|
113 | 1 | $this->entityGenerator |
|
0 ignored issues
–
show
Deprecated Code
introduced
by
![]() |
|||
114 | 1 | ->setPathToProjectRoot($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_PATH)) |
|
115 | 1 | ->setProjectRootNamespace($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_NAMESPACE)) |
|
116 | 1 | ->setPrimaryKeyType($idType); |
|
117 | 1 | $this->entityGenerator->generateEntity( |
|
118 | 1 | $input->getOption(self::OPT_FQN), |
|
119 | 1 | $input->getOption(self::OPT_ENTITY_SPECIFIC_SAVER) |
|
120 | ); |
||
121 | 1 | $output->writeln('<info>completed</info>'); |
|
122 | } catch (Exception $e) { |
||
123 | throw new DoctrineStaticMetaException( |
||
124 | 'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
||
125 | $e->getCode(), |
||
126 | $e |
||
127 | ); |
||
128 | } |
||
129 | 1 | } |
|
130 | } |
||
131 |