Skip to content

Commit

Permalink
General improvements for running in Docker env
Browse files Browse the repository at this point in the history
Signed-off-by: Joey Smith <jsmith@webinertia.net>

Signed-off-by: Joey Smith <jsmith@webinertia.net>
  • Loading branch information
tyrsson committed Jul 10, 2023
1 parent d854934 commit a61b268
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 56 deletions.
54 changes: 54 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Webinertia\Session;

use Laminas\Session\Config\ConfigInterface;
use Laminas\Session\Container as DefaultContainer;
use Laminas\Session\SaveHandler\SaveHandlerInterface;
use Laminas\Session\Service\SessionConfigFactory;
use Laminas\Session\Storage\SessionArrayStorage;
use Laminas\Session\ManagerInterface;
use Laminas\Session\Validator\HttpUserAgent;
use Laminas\Session\Validator\RemoteAddr;
use Webinertia\Session;

return [
'session_config' => [
'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
];
14 changes: 7 additions & 7 deletions src/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 6 additions & 3 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 3 additions & 5 deletions src/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
11 changes: 2 additions & 9 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
}
22 changes: 4 additions & 18 deletions src/SaveHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
);
}
}
2 changes: 0 additions & 2 deletions src/SessionContainerAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Webinertia\Session;

use Webinertia\Session\Container;

interface SessionContainerAwareInterface
{
public function setSessionContainer(Container $container);
Expand Down
9 changes: 4 additions & 5 deletions src/SessionContainerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
22 changes: 15 additions & 7 deletions src/SessionManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

0 comments on commit a61b268

Please sign in to comment.