Skip to content

Commit

Permalink
Merge pull request #1089 from encoder32/EntityRepositoryGeneratorDefa…
Browse files Browse the repository at this point in the history
…ultRepository

EntityRepositoryGenerator default repository
  • Loading branch information
Ocramius committed Nov 11, 2014
2 parents f987cf7 + 6bc003e commit ab62914
Show file tree
Hide file tree
Showing 9 changed files with 511 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$metadatas = $em->getMetadataFactory()->getAllMetadata();
$metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));

$repositoryName = $em->getConfiguration()->getDefaultRepositoryClassName();

// Process destination directory
$destPath = realpath($input->getArgument('dest-path'));

Expand All @@ -92,6 +94,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$numRepositories = 0;
$generator = new EntityRepositoryGenerator();

$generator->setDefaultRepositoryName($repositoryName);

foreach ($metadatas as $metadata) {
if ($metadata->customRepositoryClassName) {
$output->writeln(
Expand Down
82 changes: 73 additions & 9 deletions lib/Doctrine/ORM/Tools/EntityRepositoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@
*/
class EntityRepositoryGenerator
{
private $repositoryName;

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>
{
}
';
Expand All @@ -57,16 +57,49 @@ class <className> extends EntityRepository
*/
public function generateEntityRepositoryClass($fullClassName)
{
$className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));

$variables = array(
'<namespace>' => $this->generateEntityRepositoryNamespace($fullClassName),
'<className>' => $className
'<namespace>' => $this->generateEntityRepositoryNamespace($fullClassName),
'<repositoryName>' => $this->generateEntityRepositoryName($fullClassName),
'<className>' => $this->generateClassName($fullClassName)
);

return str_replace(array_keys($variables), array_values($variables), self::$_template);
}

/**
* Generates the namespace, if class do not have namespace, return empty string instead.
*
* @param string $fullClassName
*
* @return string $namespace
*/
private function getClassNamespace($fullClassName)
{
$namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));

return $namespace;
}

/**
* Generates the class name
*
* @param string $fullClassName
*
* @return string
*/
private function generateClassName($fullClassName)
{
$namespace = $this->getClassNamespace($fullClassName);

$className = $fullClassName;

if ($namespace) {
$className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
}

return $className;
}

/**
* Generates the namespace statement, if class do not have namespace, return empty string instead.
*
Expand All @@ -76,11 +109,29 @@ public function generateEntityRepositoryClass($fullClassName)
*/
private function generateEntityRepositoryNamespace($fullClassName)
{
$namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
$namespace = $this->getClassNamespace($fullClassName);

return $namespace ? 'namespace ' . $namespace . ';' : '';
}

/**
* @param string $fullClassName
*
* @return string $repositoryName
*/
private function generateEntityRepositoryName($fullClassName)
{
$namespace = $this->getClassNamespace($fullClassName);

$repositoryName = $this->repositoryName ?: 'Doctrine\ORM\EntityRepository';

if ($namespace && $repositoryName[0] !== '\\') {
$repositoryName = '\\' . $repositoryName;
}

return $repositoryName;
}

/**
* @param string $fullClassName
* @param string $outputDirectory
Expand All @@ -103,4 +154,17 @@ public function writeEntityRepositoryClass($fullClassName, $outputDirectory)
file_put_contents($path, $code);
}
}

/**
* @param string $repositoryName
*
* @return \Doctrine\ORM\Tools\EntityRepositoryGenerator
*/
public function setDefaultRepositoryName($repositoryName)
{
$this->repositoryName = $repositoryName;

return $this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Doctrine\Tests\Models\DDC3231;

class DDC3231EntityRepository extends \Doctrine\ORM\EntityRepository
{
}
23 changes: 23 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3231/DDC3231User1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Doctrine\Tests\Models\DDC3231;

/**
* @Entity(repositoryClass="DDC3231User1Repository")
* @Table(name="users")
*/
class DDC3231User1
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @Column(type="string", length=255)
*/
protected $name;

}
21 changes: 21 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3231/DDC3231User1NoNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @Entity(repositoryClass="DDC3231User1NoNamespaceRepository")
* @Table(name="no_namespace_users")
*/
class DDC3231User1NoNamespace
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @Column(type="string", length=255)
*/
protected $name;

}
23 changes: 23 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3231/DDC3231User2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Doctrine\Tests\Models\DDC3231;

