1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Repository\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
|
7
|
|
|
class RoutesGenerator extends Generator |
8
|
|
|
{ |
9
|
|
|
public $routesPlaceholder = '//:end-routes:'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Get stub name. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $stub = 'routes/web'; |
17
|
|
|
|
18
|
|
|
protected $webLumenStub = 'routes/web.lumen'; |
19
|
|
|
|
20
|
|
|
protected $webLaravelStub = 'routes/web.laravel'; |
21
|
|
|
|
22
|
|
|
protected $apiStub = 'routes/api'; |
23
|
|
|
|
24
|
|
|
protected $webPathConfigNode = 'routes.web'; |
25
|
|
|
|
26
|
|
|
protected $apiPathConfigNode = 'routes.api'; |
27
|
|
|
|
28
|
|
|
public function run() |
29
|
|
|
{ |
30
|
|
|
$filesystem = new Filesystem(); |
31
|
|
|
|
32
|
|
|
$webStub = is_lumen() ? $this->webLumenStub : $this->webLaravelStub; |
33
|
|
|
|
34
|
|
|
$webRoute = $filesystem->get($this->getWebPath()); |
35
|
|
|
$filesystem->put($this->getWebPath(), |
36
|
|
|
str_replace($this->routesPlaceholder, $this->getStub($webStub), $webRoute)); |
37
|
|
|
|
38
|
|
|
$apiRoute = $filesystem->get($this->getApiPath()); |
39
|
|
|
$filesystem->put($this->getApiPath(), |
40
|
|
|
str_replace($this->routesPlaceholder, $this->getStub($this->apiStub), $apiRoute)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get destination path for generated file. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getWebPath() |
49
|
|
|
{ |
50
|
|
|
return $this->getBasePath().'/'.parent::getConfigGeneratorClassPath($this->webPathConfigNode, true).'.php'; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get base path of destination file. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getBasePath() |
59
|
|
|
{ |
60
|
|
|
return app()->basePath().'/routes'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get stub template for generated file. |
65
|
|
|
* |
66
|
|
|
* @param string $stub |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getStub($stub = null) |
70
|
|
|
{ |
71
|
|
|
$stub = isset($stub) ? $stub : $this->stub; |
72
|
|
|
$path = config('repository.generator.stubsOverridePath', __DIR__); |
73
|
|
|
|
74
|
|
|
if (! file_exists($path.'/Stubs/'.$stub.'.stub')) { |
75
|
|
|
$path = __DIR__; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return ( new Stub($path.'/Stubs/'.$stub.'.stub', $this->getReplacements()) )->render(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getReplacements() |
82
|
|
|
{ |
83
|
|
|
return array_merge(parent::getReplacements(), [ |
84
|
|
|
'lowerclass' => str_plural(strtolower($this->getClass())), |
85
|
|
|
'controller' => $this->getControllerName(), |
86
|
|
|
'placeholder' => $this->routesPlaceholder, |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getControllerName() |
91
|
|
|
{ |
92
|
|
|
$controllerGenerator = new ControllerGenerator([ |
93
|
|
|
'name' => $this->name, |
|
|
|
|
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$controllerName = $controllerGenerator->getControllerName().'Controller'; |
97
|
|
|
|
98
|
|
|
return $controllerName; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getApiPath() |
102
|
|
|
{ |
103
|
|
|
return $this->getBasePath().'/'.parent::getConfigGeneratorClassPath($this->apiPathConfigNode, true).'.php'; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getPathConfigNode() |
107
|
|
|
{ |
108
|
|
|
// TODO: Implement getPathConfigNode() method. |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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 theSon
calls the wrong method in the parent class.