1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Generator\Plugins; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class ServiceProviderRegister extends Plugin |
9
|
|
|
{ |
10
|
2 |
|
public function process() |
11
|
|
|
{ |
12
|
2 |
|
$path = Arr::get($this->config, 'path'); |
13
|
|
|
|
14
|
2 |
|
if (empty($path) === true) { |
15
|
|
|
return; |
16
|
|
|
} |
17
|
|
|
|
18
|
2 |
|
$content = $this->files->get($path); |
19
|
|
|
|
20
|
2 |
|
if (strpos($content, '$this->registerRepositories') === false) { |
21
|
2 |
|
$content = preg_replace_callback('/public function register\(.+\n\s+{/', function ($m) { |
22
|
2 |
|
return $m[0]."\n". |
23
|
2 |
|
str_repeat(' ', 8). |
24
|
2 |
|
'$this->registerRepositories();'; |
25
|
2 |
|
}, $content); |
26
|
|
|
} |
27
|
|
|
|
28
|
2 |
|
if (strpos($content, 'protected function registerRepositories()') === false) { |
29
|
2 |
|
$content = substr($content, 0, strrpos($content, '}')). |
30
|
2 |
|
"\n".str_repeat(' ', 4). |
31
|
2 |
|
'protected function registerRepositories()'. |
32
|
2 |
|
"\n".str_repeat(' ', 4).'{'. |
33
|
2 |
|
"\n".str_repeat(' ', 4).'}'. |
34
|
2 |
|
"\n}\n"; |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
$qualifiedName = $this->attributes['qualified_name']; |
38
|
2 |
|
$class = $this->attributes['class']; |
39
|
|
|
|
40
|
2 |
View Code Duplication |
if ($qualifiedName && strpos($content, sprintf('use %s;', $qualifiedName)) === false) { |
|
|
|
|
41
|
2 |
|
$content = preg_replace_callback( |
42
|
2 |
|
'/namespace.+/', |
43
|
2 |
|
[$this, 'replaceServieProviderCallback'], |
44
|
2 |
|
$content |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
View Code Duplication |
if ($class && strpos($content, sprintf('$this->app->singleton(%sContract::class, %s::class);', $class, $class)) === false) { |
|
|
|
|
49
|
2 |
|
$content = preg_replace_callback( |
50
|
2 |
|
'/protected function registerRepositories.+\n\s+{/', |
51
|
2 |
|
[$this, 'replaceServieProviderCallback'], |
52
|
2 |
|
$content |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
$this->files->put($path, $this->useSortFixer->fix($content)); |
|
|
|
|
57
|
2 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* replaceServieProviderCallback. |
61
|
|
|
* |
62
|
|
|
* @param array $match |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
2 |
|
private function replaceServieProviderCallback($match) |
66
|
|
|
{ |
67
|
2 |
|
$qualifiedName = $this->attributes['qualified_name']; |
68
|
2 |
|
$class = $this->attributes['class']; |
69
|
2 |
|
$contractQualifiedName = $this->attributes['repository_contract_qualified_name']; |
70
|
|
|
|
71
|
2 |
|
if (Str::startsWith($match[0], 'namespace') === true) { |
72
|
2 |
|
return $match[0]."\n\n". |
73
|
2 |
|
sprintf("use %s as %sContract;\n", $contractQualifiedName, $class). |
74
|
2 |
|
sprintf("use %s;\n", $qualifiedName); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
return $match[0]."\n". |
78
|
2 |
|
str_repeat(' ', 8). |
79
|
2 |
|
sprintf('$this->app->singleton(%sContract::class, %s::class);', $class, $class); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.