Completed
Push — master ( ad149e...027074 )
by Alexey
05:01
created

ComposerCmd::initComposer()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.8571
cc 3
eloc 16
nc 4
nop 1
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 ComposerCmd
12
{
13
    public static function check()
14
    {
15
        if (!file_exists('composer/vendor/autoload.php')) {
16
            self::installComposer();
17
        }
18
        if (!file_exists('vendor/autoload.php')) {
19
            ;
20
            self::initComposer('./');
21
        }
22
        if (!file_exists(App::$primary->path . '/vendor/autoload.php')) {
23
            self::initComposer();
24
        }
25
    }
26
27
    public static function installComposer()
28
    {
29
        if (!file_exists('composer/bin/composer')) {
30
            Tools::createDir('composer');
31
            if (!file_exists('composer/composer.phar')) {
32
                file_put_contents('composer/composerInstall.php', file_get_contents('https://getcomposer.org/installer'));
33
                $argv = ['install', '--install-dir', 'composer/'];
34
                header("Location: " . filter_input(INPUT_SERVER, 'REQUEST_URI'));
35
                include_once 'composer/composerInstall.php';
36
            }
37
            $composer = new Phar('composer/composer.phar');
38
            $composer->extractTo('composer/');
39
        }
40
    }
41
42
    public static function initComposer($path = '')
43
    {
44
        if (!$path) {
45
            $path = App::$primary->path . '/';
46
        }
47
        if (!file_exists($path . 'composer.json')) {
48
            $json = [
49
                "name" => get_current_user() . "/" . App::$primary->name,
50
                "config" => [
51
                    "cache-dir" => "./composerCache/"
52
                ],
53
                "authors" => [
54
                    [
55
                        "name" => get_current_user(),
56
                        "email" => get_current_user() . "@" . INJI_DOMAIN_NAME
57
                    ]
58
                ],
59
                "require" => [
60
                    "php" => ">=5.5.0"
61
                ]
62
            ];
63
            Tools::createDir($path);
64
            file_put_contents($path . 'composer.json', json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
65
        }
66
        self::command('install', false, $path);
67
    }
68
69
    public static function command($command, $needOutput = true, $path = null)
70
    {
71
        include_once 'composer/vendor/autoload.php';
72
        if ($needOutput) {
73
            $output = new Symfony\Component\Console\Output\StreamOutput(fopen('php://output', 'w'));
74
        } else {
75
            $output = null;
76
        }
77
        $path = str_replace('\\', '/', $path === null ? App::$primary->path . '/' : $path);
78
        $input = new Symfony\Component\Console\Input\StringInput($command . ' -d ' . $path);
79
        $app = new Composer\Console\Application();
80
        $app->setAutoExit(false);
81
        $dir = getcwd();
82
        $app->run($input, $output);
83
        chdir($dir);
84
    }
85
86
    public static function requirePackage($packageName, $version = '', $path = '')
87
    {
88
        if (!$path) {
89
            $path = App::$primary->path . '/';
90
        }
91
        if (file_exists($path . 'composer.lock')) {
92
            $lockFile = json_decode(file_get_contents($path . 'composer.lock'), true);
93
        }
94
        if (!empty($lockFile['packages'])) {
95
            foreach ($lockFile['packages'] as $package) {
96
                if ($package['name'] == $packageName) {
97
                    return true;
98
                }
99
            }
100
        }
101
102
        ComposerCmd::command('require ' . $packageName . ($version ? ':' . $version : ''), false, $path);
103
        return true;
104
    }
105
106
}
107