Skip to content

Commit

Permalink
Updated for OpenMage/magento-lts
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Jul 24, 2024
1 parent 227043a commit 3a8b635
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"symfony/polyfill-php80": "*"
},
"require-dev": {
"composer/composer": "^2.0|2.0@dev"
"composer/composer": "^2.6"
},
"extra": {
"class": "Sreichel\\Composer\\Plugin\\FileCopy\\ScriptHandler"
Expand Down
2 changes: 0 additions & 2 deletions src/Sreichel/Composer/Plugin/FileCopy/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ interface ConfigInterface
{
public const COMPOSER_EXTRA_NAME = 'file-copy';

public const COMPOSER_EXTRA_NAME_DEV = 'file-copy-dev';

public const CONFIG_DEBUG = 'debug';

public const CONFIG_SOURCE = 'source';
Expand Down
9 changes: 7 additions & 2 deletions src/Sreichel/Composer/Plugin/FileCopy/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function __construct(Event $event)
$this->composer = $event->getComposer();

$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
assert(is_string($vendorDir));
$this->vendor = $vendorDir;
}

Expand Down Expand Up @@ -104,7 +103,13 @@ public function processCopy(array $config): void
protected function getProjectPath(): string
{
if ($this->projectPath === null) {
$this->projectPath = realpath($this->vendor . '/../') . '/';
$path = realpath($this->vendor . '/../') . '/';

$extras = $this->composer->getPackage()->getExtra();
if (isset($extras['magento-root-dir'])) {
$path .= $extras['magento-root-dir'] . '/';
}
$this->projectPath = $path;
}

return $this->projectPath;
Expand Down
65 changes: 53 additions & 12 deletions src/Sreichel/Composer/Plugin/FileCopy/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,68 @@ public function onPostCmd(Event $event): void
*/
public static function buildParameters(Event $event): void
{
$io = $event->getIO();
$composer = $event->getComposer();
$io = $event->getIO();
$composer = $event->getComposer();
$locker = $composer->getLocker();
$repo = $locker->getLockedRepository();

$extrasCollection = [];

// get extras from project
$extras = $composer->getPackage()->getExtra();
if (!isset($extras[ConfigInterface::COMPOSER_EXTRA_NAME])) {
$io->write('The parameter handler needs to be configured through the extra.file-copy setting.');
} else {
$configs = $extras[ConfigInterface::COMPOSER_EXTRA_NAME];
if (!is_array($configs)) {
throw new InvalidArgumentException('The extra.file-copy setting must be an array or a configuration object.');
if (isset($extras[ConfigInterface::COMPOSER_EXTRA_NAME])) {
$extrasCollection[$composer->getPackage()->getName()] = $extras[ConfigInterface::COMPOSER_EXTRA_NAME];
}

// get extras from installed packages
foreach ($repo->getPackages() as $package) {
$extras = $package->getExtra();
if (isset($extras[ConfigInterface::COMPOSER_EXTRA_NAME])) {
$extrasCollection[$package->getName()] = $extras[ConfigInterface::COMPOSER_EXTRA_NAME];
}
}

self::validateConfig($io, $extrasCollection);

if ($extrasCollection === []) {
$io->write('The parameter handler needs to be configured through the extra.file-copy settings.');
} else {
$processor = new Processor($event);

foreach ($configs as $config) {
foreach ($extrasCollection as $settings) {
foreach ($settings as $config) {
/** @var array<string, string> $config */
$processor->processCopy($config);
}
}
}
}

private static function validateConfig(IOInterface $io, array &$extrasCollection): void
{
foreach ($extrasCollection as $package => $settings) {
if (!is_array($settings)) {
unset($extrasCollection[$package]);

$io->write(sprintf(
'The extra.file-copy setting must be an array or a configuration object in package %s.',
$package
));
}

foreach ($settings as $key => $config) {
if (!is_array($config)) {
throw new InvalidArgumentException('The extra.file-copy setting must be an array of configuration objects.');
unset($extrasCollection[$package][$key]);

$io->write(sprintf(
'The extra.file-copy structure must be an array of configuration objects in package %s.',
$package
));
}
}

/** @var array<string, string> $config */
$processor->processCopy($config);
if ($extrasCollection[$package] === []) {
unset($extrasCollection[$package]);
}
}
}
Expand Down

0 comments on commit 3a8b635

Please sign in to comment.