1
1
<?php
2
+
2
3
namespace Composer \Installers ;
3
4
4
5
use Composer \IO \IOInterface ;
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 ;
@@ -28,12 +29,8 @@ public function __construct(PackageInterface $package = null, Composer $composer
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
@@ -52,18 +49,16 @@ public function getInstallPath(PackageInterface $package, $frameworkType = '')
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
}
@@ -77,7 +72,7 @@ public function getInstallPath(PackageInterface $package, $frameworkType = '')
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
}
@@ -87,19 +82,17 @@ public function inflectPackageVars($vars)
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
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 );
@@ -117,13 +110,10 @@ protected function templatePath($path, array $vars = array())
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 ;
@@ -134,4 +124,14 @@ protected function mapCustomInstallPaths(array $paths, $name, $type, $vendor = N
134
124
135
125
return false ;
136
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
+ }
137
137
}
0 commit comments