forked from kawahara/composer-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
60 lines (52 loc) · 1.26 KB
/
helper.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
<?php
if (!function_exists('checkConfig')) {
function checkConfig($app, $rep) {
if (!isset($app['repositories'][$rep])) {
$app->abort(404, "Not Found!");
}
return true;
}
}
if (!function_exists('load')) {
function load($app, $url, $localPath) {
$cache = checkLocalCache($localPath);
if (false === $cache) {
$response = loadFromRepository($app, $url);
makeLocalCache($response, $localPath);
return $response;
}
return $cache;
}
}
if (!function_exists('checkLocalCache')) {
function checkLocalCache($localPath) {
makeDirIfNeeded($localPath);
if (file_exists($localPath)) {
return file_get_contents($localPath);
}
return false;
}
}
if (!function_exists('makeLocalCache')) {
function makeLocalCache($content, $filePath) {
file_put_contents($filePath, $content);
}
}
if (!function_exists('loadFromRepository')) {
function loadFromRepository($app, $url) {
$response = $app['browser']->get($url);
if (!$response->isOk()) {
$app->abort($response->getStatusCode(), "Response Error!");
}
$responseContent = $response->getContent();
return $responseContent;
}
}
if (!function_exists('makeDirIfNeeded')) {
function makeDirIfNeeded($filePath) {
$dir = dirname($filePath);
if (!is_dir($dir)) {
@mkdir($dir, 0777, true);
}
}
}