-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor migration and command component
Reorder folder structure Improve publish and register migration file Signed-off-by: asciito <ayax.cordova@aydev.mx>
- Loading branch information
Showing
14 changed files
with
384 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
Oops, something went wrong.