Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow finding the base path when a laravel folder organisation … #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,16 @@ public function packageBooted()
protected function getPackageBaseDir(): string
{
$reflector = new ReflectionClass(get_class($this));
$packageBaseDir = dirname($reflector->getFileName());

return dirname($reflector->getFileName());
// Some packages like to keep Laravels directory structure and place
// the service providers in a Providers folder.
// move up a level when this is the case.
if (str_ends_with($packageBaseDir, DIRECTORY_SEPARATOR.'Providers')) {
$packageBaseDir = dirname($packageBaseDir);
}

return $packageBaseDir;
}

public function packageView(?string $namespace)
Expand Down
20 changes: 20 additions & 0 deletions tests/PackageServiceProviderTests/PackageBasePathTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\Tests\TestPackage\Src\Providers\ServiceProvider;

trait ConfigurePackageBasePathTest
{
public function configurePackage(Package $package)
{
$package
->name('laravel-package-tools');
}
}

uses(ConfigurePackageBasePathTest::class);

it('will set the base path to the Src dir when the laravel folder organisation is applied', function () {
$provider = new ServiceProvider(app());
expect($provider->getPackageBaseDir())->toEndWith( DIRECTORY_SEPARATOR.'TestPackage'.DIRECTORY_SEPARATOR.'Src');
});
20 changes: 20 additions & 0 deletions tests/TestPackage/Src/Providers/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Spatie\LaravelPackageTools\Tests\TestPackage\Src\Providers;

use Closure;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class ServiceProvider extends PackageServiceProvider
{
public function getPackageBaseDir():string
{
return parent::getPackageBaseDir();
}

public function configurePackage(Package $package): void
{
$package->name('laravel-package-tools');
}
}