Passed
Push — master ( fa90af...614c44 )
by Alexey
05:19
created

BowerCmd   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 6 2
A check() 0 5 2
B initBower() 0 36 4
B command() 0 26 4
B requirePackage() 0 17 6
1
<?php
2
3
/**
4
 * Composer command tool
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class BowerCmd {
12
    public static $appInstance = null;
13
14
    public static function getInstance() {
15
        if (!self::$appInstance) {
16
            self::$appInstance = new Bowerphp\Console\Application();
17
        }
18
        return self::$appInstance;
19
    }
20
21
    public static function check() {
22
        if (!file_exists(App::$primary->path . '/bower.json')) {
23
            BowerCmd::initBower();
24
        }
25
    }
26
27
    public static function initBower($path = '') {
28
        while (!Inji::$inst->blockParallel()) {
29
            sleep(2);
30
        }
31
        if (!$path) {
32
            $path = App::$primary->path . '/';
33
        }
34
35
        $json = [
36
            "name" => get_current_user() . "/" . App::$primary->name,
37
            "config" => [
38
                "cache-dir" => "./cache/bower/"
39
            ],
40
            "authors" => [
41
                [
42
                    get_current_user() . ' <' . get_current_user() . "@" . INJI_DOMAIN_NAME . '>'
43
                ]
44
            ],
45
            'private' => true,
46
            "dependencies" => [],
47
        ];
48
        Tools::createDir($path);
49
        file_put_contents($path . '/bower.json', json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
50
51
        if (!file_exists($path . '/.bowerrc')) {
52
            $json = [
53
                "directory" => 'static/bower',
54
                "interactive" => false
55
            ];
56
            Tools::createDir($path);
57
            file_put_contents($path . '/.bowerrc', json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
58
        }
59
        self::command('install', false, $path);
60
        gc_collect_cycles();
61
        Inji::$inst->unBlockParallel();
62
    }
63
64
    public static function command($command, $needOutput = true, $path = null) {
65
        while (!Inji::$inst->blockParallel()) {
66
            sleep(2);
67
        }
68
        ini_set('memory_limit', '2000M');
69
        ComposerCmd::requirePackage("injitools/bowerphp", "dev-master", '.');
70
        include_once 'vendor/injitools/bowerphp/src/bootstrap.php';
71
        if ($needOutput) {
72
            $output = new Symfony\Component\Console\Output\StreamOutput(fopen('php://output', 'w'));
73
        } else {
74
            $output = new Symfony\Component\Console\Output\NullOutput();
75
        }
76
        $path = str_replace('\\', '/', $path === null ? App::$primary->path . '/' : $path);
77
        $input = new Symfony\Component\Console\Input\StringInput($command);
78
        $app = self::getInstance();
79
        $dir = getcwd();
80
        chdir($path);
81
        putenv('HOME=' . getcwd());
82
        $app->doRun($input, $output);
83
        $app = null;
0 ignored issues
show
Unused Code introduced by
$app is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
        $output = null;
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
        $input = null;
0 ignored issues
show
Unused Code introduced by
$input is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86
        chdir($dir);
87
        gc_collect_cycles();
88
        Inji::$inst->unBlockParallel();
89
    }
90
91
    public static function requirePackage($packageName, $version = '', $path = '') {
92
        if (!$path) {
93
            $path = App::$primary->path;
94
        }
95
        $bowerJson = json_decode(file_get_contents($path . '/bower.json'), true);
96
        if (strpos($packageName, 'github') !== false) {
97
            $needPackageName = basename($packageName);
98
        } else {
99
            $needPackageName = $packageName;
100
        }
101
        if (isset($bowerJson['dependencies'][$needPackageName]) && file_exists($path . '/static/bower/' . $needPackageName)) {
102
            return true;
103
        }
104
105
        self::command('install ' . $packageName . ($version ? '#' . $version : '') . ' --save', false, $path);
106
        return true;
107
    }
108
109
110
}