Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityRepositoryGenerator default repository #1089

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{
protected $_repositoryName = 'Doctrine\ORM\EntityRepository';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use a private property and remove the leading underscore (we cannot change other properties because of BC)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why private? What's wrong with protected?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@v3labs we are very defensive about inheritance, and even disallow it in many cases.

Unless there is an explicit (already existing) use-case for the field to be protected, all should be declared as private. Same goes for new methods.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. I'll keep that in mind for future reference.


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
*/
protected function getClassNamespace($fullClassName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoud be private as well

{
$namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));

return $namespace;
}

/**
* Generates the class name
*
* @param string $fullClassName
*
* @return string
*/
protected function generateClassName($fullClassName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make these private rather than protected (so that we don't need to ensure BC on them)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

{
$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
*/
protected function generateEntityRepositoryName($fullClassName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iMO, it should be private

{
$namespace = $this->getClassNamespace($fullClassName);

$repositoryName = $this->_repositoryName;

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API is not needed, as the default repository name is already specified in configuration

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, was confusing this one with a different generator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I set default repository class in configuration to MyApp\Doctrine\DefaultRepository
  2. I created entity class MyApp\Entity\TestEntity and generated repository class for it
  3. Actually the repository class "MyApp\Entity\TestEntityRepository" turned out to be a descendant of "Doctrine\ORM\EntityRepository" when I expected "MyApp\Doctrine\DefaultRepository" as default.

{
$this->_repositoryName = $repositoryName;

return $this;
}

}