-
Notifications
You must be signed in to change notification settings - Fork 67
/
package.php
221 lines (172 loc) · 4.99 KB
/
package.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Fuel is a fast, lightweight, community driven PHP 5.4+ framework.
*
* @package Fuel
* @version 1.9-dev
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2019 Fuel Development Team
* @link https://fuelphp.com
*/
namespace Oil;
/**
* Oil\Package Class
*
* @package Fuel
* @subpackage Oil
* @category Core
* @author Phil Sturgeon
*/
class Package
{
protected static $protected = array('oil');
protected static $git = 'git';
public static function install($package = null)
{
// Make sure something is set
if ($package === null)
{
static::help();
return;
}
$config = \Config::load('package');
$version = \Cli::option('version', 'master');
// Check to see if this package is already installed
if (is_dir(PKGPATH . $package))
{
throw new Exception('Package "' . $package . '" is already installed.');
return;
}
foreach ($config['sources'] as $source)
{
$packages = array('fuel-'.$package, $package);
foreach ($packages as $package)
{
$zip_url = 'http://' . rtrim($source, '/').'/'.$package.'/zipball/'.$version;
if ($fp = @fopen($zip_url, 'r'))
{
// We don't actually need this, just checking the file is there
fclose($fp);
// Now, lets get this package
// If a direct download is requested, or git is unavailable, download it!
if (\Cli::option('direct') OR static::_use_git() === false)
{
static::_download_package_zip($zip_url, $package, $version);
exit;
}
// Otherwise, get your clone on baby!
else
{
static::_clone_package_repo($source, $package, $version);
exit;
}
}
}
}
throw new Exception('Could not find package "' . $package . '".');
}
public static function uninstall($package)
{
$package_folder = PKGPATH . $package;
// Check to see if this package is already installed
if (in_array($package, static::$protected))
{
throw new Exception('Package "' . $package . '" cannot be uninstalled.');
return false;
}
// Check to see if this package is already installed
if ( ! is_dir($package_folder))
{
throw new Exception('Package "' . $package . '" is not installed.');
return false;
}
\Cli::write('Package "' . $package . '" was uninstalled.', 'yellow');
\File::delete_dir($package_folder);
}
public static function help()
{
$output = <<<HELP
Usage:
php oil [p|package] <packagename>
Description:
Packages containing extra functionality can be downloaded (or git cloned) simply with
the following commands.
Runtime options:
--direct # Download direct from ZIP even if Git is installed
Examples:
php oil package install <packagename>
php oil package uninstall <packagename>
Documentation:
https://fuelphp.com/docs/packages/oil/package.html
HELP;
\Cli::write($output);
}
private static function _use_git()
{
exec('which git', $output);
// If this is a valid path to git, use it instead of just "git"
if (file_exists($line = trim(current($output))))
{
static::$git = $line;
}
unset($output);
// Double check git is installed (windows will fail step 1)
exec(static::$git . ' --version', $output);
preg_match('#^(git version)#', current($output), $matches);
// If we have a match, use Git!
return ! empty($matches[0]);
}
private static function _download_package_zip($zip_url, $package, $version)
{
\Cli::write('Downloading package: ' . $zip_url);
// Make the folder so we can extract the ZIP to it
mkdir($tmp_folder = APPPATH . 'tmp' . DS . $package . '-' . time());
$zip_file = $tmp_folder . '.zip';
@copy($zip_url, $zip_file);
if (is_file($zip_file))
{
$unzip = new \Unzip;
$files = $unzip->extract($zip_file, $tmp_folder);
// Grab the first folder out of it (we dont know what it's called)
foreach($pkgfolders = new \GlobIterator($tmp_folder.DS.'*') as $pkgfolder)
{
if ($pkgfolder->isDir())
{
$tmp_package_folder = $tmp_folder.DS.$pkgfolder->getFilename();
break;
}
}
if (empty($tmp_package_folder))
{
throw new \FuelException('The package zip file doesn\'t contain any install directory.');
}
$package_folder = PKGPATH . $package;
// Move that folder into the packages folder
rename($tmp_package_folder, $package_folder);
unlink($zip_file);
rmdir($tmp_folder);
foreach ($files as $file)
{
$path = str_replace($tmp_package_folder, $package_folder, $file);
chmod($path, octdec(755));
\Cli::write("\t" . $path);
}
}
else
{
\Cli::write('Package could not be found', 'red');
}
}
public static function _clone_package_repo($source, $package, $version)
{
$repo_url = 'git://' . rtrim($source, '/').'/'.$package . '.git';
\Cli::write('Downloading package: ' . $repo_url);
$package_folder = PKGPATH . $package;
// Clone to the package path
passthru(static::$git . ' clone ' . $repo_url . ' ' . $package_folder);
passthru(static::$git .' add ' . $package_folder . '/');
\Cli::write('');
}
}
/* End of file package.php */