Skip to content

Commit

Permalink
Merge branch 'next-25323/improve-composer-autoloader' into 'trunk'
Browse files Browse the repository at this point in the history
NEXT-25323 - Improve composer autoloader

See merge request shopware/6/product/platform!10241
  • Loading branch information
shyim committed Feb 21, 2023
2 parents 59ffbcc + eb5a535 commit 816ec6b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Shopware\Core\Framework\Plugin\KernelPluginLoader;

use Composer\Autoload\ClassLoader;
use Composer\Autoload\ClassMapGenerator;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Parameter\AdditionalBundleParameters;
use Shopware\Core\Framework\Plugin;
Expand Down Expand Up @@ -207,9 +206,7 @@ private function registerPluginNamespaces(string $projectDir): void
$mappedPaths = $this->mapPsrPaths($pluginName, $paths, $projectDir, $plugin['path']);
$this->classLoader->addPsr4($namespace, $mappedPaths);
if ($this->classLoader->isClassMapAuthoritative()) {
foreach ($mappedPaths as $mappedPath) {
$this->classLoader->addClassMap(ClassMapGenerator::createMap($mappedPath));
}
$this->classLoader->setClassMapAuthoritative(false);
}
}

Expand All @@ -221,9 +218,7 @@ private function registerPluginNamespaces(string $projectDir): void

$this->classLoader->add($namespace, $mappedPaths);
if ($this->classLoader->isClassMapAuthoritative()) {
foreach ($mappedPaths as $mappedPath) {
$this->classLoader->addClassMap(ClassMapGenerator::createMap($mappedPath));
}
$this->classLoader->setClassMapAuthoritative(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php declare(strict_types=1);

namespace Shopware\Tests\Unit\Core\Framework\Plugin\KernelPluginLoader;

use Composer\Autoload\ClassLoader;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Framework\Plugin\KernelPluginLoader\StaticKernelPluginLoader;
use Shopware\Tests\Unit\Core\Framework\Plugin\_fixtures\ExampleBundle\ExampleBundle;

/**
* @internal
*
* @covers \Shopware\Core\Framework\Plugin\KernelPluginLoader\KernelPluginLoader
*/
class KernelPluginLoaderTest extends TestCase
{
/**
* @dataProvider classLoaderDataProvider
*/
public function testClassMapAuthoritativeWillBeDeactivated(bool $enabled): void
{
$classLoader = new ClassLoader();
$classLoader->setClassMapAuthoritative($enabled);

$fakeLoader = new StaticKernelPluginLoader(
$classLoader,
null,
[
[
'name' => 'ExampleBundle',
'baseClass' => ExampleBundle::class,
'path' => __DIR__ . '/../_fixtures/ExampleBundle',
'active' => true,
'managedByComposer' => false,
'autoload' => [
'psr-4' => [
'ExampleBundle\\' => '',
],
],
],
]
);

$fakeLoader->initializePlugins(__DIR__);

static::assertFalse($classLoader->isClassMapAuthoritative());
}

/**
* @dataProvider classLoaderDataProvider
*/
public function testWithComposerManaged(bool $enabled): void
{
$classLoader = new ClassLoader();
$classLoader->setClassMapAuthoritative($enabled);

$fakeLoader = new StaticKernelPluginLoader(
$classLoader,
null,
[
[
'name' => 'ExampleBundle',
'baseClass' => ExampleBundle::class,
'path' => __DIR__ . '/../_fixtures/ExampleBundle',
'active' => true,
'managedByComposer' => true,
'autoload' => [
'psr-4' => [
'ExampleBundle\\' => '',
],
],
],
]
);

$fakeLoader->initializePlugins(__DIR__);

static::assertSame($enabled, $classLoader->isClassMapAuthoritative());
}

/**
* @return iterable<array<bool>>
*/
public static function classLoaderDataProvider(): iterable
{
yield 'classMapAuthoritative' => [true];
yield 'notClassMapAuthoritative' => [false];
}
}

0 comments on commit 816ec6b

Please sign in to comment.