2 namespace Composer\Installers;
4 use Composer\IO\IOInterface;
6 use Composer\Package\PackageInterface;
8 abstract class BaseInstaller
10 protected $locations = array();
16 * Initializes base installer.
18 * @param PackageInterface $package
19 * @param Composer $composer
20 * @param IOInterface $io
22 public function __construct(PackageInterface $package = null, Composer $composer = null, IOInterface $io = null)
24 $this->composer = $composer;
25 $this->package = $package;
30 * Return the install path based on package type.
32 * @param PackageInterface $package
33 * @param string $frameworkType
36 public function getInstallPath(PackageInterface $package, $frameworkType = '')
38 $type = $this->package->getType();
40 $prettyName = $this->package->getPrettyName();
41 if (strpos($prettyName, '/') !== false) {
42 list($vendor, $name) = explode('/', $prettyName);
48 $availableVars = $this->inflectPackageVars(compact('name', 'vendor', 'type'));
50 $extra = $package->getExtra();
51 if (!empty($extra['installer-name'])) {
52 $availableVars['name'] = $extra['installer-name'];
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);
65 $packageType = substr($type, strlen($frameworkType) + 1);
66 $locations = $this->getLocations();
67 if (!isset($locations[$packageType])) {
68 throw new \InvalidArgumentException(sprintf('Package type "%s" is not supported', $type));
71 return $this->templatePath($locations[$packageType], $availableVars);
75 * For an installer to override to modify the vars per installer.
80 public function inflectPackageVars($vars)
86 * Gets the installer's locations
90 public function getLocations()
92 return $this->locations;
96 * Replace vars in a path
102 protected function templatePath($path, array $vars = array())
104 if (strpos($path, '{') !== false) {
106 preg_match_all('@\{\$([A-Za-z0-9_]*)\}@i', $path, $matches);
107 if (!empty($matches[1])) {
108 foreach ($matches[1] as $var) {
109 $path = str_replace('{$' . $var . '}', $$var, $path);
118 * Search through a passed paths array for a custom install path.
120 * @param array $paths
121 * @param string $name
122 * @param string $type
123 * @param string $vendor = NULL
126 protected function mapCustomInstallPaths(array $paths, $name, $type, $vendor = NULL)
128 foreach ($paths as $path => $names) {
129 if (in_array($name, $names) || in_array('type:' . $type, $names) || in_array('vendor:' . $vendor, $names)) {