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 = MetadataFilter::filter($metadatas, $input->getOption('filter'));
$repositoryName = $em->getConfiguration()->getDefaultRepositoryClassName();
// Process destination directory
$destPath = realpath($input->getArgument('dest-path'));
@ -92,6 +94,8 @@ EOT
$numRepositories = 0;
$generator = new EntityRepositoryGenerator();
$generator->setDefaultRepositoryName($repositoryName);
foreach ($metadatas as $metadata) {
if ($metadata->customRepositoryClassName) {
$output->writeln(

View File

@ -32,20 +32,20 @@ namespace Doctrine\ORM\Tools;
*/
class EntityRepositoryGenerator
{
protected $_repositoryName = 'Doctrine\ORM\EntityRepository';
protected static $_template =
'<?php
<namespace>
use Doctrine\ORM\EntityRepository;
/**
* <className>
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class <className> extends EntityRepository
class <className> extends <repositoryName>
{
}
';
@ -61,6 +61,7 @@ class <className> extends EntityRepository
$variables = array(
'<namespace>' => $this->generateEntityRepositoryNamespace($fullClassName),
'<repositoryName>' => $this->generateEntityRepositoryName(),
'<className>' => $className
);
@ -74,13 +75,27 @@ class <className> 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 <className> 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;
}
}