Kaynağa Gözat

Updated for Composer 1.8.x and PSR-12.

Frederic G. MARAND 5 yıl önce
ebeveyn
işleme
f7f589274f
5 değiştirilmiş dosya ile 150 ekleme ve 111 silme
  1. 1 0
      .gitignore
  2. 8 2
      composer.json
  3. 34 16
      src/BuildSettingsCommand.php
  4. 65 53
      src/Builder.php
  5. 42 40
      src/BuilderCommandProvider.php

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
+/composer.lock
 /vendor/

+ 8 - 2
composer.json

@@ -14,11 +14,17 @@
   "extra": {
     "class": "Fgm\\Drupal\\Composer\\Builder"
   },
-  "license": "GPL-2.0+",
+  "license": "GPL-2.0-or-later",
   "minimum-stability": "dev",
   "name": "fgm/drupal_composer_builder",
   "require": {
     "composer-plugin-api": "^1.1"
   },
-  "type": "composer-plugin"
+  "type": "composer-plugin",
+  "require-dev": {
+    "squizlabs/php_codesniffer": "^3.0@dev"
+  },
+  "scripts": {
+    "cs": "phpcs --standard=PSR12 -v src"
+  }
 }

+ 34 - 16
src/BuildSettingsCommand.php

@@ -6,24 +6,42 @@ use Composer\Command\BaseCommand;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 
-class BuildSettingsCommand extends BaseCommand {
+class BuildSettingsCommand extends BaseCommand
+{
 
-  public function configure() {
-    parent::configure();
-    $this
-      ->setName('build-settings')
-      ->setDescription('Builds the *.settings.local.php files.')
-      ->setDefinition([])
-      ->setHelp(<<<EOT
-The build-settings command combines shared and per-environment parameters and passes them to the settings.local.php.twig template to build the settings/(build|run).settings.local.php files.
+    /**
+     * @var string
+     */
+    protected $eventName;
 
-EOT
-      )
-    ;
-  }
+    /**
+     * {@inheritDoc}
+     */
+    public function configure()
+    {
+        parent::configure();
+        $this->eventName = $this->getName();
+        $this
+        ->setName('build:settings')
+        ->setDescription('Builds the *.settings.local.php files.')
+        ->setDefinition([])
+        ->setHelp(<<<EOT
+The build:settings command combines shared and per-environment parameters and passes
+them to the settings.local.php.twig template to build the settings/(build|run).settings.local.php files.
 
-  public function execute(InputInterface $input, OutputInterface $output) {
-    $output->write("BuildSettingsCommand Executed", true);
-  }
+EOT
+        );
+    }
 
+    /**
+     * {@inheritDoc}
+     */
+    public function execute(InputInterface $input, OutputInterface $output)
+    {
+        $conf = $this->getComposer()->getPackage()->getExtra()[Builder::NAME] ?? [];
+        $messageFormat = empty($this->eventName)
+        ? "BuildSettingsCommand Executed on its own"
+        : "BuildSettingsCommand Executed on \"%s\" event";
+        $output->write(sprintf($messageFormat, $this->eventName), true);
+    }
 }

+ 65 - 53
src/Builder.php

@@ -3,74 +3,86 @@
 namespace Fgm\Drupal\Composer;
 
 use Composer\Composer;
-use Composer\EventDispatcher\Event;
 use Composer\EventDispatcher\EventSubscriberInterface;
-use Composer\Installer\InstallerEvents;
-use Composer\Installer\PackageEvent;
-use Composer\Installer\PackageEvents;
 use Composer\IO\IOInterface;
 use Composer\Plugin\Capability\Capability;
 use Composer\Plugin\Capability\CommandProvider;
 use Composer\Plugin\Capable;
-use Composer\Plugin\CommandEvent;
-use Composer\Plugin\PluginEvents;
 use Composer\Plugin\PluginInterface;
 use Composer\Script\Event as ScriptEvent;
 use Composer\Script\ScriptEvents;
 use Symfony\Component\Console\Input\ArgvInput;
 use Symfony\Component\Console\Output\ConsoleOutput;
 
-class Builder implements Capable, Capability,  EventSubscriberInterface, PluginInterface {
+class Builder implements Capable, Capability, EventSubscriberInterface, PluginInterface
+{
 
-  /**
-   * @var \Composer\Composer
-   */
-  protected $composer;
+    const NAME = "composer_builder";
 
-  /**
-   * @var \Composer\IO\IOInterface
-   */
-  protected $io;
+    /**
+     * @var \Composer\Composer
+     */
+    protected $composer;
 
-  /**
-   * Apply plugin modifications to Composer
-   *
-   * @param Composer $composer
-   * @param IOInterface $io
-   */
-  public function activate(Composer $composer, IOInterface $io) {
-    $this->composer = $composer;
-    $this->io = $io;
-  }
+    /**
+     * @var \Composer\IO\IOInterface
+     */
+    protected $io;
 
-  public function getCapabilities() {
-    return [
-      CommandProvider::class => BuilderCommandProvider::class,
-    ];
-  }
+    /**
+     * {@inheritdoc}
+     *
+     * Available events:
+     *
+     * - Composer\Installer\InstallerEvents::* ->
+     * Composer\Installer\InstallerEvent
+     * - Composer\Installer\PackageEvents::* -> Composer\Installer\PackageEvent
+     * - Composer\Installer\PluginEvents::INIT -> Composer\EventDispatcher\Event
+     * - Composer\Installer\PluginEvents::COMMAND -> Composer\Plugin\CommandEvent
+     * - Composer\Installer\PluginEvents::PRE_FILE_DOWNLOAD
+     *     -> Composer\Plugin\PreFileDownloadEvent
+     * - Composer\Script\ScriptEvents::* -> Composer\Script\Event
+     */
+    public static function getSubscribedEvents()
+    {
+        return [
+        ScriptEvents::POST_INSTALL_CMD => 'onScriptEvent',
+        ScriptEvents::POST_UPDATE_CMD => 'onScriptEvent',
+        ];
+    }
 
-  /**
-   * {@inheritdoc}
-   *
-   * Available events:
-   *
-   * - Composer\Installer\InstallerEvents::* -> Composer\Installer\InstallerEvent
-   * - Composer\Installer\PackageEvents::* -> Composer\Installer\PackageEvent
-   * - Composer\Installer\PluginEvents::INIT -> Composer\EventDispatcher\Event
-   * - Composer\Installer\PluginEvents::COMMAND -> Composer\Plugin\CommandEvent
-   * - Composer\Installer\PluginEvents::PRE_FILE_DOWNLOAD
-   *     -> Composer\Plugin\PreFileDownloadEvent
-   * - Composer\Script\ScriptEvents::* -> Composer\Script\Event
-   */
-  public static function getSubscribedEvents() {
-    return [
-      ScriptEvents::POST_INSTALL_CMD => 'onScriptEvent',
-      ScriptEvents::POST_UPDATE_CMD => 'onScriptEvent',
-    ];
-  }
+    /**
+     * Apply plugin modifications to Composer
+     *
+     * @param Composer $composer
+     * @param IOInterface $io
+     */
+    public function activate(Composer $composer, IOInterface $io)
+    {
+        $this->composer = $composer;
+        $this->io = $io;
+    }
 
-  public function onScriptEvent(ScriptEvent $event) {
-    $buildCommand = new BuildSettingsCommand($event->getName());
-    $buildCommand->run(new ArgvInput([]), new ConsoleOutput());
-  }
+    /**
+     * {@inheritDoc}
+     */
+    public function getCapabilities()
+    {
+        return [
+        CommandProvider::class => BuilderCommandProvider::class,
+        ];
+    }
+
+    /**
+     * Event callback.
+     *
+     * @param \Composer\Script\Event $event
+     *
+     * @throws \Exception
+     */
+    public function onScriptEvent(ScriptEvent $event)
+    {
+        $buildCommand = new BuildSettingsCommand($event->getName());
+        $buildCommand->run(new ArgvInput([]), new ConsoleOutput());
+    }
 }

+ 42 - 40
src/BuilderCommandProvider.php

@@ -5,44 +5,46 @@ namespace Fgm\Drupal\Composer;
 
 use Composer\Plugin\Capability\CommandProvider;
 
-class BuilderCommandProvider implements CommandProvider {
-
-  /**
-   * @var \Composer\Composer
-   */
-  protected $composer;
-
-  /**
-   * @var \Composer\IO\IOInterface
-   */
-  protected $io;
-
-  /**
-   * An instance of the plugin instantiated not-as-a-command.
-   *
-   * @var self
-   */
-  protected $plugin;
-
-  /**
-   * BuilderCommandProvider constructor.
-   *
-   * @param array|NULL $args
-   *   Guaranteed to contain composer/io/plugin as per CommandProvider.
-   */
-  public function __construct(array $args = null) {
-    $this->composer = $args['composer'];
-    $this->io = $args['io'];
-    $this->plugin = $args['plugin'];
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function getCommands() {
-    return [
-      new BuildSettingsCommand(),
-    ];
-  }
-
+class BuilderCommandProvider implements CommandProvider
+{
+
+    /**
+     * @var \Composer\Composer
+     */
+    protected $composer;
+
+    /**
+     * @var \Composer\IO\IOInterface
+     */
+    protected $io;
+
+    /**
+     * An instance of the plugin instantiated not-as-a-command.
+     *
+     * @var self
+     */
+    protected $plugin;
+
+    /**
+     * BuilderCommandProvider constructor.
+     *
+     * @param array|NULL $args
+     *   Guaranteed to contain composer/io/plugin as per CommandProvider.
+     */
+    public function __construct(array $args = null)
+    {
+        $this->composer = $args['composer'];
+        $this->io = $args['io'];
+        $this->plugin = $args['plugin'];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getCommands()
+    {
+        return [
+        new BuildSettingsCommand(),
+        ];
+    }
 }