Skip to content

Commit

Permalink
Merge pull request #14 from eleftrik/feature/custom-namespace-provider
Browse files Browse the repository at this point in the history
feat: allow non-standard namespace for SoloServiceProvider
  • Loading branch information
aarondfrancis authored Nov 20, 2024
2 parents f9fe217 + 7ab9d0a commit 4e0c49b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ Solo::allowCommandsAddedFrom([
]);
```

## Service provider in a custom location.

By default, your `SoloServiceProvider` is created in the `App\Providers` namespace, which is pre-registered as a "safe" location to add commands from. If your `SoloServiceProvider` is in a custom location, it will still be deemed "safe" as long as it resides in your application's namespace (usually `App`, but custom root namespaces are supported.)


## Contributing
Please help.

Expand Down
17 changes: 7 additions & 10 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Laravel\Prompts\Themes\Default\Renderer as PromptsRenderer;
use ReflectionClass;
Expand All @@ -43,11 +44,6 @@ class Manager
'dark' => DarkTheme::class,
];

/**
* @var array<class-string>
*/
protected array $configurationAllowedFrom = [];

/**
* @var array<class-string>
*/
Expand All @@ -56,7 +52,7 @@ class Manager
public function __construct()
{
// The only classes that are guaranteed to be fully in the user's control.
$this->configurationAllowedFrom = $this->commandsAllowedFrom = [
$this->commandsAllowedFrom = [
App::getNamespace() . 'Providers\\AppServiceProvider',
App::getNamespace() . 'Providers\\SoloServiceProvider',
];
Expand Down Expand Up @@ -231,14 +227,15 @@ protected function registrationIsAllowed(): bool

protected function ensureSafeConfigurationLocation($func): void
{
if (in_array($this->caller(), $this->configurationAllowedFrom)) {
$caller = $this->caller();
$namespace = App::getNamespace();

if (Str::startsWith($caller, $namespace)) {
return;
}

$locations = implode(' ', $this->configurationAllowedFrom);

throw new Exception(
"`$func` may only be called from one of the following classes: [$locations]"
"`$func` may only be called from the [$namespace] namespace."
);
}
}

0 comments on commit 4e0c49b

Please sign in to comment.