Skip to content

Commit

Permalink
Merge pull request #45 from kevariable/main
Browse files Browse the repository at this point in the history
Refactor package to PHP 8.2 and Introduce standard DTO and Action to readonly
  • Loading branch information
kevariable authored Dec 8, 2022
2 parents 2c38900 + 2ff04d4 commit f75985b
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 152 deletions.
42 changes: 11 additions & 31 deletions src/Actions/Command/ResolveCommandAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,33 @@
use KoalaFacade\DiamondConsole\DataTransferObjects\Filesystem\FilePresentData;
use KoalaFacade\DiamondConsole\Exceptions\FileAlreadyExistException;
use KoalaFacade\DiamondConsole\Foundation\Action;
use KoalaFacade\DiamondConsole\Foundation\EvaluatesClosures;

class ResolveCommandAction extends Action
readonly class ResolveCommandAction extends Action
{
use EvaluatesClosures;

protected Closure | null $copyStubData = null;

protected Console $console;
public function __construct(
protected Console $console,
protected Closure | null $copyStubData = null,
) {
}

/**
* @param Console $console
* @return void
*
* @throws FileAlreadyExistException
* @throws FileNotFoundException
*/
public function execute(Console $console): void
public function execute(): void
{
$this->setConsole($console);

FilePresentAction::resolve()
->execute(
data: new FilePresentData(
fileName: $console->getFileName(),
namespacePath: $console->getNamespacePath(),
fileName: $this->console->getFileName(),
namespacePath: $this->console->getNamespacePath(),
),
withForce: $console->resolveForceOption(),
withForce: $this->console->resolveForceOption(),
);

$data = $this->evaluate($this->copyStubData);

if (! $data instanceof CopyStubData) {
$data = $this->getDefaultCopyStubData();
}

CopyStubAction::resolve()->execute(data: $data);
}

public function getCopyStubDataUsing(Closure | null $callback): void
{
$this->copyStubData = $callback;
CopyStubAction::resolve()->execute(data: $this->getDefaultCopyStubData());
}

protected function getDefaultCopyStubData(): CopyStubData
Expand All @@ -64,9 +49,4 @@ protected function getDefaultCopyStubData(): CopyStubData
placeholders: $this->console->resolvePlaceholders()
);
}

protected function setConsole(Console $console): void
{
$this->console = $console;
}
}
2 changes: 1 addition & 1 deletion src/Actions/Composer/ResolveComposerAutoLoaderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use stdClass;
use Throwable;

