← All changes
|
vendor/composer/installers/src/Composer/Installers/BaseInstaller.php
+28
-28
3.0.0
→
trunk
View file →
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace Composer\Installers; |
| 3 | 4 | |
| 4 | 5 | use Composer\IO\IOInterface; |
| 5 | 6 | use Composer\Composer; |
| @@ -6,21 +7,21 @@ | ||
| 6 | 7 | use Composer\Package\PackageInterface; |
| 7 | 8 | |
| 8 | 9 | abstract class BaseInstaller |
| 9 | 10 | { |
| 11 | + /** @var array<string, string> */ | |
| 10 | 12 | protected $locations = array(); |
| 13 | + /** @var Composer */ | |
| 11 | 14 | protected $composer; |
| 15 | + /** @var PackageInterface */ | |
| 12 | 16 | protected $package; |
| 17 | + /** @var IOInterface */ | |
| 13 | 18 | protected $io; |
| 14 | 19 | |
| 15 | 20 | /** |
| 16 | 21 | * Initializes base installer. |
| 17 | - * | |
| 18 | - * @param PackageInterface $package | |
| 19 | - * @param Composer $composer | |
| 20 | - * @param IOInterface $io | |
| 21 | 22 | */ |
| 22 | - public function __construct(PackageInterface $package = null, Composer $composer = null, IOInterface $io = null) | |
| 23 | + public function __construct(PackageInterface $package, Composer $composer, IOInterface $io) | |
| 23 | 24 | { |
| 24 | 25 | $this->composer = $composer; |
| 25 | 26 | $this->package = $package; |
| 26 | 27 | $this->io = $io; |
| @@ -27,14 +28,10 @@ | ||
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | /** |
| 30 | 31 | * Return the install path based on package type. |
| 31 | - * | |
| 32 | - * @param PackageInterface $package | |
| 33 | - * @param string $frameworkType | |
| 34 | - * @return string | |
| 35 | 32 | */ |
| 36 | - public function getInstallPath(PackageInterface $package, $frameworkType = '') | |
| 33 | + public function getInstallPath(PackageInterface $package, string $frameworkType = ''): string | |
| 37 | 34 | { |
| 38 | 35 | $type = $this->package->getType(); |
| 39 | 36 | |
| 40 | 37 | $prettyName = $this->package->getPrettyName(); |
| @@ -51,20 +48,18 @@ | ||
| 51 | 48 | if (!empty($extra['installer-name'])) { |
| 52 | 49 | $availableVars['name'] = $extra['installer-name']; |
| 53 | 50 | } |
| 54 | 51 | |
| 55 | - if ($this->composer->getPackage()) { | |
| 56 | - $extra = $this->composer->getPackage()->getExtra(); | |
| 57 | - if (!empty($extra['installer-paths'])) { | |
| 58 | - $customPath = $this->mapCustomInstallPaths($extra['installer-paths'], $prettyName, $type, $vendor); | |
| 59 | - if ($customPath !== false) { | |
| 60 | - return $this->templatePath($customPath, $availableVars); | |
| 61 | - } | |
| 52 | + $extra = $this->composer->getPackage()->getExtra(); | |
| 53 | + if (!empty($extra['installer-paths'])) { | |
| 54 | + $customPath = $this->mapCustomInstallPaths($extra['installer-paths'], $prettyName, $type, $vendor); | |
| 55 | + if ($customPath !== false) { | |
| 56 | + return $this->templatePath($customPath, $availableVars); | |
| 62 | 57 | } |
| 63 | 58 | } |
| 64 | 59 | |
| 65 | 60 | $packageType = substr($type, strlen($frameworkType) + 1); |
| 66 | - $locations = $this->getLocations(); | |
| 61 | + $locations = $this->getLocations($frameworkType); | |
| 67 | 62 | if (!isset($locations[$packageType])) { |
| 68 | 63 | throw new \InvalidArgumentException(sprintf('Package type "%s" is not supported', $type)); |
| 69 | 64 | } |
| 70 | 65 | |
| @@ -76,9 +71,9 @@ | ||
| 76 | 71 | * |
| 77 | 72 | * @param array<string, string> $vars This will normally receive array{name: string, vendor: string, type: string} |
| 78 | 73 | * @return array<string, string> |
| 79 | 74 | */ |
| 80 | - public function inflectPackageVars($vars) | |
| 75 | + public function inflectPackageVars(array $vars): array | |
| 81 | 76 | { |
| 82 | 77 | return $vars; |
| 83 | 78 | } |
| 84 | 79 | |
| @@ -86,9 +81,9 @@ | ||
| 86 | 81 | * Gets the installer's locations |
| 87 | 82 | * |
| 88 | 83 | * @return array<string, string> map of package types => install path |
| 89 | 84 | */ |
| 90 | - public function getLocations() | |
| 85 | + public function getLocations(string $frameworkType) | |
| 91 | 86 | { |
| 92 | 87 | return $this->locations; |
| 93 | 88 | } |
| 94 | 89 | |
| @@ -94,13 +89,11 @@ | ||
| 94 | 89 | |
| 95 | 90 | /** |
| 96 | 91 | * Replace vars in a path |
| 97 | 92 | * |
| 98 | - * @param string $path | |
| 99 | 93 | * @param array<string, string> $vars |
| 100 | - * @return string | |
| 101 | 94 | */ |
| 102 | - protected function templatePath($path, array $vars = array()) | |
| 95 | + protected function templatePath(string $path, array $vars = array()): string | |
| 103 | 96 | { |
| 104 | 97 | if (strpos($path, '{') !== false) { |
| 105 | 98 | extract($vars); |
| 106 | 99 | preg_match_all('@\{\$([A-Za-z0-9_]*)\}@i', $path, $matches); |
| @@ -116,15 +109,12 @@ | ||
| 116 | 109 | |
| 117 | 110 | /** |
| 118 | 111 | * Search through a passed paths array for a custom install path. |
| 119 | 112 | * |
| 120 | - * @param array $paths | |
| 121 | - * @param string $name | |
| 122 | - * @param string $type | |
| 123 | - * @param string $vendor = NULL | |
| 113 | + * @param array<string, string[]|string> $paths | |
| 124 | 114 | * @return string|false |
| 125 | 115 | */ |
| 126 | - protected function mapCustomInstallPaths(array $paths, $name, $type, $vendor = NULL) | |
| 116 | + protected function mapCustomInstallPaths(array $paths, string $name, string $type, ?string $vendor = null) | |
| 127 | 117 | { |
| 128 | 118 | foreach ($paths as $path => $names) { |
| 129 | 119 | $names = (array) $names; |
| 130 | 120 | if (in_array($name, $names) || in_array('type:' . $type, $names) || in_array('vendor:' . $vendor, $names)) { |
| @@ -132,6 +122,16 @@ | ||
| 132 | 122 | } |
| 133 | 123 | } |
| 134 | 124 | |
| 135 | 125 | return false; |
| 126 | + } | |
| 127 | + | |
| 128 | + protected function pregReplace(string $pattern, string $replacement, string $subject): string | |
| 129 | + { | |
| 130 | + $result = preg_replace($pattern, $replacement, $subject); | |
| 131 | + if (null === $result) { | |
| 132 | + throw new \RuntimeException('Failed to run preg_replace with '.$pattern.': '.preg_last_error()); | |
| 133 | + } | |
| 134 | + | |
| 135 | + return $result; | |
| 136 | 136 | } |
| 137 | 137 | } |