Created
September 16, 2015 10:39
-
-
Save juice49/e2f13c52550e5e90c80f to your computer and use it in GitHub Desktop.
ZF2 Flash Data Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller\Plugin; | |
use Zend\Mvc\Controller\Plugin\AbstractPlugin; | |
use Zend\Session\Container; | |
use Zend\Session\ManagerInterface as Manager; | |
class Flash extends AbstractPlugin { | |
protected $container; | |
protected $session; | |
const GET_SYMBOL = '__flash-plugin-get-data'; | |
public function __invoke($key = null, $value = self::GET_SYMBOL) { | |
if(!$key) { | |
return $this; | |
} | |
return $value === self::GET_SYMBOL ? | |
$this->get($key) : | |
$this->set($key, $value); | |
} | |
public function get($key) { | |
return $this->getContainer()->$key; | |
} | |
public function set($key, $value) { | |
$container = $this->getContainer(); | |
$container->setExpirationHops(1, null); | |
$container->$key = $value; | |
return $this; | |
} | |
protected function getSessionManager() { | |
if(!$this->session instanceof Manager) { | |
$this->session = Container::getDefaultManager(); | |
} | |
return $this->session; | |
} | |
protected function getContainer() { | |
if($this->container instanceof Container) { | |
return $this->container; | |
} | |
$manager = $this->getSessionManager(); | |
$this->container = new Container('Flash', $manager); | |
return $this->container; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment