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

[5.x] Adjusts app extension namespace logic #11233

Closed
Closed
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
39 changes: 35 additions & 4 deletions src/Providers/ExtensionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\ServiceProvider;
use SplFileInfo;
use Statamic\Actions;
use Statamic\Actions\Action;
use Statamic\Dictionaries;
Expand Down Expand Up @@ -329,18 +330,48 @@ protected function registerCoreExtensions($extensions)
}
}

public static function getNamespaceFromFile(SplFileInfo $file, string $basePath, string $appNamespace): ?string
{
// Normalize some things.
$basePath = str_replace('\\', '/', $basePath);
$basePath = realpath($basePath);
$filePath = realpath($file->getPath()); // Note: Not using getRealPath() to avoid the filename at this point.

// Bail.
if (! $basePath || ! $filePath) {
return null;
}

// More separator normalization.
$basePath = str_replace('\\', '/', $basePath);
$filePath = str_replace('\\', '/', $filePath);

$basePath = Str::finish($basePath, '/');

$namespace = str_replace('/', '\\', mb_substr($filePath, mb_strlen($basePath)));
$class = $file->getBasename('.php');
$namespace = ltrim($namespace, '\\');

// Cleanup the app namespace.
$appNamespace = str_replace('/', '\\', $appNamespace);
$appNamespace = str_replace('\\\\', '\\', $appNamespace);
$appNamespace = ltrim($appNamespace, '\\');
$appNamespace = Str::finish($appNamespace, '\\');

return "{$appNamespace}{$namespace}\\{$class}";
}

protected function registerAppExtensions($folder, $requiredClass)
{
if (! $this->app['files']->exists($path = app_path($folder))) {
return;
}

foreach ($this->app['files']->allFiles($path) as $file) {
$relativePathOfFolder = str_replace(app_path('/'), '', $file->getPath());
$namespace = str_replace('/', '\\', $relativePathOfFolder);
$class = $file->getBasename('.php');
if (! $fqcn = self::getNamespaceFromFile($file, app_path(), $this->app->getNamespace())) {
continue;
}

$fqcn = $this->app->getNamespace()."{$namespace}\\{$class}";
if (is_subclass_of($fqcn, $requiredClass)) {
$fqcn::register();
}
Expand Down
62 changes: 62 additions & 0 deletions tests/Extend/AppExtensionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Tests\Extend;

use PHPUnit\Framework\Attributes\Test;
use SplFileInfo;
use Statamic\Providers\ExtensionServiceProvider;
use Tests\TestCase;

class AppExtensionsTest extends TestCase
{
#[Test]
public function it_resolves_namespaces_correctly()
{
$files = collect($this->app['files']->allFiles(__DIR__.'/../__fixtures__/classes/app'))
->map(fn (SplFileInfo $file) => [$file, trim(file_get_contents($file->getPathname()))]);

$basePaths = [
__DIR__.'/../__fixtures__/classes/app',
__DIR__.'/../__fixtures__/classes/app\\',
__DIR__.'/../__fixtures__/classes/app/',
__DIR__.'/..\\__fixtures__/classes/app/',
];

$appNamespaces = [
'App' => [
'App',
'App\\',
'App////',
'App/\\',
'\\App/',
'\\App/\\',
],
'Something\\Nested\\Here' => [
'Something\\Nested\\Here',
'Something\\Nested\\Here\\',
'Something\\Nested\\Here/',
'Something\\Nested\\Here/\\',
'\\Something\\Nested\\Here/',
'\\Something\\Nested\\Here/\\',
],
];

foreach ($files as $info) {
[$file, $expectedRelativeClassName] = $info;

foreach ($appNamespaces as $appNamespace => $appVariations) {
$expectedNamespace = "{$appNamespace}\\{$expectedRelativeClassName}";

foreach ($basePaths as $basePath) {
foreach ($appVariations as $variation) {
$this->assertSame(
$expectedNamespace,
ExtensionServiceProvider::getNamespaceFromFile($file, $basePath, $variation),
"App Namespace: {$appNamespace} Variation: {$variation} Base Path: {$basePath}"
);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dictionaries\DictionaryOne
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dictionaries\Nested\DictionaryTwo
1 change: 1 addition & 0 deletions tests/__fixtures__/classes/app/Tags/Nested/TagTwo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tags\Nested\TagTwo
1 change: 1 addition & 0 deletions tests/__fixtures__/classes/app/Tags/Nested/not_a_tag.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tags\Nested\not_a_tag.txt
1 change: 1 addition & 0 deletions tests/__fixtures__/classes/app/Tags/TagOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tags\TagOne
Loading