summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorArnold Daniels <arnold@jasny.net>2016-11-17 16:17:55 +0100
committerArnold Daniels <arnold@jasny.net>2016-11-17 16:17:55 +0100
commit7149ef62ca11d451dadd271f5cb63233a4ac0748 (patch)
treef141b8c05d75c5b86819edb63213ec8b93e281bd /src
parent14bc7de53fda2c44482c7cf46b896cfdd41aa68d (diff)
parentb8540d92944123d15559f9033f523f9f6c5126ec (diff)
downloadcontroller-7149ef62ca11d451dadd271f5cb63233a4ac0748.zip
controller-7149ef62ca11d451dadd271f5cb63233a4ac0748.tar.gz
controller-7149ef62ca11d451dadd271f5cb63233a4ac0748.tar.bz2
Merge branch 'Flash' of https://github.com/Minstel/controller into Minstel-Flash
Diffstat (limited to 'src')
-rw-r--r--src/Controller.php22
-rw-r--r--src/Flash.php109
-rw-r--r--src/View/Twig.php178
3 files changed, 308 insertions, 1 deletions
diff --git a/src/Controller.php b/src/Controller.php
index 057653e..9f69160 100644
--- a/src/Controller.php
+++ b/src/Controller.php
@@ -41,6 +41,12 @@ abstract class Controller
'application/x-www-form-urlencoded' => 'post',
'multipart/form-data' => 'post'
];
+
+ /**
+ * Flash
+ * @var Flash
+ */
+ protected $flash = null;
/**
* Run the controller
@@ -282,6 +288,21 @@ abstract class Controller
return $errorResponse;
}
+
+ /**
+ * Set the flash message and/or return the flash object.
+ *
+ * @param mixed $type flash type, eg. 'error', 'notice' or 'success'
+ * @param mixed $message flash message
+ * @return Flash
+ */
+ public function flash($type = null, $message = null)
+ {
+ if (!isset($this->flash)) $this->flash = new Flash();
+ if ($type && $message) $this->flash->set($type, $message);
+
+ return $this->flash;
+ }
/**
* Check if response is 2xx succesful, or empty
@@ -516,4 +537,3 @@ abstract class Controller
return $request ? $request->getMethod() : '';
}
}
-
diff --git a/src/Flash.php b/src/Flash.php
new file mode 100644
index 0000000..ad060f9
--- /dev/null
+++ b/src/Flash.php
@@ -0,0 +1,109 @@
+<?php
+
+namespace Jasny;
+
+/**
+ * Class for the flash message
+ */
+class Flash
+{
+ /**
+ * @var object
+ */
+ protected static $data = null;
+
+ /**
+ * Check if the flash is set.
+ *
+ * @return boolean
+ */
+ public static function isIssued()
+ {
+ return isset($_SESSION['flash']) || isset(static::$data);
+ }
+
+ /**
+ * Set the flash.
+ *
+ * @param mixed $type flash type, eg. 'error', 'notice' or 'success'
+ * @param mixed $message flash message
+ */
+ public static function set($type, $message)
+ {
+ if (!$type) {
+ throw new \InvalidArgumentException("Type should not be empty");
+ }
+
+ static::$data = (object)['type'=>$type, 'message'=>$message];
+
+ $_SESSION['flash'] = static::$data;
+ }
+
+ /**
+ * Get the flash.
+ *
+ * @return object
+ */
+ public static function get()
+ {
+ if (!isset(static::$data) && isset($_SESSION['flash'])) {
+ static::$data = (object)$_SESSION['flash'];
+ unset($_SESSION['flash']);
+ }
+
+ return static::$data;
+ }
+
+ /**
+ * Reissue the flash.
+ */
+ public static function reissue()
+ {
+ if (!isset(static::$data) && isset($_SESSION['flash'])) {
+ static::$data = (object)$_SESSION['flash'];
+ } else {
+ $_SESSION['flash'] = static::$data;
+ }
+ }
+
+ /**
+ * Clear the flash.
+ */
+ public static function clear()
+ {
+ self::$data = null;
+ unset($_SESSION['flash']);
+ }
+
+ /**
+ * Get the flash type
+ *
+ * @return string
+ */
+ public static function getType()
+ {
+ $data = static::get();
+ return isset($data) ? $data->type : null;
+ }
+
+ /**
+ * Get the flash message
+ *
+ * @return string
+ */
+ public static function getMessage()
+ {
+ $data = static::get();
+ return isset($data) ? $data->message : null;
+ }
+
+ /**
+ * Cast object to string
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return (string)$this->getMessage();
+ }
+}
diff --git a/src/View/Twig.php b/src/View/Twig.php
new file mode 100644
index 0000000..ee5d4ce
--- /dev/null
+++ b/src/View/Twig.php
@@ -0,0 +1,178 @@
+<?php
+
+namespace Jasny\Controller\View;
+
+use Jasny\Flash;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Message\ResponseInterface;
+
+/**
+ * View using Twig
+ */
+trait Twig
+{
+ /**
+ * Twig environment
+ * @var \Twig_Environment
+ */
+ protected $twig = null;
+
+ /**
+ * Get server request
+ * @return ServerRequestInterface
+ */
+ abstract public function getRequest();
+
+ /**
+ * Get server response
+ * @return ResponseInterface
+ */
+ abstract public function getResponse();
+
+ /**
+ * Add a global variable to the view.
+ *
+ * @param string $name Variable name
+ * @param mixed $value
+ * @return $this
+ */
+ public function setViewVariable($name, $value)
+ {
+ if (!$name) throw new \InvalidArgumentException("Name should not be empty");
+
+ $this->getTwig()->addGlobal($name, $value);
+
+ return $this;
+ }
+
+ /**
+ * Expose a function to the view.
+ *
+ * @param string $name Variable name
+ * @param mixed $function
+ * @param string $as 'function' or 'filter'
+ * @return $this
+ */
+ public function setViewFunction($name, $function = null, $as = 'function')
+ {
+ if ($as === 'function') {
+ $this->getTwig()->addFunction($this->createTwigFunction($name, $function));
+ } elseif ($as === 'filter') {
+ $this->getTwig()->addFilter($this->createTwigFilter($name, $function));
+ } else {
+ throw new \InvalidArgumentException("You should create either function or filter, not '$as'");
+ }
+
+ return $this;
+ }
+
+ /**
+ * Add extension to view
+ *
+ * @param object $extension
+ * @return $this
+ */
+ public function setViewExtension($extension)
+ {
+ $this->getTwig()->addExtension($extension);
+
+ return $this;
+ }
+
+ /**
+ * View rendered template
+ *
+ * @param string $name Template name
+ * @param array $context Template context
+ * @return ResponseInterface
+ */
+ public function view($name, array $context = [])
+ {
+ if (!pathinfo($name, PATHINFO_EXTENSION)) $name .= '.html.twig';
+
+ $twig = $this->getTwig();
+ $tmpl = $twig->loadTemplate($name);
+
+ $response = $this->getResponse();
+ $response = $response->withHeader('Content-Type', 'text/html; charset=' . $twig->getCharset());
+ $response->getBody()->write($tmpl->render($context));
+
+ return $response;
+ }
+
+ /**
+ * Get twig environment
+ *
+ * @return \Twig_Environment
+ */
+ public function getTwig()
+ {
+ if ($this->twig) return $this->twig;
+
+ $loader = $this->getTwigLoader();
+ $this->twig = $this->getTwigEnvironment($loader);
+
+ $extensions = ['DateExtension', 'PcreExtension', 'TextExtension', 'ArrayExtension'];
+ foreach ($extensions as $name) {
+ $class = "Jasny\Twig\\$name";
+
+ if (class_exists($class)) $this->setViewExtension(new $class());
+ }
+
+ $uri = $this->getRequest()->getUri()->getPath();
+
+ $this->setViewVariable('current_url', $uri);
+ $this->setViewVariable('flash', new Flash());
+
+ return $this->twig;
+ }
+
+ /**
+ * Get twig loasder for current working directory
+ *
+ * @return \Twig_Loader_Filesystem
+ */
+ public function getTwigLoader()
+ {
+ return new \Twig_Loader_Filesystem(getcwd());
+ }
+
+ /**
+ * Get twig environment instance
+ *
+ * @param \Twig_Loader_Filesystem $loader
+ * @return \Twig_Environment
+ */
+ public function getTwigEnvironment(\Twig_Loader_Filesystem $loader)
+ {
+ return new \Twig_Environment($loader);
+ }
+
+ /**
+ * Create twig function
+ *
+ * @param string $name Name of function in view
+ * @param callable|null $function
+ * @return \Twig_SimpleFunction
+ */
+ public function createTwigFunction($name, $function)
+ {
+ if (!$name) throw new \InvalidArgumentException("Function name should not be empty");
+
+ return new \Twig_SimpleFunction($name, $function ?: $name);
+ }
+
+ /**
+ * Create twig filter
+ *
+ * @param string $name Name of filter in view
+ * @param callable|null $function
+ * @return \Twig_SimpleFilter
+ */
+ public function createTwigFilter($name, $function)
+ {
+ if (!$name) throw new \InvalidArgumentException("Filter name should not be empty");
+
+ return new \Twig_SimpleFilter($name, $function ?: $name);
+ }
+}