1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\GeneratorBundle\Generator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @internal |
7
|
|
|
*/ |
8
|
|
|
final class Symfony4EntityRepositoryGenerator |
9
|
|
|
{ |
10
|
|
|
protected static $_template = |
11
|
|
|
'<?php |
12
|
|
|
|
13
|
|
|
namespace <namespace>; |
14
|
|
|
|
15
|
|
|
use <entityClassName>; |
16
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
17
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
18
|
|
|
|
19
|
|
|
class <repository> extends ServiceEntityRepository |
20
|
|
|
{ |
21
|
|
|
public function __construct(RegistryInterface $registry) |
22
|
|
|
{ |
23
|
|
|
parent::__construct($registry, <entity>::class); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $entityClassName |
|
|
|
|
30
|
|
|
* @param string $repositoryClassName |
|
|
|
|
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function generateEntityRepositoryClass(string $entityClass, string $repositoryClass) |
35
|
|
|
{ |
36
|
|
|
$variables = [ |
37
|
|
|
'<namespace>' => $this->generateEntityRepositoryNamespace($repositoryClass), |
38
|
|
|
'<entityClassName>' => $entityClass, |
39
|
|
|
'<entity>' => $this->generateEntityName($entityClass), |
40
|
|
|
'<repository>' => $this->generateClassName($repositoryClass), |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
return str_replace(array_keys($variables), array_values($variables), self::$_template); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $fullClassName |
|
|
|
|
48
|
|
|
* @param string $outputDirectory |
49
|
|
|
*/ |
50
|
|
|
public function writeEntityRepositoryClass($entityClass, $repositoryClass, $outputDirectory) |
51
|
|
|
{ |
52
|
|
|
$classNameParts = explode('\\', $repositoryClass); |
53
|
|
|
$repositoryClassName = end($classNameParts); |
54
|
|
|
$this->repositoryName = $repositoryClassName; |
|
|
|
|
55
|
|
|
$code = $this->generateEntityRepositoryClass($entityClass, $repositoryClass); |
56
|
|
|
|
57
|
|
|
$path = $outputDirectory . DIRECTORY_SEPARATOR . 'Repository' . DIRECTORY_SEPARATOR . end($classNameParts) . '.php'; |
58
|
|
|
|
59
|
|
|
$dir = dirname($path); |
60
|
|
|
|
61
|
|
|
if (!is_dir($dir)) { |
62
|
|
|
mkdir($dir, 0775, true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!file_exists($path)) { |
66
|
|
|
file_put_contents($path, $code); |
67
|
|
|
chmod($path, 0664); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $repositoryClassName |
73
|
|
|
* |
74
|
|
|
* @return bool|string |
75
|
|
|
*/ |
76
|
|
|
private function generateEntityRepositoryNamespace(string $repositoryClassName) |
77
|
|
|
{ |
78
|
|
|
return substr($repositoryClassName, 0, strrpos($repositoryClassName, '\\')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $entityClassName |
83
|
|
|
* |
84
|
|
|
* @return bool|string |
85
|
|
|
*/ |
86
|
|
|
private function generateEntityName(string $entityClassName) |
87
|
|
|
{ |
88
|
|
|
return substr(strrchr($entityClassName, '\\'), 1); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Generates the class name |
93
|
|
|
* |
94
|
|
|
* @param string $fullClassName |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function generateClassName($fullClassName) |
99
|
|
|
{ |
100
|
|
|
$namespace = $this->getClassNamespace($fullClassName); |
101
|
|
|
|
102
|
|
|
$className = $fullClassName; |
103
|
|
|
|
104
|
|
|
if ($namespace) { |
105
|
|
|
$className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $className; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Generates the namespace, if class do not have namespace, return empty string instead. |
113
|
|
|
* |
114
|
|
|
* @param string $fullClassName |
115
|
|
|
* |
116
|
|
|
* @return string $namespace |
117
|
|
|
*/ |
118
|
|
|
private function getClassNamespace($fullClassName) |
119
|
|
|
{ |
120
|
|
|
$namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\')); |
121
|
|
|
|
122
|
|
|
return $namespace; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.