Skip to content

Commit

Permalink
Fix various issues in plugins handling
Browse files Browse the repository at this point in the history
* Register the root package manually if it is a plugin
* Fix calls to entry points
* Fix typos in variable names & declare missing variables
  • Loading branch information
fpoirotte committed Jul 28, 2017
1 parent a44e212 commit 6d638da
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
32 changes: 27 additions & 5 deletions src/Cryptal/ComposerInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
Expand All @@ -70,19 +70,20 @@ 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.');
}

$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');
Expand All @@ -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;
}
}
}
6 changes: 6 additions & 0 deletions src/Cryptal/ComposerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions src/Cryptal/RegistryWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 6d638da

Please sign in to comment.