|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Repository\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
6
|
|
|
|
|
7
|
|
|
class BindingsGenerator extends Generator |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The placeholder for repository bindings. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
public $bindPlaceholder = '//:end-bindings:'; |
|
15
|
|
|
/** |
|
16
|
|
|
* Get stub name. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $stub = 'bindings/bindings'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Run the generator. |
|
24
|
|
|
* |
|
25
|
|
|
* @return int |
|
26
|
|
|
*/ |
|
27
|
|
|
public function run() |
|
28
|
|
|
{ |
|
29
|
|
|
$filesystem = new Filesystem(); |
|
30
|
|
|
|
|
31
|
|
|
// Add entity repository binding to the repository service provider |
|
32
|
|
|
$provider = $filesystem->get($this->getPath()); |
|
33
|
|
|
$repositoryInterface = '\\'.$this->getRepository().'::class'; |
|
34
|
|
|
$repositoryEloquent = '\\'.$this->getEloquentRepository().'::class'; |
|
35
|
|
|
|
|
36
|
|
|
return $filesystem->put($this->getPath(), str_replace($this->bindPlaceholder, "\$this->app->bind({$repositoryInterface}, $repositoryEloquent);".PHP_EOL.' '.$this->bindPlaceholder, $provider)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get destination path for generated file. |
|
41
|
|
|
* |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getPath() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->getBasePath().'/Providers/'.parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true).'.php'; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get base path of destination file. |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getBasePath() |
|
55
|
|
|
{ |
|
56
|
|
|
return config('repository.generator.basePath', app()->path()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get generator path config node. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getPathConfigNode() |
|
65
|
|
|
{ |
|
66
|
|
|
return 'provider'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Gets repository full class name. |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getRepository() |
|
75
|
|
|
{ |
|
76
|
|
|
$repositoryGenerator = new RepositoryInterfaceGenerator([ |
|
77
|
|
|
'name' => $this->name, |
|
|
|
|
|
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
|
|
$repository = $repositoryGenerator->getRootNamespace().'\\'.$repositoryGenerator->getName(); |
|
81
|
|
|
|
|
82
|
|
|
return str_replace([ |
|
83
|
|
|
'\\', |
|
84
|
|
|
'/', |
|
85
|
|
|
], '\\', $repository).'Repository'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Gets eloquent repository full class name. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getEloquentRepository() |
|
94
|
|
|
{ |
|
95
|
|
|
$repositoryGenerator = new RepositoryEloquentGenerator([ |
|
96
|
|
|
'name' => $this->name, |
|
|
|
|
|
|
97
|
|
|
]); |
|
98
|
|
|
|
|
99
|
|
|
$repository = $repositoryGenerator->getRootNamespace().'\\'.$repositoryGenerator->getName(); |
|
100
|
|
|
|
|
101
|
|
|
return str_replace([ |
|
102
|
|
|
'\\', |
|
103
|
|
|
'/', |
|
104
|
|
|
], '\\', $repository).'RepositoryEloquent'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get root namespace. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getRootNamespace() |
|
113
|
|
|
{ |
|
114
|
|
|
return parent::getRootNamespace().parent::getConfigGeneratorClassPath($this->getPathConfigNode()); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get array replacements. |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getReplacements() |
|
123
|
|
|
{ |
|
124
|
|
|
return array_merge(parent::getReplacements(), [ |
|
125
|
|
|
'repository' => $this->getRepository(), |
|
126
|
|
|
'eloquent' => $this->getEloquentRepository(), |
|
127
|
|
|
'placeholder' => $this->bindPlaceholder, |
|
128
|
|
|
]); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.