Skip to content

Commit

Permalink
chore: refactor migration and command component
Browse files Browse the repository at this point in the history
Reorder folder structure
Improve publish and register migration file

Signed-off-by: asciito <ayax.cordova@aydev.mx>
  • Loading branch information
asciito committed Oct 3, 2023
1 parent 77fa387 commit 18e784e
Show file tree
Hide file tree
Showing 14 changed files with 384 additions and 141 deletions.
83 changes: 83 additions & 0 deletions src/Package/Concerns/HasCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Asciito\LaravelPackage\Package\Concerns;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;

trait HasCommand
{
protected bool $preventLoadDefaultCommandFolder = false;

protected bool $shouldIncludeCommandFromFolder = false;

public function hasCommand(): bool
{
return $this->shouldLoadDefaultCommandFolder()
|| $this->getRegister('command')->isNotEmpty();
}

public function withCommand(string|array $command = []): static
{
$this->ensureRegistersInitialize('command');

if (filled($command)) {
$this->register(
'command',
$command,
);
}

$this->shouldIncludeCommandFromFolder = true;

return $this;
}

public function excludeCommand(string|array $path): static
{
$this->exclude('command', $path);

return $this;
}

public function getRegisteredCommand(): Collection
{
$files = $this->getDefaultCommandFiles();

return $files
->merge($this->getRegister('command'))
->filter(fn (string $path) => ! in_array($path, $this->getExclude('command')->all()));
}

public function getDefaultCommandFiles(): Collection
{
if (! $this->shouldLoadDefaultCommandFolder()) {
return collect();
}

return $this->getFilesFrom($this->getBasePath('console/commands'))
->map(fn (string $path): string => Str::of($path)
->basename('.php')
->prepend(
'\\',
$this->getNamespace(),
'\\Console\\Commands\\',
)
,
);

}

public function preventDefaultCommand(): static
{
$this->preventLoadDefaultCommandFolder = true;

return $this;
}

private function shouldLoadDefaultCommandFolder(): bool
{
return ! $this->preventLoadDefaultCommandFolder
&& $this->shouldIncludeCommandFromFolder;
}
}
86 changes: 0 additions & 86 deletions src/Package/Concerns/HasCommands.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

trait HasMigrations
trait HasMigration
{
protected string $migrationPath;

Expand Down
54 changes: 54 additions & 0 deletions src/Package/Contracts/WithCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Asciito\LaravelPackage\Package\Contracts;

use Illuminate\Support\Collection;

interface WithCommand
{
/**
* Check if the package has command file(s) available
*
* @return bool true if the package has command file(s), false otherwise
*/
public function hasCommand(): bool;

/**
* Register the command file(s) for the package
*
* This will register the command file(s) and will set the command to be publishable,
* and to set some files as publishable, and others not, just register the command file one
* by one, or in chunks.
*
* By calling this method this will try to load your command
* file(s) from the default command folder of your package.
*
* @param string|string[] $command The file(s) you want to register
*/
public function withCommand(string|array $command = []): static;

/**
* Exclude a command from being register
*/
public function excludeCommand(string|array $path): static;

/**
* An array with the command file(s) registered
*
* @return Collection The command file(s) registered in the package
*/
public function getRegisteredCommand(): Collection;

/**
* Get the files from the command path
*/
public function getDefaultCommandFiles(): Collection;

/**
* Prevent publishing the default command folder
*
* Calling this method ensures that the command files in the default folder
* wouldn't be loaded automatically.
*/
public function preventDefaultCommand(): static;
}
45 changes: 0 additions & 45 deletions src/Package/Contracts/WithCommands.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Support\Collection;

interface WithMigrations
interface WithMigration
{
/**
* Check if the package has migration file(s) available
Expand Down
12 changes: 6 additions & 6 deletions src/Package/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Asciito\LaravelPackage\Package;

use Asciito\LaravelPackage\Package\Concerns\HasCommands;
use Asciito\LaravelPackage\Package\Concerns\HasCommand;
use Asciito\LaravelPackage\Package\Concerns\HasConfig;
use Asciito\LaravelPackage\Package\Concerns\HasMigrations;
use Asciito\LaravelPackage\Package\Contracts\WithCommands;
use Asciito\LaravelPackage\Package\Concerns\HasMigration;
use Asciito\LaravelPackage\Package\Contracts\WithCommand;
use Asciito\LaravelPackage\Package\Contracts\WithConfig;
use Asciito\LaravelPackage\Package\Contracts\WithMigrations;
use Asciito\LaravelPackage\Package\Contracts\WithMigration;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
Expand All @@ -16,9 +16,9 @@
use Illuminate\Support\Str;
use Symfony\Component\Finder\SplFileInfo;

class Package implements WithCommands, WithConfig, WithMigrations
class Package implements WithCommand, WithConfig, WithMigration
{
use HasCommands, HasConfig, HasMigrations;
use HasCommand, HasConfig, HasMigration;

/**
* @var string The package name
Expand Down
4 changes: 2 additions & 2 deletions src/Package/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ protected function publishesMigrations(Package $package): void
*/
protected function publishesCommands(Package $package): void
{
if ($package->hasCommands()) {
$this->commands($package->getRegisteredCommands()->all());
if ($package->hasCommand()) {
$this->commands($package->getRegisteredCommand()->all());
}
}
}
51 changes: 51 additions & 0 deletions tests/Feature/Command/PackageregisterCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use Asciito\LaravelPackage\Package\Package;

use function Pest\Laravel\artisan;

trait PackageRegisterCommandTest
{
protected function configurePackage(Package $package): void
{
$package
->setName('package')
->withCommand([
\Workbench\App\Console\Commands\Extra\PackageCommandThree::class,
\Workbench\App\Console\Commands\Extra\PackageCommandFour::class,
]);
}
}

uses(PackageRegisterCommandTest::class);

test('package has registered files from folder', function () {
expect($this->package)
->getRegisteredCommand()
->toHaveCount(4);

artisan('list')
->expectsOutputToContain('package:test-one')
->expectsOutputToContain('package:test-two')
->expectsOutputToContain('package:test-three')
->expectsOutputToContain('package:test-four')
->assertSuccessful();
});

it('run registered commands', function () {
artisan('package:test-one')
->expectsOutput('Package test one')
->assertSuccessful();

artisan('package:test-two')
->expectsOutput('Package test two')
->assertSuccessful();

artisan('package:test-three')
->expectsOutput('Package test three')
->assertSuccessful();

artisan('package:test-four')
->expectsOutput('Package test four')
->assertSuccessful();
});
Loading

0 comments on commit 18e784e

Please sign in to comment.