diff --git a/src/Cryptal/ComposerInstaller.php b/src/Cryptal/ComposerInstaller.php index 5ed2008..178cd98 100644 --- a/src/Cryptal/ComposerInstaller.php +++ b/src/Cryptal/ComposerInstaller.php @@ -36,7 +36,7 @@ private function callEntryPoints($eps, $autoload, $wrapper) $interfaces = (array) class_implements($ep); if (!in_array('fpoirotte\\Cryptal\\Implementers\\PluginInterface', $interfaces)) { - throw new \InvalidArgumentException('Invalid entry point: ' . $ep); + throw new \InvalidArgumentException('Invalid entry point: ' . $ep); } call_user_func("$ep::registerAlgorithms", $wrapper); @@ -57,7 +57,7 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa try { $registry = Registry::getInstance(); $wrapper = new RegistryWrapper($registry, $package->getPrettyName()); - $this->callEntryPoint($extra['cryptal.entrypoint'], $package->getAutoload(), $wrapper); + $this->callEntryPoints($extra['cryptal.entrypoint'], $package->getAutoload(), $wrapper); $registry->save(); } catch (\Exception $e) { $this->io->writeError('Cryptal plugin installation failed, rolling back'); @@ -70,9 +70,9 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) { - $extra = $package->getExtra(); + $extra = $target->getExtra(); if (empty($extra['cryptal.entrypoint'])) { - throw new \UnexpectedValueException('Error while installing ' . $package->getPrettyName() . + throw new \UnexpectedValueException('Error while installing ' . $target->getPrettyName() . ', cryptal-plugin packages should have an entry point ' . 'defined in their extra key to be usable.'); } @@ -80,9 +80,10 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini $res = parent::update($repo, $initial, $target); try { + $registry = Registry::getInstance(); $registry->removeAlgorithms($initial->getPrettyName()); $wrapper = new RegistryWrapper($registry, $target->getPrettyName()); - $this->callEntryPoint($extra['cryptal.entrypoint'], $target->getAutoload(), $wrapper); + $this->callEntryPoints($extra['cryptal.entrypoint'], $target->getAutoload(), $wrapper); $registry->save(); } catch (\Exception $e) { $this->io->writeError('Cryptal plugin update failed, rolling back'); @@ -106,4 +107,25 @@ public function supports($packageType) { return 'cryptal-plugin' === $packageType; } + + public function registerRootPackage(PackageInterface $package) + { + $extra = $package->getExtra(); + if (empty($extra['cryptal.entrypoint'])) { + throw new \UnexpectedValueException('Error while installing ' . $package->getPrettyName() . + ', cryptal-plugin packages should have an entry point ' . + 'defined in their extra key to be usable.'); + } + + try { + $registry = Registry::getInstance(); + $registry->removeAlgorithms($package->getPrettyName()); + $wrapper = new RegistryWrapper($registry, $package->getPrettyName()); + $this->callEntryPoints($extra['cryptal.entrypoint'], $package->getAutoload(), $wrapper); + $registry->save(); + } catch (\Exception $e) { + $this->io->writeError('Cryptal plugin registration failed'); + throw $e; + } + } } diff --git a/src/Cryptal/ComposerPlugin.php b/src/Cryptal/ComposerPlugin.php index 73c2f01..a9c813a 100644 --- a/src/Cryptal/ComposerPlugin.php +++ b/src/Cryptal/ComposerPlugin.php @@ -13,5 +13,11 @@ public function activate(Composer $composer, IOInterface $io) { $installer = new ComposerInstaller($io, $composer); $composer->getInstallationManager()->addInstaller($installer); + + // Try to register the root package if it is a Cryptal plugin. + $rootPkg = $composer->getPackage(); + if ($installer->supports($rootPkg->getType())) { + $installer->registerRootPackage($rootPkg); + } } } diff --git a/src/Cryptal/RegistryWrapper.php b/src/Cryptal/RegistryWrapper.php index 60c0128..b988421 100644 --- a/src/Cryptal/RegistryWrapper.php +++ b/src/Cryptal/RegistryWrapper.php @@ -22,16 +22,16 @@ public function __construct(Registry $registry, $packageName) public function addCipher($cls, CipherEnum $algo, ModeEnum $mode, ImplementationTypeEnum $type) { - $this->registry->addCipher($this->packageName, $algo, $mode, $type); + $this->registry->addCipher($this->packageName, $cls, $algo, $mode, $type); } public function addHash($cls, HashEnum $algo, ImplementationTypeEnum $type) { - $this->registry->addHash($this->packageName, $algo, $type); + $this->registry->addHash($this->packageName, $cls, $algo, $type); } public function addMac($cls, MacEnum $algo, ImplementationTypeEnum $type) { - $this->registry->addMac($this->packageName, $algo, $type); + $this->registry->addMac($this->packageName, $cls, $algo, $type); } }