-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathComposer.php
180 lines (154 loc) · 4.97 KB
/
Composer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace bookin\composer\api;
use Composer\Console\Application;
use Composer\Factory;
use Composer\IO\NullIO;
use Composer\Repository\PlatformRepository;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\RepositoryManager;
use Symfony\Component\Console\Input\ArrayInput;
class Composer
{
public static $configFile = null;
public static $configFilePath = null;
private static $composer;
private static $app;
private static $_instance;
private function __construct() {}
private function __clone() {}
/**
* @param null $configFile
* @param null $configFilePath
* @return Composer
*/
public static function getInstance($configFile=null, $configFilePath=null)
{
/*if(!isset($configFile)|| !isset($configFilePath)){
$config = Factory::createConfig(new NullIO(), getcwd());
if(!isset($configFile))
$configFile = $config->get('home').'/composer.json';
if(!isset($configFilePath))
$configFilePath = $config->get('home');
}*/
putenv('COMPOSER='.$configFile);
self::$configFile = $configFile;
self::$configFilePath = $configFilePath;
if (null === self::$_instance)
{
self::$_instance = new self();
}
return self::$_instance;
}
/**
* @return \Composer\Composer
*/
public static function getComposer(){
if(!self::$composer){
$factory = new Factory();
self::$composer = $factory->createComposer(new NullIO(), self::$configFile, false, self::$configFilePath);
}
return self::$composer;
}
/**
* @return \Composer\Package\PackageInterface[]
*/
public static function getLocalPackages(){
return self::getComposer()->getRepositoryManager()->getLocalRepository()->getCanonicalPackages();
}
/**
* @param $name
* @param array $options
* @return string
*/
public static function updatePackage($name, $options=[]){
return self::runCommand('update', [$name]+$options);
}
/**
* @param array $options
* @return string
*/
public static function updateAllPackages($options=[]){
return self::runCommand('update', $options);
}
/**
* @param array $options
* @return string
*/
public static function deleteAllPackages($options=[]){
return self::runCommand('remove', $options);
}
/**
* @param $name
* @param array $options
* @return string
*/
public static function deletePackage($name, $options=[]){
return self::runCommand('remove', [$name]+$options);
}
/**
* @param $search
* @return array|mixed
* @throws \Composer\Json\JsonValidationException
*/
public static function searchPackage($search){
/* @var $app Application */
$app = self::getApplication();
$composer = $app->getComposer(true, false);
$platformRepo = new PlatformRepository();
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
$installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
$repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
$flags = RepositoryInterface::SEARCH_FULLTEXT;
$results = $repos->search($search, $flags);
return $results;
}
/**
* @param $name
* @param null $version
* @return \Composer\Package\PackageInterface|null
* @throws \Exception
*/
public static function findPackage($name, $version=null)
{
if (strpos($name, '/') === false) {
throw new \Exception('You need use full package name: vendor/vendor1');
}
/** @var RepositoryManager $repositoriManager */
$repositoryManager = self::getComposer()->getRepositoryManager();
$package = $repositoryManager->findPackage($name, $version);
return $package;
}
/**
* @return WebApplication
*/
public static function getApplication(){
if(empty(self::$app)){
$app = new WebApplication();
$app->setComposer(self::getComposer());
$app->setAutoExit(false);
self::$app = $app;
}
return self::$app;
}
/**
* @param string $command
* @param array $params
* @return string
*/
public static function runCommand($command='', $params=[]){
if(empty($command)){
$command='list';
}
$parameters = ['command'=>$command]+$params;
$input = new ArrayInput($parameters);
$output = new ComposerOutput();
$output->setFormatter(new BootstrapOutputFormatter());
try {
$app = self::getApplication();
$app->run($input, $output);
}catch (\Exception $c){
$output->write($c->getMessage());
}
return $output->getMessage();
}
}