Skip to content

Commit

Permalink
use module filter in module package loader and load module filter in …
Browse files Browse the repository at this point in the history
…plugin entrypoint
  • Loading branch information
Mike Marschall committed Nov 10, 2020
1 parent 686b0f9 commit f6fb7e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
22 changes: 19 additions & 3 deletions src/Module/ModulePackageLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Composer\IO\IOInterface;

use function basename;
use function dirname;
use function glob;
use function rtrim;
use function sprintf;
Expand All @@ -18,11 +20,14 @@ final class ModulePackageLoader
{
private const MODULE_COMPOSER_FILE_PATTERN = 'module_*/composer.json';

private ModuleFilter $moduleFilter;
private IOInterface $io;

private array $modulePackageCache = [];

public function __construct(IOInterface $io)
public function __construct(ModuleFilter $moduleFilter, IOInterface $io)
{
$this->moduleFilter = $moduleFilter;
$this->io = $io;
}

Expand All @@ -31,8 +36,20 @@ public function __construct(IOInterface $io)
*/
public function load(string $basePath): iterable
{
$this->io->debug(
sprintf(
'loading modules at <comment>%s</comment> using <comment>%s</comment>',
$basePath,
self::MODULE_COMPOSER_FILE_PATTERN
)
);

foreach ($this->scanForComposerFiles($basePath) as $composerFile) {
yield $this->loadModule($composerFile);
$moduleName = basename(dirname($composerFile));

if ($this->moduleFilter->shouldLoad($moduleName)) {
yield $this->loadModule($composerFile);
}
}
}

Expand All @@ -44,7 +61,6 @@ private function scanForComposerFiles(string $basePath): iterable
DIRECTORY_SEPARATOR,
self::MODULE_COMPOSER_FILE_PATTERN
);
$this->io->debug(sprintf('Loading modules using glob <comment>%s</comment>', $moduleComposerFilePattern));

yield from glob($moduleComposerFilePattern, GLOB_NOSORT | GLOB_NOESCAPE);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Plugin/MergePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Artemeon\Composer\Plugin;

use Artemeon\Composer\Module\ModuleFilterLoader;
use Artemeon\Composer\Module\ModulePackageLoader;
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
Expand All @@ -25,6 +26,7 @@ final class MergePlugin implements PluginInterface, EventSubscriberInterface
private const CALLBACK_PRIORITY = 50000;
private const MODULES_BASE_PATH = '../core';
private const OVERRIDDEN_MODULES = './module_*';
private const FILTER_CONFIGURATION_PATH = './packageconfig.json';

private Composer $composer;
private IOInterface $io;
Expand All @@ -36,7 +38,8 @@ public function activate(Composer $composer, IOInterface $io): void
{
$this->composer = $composer;
$this->io = $io;
$this->modulePackageLoader = new ModulePackageLoader($io);
$moduleFilter = (new ModuleFilterLoader($io))->load(self::FILTER_CONFIGURATION_PATH);
$this->modulePackageLoader = new ModulePackageLoader($moduleFilter, $io);
}

public function deactivate(Composer $composer, IOInterface $io): void
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/Module/ModulePackageLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,37 @@ final class ModulePackageLoaderTest extends TestCase
{
use ProphecyTrait;

private IOInterface $nullIo;
private ModuleFilter $unrestrictedModuleFilter;

protected function setUp(): void
{
parent::setUp();
$this->nullIo = new NullIO();
$this->unrestrictedModuleFilter = ModuleFilter::unrestricted(new NullIO());
}

public function testLoadsModulesAtTheGivenPath(): void
{
$modulePackageLoader = new ModulePackageLoader($this->nullIo);
$modulePackageLoader = new ModulePackageLoader($this->unrestrictedModuleFilter, new NullIO());
self::assertCount(2, $modulePackageLoader->load($this->fixturePath('two_modules')));
}

public function testOnlyLoadsModulesWhoseNameMatchesTheConvention(): void
{
$modulePackageLoader = new ModulePackageLoader($this->nullIo);
$modulePackageLoader = new ModulePackageLoader($this->unrestrictedModuleFilter, new NullIO());
self::assertCount(1, $modulePackageLoader->load($this->fixturePath('two_modules_one_invalid_name')));
}

public function testOnlyLoadsModulesWhichContainAPackageFile(): void
{
$modulePackageLoader = new ModulePackageLoader($this->nullIo);
$modulePackageLoader = new ModulePackageLoader($this->unrestrictedModuleFilter, new NullIO());
self::assertCount(1, $modulePackageLoader->load($this->fixturePath('one_module_and_one_directory')));
$modulePackageLoader = new ModulePackageLoader($this->nullIo);
$modulePackageLoader = new ModulePackageLoader($this->unrestrictedModuleFilter, new NullIO());
self::assertCount(0, $modulePackageLoader->load($this->fixturePath('two_directories')));
}

public function testLoadsNothingIfNoModulesExistAtTheGivenPath(): void
{
$modulePackageLoader = new ModulePackageLoader($this->nullIo);
$modulePackageLoader = new ModulePackageLoader($this->unrestrictedModuleFilter, new NullIO());
self::assertCount(0, $modulePackageLoader->load($this->fixturePath('no_modules')));
}

Expand Down

0 comments on commit f6fb7e2

Please sign in to comment.