From b91bd78ddb495d4dcf5f3d843af3b8a079aba76c Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 21 Jun 2011 22:12:34 +0200 Subject: [PATCH] [DoctrineBundle] removed generate:doctrine:entity --- .../Command/GenerateEntityDoctrineCommand.php | 148 ------------------ 1 file changed, 148 deletions(-) delete mode 100644 src/Symfony/Bundle/DoctrineBundle/Command/GenerateEntityDoctrineCommand.php diff --git a/src/Symfony/Bundle/DoctrineBundle/Command/GenerateEntityDoctrineCommand.php b/src/Symfony/Bundle/DoctrineBundle/Command/GenerateEntityDoctrineCommand.php deleted file mode 100644 index dd53555987..0000000000 --- a/src/Symfony/Bundle/DoctrineBundle/Command/GenerateEntityDoctrineCommand.php +++ /dev/null @@ -1,148 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bundle\DoctrineBundle\Command; - -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; -use Doctrine\ORM\Tools\Export\ClassMetadataExporter; -use Doctrine\ORM\Mapping\ClassMetadataInfo; - -/** - * Initialize a new Doctrine entity inside a bundle. - * - * @author Fabien Potencier - * @author Jonathan H. Wage - */ -class GenerateEntityDoctrineCommand extends DoctrineCommand -{ - protected function configure() - { - $this - ->setName('doctrine:generate:entity') - ->setAliases(array('generate:doctrine:entity')) - ->setDescription('Generate a new Doctrine entity inside a bundle') - ->addArgument('entity', InputArgument::REQUIRED, 'The entity class name to initialize (shortcut notation)') - ->addArgument('fields', InputArgument::OPTIONAL, 'The fields to create with the new entity') - ->addOption('mapping-type', null, InputOption::VALUE_OPTIONAL, 'The mapping type to to use for the entity', 'yml') - ->setHelp(<<doctrine:generate:entity task generates a new Doctrine -entity inside a bundle: - -./app/console doctrine:generate:entity AcmeBlogBundle:Blog/Post - -The above would initialize a new entity in the following entity namespace -Acme\BlogBundle\Entity\Blog\Post. - -You can also optionally specify the fields you want to generate in the new -entity: - -./app/console doctrine:generate:entity AcmeBlogBundle:Blog/Post "title:string(255) body:text" - -By default, the command uses YAML for the mapping information; change it -with --mapping-type: - -./app/console doctrine:generate:entity AcmeBlogBundle:Blog/Post --mapping-type=annotation -EOT - ); - } - - /** - * @throws \InvalidArgumentException When the bundle doesn't end with Bundle (Example: "Bundle/MySampleBundle") - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $entity = str_replace('/', '\\', $input->getArgument('entity')); - - if (false === $pos = strpos($entity, ':')) { - throw new \InvalidArgumentException(sprintf('The entity name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Blog/Post)', $entity)); - } - - $bundleName = substr($entity, 0, $pos); - $bundle = $this->getApplication()->getKernel()->getBundle($bundleName); - - // configure the bundle (needed if the bundle does not contain any Entities yet) - $config = $this->getEntityManager(null)->getConfiguration(); - $config->setEntityNamespaces(array_merge( - array($bundle->getName() => $bundle->getNamespace().'\\Entity'), - $config->getEntityNamespaces() - )); - - $entity = substr($entity, $pos + 1); - $fullEntityClassName = $this->getContainer()->get('doctrine')->getEntityNamespace($bundleName).'\\'.$entity; - $mappingType = $input->getOption('mapping-type'); - - $class = new ClassMetadataInfo($fullEntityClassName); - $class->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); - $class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); - - // Map the specified fields - if ($fields = $input->getArgument('fields')) { - $e = explode(' ', $fields); - foreach ($e as $value) { - $e = explode(':', $value); - $name = $e[0]; - if (strlen($name)) { - $type = isset($e[1]) ? $e[1] : 'string'; - preg_match_all('/(.*)\((.*)\)/', $type, $matches); - $type = isset($matches[1][0]) ? $matches[1][0] : $type; - $length = isset($matches[2][0]) ? $matches[2][0] : null; - $class->mapField(array('fieldName' => $name, 'type' => $type, 'length' => $length)); - } - } - } - - $entityPath = $bundle->getPath().'/Entity/'.str_replace('\\', '/', $entity).'.php'; - if (file_exists($entityPath)) { - throw new \RuntimeException(sprintf('Entity "%s" already exists.', $class->name)); - } - - $entityGenerator = $this->getEntityGenerator(); - if ('annotation' === $mappingType) { - $entityGenerator->setGenerateAnnotations(true); - $entityCode = $entityGenerator->generateEntityClass($class); - $mappingPath = $mappingCode = false; - } else { - // Setup a new exporter for the mapping type specified - $cme = new ClassMetadataExporter(); - $exporter = $cme->getExporter($mappingType); - $mappingType = 'yaml' == $mappingType ? 'yml' : $mappingType; - $mappingPath = $bundle->getPath().'/Resources/config/doctrine/'.str_replace('\\', '.', $entity).'.orm.'.$mappingType; - $mappingCode = $exporter->exportClassMetadata($class); - - $entityGenerator->setGenerateAnnotations(false); - $entityCode = $entityGenerator->generateEntityClass($class); - - if (file_exists($mappingPath)) { - throw new \RuntimeException(sprintf('Cannot generate entity when mapping "%s" already exists.', $mappingPath)); - } - } - - $output->writeln(sprintf('Generating entity for "%s"', $bundle->getName())); - $output->writeln(sprintf(' > entity %s into %s', $fullEntityClassName, $entityPath)); - - if (!is_dir($dir = dirname($entityPath))) { - mkdir($dir, 0777, true); - } - file_put_contents($entityPath, $entityCode); - - if ($mappingPath) { - $output->writeln(sprintf(' > mapping into %s', $mappingPath)); - - if (!is_dir($dir = dirname($mappingPath))) { - mkdir($dir, 0777, true); - } - file_put_contents($mappingPath, $mappingCode); - } - } -}