/**
* @Entity(repositoryClass="DDC3231User2Repository")
* @Table(name="users2")
*/
class DDC3231User2
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @Column(type="string", length=255)
*/
protected $name;

}
21 changes: 21 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3231/DDC3231User2NoNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @Entity(repositoryClass="DDC3231User2NoNamespaceRepository")
* @Table(name="no_namespace_users2")
*/
class DDC3231User2NoNamespace
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @Column(type="string", length=255)
*/
protected $name;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace Doctrine\Tests\ORM\Tools\Console\Command;

use Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Application;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* GenerateRepositoriesCommandTest
*/
class GenerateRepositoriesCommandTest extends OrmFunctionalTestCase
{
/**
* @var \Symfony\Component\Console\Application
*/
private $application;

private $path;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();

$this->path = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('doctrine_');

\mkdir($this->path);


$metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl();

$metadataDriver->addPaths(array(
__DIR__ . '/../../../../Models/DDC3231/'
));

$this->application = new Application();

$this->application->setHelperSet(new HelperSet(array(
'em' => new EntityManagerHelper($this->_em)
)));

$this->application->add(new GenerateRepositoriesCommand());

}

/**
* @inheritdoc
*/
public function tearDown()
{
$dirs = array();

$ri = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path));
foreach ($ri AS $file) {
/* @var $file \SplFileInfo */
if ($file->isFile()) {
\unlink($file->getPathname());
} elseif ($file->getBasename() === '.') {
$dirs[] = $file->getRealpath();
}
}

arsort($dirs);

foreach ($dirs as $dir) {
\rmdir($dir);
}

parent::tearDown();
}

public function testGenerateRepositories()
{
$this->generateRepositories('DDC3231User1');

$cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User1Repository';
$fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php';

$this->assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname);
$this->assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php');

require $this->path . DIRECTORY_SEPARATOR . $fname;
require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User1NoNamespaceRepository.php';

$this->assertTrue(class_exists($cname));
$this->assertTrue(class_exists('DDC3231User1NoNamespaceRepository'));

$repo1 = new \ReflectionClass($cname);
$repo2 = new \ReflectionClass('DDC3231User1NoNamespaceRepository');

$this->assertSame('Doctrine\ORM\EntityRepository', $repo1->getParentClass()->getName());
$this->assertSame('Doctrine\ORM\EntityRepository', $repo2->getParentClass()->getName());
}

public function testGenerateRepositoriesCustomDefaultRepository()
{
$this->generateRepositories('DDC3231User2', 'Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository');

$cname = 'Doctrine\Tests\Models\DDC3231\DDC3231User2Repository';
$fname = str_replace('\\', DIRECTORY_SEPARATOR, $cname) . '.php';

$this->assertFileExists($this->path . DIRECTORY_SEPARATOR . $fname);
$this->assertFileExists($this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php');

require $this->path . DIRECTORY_SEPARATOR . $fname;
require $this->path . DIRECTORY_SEPARATOR . 'DDC3231User2NoNamespaceRepository.php';

$this->assertTrue(class_exists($cname));
$this->assertTrue(class_exists('DDC3231User2NoNamespaceRepository'));

$repo1 = new \ReflectionClass($cname);
$repo2 = new \ReflectionClass('DDC3231User2NoNamespaceRepository');

$this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo1->getParentClass()->getName());
$this->assertSame('Doctrine\Tests\Models\DDC3231\DDC3231EntityRepository', $repo2->getParentClass()->getName());
}

/**
* @param string $filter
* @param string $defaultRepository
*/
private function generateRepositories($filter, $defaultRepository = null)
{
if ($defaultRepository) {
$this->_em->getConfiguration()->setDefaultRepositoryClassName($defaultRepository);
}

$command = $this->application->find('orm:generate-repositories');
$tester = new CommandTester($command);
$tester->execute(array(
'command' => $command->getName(),
'dest-path' => $this->path,
'--filter' => $filter,
));
}

}
Loading

0 comments on commit ab62914

Please sign in to comment.