Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(command): installation structure directory #94

Merged
merged 6 commits into from
Oct 10, 2023
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
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
},
"require-dev": {
"laravel/pint": "^1.2",
"pestphp/pest": "^1.22",
"pestphp/pest-plugin-laravel": "^1.3",
"pestphp/pest": "^2.21",
"pestphp/pest-plugin-laravel": "^2.2",
"orchestra/testbench": "^7.14|^8.0",
"phpstan/phpstan": "^1.9"
"phpstan/phpstan": "^1.10"
},
"config": {
"allow-plugins": {
Expand Down
2 changes: 1 addition & 1 deletion config/diamond.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
'structures' => [
Layer::infrastructure->name => 'Infrastructure',
Layer::domain->name => 'Domain',
Layer::application->name => 'app',
Layer::application->name => 'Application',
],
];
2 changes: 1 addition & 1 deletion src/Actions/Command/ResolveCommandAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
public function __construct(
protected Console $console,
protected Closure | null $copyStubData = null,
protected ?Closure $copyStubData = null,
) {
}

Expand Down
37 changes: 10 additions & 27 deletions src/Actions/Composer/ResolveComposerAutoLoaderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,22 @@ public function __construct(protected Filesystem $fileSystem)
* @throws UnableToWriteFile
* @throws Throwable
*/
public function execute(): bool
public function execute(string $domain): bool
{
/** @var string $baseDirectory */
$baseDirectory = config(key: 'diamond.base_directory');
$composer = $this->fetchComposerContents();

foreach ($this->resolveBaseStructures() as $structure) {
$namespace = Str::of(string: $structure)->finish(cap: '\\');
$directory = $namespace->replace(search: '\\', replace: '/');
$namespace = Str::of(string: $domain)->finish(cap: '\\');
$directory = $namespace->replace(search: '\\', replace: '/');

if (Arr::exists(array: $composer->autoload['psr-4'], key: $namespace->toString())) {
continue;
}

Arr::set(
array: $composer->autoload['psr-4'],
key: $namespace->toString(),
value: $directory
->start(prefix: $baseDirectory)
->toString()
);
}
Arr::set(
array: $composer->autoload['psr-4'],
key: $namespace->toString(),
value: $directory
->start(prefix: $baseDirectory)
->toString()
);

return $this->updateComposerContents(contents: $composer);
}
Expand Down Expand Up @@ -97,15 +91,4 @@ protected function resolveBasePathForComposer(): string
{
return base_path(path: 'composer.json');
}

/**
* @return array<string>
*/
protected function resolveBaseStructures(): array
{
/** @var array<string> $structures */
$structures = config(key: 'diamond.structures');

return $structures;
}
}
1 change: 1 addition & 0 deletions src/Actions/Stub/ReplacePlaceholderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function ($value, $key) use (&$stub) {
}
);

/** @var string $contents */
$contents = $stub;

$filesystem->ensureDirectoryExists(path: Str::of($filePath)->beforeLast(search: '/'));
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Application/ResourceMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class: $this->getClassName(),
);
}

