Added caching for public build status badge. Issue #15
This commit is contained in:
parent
38944fca1b
commit
e4a7079954
|
@ -125,12 +125,16 @@ class BuildStatusController extends Controller
|
|||
|
||||
/**
|
||||
* Returns the appropriate build status image in SVG format for a given project.
|
||||
*
|
||||
* @param $projectId
|
||||
*
|
||||
* @return b8\Http\Response|b8\Http\Response\RedirectResponse
|
||||
*/
|
||||
public function image($projectId)
|
||||
{
|
||||
// plastic|flat|flat-squared|social
|
||||
$style = $this->getParam('style', 'flat');
|
||||
$label = $this->getParam('label', 'build');
|
||||
$style = $this->getParam('style', 'flat');
|
||||
$label = $this->getParam('label', 'build');
|
||||
|
||||
$optionalParams = [
|
||||
'logo' => $this->getParam('logo'),
|
||||
|
@ -147,21 +151,30 @@ class BuildStatusController extends Controller
|
|||
return $response;
|
||||
}
|
||||
|
||||
$color = ($status == 'passing') ? 'green' : 'red';
|
||||
$image = file_get_contents(sprintf(
|
||||
$color = ($status == 'passing') ? 'green' : 'red';
|
||||
$imageUrl = sprintf(
|
||||
'http://img.shields.io/badge/%s-%s-%s.svg?style=%s',
|
||||
$label,
|
||||
$status,
|
||||
$color,
|
||||
$style
|
||||
));
|
||||
);
|
||||
|
||||
foreach ($optionalParams as $paramName => $param) {
|
||||
if ($param) {
|
||||
$image .= '&' . $paramName . '=' . $param;
|
||||
$imageUrl .= '&' . $paramName . '=' . $param;
|
||||
}
|
||||
}
|
||||
|
||||
$cacheDir = RUNTIME_DIR . '/status_cache/';
|
||||
$cacheFile = $cacheDir . md5($imageUrl) . '.svg';
|
||||
if (!is_file($cacheFile)) {
|
||||
$image = file_get_contents($imageUrl);
|
||||
file_put_contents($cacheFile, $image);
|
||||
}
|
||||
|
||||
$image = file_get_contents($cacheFile);
|
||||
|
||||
$this->response->disableLayout();
|
||||
$this->response->setHeader('Content-Type', 'image/svg+xml');
|
||||
$this->response->setContent($image);
|
||||
|
|
Loading…
Reference in a new issue