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