Skip to content
This repository has been archived by the owner on May 4, 2021. It is now read-only.

Commit

Permalink
Initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
kwivix committed Dec 20, 2018
1 parent 3d41cd9 commit 32301b2
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 6 deletions.
11 changes: 6 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
composer.phar
/bin/
/config/
/vendor/

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
.env
composer.lock
composer.phar
symfony.lock
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# flexinline
# Flex Inline

Composer plugin for Symfony Flex to use recipes in composer.json

## Usage

To use inline recipes add `kwivix/flexinline` to composer.json of your project.

## Create recipes

Inside the composer.json add under [extra](https://getcomposer.org/doc/04-schema.md#extra) the key `recipe-manifest` with a JSON-object. The manifest supports the same [flex configurators](https://github.com/symfony/recipes#configurators) as the official flex. Except for `copy-from-recipe`, since it would be identical to `copy-from-package`.

``` json
{
"name": "example/package",
"type": "project",
"extra": {
"recipe-manifest": {
"gitignore": [
".env"
],
"copy-from-package": {
"bin/check.php": "%BIN_DIR%/check.php"
}
}
}
}
```
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "kwivix/flexinline",
"type": "composer-plugin",
"license": "MIT",
"authors": [
{
"name": "Ryan",
"email": "composer@kwivix.net"
}
],
"require": {
"php": "^7.1",
"composer-plugin-api": "^1.1",
"symfony/flex": "^1.1"
},
"require-dev": {
"composer/composer": "^1.8"
},
"autoload": {
"psr-4": {
"Kwivix\\FlexInline\\": "src"
}
},
"extra": {
"class": "Kwivix\\FlexInline\\ComposerPlugin"
}
}
152 changes: 152 additions & 0 deletions src/ComposerPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

declare(strict_types=1);

/*
* This file is part of Inline Flex.
*
* (c) Ryan <kwivix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Kwivix\FlexInline;

use Composer\Composer;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use Symfony\Flex\Configurator;
use Symfony\Flex\Options;
use Symfony\Flex\Recipe;

class ComposerPlugin implements PluginInterface, EventSubscriberInterface
{
const RECIPE_MANIFEST = 'recipe-manifest';

/**
* @var IOInterface
*/
private $io;

/**
* @var Configurator
*/
private $configurator;

/**
* @var boolean
*/
private $enabled = false;

public function activate(Composer $composer, IOInterface $io)
{
foreach (array_merge($composer->getPackage()->getRequires() ?? [], $composer->getPackage()->getDevRequires() ?? []) as $link) {
if ($link->getTarget() === 'kwivix/flexinline') {
$this->enabled = true;
break;
}
}

$this->io = $io;

$extra = $composer->getPackage()->getExtra();

$options = array_merge([
'bin-dir' => 'bin',
'conf-dir' => 'conf',
'config-dir' => 'config',
'src-dir' => 'src',
'var-dir' => 'var',
'public-dir' => 'public',
'root-dir' => $extra['symfony']['root-dir'] ?? '.',
], $extra);

$options = new Options($options);

$this->configurator = new Configurator($composer, $io, $options);
}

public static function getSubscribedEvents()
{
return [
PackageEvents::POST_PACKAGE_INSTALL => 'postPackageInstall',
PackageEvents::POST_PACKAGE_UPDATE => 'postPackageUpdate',
PackageEvents::POST_PACKAGE_UNINSTALL => 'postPackageUninstall',
];
}

public function postPackageInstall(PackageEvent $event)
{
$this->processPackageEvent($event);
}

public function postPackageUpdate(PackageEvent $event)
{
$this->processPackageEvent($event);
}

public function postPackageUninstall(PackageEvent $event)
{
$this->processPackageEvent($event);
}

private function recipeFromPackage(OperationInterface $operation, PackageInterface $package): ?Recipe
{
$name = $package->getName();
$jobType = $operation->getJobType();
$extra = $package->getExtra();

$manifest = $extra[self::RECIPE_MANIFEST] ?? null;

if ($manifest === null) {
return null;
}

if ($manifest['copy-from-recipe'] ?? false) {
$this->io->writeError(sprintf('<warning>Ignoring "copy-from-recipe" from package "%s"</warning>', $name));
unset($manifest['copy-from-recipe']);
}

$data = [
'manifest' => $manifest,
];

return new Recipe($package, $name, $jobType, $data);
}

private function processPackageEvent(PackageEvent $event)
{
$operation = $event->getOperation();

if ($operation instanceof UpdateOperation === true) {
$package = $operation->getTargetPackage();
} else {
$package = $operation->getPackage();
}

$recipe = $this->recipeFromPackage($operation, $package);

if ($recipe === null) {
return;
}

if ($this->enabled === false) {
$this->io->writeError('<warning>Inline recipes are disabled: "kwivix/flexinline" not found in the root composer.json</warning>');
return;
}

if ($operation instanceof UninstallOperation === true) {
$this->configurator->unconfigure($recipe);
} else {
$this->configurator->install($recipe);
}
}
}

0 comments on commit 32301b2

Please sign in to comment.