diff --git a/config/module.config.php b/config/module.config.php new file mode 100644 index 0000000..2ec433a --- /dev/null +++ b/config/module.config.php @@ -0,0 +1,54 @@ + [ + 'use_cookies' => true, // I mean who doesn't like cookies? + 'gc_maxlifetime' => 86400, + 'remember_me_seconds' => 86400, // Can be safely set or changed after the session has been started + 'cookie_httponly' => true, // Example only + 'cookie_secure' => false, // Example only + ], + 'session_manager' => [ + 'validators' => [ + //RemoteAddr::class, + HttpUserAgent::class, + ], + ], + 'session_containers' => [ + //Session\Container::class, + //'App_Context', + ], + 'session_storage' => [ + 'type' => SessionArrayStorage::class, // if youre Unit, Integration testing you may want to set this to ArrayStorage + ], + 'service_manager' => [ + 'factories' => [ + ConfigInterface::class => Session\ConfigFactory::class, + Session\Container::class => Session\ContainerFactory::class, + ManagerInterface::class => Session\SessionManagerFactory::class, + SaveHandlerInterface::class => SaveHandlerFactory::class, // <- uncomment this if you want to use db driven sessions + ], + ], + 'session_save_handler_options' => [ // option to db table column map + 'idColumn' => 'id', + 'nameColumn' => 'name', + 'modifiedColumn' => 'modified', + 'lifetimeColumn' => 'lifetime', + 'dataColumn' => 'data', + ], + 'session_table_name' => 'session', // db table name for the save handler to write too +]; \ No newline at end of file diff --git a/src/ConfigFactory.php b/src/ConfigFactory.php index fcbec89..ed331d2 100644 --- a/src/ConfigFactory.php +++ b/src/ConfigFactory.php @@ -6,16 +6,16 @@ use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\Session\Config\SessionConfig; +use Laminas\Session\Config\StandardConfig; use Psr\Container\ContainerInterface; -class ConfigFactory implements FactoryInterface +final class ConfigFactory implements FactoryInterface { - /** @param string $requestedName */ - public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): SessionConfig + public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): StandardConfig | SessionConfig { - $config = $container->get('config')['session_config'] ?? []; - $class = new SessionConfig(); - $class->setOptions($config); - return $class; + $config = $container->get('config')['session_config'] ?? []; + $instance = new SessionConfig(); + $instance->setOptions($config); + return $instance; } } diff --git a/src/Container.php b/src/Container.php index 9108a6a..64f0c9a 100644 --- a/src/Container.php +++ b/src/Container.php @@ -5,9 +5,12 @@ namespace Webinertia\Session; use Laminas\Session\AbstractContainer; +use Laminas\Session\ManagerInterface as Manager; -class Container extends AbstractContainer +final class Container extends AbstractContainer { - /** @var string $context */ - protected $context; + public function __construct($name = 'App_Context', ?Manager $manager = null) + { + parent::__construct($name, $manager); + } } diff --git a/src/ContainerFactory.php b/src/ContainerFactory.php index b15a638..4e119d9 100644 --- a/src/ContainerFactory.php +++ b/src/ContainerFactory.php @@ -4,16 +4,14 @@ namespace Webinertia\Session; -use Webinertia\Session\Container; use Laminas\ServiceManager\Factory\FactoryInterface; -use Laminas\Session\SessionManager; +use Laminas\Session\ManagerInterface; use Psr\Container\ContainerInterface; -class ContainerFactory implements FactoryInterface +final class ContainerFactory implements FactoryInterface { - /** @param string $requestedName*/ public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Container { - return new $requestedName('App_Context', $container->get(SessionManager::class)); + return new $requestedName('App_Context', $container->get(ManagerInterface::class)); } } diff --git a/src/Module.php b/src/Module.php index 83b87e7..d9ef7d1 100644 --- a/src/Module.php +++ b/src/Module.php @@ -8,13 +8,6 @@ final class Module { public function getConfig(): array { - $configProvider = new ConfigProvider(); - return [ - 'service_manager' => $configProvider->getDependencyConfig(), - 'session_config' => $configProvider->getSessionConfig(), - 'session_containers' => $configProvider->getSessionContainerConfig(), - 'session_storage' => $configProvider->getSessionStorageConfig(), - 'session_validators' => $configProvider->getSessionValidatorConfig(), - ]; + return include __DIR__ . '/../config/module.config.php'; } -} \ No newline at end of file +} diff --git a/src/SaveHandlerFactory.php b/src/SaveHandlerFactory.php index a520744..8095f45 100644 --- a/src/SaveHandlerFactory.php +++ b/src/SaveHandlerFactory.php @@ -9,32 +9,18 @@ use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\Session\SaveHandler\DbTableGateway; use Laminas\Session\SaveHandler\DbTableGatewayOptions; -use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; -use Psr\Container\NotFoundExceptionInterface; final class SaveHandlerFactory implements FactoryInterface { - /** - * @param string $requestedName - * @param null|mixed[] $options - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface - */ + /** @inheritdoc */ public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): DbTableGateway { - $config = $container->get('config')['db']; // db options - $dbOptions = [ - 'idColumn' => 'id', - 'nameColumn' => 'name', - 'modifiedColumn' => 'modified', - 'lifetimeColumn' => 'lifetime', - 'dataColumn' => 'data', - ]; + $config = $container->get('config'); return new DbTableGateway( - new TableGateway($config['sessions_table_name'], $container->get(AdapterInterface::class)), - new DbTableGatewayOptions($dbOptions) + new TableGateway($config['session_table_name'], $container->get(AdapterInterface::class)), + new DbTableGatewayOptions($config['session_save_handler_options']) ); } } diff --git a/src/SessionContainerAwareInterface.php b/src/SessionContainerAwareInterface.php index a7a7a0a..9c27044 100644 --- a/src/SessionContainerAwareInterface.php +++ b/src/SessionContainerAwareInterface.php @@ -4,8 +4,6 @@ namespace Webinertia\Session; -use Webinertia\Session\Container; - interface SessionContainerAwareInterface { public function setSessionContainer(Container $container); diff --git a/src/SessionContainerAwareTrait.php b/src/SessionContainerAwareTrait.php index b1283c3..c886e6a 100644 --- a/src/SessionContainerAwareTrait.php +++ b/src/SessionContainerAwareTrait.php @@ -5,20 +5,19 @@ namespace Webinertia\Session; use Laminas\Session\Container as LaminasContainer; -use Webinertia\Session\Container; trait SessionContainerAwareTrait { /** @var Container|LaminasContainer $sessionContainer */ - protected $sessionContainer; + protected $session; public function setSessionContainer(Container|LaminasContainer $container) { - $this->sessionContainer = $container; + $this->session = $container; } public function getSessionContainer(): Container { - return $this->sessionContainer; + return $this->session; } -} \ No newline at end of file +} diff --git a/src/SessionManagerFactory.php b/src/SessionManagerFactory.php index c243352..4e48456 100644 --- a/src/SessionManagerFactory.php +++ b/src/SessionManagerFactory.php @@ -7,21 +7,29 @@ use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\Session\Config\ConfigInterface; use Laminas\Session\Config\SessionConfig; +use Laminas\Session\SaveHandler\SaveHandlerInterface; use Laminas\Session\SessionManager; +use Laminas\Session\Storage\StorageInterface; use Psr\Container\ContainerInterface; -class SessionManagerFactory implements FactoryInterface +final class SessionManagerFactory implements FactoryInterface { - /** @param string $requestedName */ public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): SessionManager { - // $config = $container->has('config') ? $container->get('config') : []; - // $config = $config['session_config'] ?? []; + $config = $container->has('config') ? $container->get('config') : []; + $sessionManagerConfig = $config['session_manager']; + $validators = $sessionManagerConfig['validators'] ?? []; + $options = $sessionManagerConfig['options'] ?? []; + $config = $config['session_config'] ?? []; - // $sessionConfig = ! empty($config['config_class']) ? new $config['config_class']() : new SessionConfig(); - // $sessionConfig->setOptions($config); + $sessionConfig = ! empty($config['config_class']) ? new $config['config_class']() : new SessionConfig(); + $sessionConfig->setOptions($config); return new SessionManager( - $container->has(ConfigInterface::class) ? $container->get(ConfigInterface::class) : new SessionConfig() + $container->has(ConfigInterface::class) ? $container->get(ConfigInterface::class) : $sessionConfig, + $container->has(StorageInterface::class) ? $container->get(StorageInterface::class) : null, + $container->has(SaveHandlerInterface::class) ? $container->get(SaveHandlerInterface::class) : null, + $validators, + $options ); } }