1
0
mirror of synced 2024-12-13 22:56:04 +03:00

fixed empty namespace in generated code when repository class do not have namespace

This commit is contained in:
twinh 2012-10-11 06:01:55 -07:00
parent 65fabc20c9
commit 66efd65e64

View File

@ -35,7 +35,7 @@ class EntityRepositoryGenerator
protected static $_template = protected static $_template =
'<?php '<?php
namespace <namespace>; <namespace>
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
@ -52,16 +52,28 @@ class <className> extends EntityRepository
public function generateEntityRepositoryClass($fullClassName) public function generateEntityRepositoryClass($fullClassName)
{ {
$namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
$className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName)); $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
$variables = array( $variables = array(
'<namespace>' => $namespace, '<namespace>' => $this->generateEntityRepositoryNamespace($fullClassName),
'<className>' => $className '<className>' => $className
); );
return str_replace(array_keys($variables), array_values($variables), self::$_template); return str_replace(array_keys($variables), array_values($variables), self::$_template);
} }
/**
* Generate the namespace statement, if class do not have namespace, return empty string instead
*
* @param string $fullClassName The full repository class name
* @return string $namespace
*/
private function generateEntityRepositoryNamespace($fullClassName)
{
$namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
return $namespace ? 'namespace ' . $namespace . ';' : '';
}
public function writeEntityRepositoryClass($fullClassName, $outputDirectory) public function writeEntityRepositoryClass($fullClassName, $outputDirectory)
{ {
$code = $this->generateEntityRepositoryClass($fullClassName); $code = $this->generateEntityRepositoryClass($fullClassName);