|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright: DotKernel |
| 4 | + * @library: dot-controller-plugin-session |
| 5 | + * @author: n3vrax |
| 6 | + * Date: 2/3/2017 |
| 7 | + * Time: 12:30 AM |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types = 1); |
| 11 | + |
| 12 | +namespace Dot\Controller\Plugin\Session; |
| 13 | + |
| 14 | +use Dot\Controller\Plugin\PluginInterface; |
| 15 | +use Interop\Container\ContainerInterface; |
| 16 | +use Zend\Session\Container; |
| 17 | +use Zend\Session\ManagerInterface; |
| 18 | +use Zend\Session\SessionManager; |
| 19 | + |
| 20 | +/** |
| 21 | + * Class SessionPlugin |
| 22 | + * @package Dot\Controller\Plugin\Session |
| 23 | + */ |
| 24 | +class SessionPlugin implements PluginInterface |
| 25 | +{ |
| 26 | + /** @var ContainerInterface **/ |
| 27 | + protected $container; |
| 28 | + |
| 29 | + /** @var ManagerInterface */ |
| 30 | + protected $sessionManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * SessionPlugin constructor. |
| 34 | + * @param ContainerInterface $container |
| 35 | + */ |
| 36 | + public function __construct(ContainerInterface $container) |
| 37 | + { |
| 38 | + $this->container = $container; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param string $name |
| 43 | + * @return Container |
| 44 | + */ |
| 45 | + public function __invoke(string $name): Container |
| 46 | + { |
| 47 | + $fullName = 'dot-session.' . $name; |
| 48 | + |
| 49 | + $session = null; |
| 50 | + if ($this->container->has($fullName)) { |
| 51 | + $session = $this->container->get($fullName); |
| 52 | + } |
| 53 | + |
| 54 | + if (!$session instanceof Container) { |
| 55 | + $session = new Container($name, $this->getSessionManager()); |
| 56 | + } |
| 57 | + |
| 58 | + return $session; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return ManagerInterface |
| 63 | + */ |
| 64 | + protected function getSessionManager(): ManagerInterface |
| 65 | + { |
| 66 | + if ($this->sessionManager) { |
| 67 | + return $this->sessionManager; |
| 68 | + } |
| 69 | + |
| 70 | + if ($this->container->has(ManagerInterface::class)) { |
| 71 | + $this->sessionManager = $this->container->get(ManagerInterface::class); |
| 72 | + } |
| 73 | + |
| 74 | + if (!$this->sessionManager instanceof ManagerInterface) { |
| 75 | + $this->sessionManager = new SessionManager(); |
| 76 | + } |
| 77 | + |
| 78 | + return $this->sessionManager; |
| 79 | + } |
| 80 | +} |
0 commit comments