Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 1c7ce04

Browse files
authored
Merge pull request #34 from tanhongit/main
fix styleci config & dist env
2 parents a12772b + e14fe29 commit 1c7ce04

File tree

5 files changed

+104
-97
lines changed

5 files changed

+104
-97
lines changed

.styleci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
preset: laravel
2+
3+
disabled:
4+
- not_operator_with_successor_space

common/helpers.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@
22

33
use Lbil\LaravelGenerator\Exceptions\LaravelGeneratorException;
44

5-
if (! function_exists('laravel_generator_dist_path')) {
5+
if (!function_exists('laravel_generator_dist_path')) {
66
/**
77
* Returns laravel-generator composer dist path.
88
*
9-
* @param string|null $asset string
9+
* @param string|null $asset
1010
* @return string
1111
*/
1212
function laravel_generator_dist_path(string $asset = null): string
1313
{
1414
$defaultPath = config('laravel-generator.defaults.paths.ui_package_path').'/dist/';
15-
$path = base_path(config('laravel-generator.defaults.paths.laravel_generator_assets_path', $defaultPath));
15+
$assetPath = config('laravel-generator.defaults.paths.laravel_generator_assets_path', $defaultPath);
16+
if (!str_ends_with($assetPath, '/')) {
17+
$assetPath .= '/';
18+
}
19+
$path = base_path($assetPath);
1620

17-
if (! $asset) {
21+
if (!$asset) {
1822
return realpath($path);
1923
}
2024

2125
return realpath($path.$asset);
2226
}
2327
}
2428

25-
if (! function_exists('laravel_generator_asset')) {
29+
if (!function_exists('laravel_generator_asset')) {
2630
/**
2731
* Returns asset from laravel-generator composer package.
2832
*
@@ -35,7 +39,7 @@ function laravel_generator_asset(string $asset): string
3539
{
3640
$file = laravel_generator_dist_path($asset);
3741

38-
if (! file_exists($file)) {
42+
if (!file_exists($file)) {
3943
throw new LaravelGeneratorException(sprintf('%s - this Laravel Generator asset does not exist', $asset));
4044
}
4145

@@ -45,7 +49,7 @@ function laravel_generator_asset(string $asset): string
4549
}
4650
}
4751

48-
if (! function_exists('laravel_generator_dist_path_allowed')) {
52+
if (!function_exists('laravel_generator_dist_path_allowed')) {
4953
/**
5054
* Returns asset allowed from laravel-generator composer package.
5155
*
@@ -61,7 +65,7 @@ function laravel_generator_asset_allowed(string $asset): string
6165
'favicon-32x32.png',
6266
];
6367

64-
if (! in_array($asset, $allowed_files)) {
68+
if (!in_array($asset, $allowed_files)) {
6569
throw new LaravelGeneratorException(sprintf('%s - this Laravel Generator asset is not allowed', $asset));
6670
}
6771

src/Helpers/ConfigHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function generatorConfig(?string $generatorName = null): array
2323
$defaults = config('laravel-generator.defaults', []);
2424
$generators = config('laravel-generator.generators', []);
2525

26-
if (! isset($generators[$generatorName])) {
26+
if (!isset($generators[$generatorName])) {
2727
throw new LaravelGeneratorException('Generator name not found');
2828
}
2929

src/Http/Controllers/Detect/DetectController.php

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@
1010

1111
class DetectController extends Controller
1212
{
13+
/**
14+
* Get the type of all classes in the app folder.
15+
*
16+
* @return array[]
17+
*/
18+
public function detect()
19+
{
20+
$recursiveDirectoryIterator = new RecursiveDirectoryIterator(app_path());
21+
$files = new RecursiveIteratorIterator($recursiveDirectoryIterator);
22+
$type = [];
23+
24+
foreach ($files as $file) {
25+
if (!$file->isFile() || $file->getExtension() !== 'php') {
26+
continue;
27+
}
28+
29+
$class = $this->getClassFromFile($file);
30+
if ($class !== null) {
31+
$type[] = $this->getClassType($class);
32+
}
33+
}
34+
35+
return $type;
36+
}
37+
1338
/**
1439
* @param $file
1540
* @return ReflectionClass|null
@@ -21,7 +46,7 @@ public function getClassFromFile($file)
2146

2247
// Match namespace and class name
2348
preg_match('/namespace\s+(.*?);.*?class\s+(\w+)/s', $content, $matches);
24-
if (! isset($matches[1]) || ! isset($matches[2])) {
49+
if (!isset($matches[1]) || !isset($matches[2])) {
2550
return null;
2651
}
2752

@@ -32,19 +57,63 @@ public function getClassFromFile($file)
3257
}
3358

3459
/**
60+
* Get the type of the given class.
61+
*
3562
* @param ReflectionClass $class
36-
* @return bool
63+
* @return string
3764
*/
38-
private function dependsOnModels(ReflectionClass $class)
65+
protected function getClassType(ReflectionClass $class)
3966
{
40-
$dependencies = $class->getConstructor()->getParameters();
41-
foreach ($dependencies as $dependency) {
42-
if (preg_match('/Model$/', $dependency->getClass()->getName()) === 1) {
43-
return true;
44-
}
67+
$type = 'other';
68+
69+
switch (true) {
70+
case $this->isRepositoryClass($class):
71+
$type = 'repository';
72+
break;
73+
case $this->isServiceClass($class):
74+
$type = 'service';
75+
break;
76+
case $this->isControllerClass($class):
77+
$type = 'controller';
78+
break;
79+
case $this->isActionClass($class):
80+
$type = 'action';
81+
break;
4582
}
4683

47-
return false;
84+
return $type;
85+
}
86+
87+
/**
88+
* Check if the class is a repository class
89+
* A repository class must have a name ending with "Repository" or "EloquentRepository"
90+
* and implement the CRUD methods
91+
* and have a dependency on a model.
92+
*
93+
* @param ReflectionClass $class
94+
* @return bool
95+
*/
96+
public function isRepositoryClass(ReflectionClass $class)
97+
{
98+
return $this->checkClassType($class, 'repository');
99+
}
100+
101+
/**
102+
* Check if the class is a class of the given type
103+
* A class of the given type must have a name ending with the given type or "Eloquent" + the given type.
104+
*
105+
* @param ReflectionClass $class
106+
* @param $type
107+
* @return bool
108+
*/
109+
protected function checkClassType(ReflectionClass $class, $type)
110+
{
111+
$type = ucfirst($type);
112+
113+
return preg_match('/'.$type.'$/', $class->getName()) === 1
114+
|| preg_match('/Eloquent'.$type.'$/', $class->getName()) === 1
115+
&& $this->implementsCrudMethods($class)
116+
&& $this->dependsOnModels($class);
48117
}
49118

50119
/**
@@ -73,17 +142,19 @@ protected function implementsCrudMethods(ReflectionClass $class)
73142
}
74143

75144
/**
76-
* Check if the class is a repository class
77-
* A repository class must have a name ending with "Repository" or "EloquentRepository"
78-
* and implement the CRUD methods
79-
* and have a dependency on a model.
80-
*
81145
* @param ReflectionClass $class
82146
* @return bool
83147
*/
84-
public function isRepositoryClass(ReflectionClass $class)
148+
private function dependsOnModels(ReflectionClass $class)
85149
{
86-
return $this->checkClassType($class, 'repository');
150+
$dependencies = $class->getConstructor()->getParameters();
151+
foreach ($dependencies as $dependency) {
152+
if (preg_match('/Model$/', $dependency->getClass()->getName()) === 1) {
153+
return true;
154+
}
155+
}
156+
157+
return false;
87158
}
88159

89160
/**
@@ -123,75 +194,4 @@ public function isActionClass(ReflectionClass $class)
123194
{
124195
return $this->checkClassType($class, 'action');
125196
}
126-
127-
/**
128-
* Check if the class is a class of the given type
129-
* A class of the given type must have a name ending with the given type or "Eloquent" + the given type.
130-
*
131-
* @param ReflectionClass $class
132-
* @param $type
133-
* @return bool
134-
*/
135-
protected function checkClassType(ReflectionClass $class, $type)
136-
{
137-
$type = ucfirst($type);
138-
139-
return preg_match('/'.$type.'$/', $class->getName()) === 1
140-
|| preg_match('/Eloquent'.$type.'$/', $class->getName()) === 1
141-
&& $this->implementsCrudMethods($class)
142-
&& $this->dependsOnModels($class);
143-
}
144-
145-
/**
146-
* Get the type of the given class.
147-
*
148-
* @param ReflectionClass $class
149-
* @return string
150-
*/
151-
protected function getClassType(ReflectionClass $class)
152-
{
153-
$type = 'other';
154-
155-
switch (true) {
156-
case $this->isRepositoryClass($class):
157-
$type = 'repository';
158-
break;
159-
case $this->isServiceClass($class):
160-
$type = 'service';
161-
break;
162-
case $this->isControllerClass($class):
163-
$type = 'controller';
164-
break;
165-
case $this->isActionClass($class):
166-
$type = 'action';
167-
break;
168-
}
169-
170-
return $type;
171-
}
172-
173-
/**
174-
* Get the type of all classes in the app folder.
175-
*
176-
* @return array[]
177-
*/
178-
public function detect()
179-
{
180-
$recursiveDirectoryIterator = new RecursiveDirectoryIterator(app_path());
181-
$files = new RecursiveIteratorIterator($recursiveDirectoryIterator);
182-
$type = [];
183-
184-
foreach ($files as $file) {
185-
if (! $file->isFile() || $file->getExtension() !== 'php') {
186-
continue;
187-
}
188-
189-
$class = $this->getClassFromFile($file);
190-
if ($class !== null) {
191-
$type[] = $this->getClassType($class);
192-
}
193-
}
194-
195-
return $type;
196-
}
197197
}

src/Http/Controllers/Generator/RepositoryGeneratorController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function saveFile($fileName, $fileContent)
4343
{
4444
$filePath = app_path("Repositories/{$fileName}");
4545

46-
if (! is_dir(dirname($filePath))) {
46+
if (!is_dir(dirname($filePath))) {
4747
mkdir(dirname($filePath), 0777, true);
4848
}
4949

0 commit comments

Comments
 (0)