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;
|
2016-09-20 03:40:10 +02:00
|
|
|
use Composer\DependencyResolver\Operation\InstallOperation;
|
|
|
|
use Composer\DependencyResolver\Operation\UpdateOperation;
|
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface;
|
2017-07-17 07:36:08 +02:00
|
|
|
use Composer\Factory;
|
2016-09-20 03:40:10 +02:00
|
|
|
use Composer\Installer\PackageEvent;
|
2016-09-12 05:35:13 +02:00
|
|
|
use Composer\IO\IOInterface;
|
2017-07-17 07:36:08 +02:00
|
|
|
use Composer\Json\JsonFile;
|
2016-09-20 03:40:10 +02:00
|
|
|
use Composer\Package\PackageInterface;
|
2017-07-10 07:25:10 +02:00
|
|
|
use Composer\Plugin\Capability\CommandProvider;
|
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-10 07:25:10 +02:00
|
|
|
use Composer\Script\Event;
|
2016-09-12 05:35:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the RecipeInstaller
|
2016-09-20 03:40:10 +02:00
|
|
|
*
|
|
|
|
* 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-17 07:36:08 +02:00
|
|
|
/**
|
|
|
|
* Type of recipe to check for
|
|
|
|
*/
|
|
|
|
const RECIPE_TYPE = 'silverstripe-recipe';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'extra' key for project files
|
|
|
|
*/
|
|
|
|
const PROJECT_FILES = 'project-files';
|
|
|
|
|
2017-12-19 02:03:21 +01:00
|
|
|
/**
|
|
|
|
* 'extra' key for public files
|
|
|
|
*/
|
|
|
|
const PUBLIC_FILES = 'public-files';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hard-coded 'public' web-root folder
|
|
|
|
*/
|
|
|
|
const PUBLIC_PATH = 'public';
|
|
|
|
|
2017-07-17 07:36:08 +02:00
|
|
|
/**
|
|
|
|
* 'extra' key for list of project files installed
|
|
|
|
*/
|
|
|
|
const PROJECT_FILES_INSTALLED = 'project-files-installed';
|
|
|
|
|
2017-12-19 02:03:21 +01:00
|
|
|
/**
|
|
|
|
* 'extra' key for list of public files installed
|
|
|
|
*/
|
|
|
|
const PUBLIC_FILES_INSTALLED = 'public-files-installed';
|
|
|
|
|
2017-07-17 07:36:08 +02:00
|
|
|
/**
|
|
|
|
* 'extra' key for project dependencies installed
|
|
|
|
*/
|
|
|
|
const PROJECT_DEPENDENCIES_INSTALLED = 'project-dependencies-installed';
|
|
|
|
|
2017-07-07 05:47:49 +02:00
|
|
|
public function activate(Composer $composer, IOInterface $io)
|
|
|
|
{
|
|
|
|
}
|
2016-09-20 03:40:10 +02:00
|
|
|
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return [
|
2017-07-10 07:25:10 +02:00
|
|
|
'post-create-project-cmd' => 'cleanupProject',
|
2017-07-10 03:59:00 +02:00
|
|
|
'post-package-update' => 'installPackage',
|
2016-09-20 03:40:10 +02:00
|
|
|
'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)
|
|
|
|
{
|
2016-09-20 03:40:10 +02:00
|
|
|
$package = $this->getOperationPackage($event);
|
|
|
|
if ($package) {
|
|
|
|
$installer = new RecipeInstaller($event->getIO(), $event->getComposer());
|
|
|
|
$installer->installLibrary($package);
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 05:35:13 +02:00
|
|
|
|
2017-07-10 07:25:10 +02:00
|
|
|
/**
|
|
|
|
* Cleanup the root package on create-project
|
|
|
|
*
|
|
|
|
* @param Event $event
|
|
|
|
*/
|
|
|
|
public function cleanupProject(Event $event)
|
|
|
|
{
|
2017-07-17 07:36:08 +02:00
|
|
|
$file = new JsonFile(Factory::getComposerFile());
|
|
|
|
$data = $file->read();
|
2017-07-10 07:25:10 +02:00
|
|
|
|
2017-12-19 02:03:21 +01:00
|
|
|
// Remove project and public files from project
|
|
|
|
unset($data['extra'][self::PROJECT_FILES]);
|
|
|
|
unset($data['extra'][self::PUBLIC_FILES]);
|
|
|
|
|
|
|
|
// Remove redundant empty extra
|
2017-07-10 07:25:10 +02:00
|
|
|
if (empty($data['extra'])) {
|
|
|
|
unset($data['extra']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save back to composer.json
|
2017-07-17 07:36:08 +02:00
|
|
|
$file->write($data);
|
2017-07-10 07:25:10 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 05:35:13 +02:00
|
|
|
/**
|
2016-09-20 03:40:10 +02:00
|
|
|
* Get target package from operation
|
2016-09-12 05:35:13 +02:00
|
|
|
*
|
2016-09-20 03:40:10 +02:00
|
|
|
* @param PackageEvent $event
|
|
|
|
* @return PackageInterface
|
2016-09-12 05:35:13 +02:00
|
|
|
*/
|
2016-09-20 03:40:10 +02:00
|
|
|
protected function getOperationPackage(PackageEvent $event)
|
2016-09-12 05:35:13 +02:00
|
|
|
{
|
2016-09-20 03:40:10 +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
|
|
|
|
];
|
|
|
|
}
|
2020-10-26 01:55:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
public function deactivate(Composer $composer, IOInterface $io)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uninstall(Composer $composer, IOInterface $io)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2016-09-20 03:40:10 +02:00
|
|
|
}
|