1
0
mirror of synced 2025-01-09 18:47:10 +03:00

Default/Custom Entity Repository for Entity Repository Generator

This commit is contained in:
encoder64 2014-07-27 14:44:31 +03:00
parent 089cca636e
commit c0ee57ae55
2 changed files with 47 additions and 8 deletions

View File

@ -73,6 +73,8 @@ EOT
$metadatas = $em->getMetadataFactory()->getAllMetadata(); $metadatas = $em->getMetadataFactory()->getAllMetadata();
$metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
$repositoryName = $em->getConfiguration()->getDefaultRepositoryClassName();
// Process destination directory // Process destination directory
$destPath = realpath($input->getArgument('dest-path')); $destPath = realpath($input->getArgument('dest-path'));
@ -91,7 +93,9 @@ EOT
if (count($metadatas)) { if (count($metadatas)) {
$numRepositories = 0; $numRepositories = 0;
$generator = new EntityRepositoryGenerator(); $generator = new EntityRepositoryGenerator();
$generator->setDefaultRepositoryName($repositoryName);
foreach ($metadatas as $metadata) { foreach ($metadatas as $metadata) {
if ($metadata->customRepositoryClassName) { if ($metadata->customRepositoryClassName) {
$output->writeln( $output->writeln(

View File

@ -32,20 +32,20 @@ namespace Doctrine\ORM\Tools;
*/ */
class EntityRepositoryGenerator class EntityRepositoryGenerator
{ {
protected $_repositoryName = 'Doctrine\ORM\EntityRepository';
protected static $_template = protected static $_template =
'<?php '<?php
<namespace> <namespace>
use Doctrine\ORM\EntityRepository;
/** /**
* <className> * <className>
* *
* This class was generated by the Doctrine ORM. Add your own custom * This class was generated by the Doctrine ORM. Add your own custom
* repository methods below. * repository methods below.
*/ */
class <className> extends EntityRepository class <className> extends <repositoryName>
{ {
} }
'; ';
@ -60,8 +60,9 @@ class <className> extends EntityRepository
$className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName)); $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
$variables = array( $variables = array(
'<namespace>' => $this->generateEntityRepositoryNamespace($fullClassName), '<namespace>' => $this->generateEntityRepositoryNamespace($fullClassName),
'<className>' => $className '<repositoryName>' => $this->generateEntityRepositoryName(),
'<className>' => $className
); );
return str_replace(array_keys($variables), array_values($variables), self::$_template); return str_replace(array_keys($variables), array_values($variables), self::$_template);
@ -74,13 +75,27 @@ class <className> extends EntityRepository
* *
* @return string $namespace * @return string $namespace
*/ */
private function generateEntityRepositoryNamespace($fullClassName) protected function generateEntityRepositoryNamespace($fullClassName)
{ {
$namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\')); $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
return $namespace ? 'namespace ' . $namespace . ';' : ''; 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 $fullClassName
* @param string $outputDirectory * @param string $outputDirectory
@ -103,4 +118,24 @@ class <className> extends EntityRepository
file_put_contents($path, $code); 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;
}
} }