Skip to content

Commit

Permalink
Automated Code Formatting and Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed Jun 5, 2024
1 parent 34eec77 commit bb50821
Show file tree
Hide file tree
Showing 21 changed files with 230 additions and 255 deletions.
40 changes: 20 additions & 20 deletions src/Abstracts/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function handle(): int

$path = str_replace('\\', '/', $this->getDestinationFilePath());

if (! $this->laravel['files']->isDirectory($dir = dirname($path))) {
if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
$this->laravel['files']->makeDirectory($dir, 0777, true);
}

Expand Down Expand Up @@ -60,9 +60,19 @@ protected function getDestinationFilePath(): string
{
$config = GenerateConfigReader::read($this->type);

return config('api-crud.root_path', 'app').'/'
.$config->getPath().'/'
.$this->getFileName();
return config('api-crud.root_path', 'app') . '/'
. $config->getPath() . '/'
. $this->getFileName();
}

/**
* @return string
*/
protected function getFileName()
{
$type = Str::studly($this->type);

return str_replace($type, '', Str::studly($this->argument('name'))) . "{$type}.php";
}

/**
Expand All @@ -84,9 +94,9 @@ public function getClassNamespace(?string $module = null): string

$namespace = config('api-crud.namespace');

$namespace .= '\\'.$this->getDefaultNamespace();
$namespace .= '\\' . $this->getDefaultNamespace();

$namespace .= '\\'.$extra;
$namespace .= '\\' . $extra;

$namespace = str_replace('/', '\\', $namespace);

Expand All @@ -104,23 +114,23 @@ public function getClass(): string
/**
* Get default namespace.
*
* @param null $type
* @param null $type
*
* @throws GeneratorException
*/
public function getDefaultNamespace($type = null): string
{
if (! $type) {
if (!$type) {
if (property_exists($this, 'type')) {
$type = $this->type;
}
}

if (! $type) {
if (!$type) {
throw new GeneratorException('Stub type argument or property is not configured.');
}

if (! config("api-crud.templates.{$type}")) {
if (!config("api-crud.templates.{$type}")) {
throw new InvalidArgumentException("Generator is missing [{$type}] config, check generators.php file.");
}

Expand All @@ -129,14 +139,4 @@ public function getDefaultNamespace($type = null): string
return $config['namespace'] ?? $config['path'];

}

/**
* @return string
*/
protected function getFileName()
{
$type = Str::studly($this->type);

return str_replace($type, '', Str::studly($this->argument('name')))."{$type}.php";
}
}
73 changes: 37 additions & 36 deletions src/Commands/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,6 @@ class ControllerMakeCommand extends GeneratorCommand
*/
protected $description = 'Generate new restful controller for the specified package.';

/**
* @return array|string
*/
protected function getControllerName()
{
$controller = Str::studly($this->argument('name'));

if (Str::contains(strtolower($controller), 'controller') === false) {
$controller .= 'Controller';
}

return $controller;
}

/**
* @throws GeneratorException
*/
Expand All @@ -74,7 +60,7 @@ protected function getTemplateContents(): string
'MESSAGE_VARIABLE' => Str::title(Str::replace('-', ' ', Str::kebab($this->getResourceVariableName()))),
'RESOURCE_NAMESPACES' => '',
'REQUEST_NAMESPACES' => '',
'MODEL' => str_replace('/', '\\', $this->getDefaultNamespace('model').'\\'.$this->option('model')),
'MODEL' => str_replace('/', '\\', $this->getDefaultNamespace('model') . '\\' . $this->option('model')),
'STORE_REQUEST' => '',
'UPDATE_REQUEST' => '',
'INDEX_REQUEST' => '',
Expand All @@ -95,6 +81,20 @@ public function getClass(): string
return class_basename($this->getControllerName());
}

/**
* @return array|string
*/
protected function getControllerName()
{
$controller = Str::studly($this->argument('name'));

if (Str::contains(strtolower($controller), 'controller') === false) {
$controller .= 'Controller';
}

return $controller;
}

/**
* @return string
*/
Expand All @@ -111,35 +111,23 @@ protected function getResourceVariableName()
return Str::camel(basename($this->getResourceName()));
}

protected function getClassPath(string $prefix = '', string $suffix = 'Request')
{
$resourcePath = $this->argument('name').$suffix;

$dir = dirname($resourcePath);

$dir = ($dir == '.') ? '' : $dir.'/';

$resource = basename($resourcePath);

return $dir.$prefix.$resource;
}

