aboutsummaryrefslogtreecommitdiff
path: root/app/class/Controller.php
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2019-11-04 23:31:31 +0100
committern-peugnet <n.peugnet@free.fr>2019-11-05 19:06:40 +0100
commite802d5204b96d645ec3d40b81b4a8bdc6e0ee675 (patch)
tree8e6db5e36ad8f247b442583e1e9e5da2934f4b52 /app/class/Controller.php
parentf1f63f556c41c99d45cd610186b0982383eff375 (diff)
downloadwcms-e802d5204b96d645ec3d40b81b4a8bdc6e0ee675.tar.gz
wcms-e802d5204b96d645ec3d40b81b4a8bdc6e0ee675.zip
refactor: switch to psr-4 autoloading
Diffstat (limited to 'app/class/Controller.php')
-rw-r--r--app/class/Controller.php105
1 files changed, 105 insertions, 0 deletions
diff --git a/app/class/Controller.php b/app/class/Controller.php
new file mode 100644
index 0000000..32ffc13
--- /dev/null
+++ b/app/class/Controller.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace Wcms;
+
+use DateTimeImmutable;
+use League\Plates\Engine;
+
+class Controller
+{
+ /** @var User */
+ protected $user;
+
+ /** @var Routes */
+ protected $router;
+
+ /** @var Modeluser */
+ protected $usermanager;
+
+ /** @var Modelpage */
+ protected $pagemanager;
+
+ protected $plates;
+
+ /** @var DateTimeImmutable */
+ protected $now;
+
+ public function __construct($router) {
+ $this->setuser();
+ $this->router = $router;
+ $this->pagemanager = new Modelpage();
+ $this->initplates();
+ $this->now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+ }
+
+ public function setuser()
+ {
+ $this->usermanager = new Modeluser;
+ $this->user = $this->usermanager->readsession();
+ }
+
+ public function initplates()
+ {
+ $router = $this->router;
+ $this->plates = new Engine(Model::TEMPLATES_DIR);
+ $this->plates->registerFunction('url', function (string $string, array $vars = []) use ($router) {
+ return $router->generate($string, $vars);
+ });
+ $this->plates->registerFunction('upage', function (string $string, string $id) use ($router) {
+ return $router->generate($string, ['page' => $id]);
+ });
+ }
+
+ public function showtemplate($template, $params)
+ {
+ $params = array_merge($this->commonsparams(), $params);
+ echo $this->plates->render($template, $params);
+ }
+
+ public function commonsparams()
+ {
+ $commonsparams = [];
+ $commonsparams['router'] = $this->router;
+ $commonsparams['user'] = $this->user;
+ $commonsparams['pagelist'] = $this->pagemanager->list();
+ $commonsparams['css'] = Model::csspath();
+ return $commonsparams;
+ }
+
+
+
+
+
+ public function redirect($url)
+ {
+ header('Location: ' . $url);
+ }
+
+ public function routedirect(string $route, array $vars = [])
+ {
+ $this->redirect($this->router->generate($route, $vars));
+ }
+
+ public function routedirectget(string $route, array $vars = [])
+ {
+ $get = '?';
+ foreach ($vars as $key => $value) {
+ $get .= $key .'='. $value. '&';
+ }
+ $get = rtrim($get, '&');
+ $this->redirect($this->router->generate($route, []) . $get);
+ }
+
+ public function error(int $code)
+ {
+ http_response_code($code);
+ exit;
+ }
+
+}
+
+
+
+
+
+?> \ No newline at end of file