-
Notifications
You must be signed in to change notification settings - Fork 1
/
packagist.php
66 lines (51 loc) · 1.86 KB
/
packagist.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
<?php
/**
* Emulate a packagist composer API by serving static cache files
* from the old Roundcube plugins repository.
*/
require_once './vendor/autoload.php';
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\Response\TextResponse;
use Zend\Diactoros\Response\Serializer as ResponseSerializer;
use Zend\Diactoros\Request\Serializer as RequestSerializer;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;
use GuzzleHttp\Exception\RequestException;
use Zend\HttpHandlerRunner\Emitter\SapiEmitter;
$DATA_DIR = realpath(__dir__ . '/data');
// create a log channel
$logger = new Logger('packagist');
$logger->pushHandler((new StreamHandler('./log/packagist.log', Logger::WARNING))
->setFormatter(new LineFormatter("[%datetime%] %level_name%: %message%\n\n", null, true))
);
// create a PSR7 request based on the current browser request.
$request = ServerRequestFactory::fromGlobals();
$uri = $request->getUri();
$path = urldecode($uri->getPath());
$cacheFile = $DATA_DIR . $path;
$logger->info('>> ' . RequestSerializer::toString($request));
$logger->info('>> CACHE FILE = ' . $cacheFile);
// create PSR7 reponse object
if ($path === '/downloads/' && $request->getMethod() === 'POST') {
$response = new JsonResponse(["status" => "success"], 201);
} else if (is_readable($cacheFile)) {
$contents = file_get_contents($cacheFile);
$mtime = filemtime($cacheFile);
$response = new TextResponse(
$contents,
200,
[
'Content-Type' => 'application/json; charset=utf-8',
'Last-Modified' => date('r', $mtime),
'ETag' => '"' . md5($contents) . '"',
]
);
} else {
$response = new JsonResponse([], 404, []);
}
$logger->info('<< ' . ResponseSerializer::toString($response));
// output response to the browser.
$emitter = new SapiEmitter();
$emitter->emit($response);