Chris@0: composer = $composer; Chris@0: $this->package = $package; Chris@0: $this->io = $io; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return the install path based on package type. Chris@0: * Chris@0: * @param PackageInterface $package Chris@0: * @param string $frameworkType Chris@0: * @return string Chris@0: */ Chris@0: public function getInstallPath(PackageInterface $package, $frameworkType = '') Chris@0: { Chris@0: $type = $this->package->getType(); Chris@0: Chris@0: $prettyName = $this->package->getPrettyName(); Chris@0: if (strpos($prettyName, '/') !== false) { Chris@0: list($vendor, $name) = explode('/', $prettyName); Chris@0: } else { Chris@0: $vendor = ''; Chris@0: $name = $prettyName; Chris@0: } Chris@0: Chris@0: $availableVars = $this->inflectPackageVars(compact('name', 'vendor', 'type')); Chris@0: Chris@0: $extra = $package->getExtra(); Chris@0: if (!empty($extra['installer-name'])) { Chris@0: $availableVars['name'] = $extra['installer-name']; Chris@0: } Chris@0: Chris@0: if ($this->composer->getPackage()) { Chris@0: $extra = $this->composer->getPackage()->getExtra(); Chris@0: if (!empty($extra['installer-paths'])) { Chris@0: $customPath = $this->mapCustomInstallPaths($extra['installer-paths'], $prettyName, $type, $vendor); Chris@0: if ($customPath !== false) { Chris@0: return $this->templatePath($customPath, $availableVars); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $packageType = substr($type, strlen($frameworkType) + 1); Chris@0: $locations = $this->getLocations(); Chris@0: if (!isset($locations[$packageType])) { Chris@0: throw new \InvalidArgumentException(sprintf('Package type "%s" is not supported', $type)); Chris@0: } Chris@0: Chris@0: return $this->templatePath($locations[$packageType], $availableVars); Chris@0: } Chris@0: Chris@0: /** Chris@0: * For an installer to override to modify the vars per installer. Chris@0: * Chris@0: * @param array $vars Chris@0: * @return array Chris@0: */ Chris@0: public function inflectPackageVars($vars) Chris@0: { Chris@0: return $vars; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the installer's locations Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getLocations() Chris@0: { Chris@0: return $this->locations; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Replace vars in a path Chris@0: * Chris@0: * @param string $path Chris@0: * @param array $vars Chris@0: * @return string Chris@0: */ Chris@0: protected function templatePath($path, array $vars = array()) Chris@0: { Chris@0: if (strpos($path, '{') !== false) { Chris@0: extract($vars); Chris@0: preg_match_all('@\{\$([A-Za-z0-9_]*)\}@i', $path, $matches); Chris@0: if (!empty($matches[1])) { Chris@0: foreach ($matches[1] as $var) { Chris@0: $path = str_replace('{$' . $var . '}', $$var, $path); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $path; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Search through a passed paths array for a custom install path. Chris@0: * Chris@0: * @param array $paths Chris@0: * @param string $name Chris@0: * @param string $type Chris@0: * @param string $vendor = NULL Chris@0: * @return string Chris@0: */ Chris@0: protected function mapCustomInstallPaths(array $paths, $name, $type, $vendor = NULL) Chris@0: { Chris@0: foreach ($paths as $path => $names) { Chris@0: if (in_array($name, $names) || in_array('type:' . $type, $names) || in_array('vendor:' . $vendor, $names)) { Chris@0: return $path; Chris@0: } Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: }