-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f42a9af
commit 4602df2
Showing
6 changed files
with
137 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Artemeon\Composer\Module; | ||
|
||
final readonly class ModuleFilter implements ModuleFilterInterface | ||
{ | ||
private function __construct( | ||
private ?array $activeModules, | ||
) { | ||
} | ||
|
||
public static function unrestricted(): self | ||
{ | ||
return new self(null); | ||
} | ||
|
||
public static function restrictedTo(array $activeModules): self | ||
{ | ||
return new self($activeModules); | ||
} | ||
|
||
public function shouldLoad(string $moduleName): bool | ||
{ | ||
return !isset($this->activeModules) || in_array($moduleName, $this->activeModules, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Artemeon\Composer\Module; | ||
|
||
interface ModuleFilterInterface | ||
{ | ||
public function shouldLoad(string $moduleName): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Artemeon\Composer\Module; | ||
|
||
use Composer\IO\IOInterface; | ||
use JsonException; | ||
|
||
final readonly class ModuleFilterLoader | ||
{ | ||
public function __construct(private IOInterface $io) | ||
{ | ||
} | ||
|
||
public function load(string $configurationFilePath, string $localConfigurationFilePath): ModuleFilterInterface | ||
{ | ||
$this->io->debug( | ||
sprintf('Loading module filter configuration at <comment>%s</comment>', $configurationFilePath), | ||
); | ||
|
||
$configurationData = $this->readJsonFile($configurationFilePath); | ||
if (!isset($configurationData)) { | ||
return ModuleFilter::unrestricted(); | ||
} | ||
|
||
$localConfigurationData = $this->readJsonFile($localConfigurationFilePath); | ||
|
||
$mergedConfiguration = array_values(array_unique([...$configurationData->core, ...($localConfigurationData ?? [])])); | ||
|
||
return ModuleFilter::restrictedTo($mergedConfiguration); | ||
} | ||
|
||
private function readJsonFile(string $filePath): ?object | ||
{ | ||
$fileContents = @file_get_contents($filePath); | ||
if ($fileContents === false) { | ||
$this->io->warning('No module filter configuration found'); | ||
|
||
return null; | ||
} | ||
|
||
try { | ||
$jsonData = json_decode($fileContents, false, 512, JSON_OBJECT_AS_ARRAY | JSON_THROW_ON_ERROR); | ||
} catch (JsonException $exception) { | ||
$this->io->warning(sprintf('Invalid module filter configuration: %s', $exception->getMessage())); | ||
|
||
return null; | ||
} | ||
|
||
return $jsonData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Artemeon\Composer\Module; | ||
|
||
final class ModuleIncludeAllFilter implements ModuleFilterInterface | ||
{ | ||
public function shouldLoad(string $moduleName): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters