Skip to content

Commit

Permalink
Merge pull request #24 from savannabits/0.x-dev
Browse files Browse the repository at this point in the history
New Feature: Provider Generation Command
  • Loading branch information
coolsam726 authored Apr 8, 2024
2 parents 948d6bf + d0baef6 commit a7cd46e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Commands/ProviderMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Savannabits\Modular\Commands;

use Illuminate\Console\GeneratorCommand;
use Savannabits\Modular\Support\Concerns\GeneratesModularFiles;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'modular:make-provider')]
class ProviderMakeCommand extends GeneratorCommand
{
use GeneratesModularFiles;

/**
* The console command name.
*
* @var string
*/
protected $name = 'modular:make-provider';

protected $description = 'Create a new Service provider class in a modular package';

protected $type = 'Provider';

public function handle(): ?bool
{
$result = parent::handle();

if ($result === false) {
return $result;
}

return $result;
}

/**
* Get the stub file for the generator.
*/
protected function getStub(): string
{
return $this->resolveStubPath('/stubs/provider.stub');
}

protected function getRelativeNamespace(): string
{
return '\\Providers';
}

/**
* Get the console command arguments.
*/
protected function getOptions(): array
{
return [
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the provider already exists'],
];
}
}
28 changes: 28 additions & 0 deletions stubs/module.provider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class {{ class }} extends PackageServiceProvider
$this->loadMigrationsFrom($this->package->basePath('/../database/migrations'));
}

public function packageRegistered(): void
{
foreach ($this->getProviders() as $provider) {
$this->app->register($provider);
}
}

private function furtherInstallationSteps(InstallCommand $command)
{
//
Expand All @@ -44,6 +51,13 @@ class {{ class }} extends PackageServiceProvider
]);
}

private function getProviders(): array
{
return array_merge($this->discoverProviders(), [
// Your other providers
]);
}

private function discoverMigrations(): array
{
// Get an array of file names from the migrations directory
Expand Down Expand Up @@ -74,6 +88,20 @@ class {{ class }} extends PackageServiceProvider
->map(fn ($filename) => $this->getNamespaceFromFile($filename)->toString())
->toArray();
}
private function discoverProviders(): array
{
// automatically include all namespace classes in the Console directory
// use glob to return full paths to all files in the Console directory

$paths = array_merge(
glob($this->package->basePath('/Providers/*.php')),
glob($this->package->basePath('/Providers/**/*.php'))
);

return collect($paths)
->map(fn ($filename) => $this->getNamespaceFromFile($filename)->toString())
->toArray();
}

private function getNamespaceFromFile($path): Stringable
{
Expand Down

0 comments on commit a7cd46e

Please sign in to comment.