class ResolveComposerAutoLoaderAction extends Action
readonly class ResolveComposerAutoLoaderAction extends Action
{
public function __construct(protected Filesystem $fileSystem)
{
Expand Down
19 changes: 6 additions & 13 deletions src/Actions/Factory/FactoryContractMakeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,30 @@

namespace KoalaFacade\DiamondConsole\Actions\Factory;

use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use KoalaFacade\DiamondConsole\Commands\Concerns\InteractsWithConsole;
use KoalaFacade\DiamondConsole\Commands\FactoryMakeCommand;
use KoalaFacade\DiamondConsole\Contracts\Console;
use KoalaFacade\DiamondConsole\DataTransferObjects\PlaceholderData;
use KoalaFacade\DiamondConsole\Exceptions\FileAlreadyExistException;
use KoalaFacade\DiamondConsole\Foundation\Action;
use KoalaFacade\DiamondConsole\Support\Source;

class FactoryContractMakeAction extends Action implements Console
readonly class FactoryContractMakeAction extends Action implements Console
{
use InteractsWithConsole;

public FactoryMakeCommand $console;

/**
* @param FactoryMakeCommand $console
*/
public function setConsole(FactoryMakeCommand $console): void
{
$this->console = $console;
public function __construct(
protected Console & Command $console
) {
}

/**
* @throws FileNotFoundException
* @throws FileAlreadyExistException
*/
public function execute(FactoryMakeCommand $console): static
public function execute(): static
{
$this->setConsole(console: $console);

$this->handle();

return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/Filesystem/FilePresentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use KoalaFacade\DiamondConsole\Exceptions\FileAlreadyExistException;
use KoalaFacade\DiamondConsole\Foundation\Action;

class FilePresentAction extends Action
readonly class FilePresentAction extends Action
{
/**
* @throws FileAlreadyExistException
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/Stub/CopyStubAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use KoalaFacade\DiamondConsole\DataTransferObjects\CopyStubData;
use KoalaFacade\DiamondConsole\Foundation\Action;

class CopyStubAction extends Action
readonly class CopyStubAction extends Action
{
/**
* Copy Stub to Diamond Infrastructure
Expand Down
6 changes: 2 additions & 4 deletions src/Actions/Stub/ReplacePlaceholderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @template TKey of array-key
* @template TValue
*/
class ReplacePlaceholderAction extends Action
readonly class ReplacePlaceholderAction extends Action
{
/**
* Replace Placeholder Stub data
Expand All @@ -28,9 +28,7 @@ public function execute(string $filePath, PlaceholderData $placeholders): void
{
$filesystem = new Filesystem;

$resolved = $placeholders
->resolveArrayKeyUsing(fn (string $key) => Str::camel($key))
->toArray();
$resolved = $placeholders->toArray();

$stub = $filesystem->get(path: $filePath);

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Concerns/InteractsWithConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function handle(): void
{
$this->beforeCreate();

ResolveCommandAction::resolve()->execute(console: $this);
ResolveCommandAction::resolve(parameters: ['console' => $this])->execute();

$this->afterCreate();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function beforeCreate(): void
$this->info(string: 'Generating factory & interface file to your project');

$this->resolveFactoryContractConsole(
console: FactoryContractMakeAction::resolve()->execute(console: $this)
console: FactoryContractMakeAction::resolve(parameters: ['console' => $this])->execute()
);
}

Expand Down
11 changes: 2 additions & 9 deletions src/DataTransferObjects/CopyStubData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@

use KoalaFacade\DiamondConsole\Foundation\DataTransferObject;

final class CopyStubData extends DataTransferObject
readonly class CopyStubData extends DataTransferObject
{
/**
* @param string $stubPath
* @param string $targetPath
* @param string $fileName
* @param PlaceholderData $placeholders
*/
public function __construct(
final public function __construct(
public string $stubPath,
public string $targetPath,
public string $fileName,
public PlaceholderData $placeholders,
) {
//
}
}
4 changes: 2 additions & 2 deletions src/DataTransferObjects/Filesystem/FilePresentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use KoalaFacade\DiamondConsole\Foundation\DataTransferObject;

class FilePresentData extends DataTransferObject
readonly class FilePresentData extends DataTransferObject
{
public function __construct(
final public function __construct(
public string $fileName,
public string $namespacePath,
) {
Expand Down
18 changes: 8 additions & 10 deletions src/DataTransferObjects/PlaceholderData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@

namespace KoalaFacade\DiamondConsole\DataTransferObjects;

use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Foundation\DataTransferObject;

class PlaceholderData extends DataTransferObject
readonly class PlaceholderData extends DataTransferObject
{
/**
* @param string | null $namespace
* @param string | null $class
* @param string | null $subject
* @param string | null $tableName
* @param string | null $factoryContract
* @param string|null $factoryContractNamespace
*/
public function __construct(
final public function __construct(
public null | string $namespace = null,
public null | string $class = null,
public null | string $subject = null,
Expand All @@ -23,4 +16,9 @@ public function __construct(
public null | string $factoryContractNamespace = null,
) {
}

protected function resolveArrayKey(string $key): string
{
return Str::camel($key);
}
}
6 changes: 3 additions & 3 deletions src/DiamondConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DiamondConsoleServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->registerPublishables();
$this->registerPublishers();

$this->commands(commands: $this->diamondCommands());
}
Expand All @@ -22,11 +22,11 @@ public function register(): void
$this->mergeConfigFrom(path: __DIR__ . '/../config/diamond.php', key: 'diamond');
}

protected function registerPublishables(): void
protected function registerPublishers(): void
{
$this->publishes([
__DIR__ . '/../config/diamond.php' => config_path(path: 'diamond.php'),
], 'config');
], groups: 'config');
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Foundation/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace KoalaFacade\DiamondConsole\Foundation;

abstract class Action
readonly abstract class Action
{
/**
* Resolve an action class
*
* @param array<array-key, mixed> $parameters
* @return static
*/
public static function resolve(): static
public static function resolve(array $parameters = []): static
{
/**
* @var static $static
*/
$static = resolve(name: static::class);
$static = resolve(name: static::class, parameters: $parameters);

return $static;
}
Expand Down
64 changes: 19 additions & 45 deletions src/Foundation/DataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,33 @@

namespace KoalaFacade\DiamondConsole\Foundation;

use Closure;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Foundation\DataTransferObject\HasResolvable;

abstract class DataTransferObject
abstract readonly class DataTransferObject
{
use HasResolvable, EvaluatesClosures;
use HasResolvable;

/**
* Prevent properties to included on update
* Prevent properties to included on create
*
* @var array<string>
* @return array<empty>
*/
protected array $excludedPropertiesOnCreate = [];
protected function toExcludedPropertiesOnCreate(): array
{
return [];
}

/**
* Prevent properties to included on update
* Prevent properties to included on create
*
* @var array<string>
* @return array<empty>
*/
protected array $excludedPropertiesOnUpdate = [];

protected Closure | null $resolveArrayKeyUsing = null;
protected function toExcludedPropertiesOnUpdate(): array
{
return [];
}

/**
* The method that will resolve the inheritance properties
Expand All @@ -44,48 +47,19 @@ public function toArray(): array
return Collection::wrap((array) $this)
->except(keys: $excludedPropertyKeys)
->mapWithKeys(
function ($value, $key): array {
$evaluate = $this->evaluate(
value: $this->resolveArrayKeyUsing,
parameters: ['key' => $key]
);

return [$evaluate ?? Str::snake($key) => $value];
}
fn ($value, $key): array => [$this->resolveArrayKey($key) => $value]
)
->toArray();
}

/**
* Resolve result array-key of toArray method from behaviour
*
* @param Closure | null $callback
* @return static
*/
public function resolveArrayKeyUsing(Closure | null $callback): static
{
$this->resolveArrayKeyUsing = $callback;

return $this;
}

/**
* Lookup the properties those excluded in create
*
* @return array<string>
*/
public function toExcludedPropertiesOnCreate(): array
{
return $this->excludedPropertiesOnCreate;
}

/**
* Lookup the properties those excluded in create
*
* @return array<string>
* @param string $key
* @return string
*/
public function toExcludedPropertiesOnUpdate(): array
protected function resolveArrayKey(string $key): string
{
return $this->excludedPropertiesOnUpdate;
return Str::snake($key);
}
}
Loading

0 comments on commit f75985b

Please sign in to comment.