Skip to content

Commit ad8e52d

Browse files
hjuarez20enzolutions
authored andcommitted
Add custom installer for drupal-console-library type (#22)
1 parent 7a943bf commit ad8e52d

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"require": {
1616
"composer-plugin-api": "^1.0",
17+
"composer/installers": "^1.2",
1718
"symfony/yaml": "~2.7|~3.0",
1819
"symfony/finder": "~2.7|~3.0"
1920
},

src/DrupalConsoleInstaller.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Drupal\Console\Composer\Plugin;
3+
4+
use Composer\Installers\BaseInstaller;
5+
6+
/**
7+
* Class DemoInstaller
8+
*
9+
* @package Composer\Installers
10+
*/
11+
class DrupalConsoleInstaller extends BaseInstaller
12+
{
13+
/**
14+
* @var array
15+
*/
16+
protected $locations = array(
17+
'console-library' => 'vendor/drupal/{$name}/',
18+
);
19+
}

src/Extender.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public function activate(Composer $composer, IOInterface $io)
3838
{
3939
$this->composer = $composer;
4040
$this->io = $io;
41+
42+
$installer = new Installer($io, $composer);
43+
44+
$composer->getInstallationManager()->addInstaller($installer);
45+
4146
}
4247

4348
/**

src/Installer.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Drupal\Console\Composer\Plugin;
4+
5+
use Composer\IO\IOInterface;
6+
use Composer\Package\PackageInterface;
7+
use Composer\Installers\Installer as BaseInstaller;
8+
9+
class Installer extends BaseInstaller
10+
{
11+
12+
/**
13+
* Package types to installer class map
14+
*
15+
* @var array
16+
*/
17+
private $supportedTypes = array(
18+
'drupal' => 'DrupalConsoleInstaller'
19+
);
20+
21+
/**
22+
* {@inheritDoc}
23+
*/
24+
public function getInstallPath(PackageInterface $package)
25+
{
26+
$type = $package->getType();
27+
$frameworkType = $this->findFrameworkType($type);
28+
29+
if ($frameworkType === false) {
30+
throw new \InvalidArgumentException(
31+
'Sorry the package type of this package is not yet supported.'
32+
);
33+
}
34+
35+
$class = 'Drupal\\Console\\Composer\\Plugin\\' . $this->supportedTypes[$frameworkType];
36+
$installer = new $class($package, $this->composer, $this->getIO());
37+
38+
return $installer->getInstallPath($package, $frameworkType);
39+
}
40+
41+
/**
42+
* Get the second part of the regular expression to check for support of a
43+
* package type
44+
*
45+
* @param string $frameworkType
46+
* @return string
47+
*/
48+
protected function getLocationPattern($frameworkType)
49+
{
50+
$pattern = false;
51+
if (!empty($this->supportedTypes[$frameworkType])) {
52+
$frameworkClass = 'Drupal\\Console\\Composer\\Plugin\\' . $this->supportedTypes[$frameworkType];
53+
/** @var BaseInstaller $framework */
54+
$framework = new $frameworkClass(null, $this->composer, $this->getIO());
55+
$locations = array_keys($framework->getLocations());
56+
$pattern = $locations ? '(' . implode('|', $locations) . ')' : false;
57+
}
58+
59+
return $pattern ? : '(\w+)';
60+
}
61+
62+
/**
63+
* Get I/O object
64+
*
65+
* @return IOInterface
66+
*/
67+
private function getIO()
68+
{
69+
return $this->io;
70+
}
71+
}

0 commit comments

Comments
 (0)