Skip to content

Commit

Permalink
refactoring (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
shavonn committed Jan 7, 2024
1 parent 2c263ea commit 66ea649
Show file tree
Hide file tree
Showing 78 changed files with 2,983 additions and 571 deletions.
17 changes: 10 additions & 7 deletions src/Commands/ArtiMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace HandsomeBrown\Laraca\Commands;

use HandsomeBrown\Laraca\Commands\Traits\Directable;
use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand;
use HandsomeBrown\Laraca\Commands\Traits\Shared;
use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
use Illuminate\Database\Migrations\MigrationCreator;
use Illuminate\Support\Composer;
Expand All @@ -12,10 +12,9 @@
#[AsCommand(name: 'arti:migration')]
class ArtiMigrationCommand extends MigrateMakeCommand
{
use Directable, LaracaCommand;
use Directable, Shared;

/**
* signature
* The console command signature.
*
* @var string
Expand All @@ -27,6 +26,13 @@ class ArtiMigrationCommand extends MigrateMakeCommand
{--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}
{--fullpath : Output the full path of the migration (Deprecated)}';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Migration';

/**
* Create a new migration install command instance.
*
Expand All @@ -46,19 +52,16 @@ public function __construct(MigrationCreator $creator, Composer $composer)
}

/**
* getMigrationPath
* Get migration path (either specified by '--path' option or default location).
*/
protected function getMigrationPath(): string
{
[$domain, $service] = $this->gatherPathAssets();

if (! is_null($targetPath = $this->input->getOption('path'))) {
return ! $this->usingRealPath()
? $this->laravel->basePath().'/'.$targetPath
: $targetPath;
}

return self::assembleFullPath('migration', $domain, $service);
return $this->getConfigPathWithOptions('migration');
}
}
8 changes: 3 additions & 5 deletions src/Commands/DomainListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HandsomeBrown\Laraca\Commands;

use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand;
use HandsomeBrown\Laraca\Commands\Traits\Shared;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Config;
Expand All @@ -13,7 +13,7 @@
#[AsCommand(name: 'domain:list')]
class DomainListCommand extends Command
{
use LaracaCommand;
use Shared;

/**
* The console command name.
Expand Down Expand Up @@ -51,7 +51,7 @@ public function __construct(Filesystem $files)
/**
* Execute the console command.
*
* @return bool|null
* @return bool|void
*/
public function handle()
{
Expand All @@ -77,7 +77,5 @@ public function handle()
$this->table(['Domain', 'Slug', 'Path'], array_map(function ($domain) {
return [$domain['name'], $domain['slug'], $domain['path']];
}, $domains));

return Command::SUCCESS;
}
}
42 changes: 18 additions & 24 deletions src/Commands/InitMicroserviceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace HandsomeBrown\Laraca\Commands;

use HandsomeBrown\Laraca\Commands\Traits\Directable;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -41,23 +40,14 @@ class InitMicroserviceCommand extends LaracaGeneratorCommand
*/
protected $serviceName = '';

/**
* The microservice path.
*
* @var string
*/
protected $servicePath = '';

/**
* Execute the console command.
*
* @return bool|null
* @return bool|void
*/
public function handle()
{
$this->serviceName = $this->getClassName($this->input->getArgument('name'));
$servicePath = $this->getFullPath('microservice');
$this->servicePath = "$servicePath/$this->serviceName";
$this->serviceName = $this->formatName($this->input->getArgument('name'));

if (! parent::handle()) {
return false;
Expand All @@ -74,20 +64,17 @@ public function handle()
$this->components->bulletList($this->generated);
$this->components->info('Microservice created successfully.');
$this->components->info('Don\'t forget to <info>'.$this->serviceName.'ServiceProvider.php</info> to providers in <info>app.php</info>');

return Command::SUCCESS;
}

/**
* Replace the tags for the given stub.
*/
protected function replaceTags(string &$stub, string $name): string
{
$namespace = $this->getDefaultNamespace($this->rootNamespace());
$controllerNamespace = $this->assembleNamespace('controller', null, $this->serviceName);
$controllerNamespace = $this->getConfigNamespace('controller', null, $this->serviceName);

$search = ['{{ namespace }}', '{{ slug }}', '{{ service }}', '{{ controller_namespace }}'];
$replace = [$namespace, Str::slug($this->serviceName), $this->serviceName, $controllerNamespace];
$replace = [$this->getServiceNamespace(), Str::slug($this->serviceName), $this->serviceName, $controllerNamespace];

$stub = str_replace($search, $replace, $stub);

Expand Down Expand Up @@ -119,7 +106,7 @@ protected function makeDirectories(): void
'view',
];

$path = $this->servicePath.'/';
$path = $this->getServicePath().'/';

foreach ($elements as $element) {
$dir = '';
Expand All @@ -139,7 +126,7 @@ protected function makeDirectories(): void
/**
* Get the console command arguments.
*/
protected function makeProviders()
protected function makeProviders(): void
{
// RouteServiceProvider
$this->makeFile('RouteServiceProvider', __DIR__.'/stubs/serviceprovider-route.stub');
Expand All @@ -157,9 +144,9 @@ protected function makeProviders()
*/
protected function getPath(string $name): string
{
$name = Str::of($name)->replaceFirst($this->getDefaultNamespace($this->rootNamespace()), '')->replace('\\', '/');
$name = Str::of($name)->replaceFirst($this->getServiceNamespace(), '')->replace('\\', '/');

$path = $this->servicePath.'/';
$path = $this->getServicePath().'/';

switch ($name) {
case 'BroadcastServiceProvider':
Expand All @@ -181,13 +168,20 @@ protected function getPath(string $name): string
return $path;
}

protected function getDefaultNamespace($rootNamespace): string
protected function getServiceNamespace(): string
{
$namespace = $this->getFullNamespace('microservice');
$namespace = $this->getConfigNamespaceWithOptions($this->configKey);

return "$namespace\\$this->serviceName";
}

protected function getServicePath(): string
{
$path = $this->getConfigPathWithOptions($this->configKey);

return "$path/$this->serviceName";
}

/**
* Determine if the class already exists.
*
Expand All @@ -204,7 +198,7 @@ protected function alreadyExists($rawName)
/**
* Get the console command arguments.
*
* @return array
* @return array<int,array<int,int|string>>
*/
protected function getArguments()
{
Expand Down
7 changes: 3 additions & 4 deletions src/Commands/InitStructureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,27 @@ class InitStructureCommand extends LaracaGeneratorCommand
/**
* Execute the console command.
*
* @return bool|null
* @return bool|void
*/
public function handle()
{
$config = Config::get('laraca.struct');

$this->components->info('Creating directory structure from Laraca config.');

/** @var string $key */
foreach (array_keys($config) as $key) {
if ($key == 'domain' && ! $config['domain']['enabled']) {
continue;
} elseif ($key == 'microservice' && ! $config['microservice']['enabled']) {
continue;
}

$fullPath = self::assembleFullPath($key);
$fullPath = $this->getConfigPathWithOptions($key);

$this->makeEmptyDirectory($fullPath);
}

$this->components->info('Configured structure generated successfully.');

return Command::SUCCESS;
}
}
58 changes: 12 additions & 46 deletions src/Commands/LaracaGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace HandsomeBrown\Laraca\Commands;

use HandsomeBrown\Laraca\Commands\Traits\LaracaCommand;
use HandsomeBrown\Laraca\Commands\Traits\Shared;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;

class LaracaGeneratorCommand extends Command
{
use LaracaCommand;
use Shared;

/**
* The filesystem instance.
Expand All @@ -26,37 +26,17 @@ class LaracaGeneratorCommand extends Command
*/
protected $type;

/**
* Config key.
*
* @var string
*/
protected $key;

/**
* Generated files.
*
* @var array
* @var array<string>
*/
protected $generated = [];

/**
* Create a new controller creator command instance.
*
* @return void
*/
public function __construct(Filesystem $files)
{
parent::__construct();

$this->key = strtolower($this->type);
$this->files = $files;
}

/**
* Execute the console command.
*
* @return bool|null
* @return bool|void
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
Expand Down Expand Up @@ -91,11 +71,8 @@ protected function makeDirectory($path)

/**
* Build the directory for the class if necessary.
*
* @param string $path
* @return string
*/
protected function makeEmptyDirectory($path)
protected function makeEmptyDirectory(string $path): string
{
$this->makeDirectory($path);

Expand All @@ -115,11 +92,11 @@ protected function makeEmptyDirectory($path)
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function build($name, $stub)
protected function buildClass($name, $stub)
{
$stub = $this->files->get($stub);

return $this->replaceNamespace($stub, $name);
return $this->replaceTags($stub, $name);
}

/**
Expand Down Expand Up @@ -152,7 +129,7 @@ protected function makeFile($name, $stub)
protected function replaceTags(string &$stub, string $name): string
{
$search = ['{{ namespace }}', '{{ class }}'];
$replace = [$this->assembleNamespace($this->key), $name];
$replace = [$this->getConfigNamespaceWithOptions($this->configKey), $name];

$stub = str_replace($search, $replace, $stub);

Expand All @@ -166,18 +143,7 @@ protected function getPath(string $name): string
{
$name = Str::of($name)->replaceFirst($this->rootNamespace(), '')->replace('\\', '/');

return self::assembleFullPath($this->key)."/$name.php";
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $this->getFullNamespace($this->key);
return $this->getConfigPathWithOptions($this->configKey)."/$name.php";
}

/**
Expand All @@ -188,7 +154,7 @@ protected function getDefaultNamespace($rootNamespace)
*/
protected function alreadyExists($rawName)
{
return $this->files->exists($this->getPath($this->getClassName($rawName)));
return $this->files->exists($this->getPath($this->formatName($rawName)));
}

/**
Expand All @@ -198,13 +164,13 @@ protected function alreadyExists($rawName)
*/
protected function rootNamespace()
{
return $this->laravel->getNamespace();
return app()->getNamespace();
}

/**
* Get the console command options.
*
* @return array
* @return array<int,array<int,int|string>>
*/
protected function getOptions()
{
Expand Down

0 comments on commit 66ea649

Please sign in to comment.