Core/src/Controllers/AbstractController.php

151 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace Benzine\Controllers;
2020-06-19 07:53:59 +00:00
use Benzine\Controllers\Filters\Filter;
2020-06-22 19:38:55 +00:00
use Benzine\Exceptions\FilterDecodeException;
2020-08-31 18:57:20 +00:00
use Benzine\ORM\Abstracts\AbstractService;
use League\Flysystem\Filesystem;
2020-11-13 08:19:24 +00:00
use League\MimeTypeDetection\ExtensionMimeTypeDetector;
2020-07-27 02:44:36 +00:00
use Monolog\Logger;
2020-11-13 08:50:21 +00:00
use Slim\HttpCache\CacheProvider;
2020-06-19 07:53:59 +00:00
use Slim\Psr7\Request;
use Slim\Psr7\Response;
2020-09-01 03:15:02 +00:00
abstract class AbstractController
{
2020-07-27 02:44:36 +00:00
protected Logger $logger;
2020-08-31 18:57:20 +00:00
protected AbstractService $service;
2020-07-27 02:44:36 +00:00
protected bool $apiExplorerEnabled = true;
2020-11-13 08:50:21 +00:00
protected CacheProvider $cacheProvider;
2020-11-13 08:50:21 +00:00
public function __construct(Logger $logger, CacheProvider $cacheProvider)
{
2020-07-27 02:44:36 +00:00
$this->logger = $logger;
$this->logger->debug(sprintf('Entered Controller in %sms', number_format((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000, 2)));
2020-11-13 08:50:21 +00:00
$this->cacheProvider = $cacheProvider;
}
2020-08-31 18:57:20 +00:00
public function getService(): AbstractService
{
return $this->service;
}
2020-08-31 18:57:20 +00:00
public function setService(AbstractService $service): self
{
$this->service = $service;
return $this;
}
2020-07-27 02:44:36 +00:00
public function isApiExplorerEnabled(): bool
{
return $this->apiExplorerEnabled;
}
public function setApiExplorerEnabled(bool $apiExplorerEnabled): self
{
$this->apiExplorerEnabled = $apiExplorerEnabled;
return $this;
}
public function xmlResponse(\SimpleXMLElement $root, Request $request, Response $response): Response
{
2020-06-22 19:38:55 +00:00
$response->getBody()->write($root->asXML());
return $response->withHeader('Content-type', 'text/xml');
}
public function jsonResponse($json, Request $request, Response $response): Response
{
2020-06-22 19:38:55 +00:00
$content = json_encode($json, JSON_PRETTY_PRINT);
$response->getBody()->write($content);
return $response->withHeader('Content-type', 'application/json');
}
public function jsonResponseException(\Exception $e, Request $request, Response $response): Response
{
return $this->jsonResponse(
[
'Status' => 'Fail',
'Reason' => $e->getMessage(),
],
$request,
$response
);
}
/**
* Decide if a request has a filter attached to it.
*
* @throws FilterDecodeException
*/
protected function requestHasFilters(Request $request, Response $response): bool
{
if ($request->hasHeader('Filter')) {
$filterText = trim($request->getHeader('Filter')[0]);
if (!empty($filterText)) {
$decode = json_decode($filterText);
if (null !== $decode) {
return true;
}
throw new FilterDecodeException('Could not decode given Filter. Reason: Not JSON. Given: "'.$filterText.'"');
}
}
return false;
}
/**
* Parse filters header into filter objects.
*/
protected function parseFilters(Request $request, Response $response): Filter
{
$filter = new Filter();
$filter->parseFromHeader(json_decode($request->getHeader('Filter')[0], true));
return $filter;
}
2020-08-27 17:24:10 +00:00
2020-08-27 17:25:48 +00:00
protected function pageNotFound(): Response
{
2020-08-27 17:24:10 +00:00
return (new Response())
2020-08-27 17:25:48 +00:00
->withStatus(404)
;
2020-08-27 17:24:10 +00:00
}
protected function returnFile(Filesystem $filesystem, string $filename): Response
{
$response = new Response();
2020-11-13 07:49:36 +00:00
if (!$filesystem->has($filename)) {
2020-11-13 07:49:36 +00:00
!\Kint::dump($filesystem->listContents(), $filesystem->has($filename));
exit;
return $this->pageNotFound();
}
2020-11-13 08:50:21 +00:00
// Get file metadata from flysystem
$meta = $filesystem->getMetadata($filename);
$etag = md5(implode($meta));
2020-11-13 08:19:24 +00:00
// Detect mimetype for content-type header
$mimetype = (new ExtensionMimeTypeDetector())
2020-11-13 09:52:44 +00:00
->detectMimeTypeFromPath($meta['path'])
;
2020-11-13 08:19:24 +00:00
$response = $response->withHeader('Content-Type', $mimetype);
2020-11-13 08:50:21 +00:00
// Attach ETag
$response = $this->cacheProvider->withEtag($response, $etag);
$response->getBody()
->write($filesystem->read($filename))
;
2020-11-13 07:49:36 +00:00
return $response;
}
}