From 700060cfb27f3b44f8fce811be35d234fd42bc1c Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 11 Apr 2010 09:30:01 +0200 Subject: [PATCH] DDC-510 Refactored all Command Tools to use ClassMetadataFactory instead of ClassMetadataReader --- .../Command/ConvertDoctrine1SchemaCommand.php | 4 +- .../Console/Command/ConvertMappingCommand.php | 49 ++++---------- .../Command/GenerateEntitiesCommand.php | 66 ++++++------------- .../Command/GenerateProxiesCommand.php | 43 +++--------- .../Command/GenerateRepositoriesCommand.php | 43 +++--------- 5 files changed, 56 insertions(+), 149 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php index 48ce45fb0..ae297c84f 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ConvertDoctrine1SchemaCommand.php @@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command; use Symfony\Components\Console\Input\InputArgument, Symfony\Components\Console\Input\InputOption, - Symfony\Components\Console; + Symfony\Components\Console, + Doctrine\ORM\Tools\Export\ClassMetadataExporter, + Doctrine\ORM\Tools\ConvertDoctrine1Schema; /** * Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file. diff --git a/lib/Doctrine/ORM/Tools/Console/Command/ConvertMappingCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/ConvertMappingCommand.php index 45bc79714..48e6f3eb2 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/ConvertMappingCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/ConvertMappingCommand.php @@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command; use Symfony\Components\Console\Input\InputArgument, Symfony\Components\Console\Input\InputOption, - Symfony\Components\Console; + Symfony\Components\Console, + Doctrine\ORM\Tools\Console\MetadataFilter, + Doctrine\ORM\Tools\Export\ClassMetadataExporter; /** * Command to convert your mapping information between the various formats. @@ -48,8 +50,9 @@ class ConvertMappingCommand extends Console\Command\Command ->setName('orm:convert-mapping') ->setDescription('Convert mapping information between supported formats.') ->setDefinition(array( - new InputArgument( - 'from-path', InputArgument::REQUIRED, 'The path of mapping information.' + new InputOption( + 'filter', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, + 'A string pattern used to match entities that should be processed.' ), new InputArgument( 'to-type', InputArgument::REQUIRED, 'The mapping type to be converted.' @@ -58,10 +61,8 @@ class ConvertMappingCommand extends Console\Command\Command 'dest-path', InputArgument::REQUIRED, 'The path to generate your entities classes.' ), - new InputOption( - 'from', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, - 'Optional paths of mapping information.', - array() + new InputArgument( + 'from-database', InputArgument::OPTIONAL, 'The path of mapping information.' ), new InputOption( 'extend', null, InputOption::PARAMETER_OPTIONAL, @@ -84,37 +85,16 @@ EOT protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); - $cme = new ClassMetadataExporter(); - // Process source directories - $fromPath = $input->getArgument('from-path'); + $metadatas = $em->getMetadataFactory()->getAllMetadata(); + $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); - if (strtolower($fromPath) !== 'database') { - $fromPaths = array_merge(array($fromPath), $input->getOption('from')); - - foreach ($fromPaths as &$dirName) { - $dirName = realpath($dirName); - - if ( ! file_exists($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not exist.", $dirName) - ); - } else if ( ! is_readable($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not have read permissions.", $dirName) - ); - } - - $cme->addMappingSource($dirName); - } - } else { + if ($input->getArgument('from-database') === true) { $em->getConfiguration()->setMetadataDriverImpl( new \Doctrine\ORM\Mapping\Driver\DatabaseDriver( $em->getConnection()->getSchemaManager() ) ); - - $cme->addMappingSource($fromPath); } // Process destination directory @@ -132,6 +112,7 @@ EOT $toType = strtolower($input->getArgument('to-type')); + $cme = new ClassMetadataExporter(); $exporter = $cme->getExporter($toType, $destPath); if ($toType == 'annotation') { @@ -145,9 +126,7 @@ EOT } } - $metadatas = $cme->getMetadatas(); - - if ($metadatas) { + if (count($metadatas)) { foreach ($metadatas as $metadata) { $output->write(sprintf('Processing entity "%s"', $metadata->name) . PHP_EOL); } @@ -156,7 +135,7 @@ EOT $exporter->export(); $output->write(PHP_EOL . sprintf( - 'Exporting "%s" mapping information to "%s"', $toType, $destPath + 'Exporting "%s" mapping information to "%s"' . PHP_EOL, $toType, $destPath )); } else { $output->write('No Metadata Classes to process.' . PHP_EOL); diff --git a/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php index 0cf0230dd..119a08c1b 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php @@ -23,7 +23,9 @@ namespace Doctrine\ORM\Tools\Console\Command; use Symfony\Components\Console\Input\InputArgument, Symfony\Components\Console\Input\InputOption, - Symfony\Components\Console; + Symfony\Components\Console, + Doctrine\ORM\Tools\Console\MetadataFilter, + Doctrine\ORM\Tools\EntityGenerator; /** * Command to generate entity classes and method stubs from your mapping information. @@ -48,17 +50,13 @@ class GenerateEntitiesCommand extends Console\Command\Command ->setName('orm:generate-entities') ->setDescription('Generate entity classes and method stubs from your mapping information.') ->setDefinition(array( - new InputArgument( - 'from-path', InputArgument::REQUIRED, 'The path of mapping information.' + new InputOption( + 'filter', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, + 'A string pattern used to match entities that should be processed.' ), new InputArgument( 'dest-path', InputArgument::REQUIRED, 'The path to generate your entity classes.' ), - new InputOption( - 'from', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, - 'Optional paths of mapping information.', - array() - ), new InputOption( 'generate-annotations', null, InputOption::PARAMETER_OPTIONAL, 'Flag to define if generator should generate annotation metadata on entities.', false @@ -96,29 +94,10 @@ EOT protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); - - $reader = new ClassMetadataReader(); - $reader->setEntityManager($em); - - // Process source directories - $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from')); - - foreach ($fromPaths as $dirName) { - $dirName = realpath($dirName); - - if ( ! file_exists($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not exist.", $dirName) - ); - } else if ( ! is_readable($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not have read permissions.", $dirName) - ); - } - - $reader->addMappingSource($dirName); - } - + + $metadatas = $em->getMetadataFactory()->getAllMetadata(); + $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); + // Process destination directory $destPath = realpath($input->getArgument('dest-path')); @@ -132,23 +111,20 @@ EOT ); } - // Create EntityGenerator - $entityGenerator = new EntityGenerator(); + if ( count($metadatas)) { + // Create EntityGenerator + $entityGenerator = new EntityGenerator(); - $entityGenerator->setGenerateAnnotations($input->getOption('generate-annotations')); - $entityGenerator->setGenerateStubMethods($input->getOption('generate-methods')); - $entityGenerator->setRegenerateEntityIfExists($input->getOption('regenerate-entities')); - $entityGenerator->setUpdateEntityIfExists($input->getOption('update-entities')); - $entityGenerator->setNumSpaces($input->getOption('num-spaces')); + $entityGenerator->setGenerateAnnotations($input->getOption('generate-annotations')); + $entityGenerator->setGenerateStubMethods($input->getOption('generate-methods')); + $entityGenerator->setRegenerateEntityIfExists($input->getOption('regenerate-entities')); + $entityGenerator->setUpdateEntityIfExists($input->getOption('update-entities')); + $entityGenerator->setNumSpaces($input->getOption('num-spaces')); - if (($extend = $input->getOption('extend')) !== null) { - $entityGenerator->setClassToExtend($extend); - } + if (($extend = $input->getOption('extend')) !== null) { + $entityGenerator->setClassToExtend($extend); + } - // Retrieving ClassMetadatas - $metadatas = $reader->getMetadatas(); - - if ( ! empty($metadatas)) { foreach ($metadatas as $metadata) { $output->write( sprintf('Processing entity "%s"', $metadata->name) . PHP_EOL diff --git a/lib/Doctrine/ORM/Tools/Console/Command/GenerateProxiesCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/GenerateProxiesCommand.php index 4b6333b6f..5f71125d0 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/GenerateProxiesCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/GenerateProxiesCommand.php @@ -23,7 +23,8 @@ namespace Doctrine\ORM\Tools\Console\Command; use Symfony\Components\Console\Input\InputArgument, Symfony\Components\Console\Input\InputOption, - Symfony\Components\Console; + Symfony\Components\Console, + Doctrine\ORM\Tools\Console\MetadataFilter; /** * Command to (re)generate the proxy classes used by doctrine. @@ -48,18 +49,14 @@ class GenerateProxiesCommand extends Console\Command\Command ->setName('orm:generate-proxies') ->setDescription('Generates proxy classes for entity classes.') ->setDefinition(array( - new InputArgument( - 'from-path', InputArgument::REQUIRED, 'The path of mapping information.' + new InputOption( + 'filter', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, + 'A string pattern used to match entities that should be processed.' ), new InputArgument( 'dest-path', InputArgument::OPTIONAL, 'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.' ), - new InputOption( - 'from', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, - 'Optional paths of mapping information.', - array() - ) )) ->setHelp(<<getHelper('em')->getEntityManager(); - - $reader = new ClassMetadataReader(); - $reader->setEntityManager($em); - - // Process source directories - $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from')); - - foreach ($fromPaths as $dirName) { - $dirName = realpath($dirName); - - if ( ! file_exists($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not exist.", $dirName) - ); - } else if ( ! is_readable($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not have read permissions.", $dirName) - ); - } - - $reader->addMappingSource($dirName); - } + + $metadatas = $em->getMetadataFactory()->getAllMetadata(); + $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); // Process destination directory if (($destPath = $input->getArgument('dest-path')) === null) { @@ -113,10 +91,7 @@ EOT ); } - // Retrieving ClassMetadatas - $metadatas = $reader->getMetadatas(); - - if ( ! empty($metadatas)) { + if ( count($metadatas)) { foreach ($metadatas as $metadata) { $output->write( sprintf('Processing entity "%s"', $metadata->name) . PHP_EOL diff --git a/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php index 1d24fccb3..9c1477fcd 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php @@ -23,7 +23,8 @@ namespace Doctrine\ORM\Tools\Console\Command; use Symfony\Components\Console\Input\InputArgument, Symfony\Components\Console\Input\InputOption, - Symfony\Components\Console; + Symfony\Components\Console, + Doctrine\ORM\Tools\Console\MetadataFilter; /** * Command to generate repository classes for mapping information. @@ -65,16 +66,12 @@ class extends EntityRepository ->setName('orm:generate-repositories') ->setDescription('Generate repository classes from your mapping information.') ->setDefinition(array( - new InputArgument( - 'from-path', InputArgument::REQUIRED, 'The path of mapping information.' + new InputOption( + 'filter', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, + 'A string pattern used to match entities that should be processed.' ), new InputArgument( 'dest-path', InputArgument::REQUIRED, 'The path to generate your repository classes.' - ), - new InputOption( - 'from', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, - 'Optional paths of mapping information.', - array() ) )) ->setHelp(<<getHelper('em')->getEntityManager(); - - $reader = new ClassMetadataReader(); - $reader->setEntityManager($em); - - // Process source directories - $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from')); - - foreach ($fromPaths as $dirName) { - $dirName = realpath($dirName); - - if ( ! file_exists($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not exist.", $dirName) - ); - } else if ( ! is_readable($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not have read permissions.", $dirName) - ); - } - - $reader->addMappingSource($dirName); - } + + $metadatas = $em->getMetadataFactory()->getAllMetadata(); + $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); // Process destination directory $destPath = realpath($input->getArgument('dest-path')); @@ -125,10 +103,7 @@ EOT ); } - // Retrieving ClassMetadatas - $metadatas = $reader->getMetadatas(); - - if ( ! empty($metadatas)) { + if ( count($metadatas)) { $numRepositories = 0; foreach ($metadatas as $metadata) {