Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

php artisan csfixer:run
php artisan pint
6 changes: 4 additions & 2 deletions app/Console/Commands/GenerateCrudStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function handle()
[
'name' => "Api/V1/{$name}/{$name}Controller",
'--api' => true,
'--model' => $name
]
'--model' => $name,
],
],
'request' => ['make:request', ['name' => "{$name}/{$name}InsertUpdateRequest"]],
'resource' => ['make:resource', ['name' => "{$name}/{$name}Resource"]],
Expand Down Expand Up @@ -66,9 +66,11 @@ private function callArtisanCommand($taskName, $command, $arguments)
try {
$exitCode = Artisan::call($command, $arguments);
$this->line(Artisan::output());

return $exitCode !== 0;
} catch (\Exception $e) {
$this->error("Error creating {$taskName}: {$e->getMessage()}");

return true;
}
}
Expand Down
152 changes: 76 additions & 76 deletions app/Console/Commands/MakeDTOCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,113 +19,128 @@ class MakeDTOCommand extends Command
/**
* Name and signiture of Command.
* name
*
* @var string
*/
protected $name = 'make:dto';

/**
* command description.
* description
*
* @var string
*/
protected $description = 'create a new DTO';

/**
* Get Command argumant EX : HasAuth
* getArguments
* __construct
*
* @return array
* @return void
*/
protected function getArguments()
public function __construct()
{
return [
['dto', InputArgument::REQUIRED, 'The name of the DTO'],
];
parent::__construct();
}


/**
* __construct
*
* @return void
* getClassNamespace
*/
public function __construct()
public function getDefaultNamespace(): string
{
parent::__construct();
return 'App\\Http\\DTOs';
}

/**
* getDTOName
* Return a vaid class name
* getClass
*
* @return string
*/
private function getDTOName()
public function getClass()
{
$dto = Str::studly($this->argument('dto'));
return $dto;
return class_basename($this->argument($this->argumentName));
}

/**
* getDestinationFilePath
* Generate class namespace dinamacally
* getClassNamespace
*
* @return string
*/
protected function getDestinationFilePath()
public function getClassNamespace()
{
return app_path() . "/Http/DTOs" . '/' . $this->getDTOName() . '.php';
$extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));

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

$namespace = $this->getDefaultNamespace();

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

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

return trim($namespace, '\\');
}

/**
* getDTONameWithoutNamespace
*
* @return string
* Create view directory if not exists.
*/
private function getDTONameWithoutNamespace()
public function createDir($path)
{
return class_basename($this->getDTOName());
$dir = dirname($path);

if (! file_exists($dir)) {
mkdir($dir, 0777, true);
}
}

/**
* getClassNamespace
* Execute the console command.
*
* @return string
* @return int
*/
public function getDefaultNamespace(): string
public function handle()
{
return "App\\Http\\DTOs";
$path = str_replace('\\', '/', $this->getDestinationFilePath());

$fileContents = $this->getTemplateContents();

$this->createDir($path);

if (File::exists($path)) {
$this->error("File {$path} already exists!");

return 1;
}

File::put($path, $fileContents);
$this->info("DTO generated successfully! path : {$path}");

return 0;

}

/**
* Return a vaid class name
* getClass
* Get Command argumant EX : HasAuth
* getArguments
*
* @return string
* @return array
*/
public function getClass()
protected function getArguments()
{
return class_basename($this->argument($this->argumentName));
return [
['dto', InputArgument::REQUIRED, 'The name of the DTO'],
];
}


/**
* Generate class namespace dinamacally
* getClassNamespace
* getDestinationFilePath
*
* @return string
*/
public function getClassNamespace()
protected function getDestinationFilePath()
{
$extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));

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

$namespace = $this->getDefaultNamespace();

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

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

return trim($namespace, '\\');
return app_path() . '/Http/DTOs' . '/' . $this->getDTOName() . '.php';
}

