]> _ Git - couzy.git/blob
cc27d3e2866c48177e77b4b115041a0de11a0fcd
[couzy.git] /
1 <?php
2 namespace Composer\Installers;
3
4 use Composer\Composer;
5 use Composer\Package\PackageInterface;
6
7 abstract class BaseInstaller
8 {
9     protected $locations = array();
10     protected $composer;
11     protected $package;
12
13     /**
14      * Initializes base installer.
15      *
16      * @param PackageInterface $package
17      * @param Composer         $composer
18      */
19     public function __construct(PackageInterface $package = null, Composer $composer = null)
20     {
21         $this->composer = $composer;
22         $this->package = $package;
23     }
24
25     /**
26      * Return the install path based on package type.
27      *
28      * @param  PackageInterface $package
29      * @param  string           $frameworkType
30      * @return string
31      */
32     public function getInstallPath(PackageInterface $package, $frameworkType = '')
33     {
34         $type = $this->package->getType();
35
36         $prettyName = $this->package->getPrettyName();
37         if (strpos($prettyName, '/') !== false) {
38             list($vendor, $name) = explode('/', $prettyName);
39         } else {
40             $vendor = '';
41             $name = $prettyName;
42         }
43
44         $availableVars = $this->inflectPackageVars(compact('name', 'vendor', 'type'));
45
46         $extra = $package->getExtra();
47         if (!empty($extra['installer-name'])) {
48             $availableVars['name'] = $extra['installer-name'];
49         }
50
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);
57                 }
58             }
59         }
60
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));
65         }
66
67         return $this->templatePath($locations[$packageType], $availableVars);
68     }
69
70     /**
71      * For an installer to override to modify the vars per installer.
72      *
73      * @param  array $vars
74      * @return array
75      */
76     public function inflectPackageVars($vars)
77     {
78         return $vars;
79     }
80
81     /**
82      * Gets the installer's locations
83      *
84      * @return array
85      */
86     public function getLocations()
87     {
88         return $this->locations;
89     }
90
91     /**
92      * Replace vars in a path
93      *
94      * @param  string $path
95      * @param  array  $vars
96      * @return string
97      */
98     protected function templatePath($path, array $vars = array())
99     {
100         if (strpos($path, '{') !== false) {
101             extract($vars);
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);
106                 }
107             }
108         }
109
110         return $path;
111     }
112
113     /**
114      * Search through a passed paths array for a custom install path.
115      *
116      * @param  array  $paths
117      * @param  string $name
118      * @param  string $type
119      * @return string
120      */
121     protected function mapCustomInstallPaths(array $paths, $name, $type)
122     {
123         foreach ($paths as $path => $names) {
124             if (in_array($name, $names) || in_array('type:' . $type, $names)) {
125                 return $path;
126             }
127         }
128
129         return false;
130     }
131 }