HummingBirdAnimeClient/src/Controller.php

430 lines
9.8 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
2015-11-16 11:40:01 -05:00
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
2015-11-16 11:40:01 -05:00
*
2016-10-20 22:09:36 -04:00
* PHP version 7
2016-08-30 10:01:18 -04:00
*
2015-11-16 11:40:01 -05:00
* @package HummingbirdAnimeClient
2016-08-30 10:01:18 -04:00
* @author Timothy J. Warren <tim@timshomepage.net>
2018-01-15 14:43:15 -05:00
* @copyright 2015 - 2018 Timothy J. Warren
2016-08-30 10:01:18 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
2016-10-20 22:09:36 -04:00
2015-09-15 13:19:29 -04:00
namespace Aviat\AnimeClient;
2017-03-30 16:57:58 -04:00
use function Aviat\Ion\_dir;
2017-02-22 14:46:35 -05:00
2018-01-16 14:58:07 -05:00
use Aviat\Ion\Di\{
ContainerAware,
ContainerInterface
};
use Aviat\Ion\Di\Exception\{
ContainerException,
NotFoundException
};
2017-12-06 14:40:13 -05:00
use Aviat\Ion\Exception\DoubleRenderException;
2016-12-20 12:58:37 -05:00
use Aviat\Ion\View\{HtmlView, HttpView, JsonView};
2016-10-20 22:09:36 -04:00
use InvalidArgumentException;
2015-09-17 23:11:18 -04:00
/**
2015-09-17 23:11:18 -04:00
* Controller base, defines output methods
*/
class Controller {
2017-03-31 13:37:53 -04:00
use ContainerAware;
/**
* Cache manager
* @var \Psr\Cache\CacheItemPoolInterface
*/
protected $cache;
/**
* The global configuration object
* @var \Aviat\Ion\ConfigInterface $config
*/
public $config;
/**
* Request object
2018-01-16 14:58:07 -05:00
* @var \Psr\Http\Message\ServerRequestInterface $request
2017-03-31 13:37:53 -04:00
*/
protected $request;
/**
* Response object
2018-01-16 14:58:07 -05:00
* @var \Psr\Http\Message\ResponseInterface $response
2017-03-31 13:37:53 -04:00
*/
public $response;
/**
* Url generation class
* @var UrlGenerator
*/
protected $urlGenerator;
/**
* Aura url generator
* @var \Aura\Router\Generator
*/
protected $url;
/**
* Session segment
* @var \Aura\Session\Segment
*/
protected $session;
/**
* Common data to be sent to views
* @var array
*/
protected $baseData = [
'url_type' => 'anime',
'other_type' => 'manga',
'menu_name' => ''
];
2015-06-26 16:39:10 -04:00
/**
* Constructor
*
* @param ContainerInterface $container
2018-01-16 14:58:07 -05:00
* @throws ContainerException
* @throws NotFoundException
*/
2015-09-17 23:11:18 -04:00
public function __construct(ContainerInterface $container)
2015-05-22 12:36:26 -04:00
{
2015-09-17 23:11:18 -04:00
$this->setContainer($container);
2016-03-03 16:53:17 -05:00
$auraUrlGenerator = $container->get('aura-router')->getGenerator();
2015-09-17 23:11:18 -04:00
$urlGenerator = $container->get('url-generator');
2016-04-21 11:14:21 -04:00
$this->cache = $container->get('cache');
$this->config = $container->get('config');
$this->request = $container->get('request');
$this->response = $container->get('response');
$this->baseData = array_merge($this->baseData, [
2017-02-17 08:25:19 -05:00
'url' => $auraUrlGenerator,
'urlGenerator' => $urlGenerator,
'auth' => $container->get('auth'),
'config' => $this->config
]);
2017-03-30 16:16:40 -04:00
$this->url = $auraUrlGenerator;
2015-09-17 23:11:18 -04:00
$this->urlGenerator = $urlGenerator;
2016-01-04 10:53:03 -05:00
$session = $container->get('session');
$this->session = $session->getSegment(SESSION_SEGMENT);
2016-01-04 10:53:03 -05:00
// Set a 'previous' flash value for better redirects
2017-02-16 14:30:06 -05:00
$serverParams = $this->request->getServerParams();
2017-12-06 14:40:13 -05:00
if (array_key_exists('HTTP_REFERER', $serverParams) && false === stripos($serverParams['HTTP_REFERER'], 'login'))
{
2017-02-16 14:30:06 -05:00
$this->session->setFlash('previous', $serverParams['HTTP_REFERER']);
}
2016-01-04 10:53:03 -05:00
// Set a message box if available
2017-02-15 11:30:16 -05:00
$this->baseData['message'] = $this->session->getFlash('message');
2015-05-22 12:36:26 -04:00
}
2017-03-10 12:50:48 -05:00
/**
2017-03-31 13:37:53 -04:00
* Redirect to the previous page
2017-03-10 12:50:48 -05:00
*
* @return void
*/
2017-03-31 13:37:53 -04:00
public function redirectToPrevious()
2017-03-08 12:55:49 -05:00
{
2017-03-31 13:37:53 -04:00
$previous = $this->session->getFlash('previous');
$this->redirect($previous, 303);
2017-03-08 12:55:49 -05:00
}
2015-11-04 16:36:54 -05:00
/**
2017-03-31 13:37:53 -04:00
* Set the current url in the session as the target of a future redirect
2015-11-04 16:36:54 -05:00
*
2017-03-31 13:37:53 -04:00
* @param string|null $url
2018-01-16 14:58:07 -05:00
* @throws ContainerException
* @throws NotFoundException
2015-11-04 16:36:54 -05:00
* @return void
*/
2017-03-31 13:37:53 -04:00
public function setSessionRedirect(string $url = NULL)
2015-11-04 16:36:54 -05:00
{
2017-03-31 13:37:53 -04:00
$serverParams = $this->request->getServerParams();
2015-11-04 16:36:54 -05:00
2017-03-31 13:37:53 -04:00
if ( ! array_key_exists('HTTP_REFERER', $serverParams))
{
return;
}
$util = $this->container->get('util');
$doubleFormPage = $serverParams['HTTP_REFERER'] === $this->request->getUri();
2017-12-06 14:40:13 -05:00
$isLoginPage = (bool) strpos($serverParams['HTTP_REFERER'], 'login');
2017-03-31 13:37:53 -04:00
// Don't attempt to set the redirect url if
// the page is one of the form type pages,
// and the previous page is also a form type page_segments
2017-12-06 14:40:13 -05:00
if ($doubleFormPage || $isLoginPage)
2017-03-31 13:37:53 -04:00
{
return;
}
2017-12-06 14:40:13 -05:00
if (null === $url)
2017-03-31 13:37:53 -04:00
{
$url = $util->isViewPage()
2018-01-16 14:58:07 -05:00
? $this->request->getUri()->__toString()
2017-03-31 13:37:53 -04:00
: $serverParams['HTTP_REFERER'];
}
$this->session->set('redirect_url', $url);
}
/**
* Redirect to the url previously set in the session
*
2017-12-06 14:40:13 -05:00
* @throws InvalidArgumentException
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
2017-03-31 13:37:53 -04:00
* @return void
*/
public function sessionRedirect()
{
$target = $this->session->get('redirect_url');
if (empty($target))
{
$this->notFound();
}
else
{
$this->redirect($target, 303);
$this->session->set('redirect_url', NULL);
}
}
2015-11-04 16:36:54 -05:00
2017-03-31 13:37:53 -04:00
/**
* Get the string output of a partial template
*
* @param HtmlView $view
* @param string $template
* @param array $data
* @throws InvalidArgumentException
2017-12-06 14:40:13 -05:00
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
2017-03-31 13:37:53 -04:00
* @return string
*/
protected function loadPartial($view, string $template, array $data = [])
{
$router = $this->container->get('dispatcher');
if (isset($this->baseData))
2015-11-04 16:36:54 -05:00
{
2017-03-31 13:37:53 -04:00
$data = array_merge($this->baseData, $data);
2015-11-04 16:36:54 -05:00
}
2017-03-31 13:37:53 -04:00
$route = $router->getRoute();
$data['route_path'] = $route ? $router->getRoute()->path : '';
$templatePath = _dir($this->config->get('view_path'), "{$template}.php");
2016-01-04 10:53:03 -05:00
2017-03-31 13:37:53 -04:00
if ( ! is_file($templatePath))
{
throw new InvalidArgumentException("Invalid template : {$template}");
}
return $view->renderTemplate($templatePath, (array)$data);
}
/**
* Render a template with header and footer
*
* @param HtmlView $view
* @param string $template
* @param array $data
2017-12-06 14:40:13 -05:00
* @throws InvalidArgumentException
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
2017-03-31 13:37:53 -04:00
* @return void
*/
protected function renderFullPage($view, string $template, array $data)
{
$csp = [
"default-src 'self'",
"object-src 'none'",
"child-src 'none'",
];
$view->addHeader('Content-Security-Policy', implode('; ', $csp));
2017-03-31 13:37:53 -04:00
$view->appendOutput($this->loadPartial($view, 'header', $data));
if (array_key_exists('message', $data) && is_array($data['message']))
{
$view->appendOutput($this->loadPartial($view, 'message', $data['message']));
}
$view->appendOutput($this->loadPartial($view, $template, $data));
$view->appendOutput($this->loadPartial($view, 'footer', $data));
}
/**
* 404 action
*
2017-12-06 14:40:13 -05:00
* @param string $title
* @param string $message
* @throws InvalidArgumentException
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
2017-03-31 13:37:53 -04:00
* @return void
*/
public function notFound(
string $title = 'Sorry, page not found',
string $message = 'Page Not Found'
)
{
$this->outputHTML('404', [
'title' => $title,
'message' => $message,
], NULL, 404);
}
/**
* Display a generic error page
*
* @param int $httpCode
* @param string $title
* @param string $message
* @param string $long_message
2017-12-06 14:40:13 -05:00
* @throws InvalidArgumentException
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
2017-03-31 13:37:53 -04:00
* @return void
*/
public function errorPage(int $httpCode, string $title, string $message, string $long_message = "")
{
$this->outputHTML('error', [
'title' => $title,
'message' => $message,
'long_message' => $long_message
], NULL, $httpCode);
}
/**
* Redirect to the default controller/url from an empty path
*
* @return void
*/
public function redirectToDefaultRoute()
{
$defaultType = $this->config->get(['routes', 'route_config', 'default_list']) ?? 'anime';
$this->redirect($this->urlGenerator->defaultUrl($defaultType), 303);
}
2017-03-31 13:37:53 -04:00
/**
* Set a session flash variable to display a message on
* next page load
*
* @param string $message
* @param string $type
* @return void
*/
public function setFlashMessage(string $message, string $type = "info")
{
static $messages;
if ( ! $messages)
{
$messages = [];
}
$messages[] = [
'message_type' => $type,
2015-11-04 16:36:54 -05:00
'message' => $message
2017-03-31 13:37:53 -04:00
];
$this->session->setFlash('message', $messages);
}
/**
* Helper for consistent page titles
*
2017-12-06 14:40:13 -05:00
* @param string[] ...$parts Title segments
2017-03-31 13:37:53 -04:00
* @return string
*/
public function formatTitle(string ...$parts) : string
{
return implode(' &middot; ', $parts);
}
/**
* Add a message box to the page
*
* @param HtmlView $view
* @param string $type
* @param string $message
2017-12-06 14:40:13 -05:00
* @throws InvalidArgumentException
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
2017-03-31 13:37:53 -04:00
* @return string
*/
protected function showMessage($view, string $type, string $message): string
{
return $this->loadPartial($view, 'message', [
'message_type' => $type,
'message' => $message
]);
2015-11-04 16:36:54 -05:00
}
2015-11-13 11:33:27 -05:00
/**
2017-03-31 13:37:53 -04:00
* Output a template to HTML, using the provided data
2015-11-13 11:33:27 -05:00
*
2017-03-31 13:37:53 -04:00
* @param string $template
* @param array $data
* @param HtmlView|null $view
* @param int $code
2017-12-06 14:40:13 -05:00
* @throws InvalidArgumentException
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
2015-11-18 10:48:05 -05:00
* @return void
2015-11-13 11:33:27 -05:00
*/
2017-03-31 13:37:53 -04:00
protected function outputHTML(string $template, array $data = [], $view = NULL, int $code = 200)
2015-11-13 11:33:27 -05:00
{
2017-12-06 14:40:13 -05:00
if (null === $view)
2015-11-13 11:33:27 -05:00
{
2017-03-31 13:37:53 -04:00
$view = new HtmlView($this->container);
2015-11-13 11:33:27 -05:00
}
2017-03-31 13:37:53 -04:00
$view->setStatusCode($code);
$this->renderFullPage($view, $template, $data);
2015-11-13 11:33:27 -05:00
}
/**
2017-03-31 13:37:53 -04:00
* Output a JSON Response
2015-11-13 11:33:27 -05:00
*
2017-03-31 13:37:53 -04:00
* @param mixed $data
* @param int $code - the http status code
2017-12-06 14:40:13 -05:00
* @throws DoubleRenderException
2015-11-13 11:33:27 -05:00
* @return void
*/
2017-03-31 13:37:53 -04:00
protected function outputJSON($data = 'Empty response', int $code = 200)
2015-11-13 11:33:27 -05:00
{
2017-03-31 13:37:53 -04:00
(new JsonView($this->container))
->setStatusCode($code)
->setOutput($data)
->send();
}
2015-11-13 11:33:27 -05:00
2017-03-31 13:37:53 -04:00
/**
* Redirect to the selected page
*
* @param string $url
* @param int $code
* @return void
*/
protected function redirect(string $url, int $code)
{
$http = new HttpView($this->container);
$http->redirect($url, $code);
2015-11-13 11:33:27 -05:00
}
}
2015-09-15 13:19:29 -04:00
// End of BaseController.php