phabel/src/Composer/Plugin.php

129 lines
3.5 KiB
PHP
Raw Normal View History

2020-08-13 18:30:12 +02:00
<?php
namespace Phabel\Composer;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\InstallerEvent;
2020-11-01 20:44:11 +01:00
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
2020-08-13 18:30:12 +02:00
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
2020-11-04 10:49:05 +01:00
use Phabel\Composer\Repository\Repository;
2020-11-04 12:00:16 +01:00
use ReflectionClass;
2020-11-03 22:01:34 +01:00
use ReflectionObject;
2020-08-13 18:30:12 +02:00
/**
* @author Daniil Gentili <daniil@daniil.it>
* @license MIT
*/
class Plugin implements PluginInterface, EventSubscriberInterface
{
/**
* IO interface.
*/
private IOInterface $io;
/**
* Apply plugin modifications to Composer.
*
* @param Composer $composer Composer instance
* @param IOInterface $io IO instance
*
* @return void
*/
public function activate(Composer $composer, IOInterface $io): void
{
$repoManager = $composer->getRepositoryManager();
$repos = $repoManager->getRepositories();
2020-11-03 22:01:34 +01:00
$reflect = (new ReflectionObject($repoManager))->getProperty('repositories');
$reflect->setAccessible(true);
$reflect->setValue($repoManager, []);
foreach ($repos as $repo) {
2020-11-04 12:00:16 +01:00
$traits = [Repository::class];
$traitsStr = '';
foreach ($traits as $trait) {
$traitsStr .= "use \\$trait;\n";
2020-11-03 22:01:34 +01:00
}
2020-11-04 12:00:16 +01:00
$reflect = new ReflectionClass($repo);
$extend = "extends \\".\get_class($repo);
$eval = "\$newRepo = new class $extend {
$traitsStr
public function __construct() {}
};";
eval($eval);
$reflectNew = new ReflectionClass($newRepo);
$reflectNew = $reflectNew->getParentClass();
do {
foreach ($reflect->getProperties() as $prop) {
$propNew = $reflectNew->getProperty($prop->getName());
$propNew->setAccessible(true);
$prop->setAccessible(true);
$propNew->setValue($newRepo, $prop->getValue($repo));
}
$reflectNew = $reflectNew->getParentClass();
} while ($reflect = $reflect->getParentClass());
$repo = $newRepo;
2020-11-03 22:01:34 +01:00
$repoManager->prependRepository($repo);
}
2020-11-04 10:49:05 +01:00
\var_dump(\array_map('get_class', $repoManager->getRepositories()));
2020-08-13 18:30:12 +02:00
$this->io = $io;
}
2020-11-01 21:44:03 +01:00
/**
* Remove any hooks from Composer.
*
* This will be called when a plugin is deactivated before being
* uninstalled, but also before it gets upgraded to a new version
* so the old one can be deactivated and the new one activated.
*
* @param Composer $composer
* @param IOInterface $io
*/
public function deactivate(Composer $composer, IOInterface $io)
{
}
/**
* Prepare the plugin to be uninstalled.
*
* This will be called after deactivate.
*
* @param Composer $composer
* @param IOInterface $io
*/
public function uninstall(Composer $composer, IOInterface $io)
{
}
2020-08-13 18:30:12 +02:00
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
2020-11-01 20:44:11 +01:00
PackageEvents::POST_PACKAGE_INSTALL =>
['onInstall', 100000],
2020-08-13 18:30:12 +02:00
];
}
2020-11-01 20:44:11 +01:00
public function onInstall(PackageEvent $event): void
{
}
2020-08-13 18:30:12 +02:00
/**
2020-08-23 20:54:56 +02:00
* Emitted before composer solves dependencies.
2020-08-13 18:30:12 +02:00
*
* @param InstallerEvent $event Event
2020-08-23 20:54:56 +02:00
*
2020-08-13 18:30:12 +02:00
* @return void
*/
public function onDependencySolve(InstallerEvent $event): void
{
}
}