-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from 7 commits
c0ee57a
90efaee
3dc2a68
c04b01c
a4c7a89
5d608b6
df80d82
20b72ef
c8565c2
dc3bc45
859a5f8
cd547fe
409f6b4
e295a6d
6bc003e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,20 +32,20 @@ | |
*/ | ||
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> | ||
{ | ||
} | ||
'; | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
* | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nvm, was confusing this one with a different generator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
$this->_repositoryName = $repositoryName; | ||
|
||
return $this; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 asprivate
. Same goes for new methods.There was a problem hiding this comment.
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.