From 0781c4a4869cfd1f2f34b53d81373d489a00eb48 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Tue, 20 Sep 2016 13:40:10 +1200 Subject: [PATCH] API try using subscribed events instead of separate installer --- src/RecipeInstaller.php | 48 ++++++++++++++++++------------------- src/RecipePlugin.php | 52 ++++++++++++++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 34 deletions(-) diff --git a/src/RecipeInstaller.php b/src/RecipeInstaller.php index 161c808..b4d2f53 100644 --- a/src/RecipeInstaller.php +++ b/src/RecipeInstaller.php @@ -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'] + ); + } + } } diff --git a/src/RecipePlugin.php b/src/RecipePlugin.php index cb6a383..a29aad9 100644 --- a/src/RecipePlugin.php +++ b/src/RecipePlugin.php @@ -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); + } } -} \ No newline at end of file + + /** + * 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; + } +}