2 namespace Composer\Installers;
5 use Composer\Package\PackageInterface;
7 abstract class BaseInstaller
9 protected $locations = array();
14 * Initializes base installer.
16 * @param PackageInterface $package
17 * @param Composer $composer
19 public function __construct(PackageInterface $package = null, Composer $composer = null)
21 $this->composer = $composer;
22 $this->package = $package;
26 * Return the install path based on package type.
28 * @param PackageInterface $package
29 * @param string $frameworkType
32 public function getInstallPath(PackageInterface $package, $frameworkType = '')
34 $type = $this->package->getType();
36 $prettyName = $this->package->getPrettyName();
37 if (strpos($prettyName, '/') !== false) {
38 list($vendor, $name) = explode('/', $prettyName);
44 $availableVars = $this->inflectPackageVars(compact('name', 'vendor', 'type'));
46 $extra = $package->getExtra();
47 if (!empty($extra['installer-name'])) {
48 $availableVars['name'] = $extra['installer-name'];
51 if ($this->composer->getPackage()) {
52 $extra = $this->composer->getPackage()->getExtra();
53 if (!empty($extra['installer-paths'])) {
54 $customPath = $this->mapCustomInstallPaths($extra['installer-paths'], $prettyName, $type);
55 if ($customPath !== false) {
56 return $this->templatePath($customPath, $availableVars);
61 $packageType = substr($type, strlen($frameworkType) + 1);
62 $locations = $this->getLocations();
63 if (!isset($locations[$packageType])) {
64 throw new \InvalidArgumentException(sprintf('Package type "%s" is not supported', $type));
67 return $this->templatePath($locations[$packageType], $availableVars);
71 * For an installer to override to modify the vars per installer.
76 public function inflectPackageVars($vars)
82 * Gets the installer's locations
86 public function getLocations()
88 return $this->locations;
92 * Replace vars in a path
98 protected function templatePath($path, array $vars = array())
100 if (strpos($path, '{') !== false) {
102 preg_match_all('@\{\$([A-Za-z0-9_]*)\}@i', $path, $matches);
103 if (!empty($matches[1])) {
104 foreach ($matches[1] as $var) {
105 $path = str_replace('{$' . $var . '}', $$var, $path);
114 * Search through a passed paths array for a custom install path.
116 * @param array $paths
117 * @param string $name
118 * @param string $type
121 protected function mapCustomInstallPaths(array $paths, $name, $type)
123 foreach ($paths as $path => $names) {
124 if (in_array($name, $names) || in_array('type:' . $type, $names)) {