1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the slince/composer-alias package. |
4
|
|
|
* |
5
|
|
|
* (c) Slince <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Slince\ComposerAlias; |
12
|
|
|
|
13
|
|
|
use Composer\Config\JsonConfigSource; |
14
|
|
|
use Composer\Json\JsonFile; |
15
|
|
|
use Composer\Util\Silencer; |
16
|
|
|
|
17
|
|
|
class AliasCollection implements \IteratorAggregate |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var JsonFile |
21
|
|
|
*/ |
22
|
|
|
protected $configFile; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var JsonConfigSource |
26
|
|
|
*/ |
27
|
|
|
protected $configSource; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $aliases; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param JsonFile $configFile |
36
|
|
|
* |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
|
|
public function __construct(JsonFile $configFile) |
40
|
|
|
{ |
41
|
|
|
$this->configFile = $configFile; |
42
|
|
|
$this->configSource = new JsonConfigSource($this->configFile); |
43
|
|
|
if (!$this->configFile->exists()) { |
44
|
|
|
touch($this->configFile->getPath()); |
45
|
|
|
$this->configFile->write(['config' => new \ArrayObject()]); |
46
|
|
|
Silencer::call('chmod', $this->configFile->getPath(), 0600); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets all aliases. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function all() |
56
|
|
|
{ |
57
|
|
|
$this->readFromFile(); |
58
|
|
|
|
59
|
|
|
return $this->aliases; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add an alias. |
64
|
|
|
* |
65
|
|
|
* @param string $alias |
66
|
|
|
* @param string $raw |
67
|
|
|
* |
68
|
|
|
* @return static |
69
|
|
|
*/ |
70
|
|
|
public function add($alias, $raw) |
71
|
|
|
{ |
72
|
|
|
$this->readFromFile(); |
73
|
|
|
$this->aliases[$alias] = $raw; |
74
|
|
|
$this->configSource->addConfigSetting('_alias', $this->aliases); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Removes an alias. |
81
|
|
|
* |
82
|
|
|
* @param string $alias |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function remove($alias) |
87
|
|
|
{ |
88
|
|
|
$this->readFromFile(); |
89
|
|
|
unset($this->aliases[$alias]); |
90
|
|
|
$this->configSource->addConfigSetting('_alias', $this->aliases); |
|
|
|
|
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Gets the alias. |
97
|
|
|
* |
98
|
|
|
* @param string $alias |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function get($alias) |
103
|
|
|
{ |
104
|
|
|
$this->readFromFile(); |
105
|
|
|
|
106
|
|
|
return isset($this->aliases[$alias]) ? $this->aliases[$alias] : null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Checks whether the alias exists. |
111
|
|
|
* |
112
|
|
|
* @param string $alias |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function has($alias) |
117
|
|
|
{ |
118
|
|
|
return isset($this->aliases[$alias]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function getIterator() |
125
|
|
|
{ |
126
|
|
|
return new \ArrayIterator($this->all()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function readFromFile() |
130
|
|
|
{ |
131
|
|
|
if ($this->aliases) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
$config = $this->configFile->read(); |
135
|
|
|
if (!isset($config['config']['_alias']) || !is_array($config['config']['_alias'])) { |
136
|
|
|
$aliases = []; |
137
|
|
|
} else { |
138
|
|
|
$aliases = $config['config']['_alias']; |
139
|
|
|
} |
140
|
|
|
$this->aliases = $aliases; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: