|
| 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