public function resolveModelNamespace(): string | null
public function resolveModelNamespace(): ?string
{
if ($this->resolveModelOption()) {
$namespace = Source::resolveNamespace(
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/Concerns/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ protected function resolveFactoryOption(): bool
return (bool) $this->option(key: 'factory');
}

protected function resolveTableName(): string | null
protected function resolveTableName(): ?string
{
/** @var string|null $name */
$name = $this->option(key: 'create') ?: $this->option(key: 'table');

return $name;
}

public function resolveEventOption(): string | null
public function resolveEventOption(): ?string
{
/** @var string|null $name */
$name = $this->option(key: 'event');

return $name;
}

public function resolveModelOption(): string | null
public function resolveModelOption(): ?string
{
/** @var string|null $name */
$name = $this->option(key: 'model');

return $name;
}

public function resolveRenderOption(): string | null
public function resolveRenderOption(): ?string
{
/** @var string|null $name */
$name = $this->option(key: 'render');
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Domain/BuilderMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class: $this->getClassName(),
);
}

public function resolveModelNamespace(): string | null
public function resolveModelNamespace(): ?string
{
if ($this->resolveModelOption()) {
$namespace = Source::resolveNamespace(
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Infrastructure/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class FactoryMakeCommand extends Command implements Console
{
use InteractsWithConsole, HasOptions, HasArguments;
use HasArguments, HasOptions, InteractsWithConsole;

protected $signature = 'infrastructure:make:factory {name} {domain} {--model=} {--force}';

Expand Down Expand Up @@ -60,7 +60,7 @@ class: $this->resolveNameArgument(),
);
}

protected function getModelNamespace(): string | null
protected function getModelNamespace(): ?string
{
return Source::resolveNamespace(
data: new NamespaceData(
Expand Down
80 changes: 44 additions & 36 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
// use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Actions\Composer\ResolveComposerAutoLoaderAction;
use KoalaFacade\DiamondConsole\Commands\Concerns\HasArguments;
use KoalaFacade\DiamondConsole\Enums\Layer;
use KoalaFacade\DiamondConsole\Support\Source;
use Throwable;

class InstallCommand extends Command
{
protected $signature = 'diamond:install {--skip-refactor}';
use HasArguments;

protected $signature = 'domain:install {domain}';

protected $description = 'Install the Domain Driven Structure in your project';

Expand All @@ -30,21 +32,23 @@ public function handle(): void

$fileSystem->ensureDirectoryExists(path: $this->resolveBaseDirectoryPath());

$domainName = $this->resolveDomainArgument();

foreach ($this->resolveBaseStructures() as $structure) {
$path = $this->resolveBaseDirectoryPath() . $structure;
$path = $this->resolveBaseDirectoryPath() . $domainName . '/' . $structure;

if ($fileSystem->exists(path: $path)) {
$this->line(string: 'Skipping generate ' . $structure . ' , the base directory is exists');

continue;
}

$fileSystem->makeDirectory(path: $path);
$fileSystem->ensureDirectoryExists(path: $path);
}

ResolveComposerAutoLoaderAction::resolve()->execute();
ResolveComposerAutoLoaderAction::resolve()->execute($domainName);

$this->resolveRefactor();
// $this->resolveRefactor();

$this->info(string: 'Successfully generate base file');
}
Expand All @@ -62,43 +66,47 @@ protected function resolveBaseStructures(): array
/** @var array<string> $structures */
$structures = config(key: 'diamond.structures');

return Arr::except(array: $structures, keys: Layer::application->name);
return $structures;
}

protected function resolveInfrastructurePath(): string
{
return Layer::infrastructure->resolvePath(suffix: '/Laravel/Providers');
return Layer::infrastructure->resolvePath(prefix: $this->resolveDomainArgument(), suffix: '/Laravel/Providers');
}

protected function resolveNamespace(): string
{
return Layer::infrastructure->resolveNamespace(suffix: '\\Laravel\\Providers');
return Layer::infrastructure->resolveNamespace(prefix: $this->resolveDomainArgument() . '\\', suffix: '\\Laravel\\Providers');
}

protected function resolveRefactor(): void
{
if (! $this->option('skip-refactor')) {
$filesystem = new Filesystem;
$filesystem->ensureDirectoryExists($this->resolveInfrastructurePath());
$filesystem->moveDirectory(from: app_path(path: 'Providers'), to: $this->resolveInfrastructurePath());
$configPath = base_path(path: '/config/app.php');
$contents = Str::replace(
search: 'App\\Providers',
replace: $this->resolveNamespace(),
subject: $filesystem->get($configPath)
);

$filesystem->put(path: $configPath, contents: $contents);

foreach ($filesystem->files($this->resolveInfrastructurePath()) as $file) {
$contents = Str::replace(
search: 'App\\Providers',
replace: $this->resolveNamespace(),
subject: $filesystem->get($file)
);

$filesystem->put(path: $file, contents: $contents);
}
}
}
// disable auto refactor app path for now, because we need further planning
//
// protected function resolveRefactor(): void
// {
// if (! $this->option('skip-refactor')) {
// $filesystem = new Filesystem;
// $filesystem->ensureDirectoryExists($this->resolveInfrastructurePath());
// $filesystem->moveDirectory(from: app_path(path: 'Providers'), to: $this->resolveInfrastructurePath());
// $configPath = base_path(path: '/config/app.php');
// /** @var string $contents */
// $contents = Str::replace(
// search: 'App\\Providers',
// replace: $this->resolveNamespace(),
// subject: $filesystem->get($configPath)
// );
//
// $filesystem->put(path: $configPath, contents: $contents);
//
// foreach ($filesystem->files($this->resolveInfrastructurePath()) as $file) {
// /** @var string $contents */
// $contents = Str::replace(
// search: 'App\\Providers',
// replace: $this->resolveNamespace(),
// subject: $filesystem->get($file)
// );
//
// $filesystem->put(path: $file, contents: $contents);
// }
// }
// }
Comment on lines +84 to +111
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put an commen why we commented it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

}
2 changes: 1 addition & 1 deletion src/Contracts/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace KoalaFacade\DiamondConsole\Contracts;

interface Console extends Options, Placeholders, Arguments, LifeCycle
interface Console extends Arguments, LifeCycle, Options, Placeholders
{
public function getFullPath(): string;

Expand Down
2 changes: 1 addition & 1 deletion src/DataTransferObjects/NamespaceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct(
public string $structures,
public string $domainArgument,
public string $nameArgument,
public string | null $endsWith = null
public ?string $endsWith = null
) {
}
}
22 changes: 11 additions & 11 deletions src/DataTransferObjects/PlaceholderData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
readonly class PlaceholderData extends DataTransferObject
{
final public function __construct(
public null | string $namespace = null,
public null | string $class = null,
public null | string $subject = null,
public null | string $tableName = null,
public null | string $factoryContract = null,
public null | string $factoryContractNamespace = null,
public null | string $factoryContractAlias = null,
public null | string $event = null,
public null | string $eventNamespace = null,
public null | string $model = null,
public null | string $modelNamespace = null,
public ?string $namespace = null,
public ?string $class = null,
public ?string $subject = null,
public ?string $tableName = null,
public ?string $factoryContract = null,
public ?string $factoryContractNamespace = null,
public ?string $factoryContractAlias = null,
public ?string $event = null,
public ?string $eventNamespace = null,
public ?string $model = null,
public ?string $modelNamespace = null,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/DiamondConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function diamondCommands(): array
return Arr::flatten(array: array_merge($generalCommands, $domainDrivenCommands));
}

protected function resolveCommandNamespace(SplFileInfo $file, string | null $directory = null): string
protected function resolveCommandNamespace(SplFileInfo $file, string $directory = null): string
{
return Str::of(string: $file->getFilenameWithoutExtension())
->prepend(values: $directory ? "$directory\\" : '')
Expand Down
8 changes: 4 additions & 4 deletions src/Enums/Layer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ enum Layer

case infrastructure;

public function resolveNamespace(string $suffix = ''): string
public function resolveNamespace(string $prefix = '', string $suffix = ''): string
{
$key = $this->name;

return config(key: 'diamond.structures.' . $key) . $suffix;
return $prefix . config(key: 'diamond.structures.' . $key) . $suffix;
}

public function resolvePath(string $suffix): string
public function resolvePath(string $prefix, string $suffix): string
{
return base_path() . '/' . config(key: 'diamond.base_directory') . $this->resolveNamespace(suffix: $suffix);
return base_path() . '/' . config(key: 'diamond.base_directory') . $prefix . '/' . $this->resolveNamespace(suffix: $suffix);
}
}
2 changes: 1 addition & 1 deletion src/Foundation/DataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

abstract readonly class DataTransferObject
{
use Cloneable;
use HasResolvable;
use Tappable;
use Cloneable;

/**
* Prevent properties to included on create
Expand Down
Loading