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
|
|
|
public static $appInstance = null; |
13
|
|
|
|
14
|
|
|
public static function getInstance() { |
15
|
|
|
if (!self::$appInstance) { |
16
|
|
|
self::$appInstance = new Composer\Console\Application(); |
17
|
|
|
} |
18
|
|
|
return self::$appInstance; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public static function check() { |
22
|
|
|
if (!file_exists(getenv('COMPOSER_HOME') . '/composer/vendor/autoload.php')) { |
23
|
|
|
self::installComposer(getenv('COMPOSER_HOME')); |
24
|
|
|
} |
25
|
|
|
if (!file_exists(getenv('COMPOSER_HOME') . '/vendor/autoload.php')) { |
26
|
|
|
self::initComposer(getenv('COMPOSER_HOME')); |
27
|
|
|
} |
28
|
|
|
if (!file_exists(App::$primary->path . '/vendor/autoload.php')) { |
29
|
|
|
self::initComposer(); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $path |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public static function installComposer($path) { |
38
|
|
|
while (!Inji::$inst->blockParallel()) { |
39
|
|
|
sleep(2); |
40
|
|
|
} |
41
|
|
|
ini_set('memory_limit', '1000M'); |
42
|
|
|
if (file_exists($path . '/composer/bin/composer')) { |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
Tools::createDir($path . '/composer'); |
46
|
|
|
if (!file_exists($path . '/composer/composer.phar')) { |
47
|
|
|
file_put_contents($path . '/composer/composerInstall.php', str_replace('process(is_array($argv) ? $argv : array());', '', file_get_contents('https://getcomposer.org/installer'))); |
48
|
|
|
include_once $path . '/composer/composerInstall.php'; |
49
|
|
|
|
50
|
|
|
$quiet = false; |
51
|
|
|
$channel = 'stable'; |
52
|
|
|
$disableTls = false; |
53
|
|
|
$installDir = $path . '/composer/'; |
54
|
|
|
$version = false; |
55
|
|
|
$filename = 'composer.phar'; |
56
|
|
|
$cafile = false; |
57
|
|
|
setUseAnsi([]); |
58
|
|
|
ob_start(); |
59
|
|
|
$installer = new Installer($quiet, $disableTls, $cafile); |
60
|
|
|
$installer->run($version, $installDir, $filename, $channel); |
61
|
|
|
ob_end_clean(); |
62
|
|
|
} |
63
|
|
|
$composer = new Phar($path . '/composer/composer.phar'); |
64
|
|
|
$composer->extractTo($path . '/composer/'); |
65
|
|
|
$composer = null; |
|
|
|
|
66
|
|
|
gc_collect_cycles(); |
67
|
|
|
Inji::$inst->unBlockParallel(); |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function initComposer($path = '') { |
72
|
|
|
if (!$path) { |
73
|
|
|
$path = App::$primary->path . '/'; |
74
|
|
|
} |
75
|
|
|
if (!file_exists($path . '/composer.json')) { |
76
|
|
|
$json = [ |
77
|
|
|
"name" => get_current_user() . "/" . App::$primary->name, |
78
|
|
|
"config" => [ |
79
|
|
|
"cache-dir" => "./cache/composer/" |
80
|
|
|
], |
81
|
|
|
"authors" => [ |
82
|
|
|
[ |
83
|
|
|
"name" => get_current_user(), |
84
|
|
|
"email" => get_current_user() . "@" . INJI_DOMAIN_NAME |
85
|
|
|
] |
86
|
|
|
], |
87
|
|
|
"require" => [ |
88
|
|
|
"php" => ">=5.5.0" |
89
|
|
|
] |
90
|
|
|
]; |
91
|
|
|
Tools::createDir($path); |
92
|
|
|
file_put_contents($path . '/composer.json', json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
93
|
|
|
} |
94
|
|
|
self::command('install', false, $path); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $command |
99
|
|
|
* @param string $path |
100
|
|
|
*/ |
101
|
|
|
public static function command($command, $needOutput = true, $path = null) { |
102
|
|
|
while (!Inji::$inst->blockParallel()) { |
103
|
|
|
sleep(2); |
104
|
|
|
} |
105
|
|
|
ini_set('memory_limit', '1000M'); |
106
|
|
|
include_once getenv('COMPOSER_HOME') . '/composer/vendor/autoload.php'; |
107
|
|
|
if ($needOutput) { |
108
|
|
|
$output = new Symfony\Component\Console\Output\StreamOutput(fopen('php://output', 'w')); |
109
|
|
|
} else { |
110
|
|
|
$output = null; |
111
|
|
|
} |
112
|
|
|
$path = str_replace('\\', '/', $path === null ? App::$primary->path . '/' : $path); |
113
|
|
|
$input = new Symfony\Component\Console\Input\StringInput($command . ' -d ' . $path); |
114
|
|
|
$app = self::getInstance(); |
115
|
|
|
$app->setAutoExit(false); |
116
|
|
|
$dir = getcwd(); |
117
|
|
|
$app->run($input, $output); |
118
|
|
|
$app = null; |
|
|
|
|
119
|
|
|
$output = null; |
|
|
|
|
120
|
|
|
$input = null; |
|
|
|
|
121
|
|
|
chdir($dir); |
122
|
|
|
gc_collect_cycles(); |
123
|
|
|
Inji::$inst->unBlockParallel(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public static function requirePackage($packageName, $version = '', $path = '') { |
127
|
|
|
if (!$path) { |
128
|
|
|
$path = App::$primary->path; |
129
|
|
|
} |
130
|
|
|
if (file_exists($path . '/composer.lock')) { |
131
|
|
|
$lockFile = json_decode(file_get_contents($path . '/composer.lock'), true); |
132
|
|
|
} |
133
|
|
|
if (!empty($lockFile['packages'])) { |
134
|
|
|
foreach ($lockFile['packages'] as $package) { |
135
|
|
|
if ($package['name'] == $packageName) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
self::command('require ' . $packageName . ($version ? ':' . $version : ''), false, $path); |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.