firefly-iii/app/Http/Controllers/HelpController.php

92 lines
2.2 KiB
PHP
Raw Normal View History

2015-02-25 09:10:02 -06:00
<?php namespace FireflyIII\Http\Controllers;
2015-03-10 11:26:31 -05:00
use Cache;
2015-02-25 09:10:02 -06:00
use ErrorException;
use League\CommonMark\CommonMarkConverter;
use Response;
2015-03-10 11:26:31 -05:00
use Route;
2015-02-25 09:10:02 -06:00
/**
* Class HelpController
*
* @package FireflyIII\Http\Controllers
*/
2015-03-10 11:26:31 -05:00
class HelpController extends Controller
{
2015-02-25 09:10:02 -06:00
/**
* @param $route
*
* @return \Illuminate\Http\JsonResponse
*/
public function show($route)
{
$content = [
'text' => '<p>There is no help for this route!</p>',
'title' => 'Help',
];
if (!Route::has($route)) {
\Log::error('No such route: ' . $route);
return Response::json($content);
}
2015-03-29 01:24:56 -05:00
if ($this->inCache($route)) {
2015-02-25 09:10:02 -06:00
$content = [
'text' => Cache::get('help.' . $route . '.text'),
'title' => Cache::get('help.' . $route . '.title'),
];
return Response::json($content);
}
2015-03-29 01:24:56 -05:00
$content = $this->getFromGithub($route);
2015-02-25 09:10:02 -06:00
Cache::put('help.' . $route . '.text', $content['text'], 10080); // a week.
Cache::put('help.' . $route . '.title', $content['title'], 10080);
return Response::json($content);
}
/**
* @param $route
*
* @return bool
*/
2015-03-29 01:24:56 -05:00
protected function inCache($route)
2015-02-25 09:10:02 -06:00
{
return Cache::has('help.' . $route . '.title') && Cache::has('help.' . $route . '.text');
}
/**
* @param $route
*
* @return array
*/
2015-03-29 01:24:56 -05:00
protected function getFromGithub($route)
2015-02-25 09:10:02 -06:00
{
$uri = 'https://raw.githubusercontent.com/JC5/firefly-iii-help/master/' . e($route) . '.md';
$content = [
'text' => '<p>There is no help for this route!</p>',
'title' => $route,
];
try {
$content['text'] = file_get_contents($uri);
} catch (ErrorException $e) {
\Log::error(trim($e->getMessage()));
}
if (strlen(trim($content['text'])) == 0) {
$content['text'] = '<p>There is no help for this route.</p>';
}
2015-03-10 11:26:31 -05:00
$converter = new CommonMarkConverter();
2015-02-25 09:10:02 -06:00
$content['text'] = $converter->convertToHtml($content['text']);
return $content;
}
}