Skip to content

Commit

Permalink
Add support for Mage-OS
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Oct 27, 2023
1 parent 54fbacb commit 90e46a2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
45 changes: 44 additions & 1 deletion src/Composer/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
namespace Yireo\ReplaceTools\Composer;

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\IO\IOInterface;
use Composer\Package\BasePackage;
use Composer\Package\Package;
use Composer\Plugin\Capability\CommandProvider as CommandProviderCapability;
use Composer\Plugin\Capable;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use Composer\Plugin\PrePoolCreateEvent;

class Plugin implements PluginInterface, Capable
class Plugin implements PluginInterface, Capable, EventSubscriberInterface
{
/**
* @param Composer $composer
Expand Down Expand Up @@ -49,4 +55,41 @@ public function getCapabilities()
CommandProviderCapability::class => CommandProvider::class,
];
}

/**
* @return string[]
*/
public static function getSubscribedEvents()
{
return [
'pre-pool-create' => 'prePoolCreate',
];
}

public function prePoolCreate(PrePoolCreateEvent $event)
{
$newPackages = [];
foreach ($event->getPackages() as $package) {
/*if (false === $this->hasReplaceSource($package, '')) {
$newPackages[] = $package;
continue;
}
$package->setReplaces([]);*/
$newPackages[] = $package;
}

$event->setPackages($newPackages);
}

private function hasReplaceSource(BasePackage $package, string $replaceSource):bool
{
foreach ($package->getReplaces() as $replace) {
if ($replace->getSource() === $replaceSource) {
return true;
}
}

return false;
}
}
24 changes: 22 additions & 2 deletions src/Composer/Service/ReplaceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,18 @@ public function read(): ReplacementCollection
*/
public function write(ReplacementCollection $replacements)
{
$jsonData = $this->readJsonData();
$replacementData = $replacements->toArray();
$replacementData = [];
foreach ($replacements->get() as $replacement) {
$composerName = $replacement->getComposerName();
if ($this->isMageOS()) {
$composerName = preg_replace('#^magento\/#', 'mage-os/', $composerName);
}
$replacementData[$composerName] = $replacement->getVersion();
}

ksort($replacementData);

$jsonData = $this->readJsonData();
$jsonData['replace'] = $replacementData;
$this->writeJsonData($jsonData);
}
Expand Down Expand Up @@ -110,6 +119,17 @@ public function suggestedBulks(): array
];
}

public function isMageOS(): bool
{
foreach ($this->readRequires() as $require) {
if ($require === 'mage-os/product-community-edition') {
return true;
}
}

return false;
}

/**
* @return BulkReplacement[]
* @throws FilesystemException
Expand Down

0 comments on commit 90e46a2

Please sign in to comment.