recipe-plugin/src/RecipePlugin.php

80 lines
2.0 KiB
PHP
Raw Normal View History

2016-09-12 05:35:13 +02:00
<?php
2016-09-12 05:43:34 +02:00
namespace SilverStripe\RecipePlugin;
2016-09-12 05:35:13 +02:00
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
2016-09-12 05:35:13 +02:00
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
2017-07-07 05:47:49 +02:00
use Composer\Plugin\Capable;
2016-09-12 05:35:13 +02:00
use Composer\Plugin\PluginInterface;
2017-07-07 05:47:49 +02:00
use Composer\Plugin\Capability\CommandProvider;
2016-09-12 05:35:13 +02:00
/**
* Register the RecipeInstaller
*
* Credit to http://stackoverflow.com/questions/27194348/get-package-install-path-from-composer-script-composer-api
2016-09-12 05:35:13 +02:00
*/
2017-07-07 05:47:49 +02:00
class RecipePlugin implements PluginInterface, EventSubscriberInterface, Capable
2016-09-12 05:35:13 +02:00
{
2017-07-07 05:47:49 +02:00
/**
* Recipe type
*/
const RECIPE = 'silverstripe-recipe';
public function activate(Composer $composer, IOInterface $io)
{
}
public static function getSubscribedEvents()
{
return [
'post-package-install' => 'installPackage',
];
}
/**
* Install resources from an installed or updated package
*
* @param PackageEvent $event
*/
2017-07-07 05:47:49 +02:00
public function installPackage(PackageEvent $event)
{
$package = $this->getOperationPackage($event);
if ($package) {
$installer = new RecipeInstaller($event->getIO(), $event->getComposer());
$installer->installLibrary($package);
}
}
2016-09-12 05:35:13 +02:00
/**
* Get target package from operation
2016-09-12 05:35:13 +02:00
*
* @param PackageEvent $event
* @return PackageInterface
2016-09-12 05:35:13 +02:00
*/
protected function getOperationPackage(PackageEvent $event)
2016-09-12 05:35:13 +02:00
{
$operation = $event->getOperation();
if ($operation instanceof UpdateOperation) {
return $operation->getTargetPackage();
}
if ($operation instanceof InstallOperation) {
return $operation->getPackage();
}
return null;
2016-09-12 05:35:13 +02:00
}
2017-07-07 05:47:49 +02:00
public function getCapabilities()
{
return [
CommandProvider::class => RecipeCommandProvider::class
];
}
}