1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Composer plugin for bower/npm assets |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/composer-asset-plugin |
7
|
|
|
* @package composer-asset-plugin |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\composerassetplugin; |
13
|
|
|
|
14
|
|
|
use Composer\Json\JsonFile; |
15
|
|
|
use Composer\Package\CompletePackage; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract package manager class. |
19
|
|
|
* |
20
|
|
|
* @author Andrii Vasyliev <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class PackageManager |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The plugin. |
26
|
|
|
* @var Plugin |
27
|
|
|
*/ |
28
|
|
|
protected $plugin; |
29
|
|
|
|
30
|
|
|
protected $config = []; |
31
|
|
|
|
32
|
|
|
protected $opTable = [ |
33
|
|
|
'require' => 'dependencies', |
34
|
|
|
'require-dev' => 'devDependencies', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
protected $file; |
38
|
|
|
|
39
|
|
|
protected $name; |
40
|
|
|
|
41
|
|
|
protected $jsonFile; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Reads config file or dist config if exists. |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Plugin $plugin) |
47
|
|
|
{ |
48
|
|
|
$this->plugin = $plugin; |
49
|
|
|
$dist = $this->file . '.dist'; |
50
|
|
|
$this->config = $this->readConfig(file_exists($dist) ? $dist : $this->file); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function readConfig($file) |
54
|
|
|
{ |
55
|
|
|
$jsonFile = new JsonFile($file); |
56
|
|
|
$config = $jsonFile->exists() ? $jsonFile->read() : []; |
57
|
|
|
foreach ($this->opTable as $key) { |
58
|
|
|
if (!isset($config[$key])) { |
59
|
|
|
$config[$key] = []; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
return $config; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function writeConfig($file, $config) |
66
|
|
|
{ |
67
|
|
|
foreach ($this->opTable as $key) { |
68
|
|
|
if (isset($config[$key]) && !$config[$key]) { |
69
|
|
|
unset($config[$key]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
$jsonFile = new JsonFile($file); |
73
|
|
|
$jsonFile->write($config); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function scanPackage(CompletePackage $package) |
77
|
|
|
{ |
78
|
|
|
$extra = $package->getExtra(); |
79
|
|
|
$config = []; |
80
|
|
|
foreach (['require', 'require-dev'] as $key) { |
81
|
|
|
$name = $this->name . '-' . $key; |
82
|
|
|
if (isset($extra[$name])) { |
83
|
|
|
$config[$key] = $extra[$name]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
if ($config) { |
|
|
|
|
87
|
|
|
$this->mergeConfig($config); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function mergeConfig($config) |
92
|
|
|
{ |
93
|
|
|
foreach ($config as $key => $packages) { |
94
|
|
|
$key = $this->opTable[$key]; |
95
|
|
|
foreach ($packages as $name => $version) { |
96
|
|
|
$this->config[$key][$name] = isset($this->config[$key][$name]) |
97
|
|
|
? $this->mergeVersions($this->config[$key][$name], $version) |
98
|
|
|
: $version; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function mergeVersions($a, $b) |
104
|
|
|
{ |
105
|
|
|
$a = trim($a); |
106
|
|
|
$b = trim($b); |
107
|
|
|
if ($a === $b || $this->isMoreVersion($b, $a)) { |
108
|
|
|
return $a; |
109
|
|
|
} elseif ($this->isMoreVersion($a, $b)) { |
110
|
|
|
return $b; |
111
|
|
|
} else { |
112
|
|
|
return $a . ' ' . $b; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check if $a is more then $b, like: a="1.1 || 2.2" b="1.1" |
118
|
|
|
* Possible optimization. |
119
|
|
|
*/ |
120
|
|
|
public function isMoreVersion($a, $b) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
return $this->isAnyVersion($a); |
123
|
|
|
// WRONG: return strpos($b, $a) !== false; |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function isAnyVersion($version) |
127
|
|
|
{ |
128
|
|
|
return !$version || $version === '*' || $version === '>=0.0.0'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function loadPackages(array $config) |
132
|
|
|
{ |
133
|
|
|
$this->config = $config; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
abstract public function installPackages(); |
137
|
|
|
} |
138
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..