diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Controller.php | 21 | ||||
-rw-r--r-- | src/Flash.php | 109 | ||||
-rw-r--r-- | src/View/Twig.php | 11 |
3 files changed, 137 insertions, 4 deletions
diff --git a/src/Controller.php b/src/Controller.php index ddec2c0..f7ea6f3 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -23,6 +23,12 @@ abstract class Controller protected $response = null; /** + * Flash + * @var Flash + **/ + protected $flash = null; + + /** * Run the controller * * @return ResponseInterface @@ -65,6 +71,21 @@ abstract class Controller } /** + * 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 * * @return boolean 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 index 6d0f6ae..ee5d4ce 100644 --- a/src/View/Twig.php +++ b/src/View/Twig.php @@ -2,6 +2,7 @@ namespace Jasny\Controller\View; +use Jasny\Flash; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; @@ -47,8 +48,8 @@ trait Twig /** * Expose a function to the view. * - * @param string $function Variable name - * @param mixed $callback + * @param string $name Variable name + * @param mixed $function * @param string $as 'function' or 'filter' * @return $this */ @@ -119,7 +120,9 @@ trait Twig } $uri = $this->getRequest()->getUri()->getPath(); + $this->setViewVariable('current_url', $uri); + $this->setViewVariable('flash', new Flash()); return $this->twig; } @@ -149,7 +152,7 @@ trait Twig * Create twig function * * @param string $name Name of function in view - * @param callable $function + * @param callable|null $function * @return \Twig_SimpleFunction */ public function createTwigFunction($name, $function) @@ -163,7 +166,7 @@ trait Twig * Create twig filter * * @param string $name Name of filter in view - * @param callable $function + * @param callable|null $function * @return \Twig_SimpleFilter */ public function createTwigFilter($name, $function) |