1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\NamespaceCorrector; |
7
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\Refactor; |
8
|
|
|
use Imanghafoori\LaravelMicroscope\LaravelPaths\FilePath; |
9
|
|
|
use Imanghafoori\LaravelMicroscope\Stubs\ServiceProviderStub; |
10
|
|
|
|
11
|
|
|
class GenerateCode |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Get all of the listeners and their corresponding events. |
15
|
|
|
* |
16
|
|
|
* @param iterable $paths |
17
|
|
|
* @param $composerPath |
18
|
|
|
* @param $composerNamespace |
19
|
|
|
* @param $command |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public static function serviceProvider($paths, $composerPath, $composerNamespace, $command) |
24
|
|
|
{ |
25
|
|
|
foreach ($paths as $classFilePath) { |
26
|
|
|
/** |
27
|
|
|
* @var $classFilePath \Symfony\Component\Finder\SplFileInfo |
28
|
|
|
*/ |
29
|
|
|
if (! Str::endsWith($classFilePath->getFilename(), ['ServiceProvider.php'])) { |
30
|
|
|
continue; |
31
|
|
|
} |
32
|
|
|
$absFilePath = $classFilePath->getRealPath(); |
33
|
|
|
$content = file_get_contents($absFilePath); |
34
|
|
|
|
35
|
|
|
if (strlen(\trim($content)) > 10) { |
36
|
|
|
// file is not empty |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$relativePath = FilePath::getRelativePath($absFilePath); |
41
|
|
|
$correctNamespace = NamespaceCorrector::calculateCorrectNamespace($relativePath, $composerPath, $composerNamespace); |
42
|
|
|
|
43
|
|
|
$className = \str_replace('.php', '', $classFilePath->getFilename()); |
44
|
|
|
$answer = self::ask($command, $correctNamespace.'\\'.$className); |
45
|
|
|
if (! $answer) { |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
$prefix = strtolower(str_replace('ServiceProvider', '', $className)); |
49
|
|
|
file_put_contents($absFilePath, ServiceProviderStub::providerContent($correctNamespace, $className, $prefix)); |
50
|
|
|
|
51
|
|
|
self::generateFolderStructure($classFilePath, $correctNamespace, $prefix); |
52
|
|
|
self::addToProvidersArray($correctNamespace.'\\'.$className); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Build the directory for the class if necessary. |
58
|
|
|
* |
59
|
|
|
* @param string $path |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
protected static function makeDirectory($path) |
63
|
|
|
{ |
64
|
|
|
if (! is_dir($path)) { |
65
|
|
|
@mkdir($path, 0777, true); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $path; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private static function ask($command, $name) |
72
|
|
|
{ |
73
|
|
|
return $command->getOutput()->confirm('Do you want to generate a service provider: '.$name, true); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private static function isProvidersKey($tokens, $i) |
77
|
|
|
{ |
78
|
|
|
$token = $tokens[$i]; |
79
|
|
|
|
80
|
|
|
return $token[0] == T_CONSTANT_ENCAPSED_STRING && |
81
|
|
|
\trim($token[1], '\'\"') == 'providers' && |
82
|
|
|
\in_array(T_DOUBLE_ARROW, [$tokens[$i + 1][0], $tokens[$i + 2][0]]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private static function addToProvidersArray($providerPath) |
86
|
|
|
{ |
87
|
|
|
$tokens = token_get_all(file_get_contents(config_path('app.php'))); |
88
|
|
|
|
89
|
|
|
foreach ($tokens as $i => $token) { |
90
|
|
|
if (! self::isProvidersKey($tokens, $i)) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
$closeBracketIndex = Analyzers\TokenManager::readBody($tokens, $i + 15, ']')[1]; |
94
|
|
|
|
95
|
|
|
$j = $closeBracketIndex; |
96
|
|
|
while ($tokens[--$j][0] == T_WHITESPACE && $tokens[--$j][0] == T_COMMENT) { |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// put a comma at the end of the array if it is not there |
100
|
|
|
$tokens[$j] !== ',' && array_splice($tokens, $j + 1, 0, [[',']]); |
101
|
|
|
|
102
|
|
|
array_splice($tokens, (int) $closeBracketIndex, 0, [["\n ".$providerPath.'::class,'."\n "]]); |
103
|
|
|
file_put_contents(config_path('app.php'), Refactor::toString($tokens)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $tokens; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected static function generateFolderStructure($classFilePath, $namespace, $prefix) |
110
|
|
|
{ |
111
|
|
|
$_basePath = $classFilePath->getPath().DIRECTORY_SEPARATOR; |
112
|
|
|
file_put_contents($_basePath.$prefix.'_routes.php', self::routeContent($namespace)); |
113
|
|
|
self::makeDirectory($_basePath.'Database'.DIRECTORY_SEPARATOR.'migrations'); |
114
|
|
|
self::makeDirectory($_basePath.'views'); |
115
|
|
|
self::makeDirectory($_basePath.'Http'); |
116
|
|
|
self::makeDirectory($_basePath.'Database'.DIRECTORY_SEPARATOR.'Models'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected static function routeContent($namespace) |
120
|
|
|
{ |
121
|
|
|
return "<?php |
122
|
|
|
|
123
|
|
|
use Illuminate\Support\Facades\Route; |
124
|
|
|
|
125
|
|
|
Route::group(['middleware' => ['web'], 'namespace' => '$namespace\Http'], function () { |
126
|
|
|
|
127
|
|
|
});"; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: