Skip to content

Commit

Permalink
Add custom installer for drupal-console-library type (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjuarez20 authored and enzolutions committed Nov 7, 2019
1 parent 7a943bf commit ad8e52d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"require": {
"composer-plugin-api": "^1.0",
"composer/installers": "^1.2",
"symfony/yaml": "~2.7|~3.0",
"symfony/finder": "~2.7|~3.0"
},
Expand Down
19 changes: 19 additions & 0 deletions src/DrupalConsoleInstaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Drupal\Console\Composer\Plugin;

use Composer\Installers\BaseInstaller;

/**
* Class DemoInstaller
*
* @package Composer\Installers
*/
class DrupalConsoleInstaller extends BaseInstaller
{
/**
* @var array
*/
protected $locations = array(
'console-library' => 'vendor/drupal/{$name}/',
);
}
5 changes: 5 additions & 0 deletions src/Extender.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function activate(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;

$installer = new Installer($io, $composer);

$composer->getInstallationManager()->addInstaller($installer);

}

/**
Expand Down
71 changes: 71 additions & 0 deletions src/Installer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Drupal\Console\Composer\Plugin;

use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Installers\Installer as BaseInstaller;

class Installer extends BaseInstaller
{

/**
* Package types to installer class map
*
* @var array
*/
private $supportedTypes = array(
'drupal' => 'DrupalConsoleInstaller'
);

/**
* {@inheritDoc}
*/
public function getInstallPath(PackageInterface $package)
{
$type = $package->getType();
$frameworkType = $this->findFrameworkType($type);

if ($frameworkType === false) {
throw new \InvalidArgumentException(
'Sorry the package type of this package is not yet supported.'
);
}

$class = 'Drupal\\Console\\Composer\\Plugin\\' . $this->supportedTypes[$frameworkType];
$installer = new $class($package, $this->composer, $this->getIO());

return $installer->getInstallPath($package, $frameworkType);
}

/**
* Get the second part of the regular expression to check for support of a
* package type
*
* @param string $frameworkType
* @return string
*/
protected function getLocationPattern($frameworkType)
{
$pattern = false;
if (!empty($this->supportedTypes[$frameworkType])) {
$frameworkClass = 'Drupal\\Console\\Composer\\Plugin\\' . $this->supportedTypes[$frameworkType];
/** @var BaseInstaller $framework */
$framework = new $frameworkClass(null, $this->composer, $this->getIO());
$locations = array_keys($framework->getLocations());
$pattern = $locations ? '(' . implode('|', $locations) . ')' : false;
}

return $pattern ? : '(\w+)';
}

/**
* Get I/O object
*
* @return IOInterface
*/
private function getIO()
{
return $this->io;
}
}

0 comments on commit ad8e52d

Please sign in to comment.