2015-04-07 12:58:49 -05:00
|
|
|
<?php
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* Help.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 05:27:31 -05:00
|
|
|
*/
|
|
|
|
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-04-07 12:58:49 -05:00
|
|
|
namespace FireflyIII\Helpers\Help;
|
|
|
|
|
|
|
|
use Cache;
|
|
|
|
use League\CommonMark\CommonMarkConverter;
|
2016-10-31 12:31:52 -05:00
|
|
|
use Log;
|
2016-02-23 09:12:59 -06:00
|
|
|
use Requests;
|
2016-10-31 12:31:52 -05:00
|
|
|
use Requests_Exception;
|
2015-04-07 12:58:49 -05:00
|
|
|
use Route;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Help
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Helpers\Help
|
|
|
|
*/
|
|
|
|
class Help implements HelpInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2016-10-23 10:33:53 -05:00
|
|
|
* @param string $route
|
|
|
|
* @param string $language
|
2015-04-07 12:58:49 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-10-23 10:33:53 -05:00
|
|
|
public function getFromCache(string $route, string $language): string
|
2015-04-07 12:58:49 -05:00
|
|
|
{
|
2016-11-02 01:04:14 -05:00
|
|
|
$line = sprintf('help.%s.%s', $route, $language);
|
|
|
|
|
|
|
|
return Cache::get($line);
|
2015-04-07 12:58:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-23 09:12:59 -06:00
|
|
|
* @param string $language
|
2016-02-05 02:25:15 -06:00
|
|
|
* @param string $route
|
2015-04-07 12:58:49 -05:00
|
|
|
*
|
2016-10-23 10:33:53 -05:00
|
|
|
* @return string
|
2015-04-07 12:58:49 -05:00
|
|
|
*/
|
2016-10-23 10:33:53 -05:00
|
|
|
public function getFromGithub(string $language, string $route): string
|
2015-04-07 12:58:49 -05:00
|
|
|
{
|
2016-02-23 09:12:59 -06:00
|
|
|
|
2016-10-31 12:31:52 -05:00
|
|
|
$uri = sprintf('https://raw.githubusercontent.com/firefly-iii/help/master/%s/%s.md', $language, $route);
|
|
|
|
Log::debug(sprintf('Trying to get %s...', $uri));
|
|
|
|
$content = '';
|
|
|
|
try {
|
|
|
|
$result = Requests::get($uri);
|
|
|
|
} catch (Requests_Exception $e) {
|
|
|
|
Log::error($e);
|
2016-02-23 09:12:59 -06:00
|
|
|
|
2016-10-31 12:31:52 -05:00
|
|
|
return '';
|
2015-04-07 12:58:49 -05:00
|
|
|
}
|
2016-02-23 09:12:59 -06:00
|
|
|
|
|
|
|
|
2016-10-31 12:31:52 -05:00
|
|
|
Log::debug(sprintf('Status code is %d', $result->status_code));
|
|
|
|
|
|
|
|
if ($result->status_code === 200) {
|
|
|
|
$content = trim($result->body);
|
|
|
|
}
|
|
|
|
if (strlen($content) > 0) {
|
|
|
|
Log::debug('Content is longer than zero. Expect something.');
|
|
|
|
$converter = new CommonMarkConverter();
|
|
|
|
$content = $converter->convertToHtml($content);
|
2015-04-07 12:58:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-14 02:51:54 -05:00
|
|
|
*
|
2016-02-05 02:25:15 -06:00
|
|
|
* @param string $route
|
2015-05-03 05:58:55 -05:00
|
|
|
*
|
|
|
|
* @return bool
|
2015-04-07 12:58:49 -05:00
|
|
|
*/
|
2016-02-23 09:12:59 -06:00
|
|
|
public function hasRoute(string $route):bool
|
2015-04-07 12:58:49 -05:00
|
|
|
{
|
|
|
|
return Route::has($route);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-05 02:25:15 -06:00
|
|
|
* @param string $route
|
2016-10-23 10:33:53 -05:00
|
|
|
* @param string $language
|
2015-04-07 12:58:49 -05:00
|
|
|
*
|
2015-06-29 08:23:50 -05:00
|
|
|
* @return bool
|
2015-04-07 12:58:49 -05:00
|
|
|
*/
|
2016-10-23 10:33:53 -05:00
|
|
|
public function inCache(string $route, string $language):bool
|
2015-04-07 12:58:49 -05:00
|
|
|
{
|
2016-11-02 01:04:14 -05:00
|
|
|
$line = sprintf('help.%s.%s', $route, $language);
|
|
|
|
$result = Cache::has($line);
|
2016-10-31 12:31:52 -05:00
|
|
|
if ($result) {
|
|
|
|
Log::debug(sprintf('Cache has this entry: %s', 'help.' . $route . '.' . $language));
|
|
|
|
}
|
|
|
|
if (!$result) {
|
|
|
|
Log::debug(sprintf('Cache does not have this entry: %s', 'help.' . $route . '.' . $language));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
2015-04-07 12:58:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-14 02:51:54 -05:00
|
|
|
*
|
2016-02-05 02:25:15 -06:00
|
|
|
* @param string $route
|
2016-04-09 02:27:04 -05:00
|
|
|
* @param string $language
|
2016-10-23 10:33:53 -05:00
|
|
|
* @param string $content
|
2015-04-07 12:58:49 -05:00
|
|
|
*
|
2015-06-29 08:23:50 -05:00
|
|
|
* @internal param $title
|
2015-04-07 12:58:49 -05:00
|
|
|
*/
|
2016-10-23 10:33:53 -05:00
|
|
|
public function putInCache(string $route, string $language, string $content)
|
2015-04-07 12:58:49 -05:00
|
|
|
{
|
2016-11-02 01:04:14 -05:00
|
|
|
$key = sprintf('help.%s.%s', $route, $language);
|
2016-10-31 12:31:52 -05:00
|
|
|
Log::debug(sprintf('Will store entry in cache: %s', $key));
|
|
|
|
Cache::put($key, $content, 10080); // a week.
|
2015-04-07 12:58:49 -05:00
|
|
|
}
|
2015-05-05 05:51:57 -05:00
|
|
|
}
|