Passed
Pull Request — master (#56)
by Jitendra
02:16
created

Composer::initConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the PHINT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Phint\Util;
13
14
use Ahc\Cli\Exception\RuntimeException;
15
16
class Composer extends Executable
17
{
18
    /** @var array Content of composer.json decoded */
19
    protected $config = null;
20
21
    /** @var string The binary executable */
22
    protected $binary = 'composer';
23
24
    public function createProject($project, $using)
25
    {
26
        $this->runCommand(sprintf('create-project %s %s', $using, $project));
27
28
        return $this;
29
    }
30
31
    public function install()
32
    {
33
        $this->runCommand('install --prefer-dist --optimize-autoloader --no-suggest');
34
35
        return $this;
36
    }
37
38
    public function update()
39
    {
40
        $this->runCommand('update --prefer-dist --optimize-autoloader --no-suggest');
41
42
        return $this;
43
    }
44
45
    public function dumpAutoload()
46
    {
47
        $this->runCommand('dump-autoload --optimize');
48
49
        return $this;
50
    }
51
52
    public function config(string $key, $default = null)
53
    {
54
        $this->initConfig();
55
56
        $temp = $this->config;
57
        foreach (\explode('.', $key) as $part) {
58
            if (\is_array($temp) && \array_key_exists($part, $temp)) {
59
                $temp = $temp[$part];
60
            } else {
61
                return $default;
62
            }
63
        }
64
65
        return $temp;
66
    }
67
68
    protected function initConfig()
69
    {
70
        if (null === $this->config) {
71
            $this->config = (new Path)->readAsJson($this->workDir . '/composer.json');
72
        }
73
74
        if (null === $this->config) {
75
            throw new RuntimeException("Non existent or invalid composer.json at {$this->workDir}");
76
        }
77
    }
78
}
79