2020-08-18 16:31:28 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-18 16:31:28 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
use App\Entities\Page;
|
2020-08-18 16:31:28 +00:00
|
|
|
use App\Models\PageModel;
|
2021-05-19 16:35:13 +00:00
|
|
|
use CodeIgniter\Exceptions\PageNotFoundException;
|
2020-08-18 16:31:28 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
class PageController extends BaseController
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
2021-06-09 12:40:22 +00:00
|
|
|
protected Page $page;
|
2020-08-18 16:31:28 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function _remap(string $method, string ...$params): mixed
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
if (count($params) === 0) {
|
2021-05-19 16:35:13 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2021-05-06 14:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2021-06-09 12:40:22 +00:00
|
|
|
($page = (new PageModel())->where('slug', $params[0])->first()) === null
|
2021-05-06 14:00:48 +00:00
|
|
|
) {
|
2021-06-09 12:40:22 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
$this->page = $page;
|
|
|
|
|
|
|
|
return $this->{$method}();
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function index(): string
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
$cacheName = "page-{$this->page->slug}";
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-04-20 13:43:38 +00:00
|
|
|
$data = [
|
|
|
|
'page' => $this->page,
|
|
|
|
];
|
2020-08-18 16:31:28 +00:00
|
|
|
|
2021-04-20 13:43:38 +00:00
|
|
|
$found = view('page', $data);
|
|
|
|
|
|
|
|
// The page cache is set to a decade so it is deleted manually upon page update
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-02-10 16:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|