| 1: | <?php |
| 2: | namespace TIC\MakeBundle\Maker; |
| 3: | |
| 4: | use Symfony\Component\Console\Command\Command; |
| 5: | use Symfony\Component\Console\Input\InputInterface; |
| 6: | use Symfony\Component\Console\Input\InputArgument; |
| 7: | use Symfony\Component\Console\Input\InputOption; |
| 8: | use Doctrine\Common\Annotations\Annotation; |
| 9: | |
| 10: | use Symfony\Bundle\MakerBundle\Maker\MakeEntity as OriginalMaker; |
| 11: | use Symfony\Bundle\MakerBundle\Util\ClassNameDetails; |
| 12: | use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; |
| 13: | use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper; |
| 14: | |
| 15: | use TIC\MakeBundle\Doctrine\EntityClassGenerator; |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | final class MakeEntity extends BaseMaker |
| 21: | { |
| 22: | private $doctrineHelper; |
| 23: | private $entityClassGenerator; |
| 24: | private $originalEntityMaker; |
| 25: | |
| 26: | |
| 27: | public function __construct(DoctrineHelper $doctrineHelper, EntityClassGenerator $entityClassGenerator, OriginalMaker $originalEntityMaker) |
| 28: | { |
| 29: | parent::__construct(); |
| 30: | $this->doctrineHelper = $doctrineHelper; |
| 31: | $this->entityClassGenerator = $entityClassGenerator; |
| 32: | $this->originalEntityMaker = $originalEntityMaker; |
| 33: | } |
| 34: | |
| 35: | protected $dependencies = array( |
| 36: | ['class' => Annotation::class, 'value' => 'doctrine/annotations'], |
| 37: | ); |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | protected function ticConfigure(Command $command) |
| 43: | { |
| 44: | $command |
| 45: | ->addArgument('name', InputArgument::OPTIONAL, "Nom, chemin ou classname de l'entité") |
| 46: | ->addOption('regenerate', null, InputOption::VALUE_NONE, "Regénération des méthodes (e.g. getter/setter) des champs") |
| 47: | ->addOption('overwrite', null, InputOption::VALUE_NONE, "Regénération des méthodes existantes (sinon seulement manquantes") |
| 48: | ->addOption('broadcast', 'b', InputOption::VALUE_NONE, "(inutilisé)") |
| 49: | ->addOption('api-resource', 'a', InputOption::VALUE_NONE, "(inutilisé)") |
| 50: | ; |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | protected function ticGenerate(InputInterface $input) |
| 57: | { |
| 58: | $inputName = $this->fixClassName($input->getArgument('name'), true); |
| 59: | $this->io->title("Construction de l'entité : '$inputName'"); |
| 60: | |
| 61: | |
| 62: | $variables = $this->getVariablesFromName($inputName, "Entity"); |
| 63: | $tableBase = str_replace("\\", "", str_replace("\\Entity\\", "", $variables['item_class'])); |
| 64: | $variables['table_name'] = $this->doctrineHelper->getPotentialTableName($tableBase); |
| 65: | $entityDetails = $this->generator->createClassNameDetails($inputName, "Entity\\"); |
| 66: | $repoDetails = $this->generator->createClassNameDetails($entityDetails->getRelativeName(), "Repository\\", "Repository"); |
| 67: | |
| 68: | |
| 69: | $this->io->section("Paramètres utilisés :"); |
| 70: | $this->io->table(["Variable","Valeur"], self::hash2list($variables)); |
| 71: | |
| 72: | |
| 73: | $classExists = $repoExists = false; |
| 74: | try { |
| 75: | $classExists = class_exists($variables['item_class']); |
| 76: | $repoExists = class_exists($repoDetails->getFullName()); |
| 77: | } catch (\ErrorException $e) { |
| 78: | } |
| 79: | if ($entityDetails->getFullName() !== $variables['item_class']) |
| 80: | throw new RuntimeCommandException("Le namespace de l'entité ne correspond pas à ce qui est attendu : " . $entityDetails->getFullName()); |
| 81: | if ($classExists && !$this->doctrineHelper->isClassAnnotated($variables['item_class'])) |
| 82: | throw new RuntimeCommandException("L'entité doit utiliser les annotations pour son mapping ORM"); |
| 83: | if ($this->test) return; |
| 84: | if (! $this->confirm("Lancer la construction ?")) return; |
| 85: | |
| 86: | |
| 87: | $newPaths = array(); |
| 88: | if (! $classExists) { |
| 89: | $this->io->section("Construction de l'entité..."); |
| 90: | $entityPath = $this->entityClassGenerator->generateEntityClass($entityDetails, $variables['table_name']); |
| 91: | $newPaths[] = $entityPath; |
| 92: | } else $this->io->warning("Entité déjà existante"); |
| 93: | if (! $repoExists) { |
| 94: | $this->io->section("Construction du repository..."); |
| 95: | $repoPath = $this->entityClassGenerator->generateRepositoryClass($repoDetails, $entityDetails); |
| 96: | $newPaths[] = $repoPath; |
| 97: | } |
| 98: | if (! empty($newPaths)) $this->generator->writeChanges(); |
| 99: | $this->io->listing( $newPaths ); |
| 100: | |
| 101: | |
| 102: | if ($classExists) $entityPath = (new \ReflectionClass($entityDetails->getFullName()))->getFileName(); |
| 103: | $input->setArgument('name', $variables['item_name']); |
| 104: | $input->setOption('api-resource', false); |
| 105: | $input->setOption('broadcast', false); |
| 106: | if (! $classExists) require($entityPath); |
| 107: | self::indentFile($entityPath, true); |
| 108: | $this->originalEntityMaker->generate($input, $this->io, $this->generator); |
| 109: | self::indentFile($entityPath, false); |
| 110: | |
| 111: | return true; |
| 112: | } |
| 113: | |
| 114: | } |
| 115: | |