From c0ee57ae55157d2258549ae5c0d13e4bcd3668a6 Mon Sep 17 00:00:00 2001 From: encoder64 Date: Sun, 27 Jul 2014 14:44:31 +0300 Subject: [PATCH] Default/Custom Entity Repository for Entity Repository Generator --- .../Command/GenerateRepositoriesCommand.php | 6 ++- .../ORM/Tools/EntityRepositoryGenerator.php | 49 ++++++++++++++++--- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php index 975bc6938..917f7ac76 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/GenerateRepositoriesCommand.php @@ -73,6 +73,8 @@ EOT $metadatas = $em->getMetadataFactory()->getAllMetadata(); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); + $repositoryName = $em->getConfiguration()->getDefaultRepositoryClassName(); + // Process destination directory $destPath = realpath($input->getArgument('dest-path')); @@ -91,7 +93,9 @@ EOT if (count($metadatas)) { $numRepositories = 0; $generator = new EntityRepositoryGenerator(); - + + $generator->setDefaultRepositoryName($repositoryName); + foreach ($metadatas as $metadata) { if ($metadata->customRepositoryClassName) { $output->writeln( diff --git a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php index 5093cd54d..24c30568c 100644 --- a/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php @@ -32,20 +32,20 @@ namespace Doctrine\ORM\Tools; */ class EntityRepositoryGenerator { + protected $_repositoryName = 'Doctrine\ORM\EntityRepository'; + protected static $_template = ' -use Doctrine\ORM\EntityRepository; - /** * * * This class was generated by the Doctrine ORM. Add your own custom * repository methods below. */ -class extends EntityRepository +class extends { } '; @@ -60,8 +60,9 @@ class extends EntityRepository $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName)); $variables = array( - '' => $this->generateEntityRepositoryNamespace($fullClassName), - '' => $className + '' => $this->generateEntityRepositoryNamespace($fullClassName), + '' => $this->generateEntityRepositoryName(), + '' => $className ); return str_replace(array_keys($variables), array_values($variables), self::$_template); @@ -74,13 +75,27 @@ class extends EntityRepository * * @return string $namespace */ - private function generateEntityRepositoryNamespace($fullClassName) + protected function generateEntityRepositoryNamespace($fullClassName) { $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\')); - + return $namespace ? 'namespace ' . $namespace . ';' : ''; } + /** + * @return string $repositoryName + */ + protected function generateEntityRepositoryName() + { + $repositoryName = $this->getDefaultRepositoryName(); + + if (substr($repositoryName, 0, 1) != '\\') { + $repositoryName = '\\'.$repositoryName; + } + + return $repositoryName; + } + /** * @param string $fullClassName * @param string $outputDirectory @@ -103,4 +118,24 @@ class extends EntityRepository file_put_contents($path, $code); } } + + /** + * @param string $repositoryName + * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator + */ + public function setDefaultRepositoryName($repositoryName) + { + $this->_repositoryName = $repositoryName; + + return $this; + } + + /** + * @return string + */ + public function getDefaultRepositoryName() + { + return $this->_repositoryName; + } + }