recipe-plugin/src/RecipePlugin.php

145 lines
3.7 KiB
PHP
Raw Permalink 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\Factory;
use Composer\Installer\PackageEvent;
2016-09-12 05:35:13 +02:00
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
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
*
* 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
{
/**
* Type of recipe to check for
*/
2022-07-06 04:02:51 +02:00
public const RECIPE_TYPE = 'silverstripe-recipe';
/**
* 'extra' key for project files
*/
2022-07-06 04:02:51 +02:00
public const PROJECT_FILES = 'project-files';
2017-12-19 02:03:21 +01:00
/**
* 'extra' key for public files
*/
2022-07-06 04:02:51 +02:00
public const PUBLIC_FILES = 'public-files';
2017-12-19 02:03:21 +01:00
/**
* Hard-coded 'public' web-root folder
*/
2022-07-06 04:02:51 +02:00
public const PUBLIC_PATH = 'public';
2017-12-19 02:03:21 +01:00
/**
* 'extra' key for list of project files installed
*/
2022-07-06 04:02:51 +02:00
public const PROJECT_FILES_INSTALLED = 'project-files-installed';
2017-12-19 02:03:21 +01:00
/**
* 'extra' key for list of public files installed
*/
2022-07-06 04:02:51 +02:00
public const PUBLIC_FILES_INSTALLED = 'public-files-installed';
2017-12-19 02:03:21 +01:00
/**
* 'extra' key for project dependencies installed
*/
2022-07-06 04:02:51 +02:00
public const PROJECT_DEPENDENCIES_INSTALLED = 'project-dependencies-installed';
2017-07-07 05:47:49 +02:00
public function activate(Composer $composer, IOInterface $io)
{
}
public static function getSubscribedEvents()
{
return [
2017-07-10 07:25:10 +02:00
'post-create-project-cmd' => 'cleanupProject',
'post-package-update' => 'installPackage',
'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
2017-07-10 07:25:10 +02:00
/**
* Cleanup the root package on create-project
*
* @param Event $event
*/
public function cleanupProject(Event $event)
{
$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
$file->write($data);
2017-07-10 07:25:10 +02:00
}
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
];
}
2020-10-26 01:55:21 +01:00
public function deactivate(Composer $composer, IOInterface $io)
{
}
public function uninstall(Composer $composer, IOInterface $io)
{
}
}