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