private function setRequestNamespaces(array &$replacements)
{
$namespaces = [];

foreach (['Store', 'Update', 'Index'] as $prefix) {
$path = $this->getModuleName().'/'
.$this->getDefaultNamespace('request').'/'
.dirname($this->option('model')).'/'.$prefix.class_basename($this->option('model')).'Request';
$path = $this->getModuleName() . '/'
. $this->getDefaultNamespace('request') . '/'
. dirname($this->option('model')) . '/' . $prefix . class_basename($this->option('model')) . 'Request';

$path = str_replace('/./', '/', $path);
match ($prefix) {
'Store' => $replacements['STORE_REQUEST'] = basename($path),
'Index' => $replacements['INDEX_REQUEST'] = basename($path),
'Update' => $replacements['UPDATE_REQUEST'] = basename($path),
};

$namespaces[] = ('use '.implode('\\', explode('/', $path)).';');
$namespaces[] = ('use ' . implode('\\', explode('/', $path)) . ';');

}

Expand All @@ -154,17 +142,30 @@ private function setResourceNamespaces(array &$replacements)
$namespaces = [];

foreach (['Resource', 'Collection'] as $suffix) {
$path = $this->getModuleName().'/'
.$this->getDefaultNamespace('resource').'/'
.$this->option('model').$suffix;
$path = $this->getModuleName() . '/'
. $this->getDefaultNamespace('resource') . '/'
. $this->option('model') . $suffix;

$namespaces[] = ('use '.implode('\\', explode('/', $path)).';');
$namespaces[] = ('use ' . implode('\\', explode('/', $path)) . ';');

}

$replacements['RESOURCE_NAMESPACES'] = implode("\n", $namespaces);
}

protected function getClassPath(string $prefix = '', string $suffix = 'Request')
{
$resourcePath = $this->argument('name') . $suffix;

$dir = dirname($resourcePath);

$dir = ($dir == '.') ? '' : $dir . '/';

$resource = basename($resourcePath);

return $dir . $prefix . $resource;
}

/**
* Get the console command arguments.
*
Expand Down
60 changes: 30 additions & 30 deletions src/Commands/CrudMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ public function handle()

private function createRequest()
{
if (! config('api-crud.templates.request.generate', true)) {
if (!config('api-crud.templates.request.generate', true)) {
return;
}
foreach (['Index', 'Store', 'Update'] as $prefix) {

$resourcePath = $this->getResourceName().'Request';
$resourcePath = $this->getResourceName() . 'Request';

$dir = dirname($resourcePath);

$dir = ($dir == '.') ? '' : $dir.'/';
$dir = ($dir == '.') ? '' : $dir . '/';

$resource = basename($resourcePath);

$options = [
'name' => $dir.$prefix.$resource,
'name' => $dir . $prefix . $resource,
'module' => $this->getModuleName(),
];

Expand All @@ -105,42 +105,28 @@ private function getResourceName()

private function createResource()
{
if (! config('api-crud.templates.resource.generate', true)) {
if (!config('api-crud.templates.resource.generate', true)) {
return;
}
$this->call('laraflow:make-resource', [
'name' => $this->getResourceName().'Resource',
'name' => $this->getResourceName() . 'Resource',
'module' => $this->getModuleName(),
]);

$this->call('laraflow:make-resource', [
'name' => $this->getResourceName().'Collection',
'name' => $this->getResourceName() . 'Collection',
'module' => $this->getModuleName(),
'--collection' => true,
]);
}

private function createController()
{
if (! config('api-crud.templates.controller.generate', true)) {
return;
}

$this->call('laraflow:make-controller', [
'name' => $this->getResourceName().'Controller',
'--model' => $this->getResourceName(),
'module' => $this->getModuleName(),
'--crud' => true,
]);
}

/**
* @throws GeneratorException
*/
private function createModel()
{

if (! config('api-crud.templates.model.generate', true)) {
if (!config('api-crud.templates.model.generate', true)) {
return;
}
$this->call('laraflow:make-model', [
Expand All @@ -150,20 +136,34 @@ private function createModel()
]);
}

private function createController()
{
if (!config('api-crud.templates.controller.generate', true)) {
return;
}

$this->call('laraflow:make-controller', [
'name' => $this->getResourceName() . 'Controller',
'--model' => $this->getResourceName(),
'module' => $this->getModuleName(),
'--crud' => true,
]);
}

/**
* @throws GeneratorException
*/
private function updateRouteFile()
{
$filePath = base_path(config('api-crud.route_path', 'routes/api.php'));

if (! file_exists($filePath)) {
if (!file_exists($filePath)) {
throw new InvalidArgumentException("Route file location doesn't exist");
}

$fileContent = file_get_contents($filePath);

if (! str_contains($fileContent, '//DO NOT REMOVE THIS LINE//')) {
if (!str_contains($fileContent, '//DO NOT REMOVE THIS LINE//')) {
throw new GeneratorException('Route file missing the CRUD Pointer comment.');
}

Expand All @@ -174,12 +174,12 @@ private function updateRouteFile()
$controller =
GeneratorPath::convertPathToNamespace(
'\\'
.$this->getModuleName()
.'\\'
.GenerateConfigReader::read('controller')->getNamespace()
.'\\'
.$this->getResourceName()
.'Controller::class'
. $this->getModuleName()
. '\\'
. GenerateConfigReader::read('controller')->getNamespace()
. '\\'
. $this->getResourceName()
. 'Controller::class'
);

$template = <<<HTML
Expand Down
Loading

0 comments on commit bb50821

Please sign in to comment.