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

Commit

Permalink
Merge pull request #34 from tanhongit/main
Browse files Browse the repository at this point in the history
fix styleci config & dist env
  • Loading branch information
tanhongit committed Apr 27, 2023
2 parents a12772b + e14fe29 commit 1c7ce04
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 97 deletions.
3 changes: 3 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
preset: laravel

disabled:
- not_operator_with_successor_space
20 changes: 12 additions & 8 deletions common/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@

use Lbil\LaravelGenerator\Exceptions\LaravelGeneratorException;

if (! function_exists('laravel_generator_dist_path')) {
if (!function_exists('laravel_generator_dist_path')) {
/**
* Returns laravel-generator composer dist path.
*
* @param string|null $asset string
* @param string|null $asset
* @return string
*/
function laravel_generator_dist_path(string $asset = null): string
{
$defaultPath = config('laravel-generator.defaults.paths.ui_package_path').'/dist/';
$path = base_path(config('laravel-generator.defaults.paths.laravel_generator_assets_path', $defaultPath));
$assetPath = config('laravel-generator.defaults.paths.laravel_generator_assets_path', $defaultPath);
if (!str_ends_with($assetPath, '/')) {
$assetPath .= '/';
}
$path = base_path($assetPath);

if (! $asset) {
if (!$asset) {
return realpath($path);
}

return realpath($path.$asset);
}
}

if (! function_exists('laravel_generator_asset')) {
if (!function_exists('laravel_generator_asset')) {
/**
* Returns asset from laravel-generator composer package.
*
Expand All @@ -35,7 +39,7 @@ function laravel_generator_asset(string $asset): string
{
$file = laravel_generator_dist_path($asset);

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

Expand All @@ -45,7 +49,7 @@ function laravel_generator_asset(string $asset): string
}
}

if (! function_exists('laravel_generator_dist_path_allowed')) {
if (!function_exists('laravel_generator_dist_path_allowed')) {
/**
* Returns asset allowed from laravel-generator composer package.
*
Expand All @@ -61,7 +65,7 @@ function laravel_generator_asset_allowed(string $asset): string
'favicon-32x32.png',
];

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

Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/ConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function generatorConfig(?string $generatorName = null): array
$defaults = config('laravel-generator.defaults', []);
$generators = config('laravel-generator.generators', []);

if (! isset($generators[$generatorName])) {
if (!isset($generators[$generatorName])) {
throw new LaravelGeneratorException('Generator name not found');
}

Expand Down
174 changes: 87 additions & 87 deletions src/Http/Controllers/Detect/DetectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@

class DetectController extends Controller
{
/**
* Get the type of all classes in the app folder.
*
* @return array[]
*/
public function detect()
{
$recursiveDirectoryIterator = new RecursiveDirectoryIterator(app_path());
$files = new RecursiveIteratorIterator($recursiveDirectoryIterator);
$type = [];

foreach ($files as $file) {
if (!$file->isFile() || $file->getExtension() !== 'php') {
continue;
}

$class = $this->getClassFromFile($file);
if ($class !== null) {
$type[] = $this->getClassType($class);
}
}

return $type;
}

/**
* @param $file
* @return ReflectionClass|null
Expand All @@ -21,7 +46,7 @@ public function getClassFromFile($file)

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

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

/**
* Get the type of the given class.
*
* @param ReflectionClass $class
* @return bool
* @return string
*/
private function dependsOnModels(ReflectionClass $class)
protected function getClassType(ReflectionClass $class)
{
$dependencies = $class->getConstructor()->getParameters();
foreach ($dependencies as $dependency) {
if (preg_match('/Model$/', $dependency->getClass()->getName()) === 1) {
return true;
}
$type = 'other';

switch (true) {
case $this->isRepositoryClass($class):
$type = 'repository';
break;
case $this->isServiceClass($class):
$type = 'service';
break;
case $this->isControllerClass($class):
$type = 'controller';
break;
case $this->isActionClass($class):
$type = 'action';
break;
}

return false;
return $type;
}

/**
* Check if the class is a repository class
* A repository class must have a name ending with "Repository" or "EloquentRepository"
* and implement the CRUD methods
* and have a dependency on a model.
*
* @param ReflectionClass $class
* @return bool
*/
public function isRepositoryClass(ReflectionClass $class)
{
return $this->checkClassType($class, 'repository');
}

/**
* Check if the class is a class of the given type
* A class of the given type must have a name ending with the given type or "Eloquent" + the given type.
*
* @param ReflectionClass $class
* @param $type
* @return bool
*/
protected function checkClassType(ReflectionClass $class, $type)
{
$type = ucfirst($type);

return preg_match('/'.$type.'$/', $class->getName()) === 1
|| preg_match('/Eloquent'.$type.'$/', $class->getName()) === 1
&& $this->implementsCrudMethods($class)
&& $this->dependsOnModels($class);
}

/**
Expand Down Expand Up @@ -73,17 +142,19 @@ protected function implementsCrudMethods(ReflectionClass $class)
}

/**
* Check if the class is a repository class
* A repository class must have a name ending with "Repository" or "EloquentRepository"
* and implement the CRUD methods
* and have a dependency on a model.
*
* @param ReflectionClass $class
* @return bool
*/
public function isRepositoryClass(ReflectionClass $class)
private function dependsOnModels(ReflectionClass $class)
{
return $this->checkClassType($class, 'repository');
$dependencies = $class->getConstructor()->getParameters();
foreach ($dependencies as $dependency) {
if (preg_match('/Model$/', $dependency->getClass()->getName()) === 1) {
return true;
}
}

return false;
}

/**
Expand Down Expand Up @@ -123,75 +194,4 @@ public function isActionClass(ReflectionClass $class)
{
return $this->checkClassType($class, 'action');
}

/**
* Check if the class is a class of the given type
* A class of the given type must have a name ending with the given type or "Eloquent" + the given type.
*
* @param ReflectionClass $class
* @param $type
* @return bool
*/
protected function checkClassType(ReflectionClass $class, $type)
{
$type = ucfirst($type);

return preg_match('/'.$type.'$/', $class->getName()) === 1
|| preg_match('/Eloquent'.$type.'$/', $class->getName()) === 1
&& $this->implementsCrudMethods($class)
&& $this->dependsOnModels($class);
}

/**
* Get the type of the given class.
*
* @param ReflectionClass $class
* @return string
*/
protected function getClassType(ReflectionClass $class)
{
$type = 'other';

switch (true) {
case $this->isRepositoryClass($class):
$type = 'repository';
break;
case $this->isServiceClass($class):
$type = 'service';
break;
case $this->isControllerClass($class):
$type = 'controller';
break;
case $this->isActionClass($class):
$type = 'action';
break;
}

return $type;
}

/**
* Get the type of all classes in the app folder.
*
* @return array[]
*/
public function detect()
{
$recursiveDirectoryIterator = new RecursiveDirectoryIterator(app_path());
$files = new RecursiveIteratorIterator($recursiveDirectoryIterator);
$type = [];

foreach ($files as $file) {
if (! $file->isFile() || $file->getExtension() !== 'php') {
continue;
}

$class = $this->getClassFromFile($file);
if ($class !== null) {
$type[] = $this->getClassType($class);
}
}

return $type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function saveFile($fileName, $fileContent)
{
$filePath = app_path("Repositories/{$fileName}");

if (! is_dir(dirname($filePath))) {
if (!is_dir(dirname($filePath))) {
mkdir(dirname($filePath), 0777, true);
}

Expand Down

0 comments on commit 1c7ce04

Please sign in to comment.