API try using subscribed events instead of separate installer

This commit is contained in:
Damian Mooyman 2016-09-20 13:40:10 +12:00
parent c211463283
commit 0781c4a486
2 changed files with 66 additions and 34 deletions

View File

@ -2,13 +2,10 @@
namespace SilverStripe\RecipePlugin;
use Composer\Installer\BinaryInstaller;
use Composer\Installer\LibraryInstaller;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Util\Filesystem;
use FilesystemIterator;
use Iterator;
use RecursiveDirectoryIterator;
@ -16,27 +13,9 @@ use RecursiveIteratorIterator;
use RegexIterator;
class RecipeInstaller extends LibraryInstaller {
public function __construct(
IOInterface $io,
Composer $composer,
$type = 'silverstripe-recipe',
Filesystem $filesystem = null,
BinaryInstaller $binaryInstaller = null
) {
parent::__construct($io, $composer, $type, $filesystem, $binaryInstaller);
}
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{
parent::install($repo, $package);
// Copy project files to root
$destinationPath = getcwd();
$name = $package->getName();
$extra = $package->getExtra();
if (isset($extra['project-files'])) {
$this->installProjectFiles($name, $this->getInstallPath($package), $destinationPath, $extra['project-files']);
}
public function __construct(IOInterface $io, Composer $composer) {
parent::__construct($io, $composer, null);
}
/**
@ -47,7 +26,7 @@ class RecipeInstaller extends LibraryInstaller {
* @param string $destinationRoot Base of destination directory (no trailing slash)
* @param array $filePatterns List of file patterns in wildcard format (e.g. `code/My*.php`)
*/
public function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $filePatterns)
protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $filePatterns)
{
$fileIterator = $this->getFileIterator($sourceRoot, $filePatterns);
foreach($fileIterator as $path => $info) {
@ -71,7 +50,7 @@ class RecipeInstaller extends LibraryInstaller {
* @param array $patterns List of wildcard patterns to match
* @return Iterator File iterator, where key is path and value is file info object
*/
public function getFileIterator($sourceRoot, $patterns) {
protected function getFileIterator($sourceRoot, $patterns) {
// Build regexp pattern
$expressions = [];
foreach($patterns as $pattern) {
@ -106,4 +85,23 @@ class RecipeInstaller extends LibraryInstaller {
}, $sourceParts);
return implode('(.+)', $regexParts);
}
/**
* @param PackageInterface $package
*/
public function installLibrary(PackageInterface $package)
{
// Copy project files to root
$destinationPath = getcwd();
$name = $package->getName();
$extra = $package->getExtra();
if (isset($extra['project-files'])) {
$this->installProjectFiles(
$name,
$this->getInstallPath($package),
$destinationPath,
$extra['project-files']
);
}
}
}

View File

@ -4,24 +4,58 @@
namespace SilverStripe\RecipePlugin;
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
/**
* Register the RecipeInstaller
*
* Credit to http://stackoverflow.com/questions/27194348/get-package-install-path-from-composer-script-composer-api
*/
class RecipePlugin implements PluginInterface
class RecipePlugin implements PluginInterface, EventSubscriberInterface
{
public function activate(Composer $composer, IOInterface $io) { }
public static function getSubscribedEvents()
{
return [
'post-package-install' => 'installPackage',
];
}
/**
* Apply plugin modifications to Composer
* Install resources from an installed or updated package
*
* @param Composer $composer
* @param IOInterface $io
* @param PackageEvent $event
*/
public function activate(Composer $composer, IOInterface $io)
{
$installer = new RecipeInstaller($io, $composer);
$composer->getInstallationManager()->addInstaller($installer);
public function installPackage(PackageEvent $event) {
$package = $this->getOperationPackage($event);
if ($package) {
$installer = new RecipeInstaller($event->getIO(), $event->getComposer());
$installer->installLibrary($package);
}
}
}
/**
* Get target package from operation
*
* @param PackageEvent $event
* @return PackageInterface
*/
protected function getOperationPackage(PackageEvent $event)
{
$operation = $event->getOperation();
if ($operation instanceof UpdateOperation) {
return $operation->getTargetPackage();
}
if ($operation instanceof InstallOperation) {
return $operation->getPackage();
}
return null;
}
}