annotate vendor/composer/installers/src/Composer/Installers/BaseInstaller.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 namespace Composer\Installers;
Chris@0 3
Chris@0 4 use Composer\IO\IOInterface;
Chris@0 5 use Composer\Composer;
Chris@0 6 use Composer\Package\PackageInterface;
Chris@0 7
Chris@0 8 abstract class BaseInstaller
Chris@0 9 {
Chris@0 10 protected $locations = array();
Chris@0 11 protected $composer;
Chris@0 12 protected $package;
Chris@0 13 protected $io;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Initializes base installer.
Chris@0 17 *
Chris@0 18 * @param PackageInterface $package
Chris@0 19 * @param Composer $composer
Chris@0 20 * @param IOInterface $io
Chris@0 21 */
Chris@0 22 public function __construct(PackageInterface $package = null, Composer $composer = null, IOInterface $io = null)
Chris@0 23 {
Chris@0 24 $this->composer = $composer;
Chris@0 25 $this->package = $package;
Chris@0 26 $this->io = $io;
Chris@0 27 }
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Return the install path based on package type.
Chris@0 31 *
Chris@0 32 * @param PackageInterface $package
Chris@0 33 * @param string $frameworkType
Chris@0 34 * @return string
Chris@0 35 */
Chris@0 36 public function getInstallPath(PackageInterface $package, $frameworkType = '')
Chris@0 37 {
Chris@0 38 $type = $this->package->getType();
Chris@0 39
Chris@0 40 $prettyName = $this->package->getPrettyName();
Chris@0 41 if (strpos($prettyName, '/') !== false) {
Chris@0 42 list($vendor, $name) = explode('/', $prettyName);
Chris@0 43 } else {
Chris@0 44 $vendor = '';
Chris@0 45 $name = $prettyName;
Chris@0 46 }
Chris@0 47
Chris@0 48 $availableVars = $this->inflectPackageVars(compact('name', 'vendor', 'type'));
Chris@0 49
Chris@0 50 $extra = $package->getExtra();
Chris@0 51 if (!empty($extra['installer-name'])) {
Chris@0 52 $availableVars['name'] = $extra['installer-name'];
Chris@0 53 }
Chris@0 54
Chris@0 55 if ($this->composer->getPackage()) {
Chris@0 56 $extra = $this->composer->getPackage()->getExtra();
Chris@0 57 if (!empty($extra['installer-paths'])) {
Chris@0 58 $customPath = $this->mapCustomInstallPaths($extra['installer-paths'], $prettyName, $type, $vendor);
Chris@0 59 if ($customPath !== false) {
Chris@0 60 return $this->templatePath($customPath, $availableVars);
Chris@0 61 }
Chris@0 62 }
Chris@0 63 }
Chris@0 64
Chris@0 65 $packageType = substr($type, strlen($frameworkType) + 1);
Chris@0 66 $locations = $this->getLocations();
Chris@0 67 if (!isset($locations[$packageType])) {
Chris@0 68 throw new \InvalidArgumentException(sprintf('Package type "%s" is not supported', $type));
Chris@0 69 }
Chris@0 70
Chris@0 71 return $this->templatePath($locations[$packageType], $availableVars);
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * For an installer to override to modify the vars per installer.
Chris@0 76 *
Chris@0 77 * @param array $vars
Chris@0 78 * @return array
Chris@0 79 */
Chris@0 80 public function inflectPackageVars($vars)
Chris@0 81 {
Chris@0 82 return $vars;
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Gets the installer's locations
Chris@0 87 *
Chris@0 88 * @return array
Chris@0 89 */
Chris@0 90 public function getLocations()
Chris@0 91 {
Chris@0 92 return $this->locations;
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Replace vars in a path
Chris@0 97 *
Chris@0 98 * @param string $path
Chris@0 99 * @param array $vars
Chris@0 100 * @return string
Chris@0 101 */
Chris@0 102 protected function templatePath($path, array $vars = array())
Chris@0 103 {
Chris@0 104 if (strpos($path, '{') !== false) {
Chris@0 105 extract($vars);
Chris@0 106 preg_match_all('@\{\$([A-Za-z0-9_]*)\}@i', $path, $matches);
Chris@0 107 if (!empty($matches[1])) {
Chris@0 108 foreach ($matches[1] as $var) {
Chris@0 109 $path = str_replace('{$' . $var . '}', $$var, $path);
Chris@0 110 }
Chris@0 111 }
Chris@0 112 }
Chris@0 113
Chris@0 114 return $path;
Chris@0 115 }
Chris@0 116
Chris@0 117 /**
Chris@0 118 * Search through a passed paths array for a custom install path.
Chris@0 119 *
Chris@0 120 * @param array $paths
Chris@0 121 * @param string $name
Chris@0 122 * @param string $type
Chris@0 123 * @param string $vendor = NULL
Chris@0 124 * @return string
Chris@0 125 */
Chris@0 126 protected function mapCustomInstallPaths(array $paths, $name, $type, $vendor = NULL)
Chris@0 127 {
Chris@0 128 foreach ($paths as $path => $names) {
Chris@0 129 if (in_array($name, $names) || in_array('type:' . $type, $names) || in_array('vendor:' . $vendor, $names)) {
Chris@0 130 return $path;
Chris@0 131 }
Chris@0 132 }
Chris@0 133
Chris@0 134 return false;
Chris@0 135 }
Chris@0 136 }