Package::getVersion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Composer plugin for config assembling
4
 *
5
 * @link      https://github.com/hiqdev/composer-config-plugin
6
 * @package   composer-config-plugin
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\composer\config;
12
13
use Composer\Composer;
14
use Composer\Package\CompletePackageInterface;
15
use Composer\Package\PackageInterface;
16
use Composer\Package\RootPackageInterface;
17
use Composer\Util\Filesystem;
18
19
/**
20
 * Class Package.
21
 * @author Andrii Vasyliev <[email protected]>
22
 *
23
 * @since php5.5
24
 */
25
class Package
26
{
27
    protected $package;
28
29
    /**
30
     * @var array composer.json raw data array
31
     */
32
    protected $data;
33
34
    /**
35
     * @var string absolute path to the root base directory
36
     */
37
    protected $baseDir;
38
39
    /**
40
     * @var string absolute path to vendor directory
41
     */
42
    protected $vendorDir;
43
44
    /**
45
     * @var Filesystem utility
46
     */
47
    protected $filesystem;
48
49
    private $composer;
50
51
    public function __construct(PackageInterface $package, Composer $composer)
52
    {
53
        $this->package = $package;
54
        $this->composer = $composer;
55
    }
56
57
    /**
58
     * Collects package aliases.
59
     * @return array collected aliases
60
     */
61
    public function collectAliases()
62
    {
63
        $aliases = array_merge(
64
            $this->prepareAliases('psr-0'),
65
            $this->prepareAliases('psr-4')
66
        );
67
        if ($this->isRoot()) {
68
            $aliases = array_merge($aliases,
69
                $this->prepareAliases('psr-0', true),
70
                $this->prepareAliases('psr-4', true)
71
            );
72
        }
73
74
        return $aliases;
75
    }
76
77
    /**
78
     * Prepare aliases.
79
     * @param string 'psr-0' or 'psr-4'
0 ignored issues
show
Documentation Bug introduced by
The doc comment 'psr-0' at position 0 could not be parsed: Unknown type name ''psr-0'' at position 0 in 'psr-0'.
Loading history...
80
     * @param bool $dev
81
     * @return array
82
     */
83
    protected function prepareAliases($psr, $dev = false)
84
    {
85
        $autoload = $dev ? $this->getDevAutoload() : $this->getAutoload();
86
        if (empty($autoload[$psr])) {
87
            return [];
88
        }
89
90
        $aliases = [];
91
        foreach ($autoload[$psr] as $name => $path) {
92
            if (is_array($path)) {
93
                // ignore psr-4 autoload specifications with multiple search paths
94
                // we can not convert them into aliases as they are ambiguous
95
                continue;
96
            }
97
            $name = str_replace('\\', '/', trim($name, '\\'));
98
            $path = $this->preparePath($path);
99
            if ('psr-0' === $psr) {
100
                $path .= '/' . $name;
101
            }
102
            $aliases["@$name"] = $path;
103
        }
104
105
        return $aliases;
106
    }
107
108
    /**
109
     * @return string package pretty name, like: vendor/name
110
     */
111
    public function getPrettyName()
112
    {
113
        return $this->package->getPrettyName();
114
    }
115
116
    /**
117
     * @return string package version, like: 3.0.16.0, 9999999-dev
118
     */
119
    public function getVersion()
120
    {
121
        return $this->package->getVersion();
122
    }
123
124
    /**
125
     * @return string package human friendly version, like: 5.x-dev d9aed42, 2.1.1, dev-master f6561bf
126
     */
127
    public function getFullPrettyVersion()
128
    {
129
        return $this->package->getFullPrettyVersion();
130
    }
131
132
    /**
133
     * @return string|null package CVS revision, like: 3a4654ac9655f32888efc82fb7edf0da517d8995
134
     */
135
    public function getSourceReference()
136
    {
137
        return $this->package->getSourceReference();
138
    }
139
140
    /**
141
     * @return string|null package dist revision, like: 3a4654ac9655f32888efc82fb7edf0da517d8995
142
     */
143
    public function getDistReference()
144
    {
145
        return $this->package->getDistReference();
146
    }
147
148
    /**
149
     * @return bool is package complete
150
     */
151
    public function isComplete()
152
    {
153
        return $this->package instanceof CompletePackageInterface;
154
    }
155
156
    /**
157
     * @return bool is this a root package
158
     */
159
    public function isRoot()
160
    {
161
        return $this->package instanceof RootPackageInterface;
162
    }
163
164
    /**
165
     * @return string package type, like: package, library
166
     */
167
    public function getType()
168
    {
169
//        return $this->getRawValue('type') ?? $this->package->getType();
170
        $type = $this->getRawValue('type');
171
        return isset($type) ? $type : $this->package->getType();
172
    }
173
174
    /**
175
     * @return array autoload configuration array
176
     */
177
    public function getAutoload()
178
    {
179
//        return $this->getRawValue('autoload') ?? $this->package->getAutoload();
180
        $autoload = $this->getRawValue('autoload');
181
        return isset($autoload) ? $autoload : $this->package->getAutoload();
182
    }
183
184
    /**
185
     * @return array autoload-dev configuration array
186
     */
187
    public function getDevAutoload()
188
    {
189
//        return $this->getRawValue('autoload-dev') ?? $this->package->getDevAutoload();
190
        $autoloadDev = $this->getRawValue('autoload-dev');
191
        return isset($autoloadDev) ? $autoloadDev : $this->package->getDevAutoload();
192
    }
193
194
    /**
195
     * @return array requre configuration array
196
     */
197
    public function getRequires()
198
    {
199
//        return $this->getRawValue('require') ?? $this->package->getRequires();
200
        $require = $this->getRawValue('require');
201
        return isset($require) ? $require : $this->package->getRequires();
202
    }
203
204
    /**
205
     * @return array requre-dev configuration array
206
     */
207
    public function getDevRequires()
208
    {
209
//        return $this->getRawValue('require-dev') ?? $this->package->getDevRequires();
210
        $requireDev = $this->getRawValue('require-dev');
211
        return isset($requireDev) ? $requireDev : $this->package->getDevRequires();
212
    }
213
214
    /**
215
     * @return array extra configuration array
216
     */
217
    public function getExtra()
218
    {
219
//        return $this->getRawValue('extra') ?? $this->package->getExtra();
220
        $extra = $this->getRawValue('extra');
221
        return isset($extra) ? $extra : $this->package->getExtra();
222
    }
223
224
    /**
225
     * @param string $name option name
226
     * @return mixed raw value from composer.json if available
227
     */
228
    public function getRawValue($name)
229
    {
230
        if ($this->data === null) {
231
            $this->data = $this->readRawData();
232
        }
233
234
//        return $this->data[$name] ?? null;
235
        return isset($this->data[$name]) ? $this->data[$name] : null;
236
    }
237
238
    /**
239
     * @return mixed all raw data from composer.json if available
240
     */
241
    public function getRawData()
242
    {
243
        if ($this->data === null) {
244
            $this->data = $this->readRawData();
245
        }
246
247
        return $this->data;
248
    }
249
250
    /**
251
     * @return array composer.json contents as array
252
     */
253
    protected function readRawData()
254
    {
255
        $path = $this->preparePath('composer.json');
256
        if (file_exists($path)) {
257
            return json_decode(file_get_contents($path), true);
258
        }
259
260
        return [];
261
    }
262
263
    /**
264
     * Builds path inside of a package.
265
     * @param string $file
266
     * @return string absolute paths will stay untouched
267
     */
268
    public function preparePath($file)
269
    {
270
        if (0 === strncmp($file, '$', 1)) {
271
            return $file;
272
        }
273
274
        $skippable = 0 === strncmp($file, '?', 1) ? '?' : '';
275
        if ($skippable) {
276
            $file = substr($file, 1);
277
        }
278
279
        if (!$this->getFilesystem()->isAbsolutePath($file)) {
280
            $prefix = $this->isRoot()
281
                ? $this->getBaseDir()
282
                : $this->getVendorDir() . '/' . $this->getPrettyName();
283
            $file = $prefix . '/' . $file;
284
        }
285
286
        return $skippable . $this->getFilesystem()->normalizePath($file);
287
    }
288
289
    /**
290
     * Get absolute path to package base dir.
291
     * @return string
292
     */
293
    public function getBaseDir()
294
    {
295
        if (null === $this->baseDir) {
296
            $this->baseDir = dirname($this->getVendorDir());
297
        }
298
299
        return $this->baseDir;
300
    }
301
302
    /**
303
     * Get absolute path to composer vendor dir.
304
     * @return string
305
     */
306
    public function getVendorDir()
307
    {
308
        if (null === $this->vendorDir) {
309
            $dir = $this->composer->getConfig()->get('vendor-dir');
310
            $this->vendorDir = $this->getFilesystem()->normalizePath($dir);
311
        }
312
313
        return $this->vendorDir;
314
    }
315
316
    /**
317
     * Getter for filesystem utility.
318
     * @return Filesystem
319
     */
320
    public function getFilesystem()
321
    {
322
        if (null === $this->filesystem) {
323
            $this->filesystem = new Filesystem();
324
        }
325
326
        return $this->filesystem;
327
    }
328
}
329