/**
Expand All @@ -136,6 +151,7 @@ public function getClassNamespace()
protected function getStubFilePath()
{
$stub = '/stubs/dto.stub';

return $stub;
}

Expand All @@ -149,8 +165,8 @@ protected function getTemplateContents()
$fileTemplate = file_get_contents(__DIR__ . $this->getStubFilePath());

$replaceOptions = [
'CLASS_NAMESPACE' => $this->getClassNamespace(),
'CLASS' => $this->getDTONameWithoutNamespace()
'CLASS_NAMESPACE' => $this->getClassNamespace(),
'CLASS' => $this->getDTONameWithoutNamespace(),
];

foreach ($replaceOptions as $search => $replace) {
Expand All @@ -161,40 +177,24 @@ protected function getTemplateContents()
}

/**
* Create view directory if not exists.
* getDTOName
*
* @param $path
* @return string
*/
public function createDir($path)
private function getDTOName()
{
$dir = dirname($path);
$dto = Str::studly($this->argument('dto'));

if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
return $dto;
}

/**
* Execute the console command.
* getDTONameWithoutNamespace
*
* @return int
* @return string
*/
public function handle()
private function getDTONameWithoutNamespace()
{
$path = str_replace('\\', '/', $this->getDestinationFilePath());

$fileContents = $this->getTemplateContents();

$this->createDir($path);

if (File::exists($path)) {
$this->error("File {$path} already exists!");
return 1;
}

File::put($path, $fileContents);
$this->info("DTO generated successfully! path : {$path}");
return 0;

return class_basename($this->getDTOName());
}
}
117 changes: 117 additions & 0 deletions app/Console/Commands/MakeMapperCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;

class MakeMapperCommand extends Command
{
protected $name = 'make:mapper';

protected $description = 'Create a new mapper class';

protected string $argumentName = 'mapper';

public function __construct()
{
parent::__construct();
}

public function getDefaultNamespace(): string
{
return 'App\\Http\\Mappers';
}

public function getClass(): string
{
return class_basename($this->argument($this->argumentName));
}

public function getClassNamespace(): string
{
$extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));
$extra = str_replace('/', '\\', $extra);
$namespace = $this->getDefaultNamespace() . '\\' . $extra;

return trim(str_replace('/', '\\', $namespace), '\\');
}

public function handle()
{
$path = str_replace('\\', '/', $this->getDestinationFilePath());
$fileContents = $this->getTemplateContents();

$this->createDir($path);

if (File::exists($path)) {
$this->error("File {$path} already exists!");

return 1;
}

File::put($path, $fileContents);
$this->info("Mapper generated successfully! path : {$path}");

return 0;
}

protected function getArguments(): array
{
return [
[$this->argumentName, InputArgument::REQUIRED, 'The name of the mapper class.'],
];
}

protected function getDestinationFilePath(): string
{
return app_path() . '/Http/Mappers/' . $this->getMapperName() . '.php';
}

protected function getStubFilePath(): string
{
return '/stubs/mappers.stub';
}

protected function getTemplateContents(): string
{
$template = file_get_contents(__DIR__ . $this->getStubFilePath());

$replaceOptions = [
'CLASS_NAMESPACE' => $this->getClassNamespace(),
'CLASS' => $this->getMapperNameWithoutNamespace(),
];

foreach ($replaceOptions as $search => $replace) {
$template = str_replace('$' . strtoupper($search) . '$', $replace, $template);
}

return $template;
}

protected function getMapperName(): string
{
$mapper = Str::studly($this->argument($this->argumentName));

if (! Str::endsWith(strtolower($mapper), 'mapper')) {
$mapper .= 'Mapper';
}

return $mapper;
}

protected function getMapperNameWithoutNamespace(): string
{
return class_basename($this->getMapperName());
}

protected function createDir(string $path)
{
$dir = dirname($path);
if (! file_exists($dir)) {
mkdir($dir, 0777, true);
}
}
}
Loading