Skip to content

Commit

Permalink
Initial migration commit
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 Jun 16, 2023
1 parent 134b379 commit c510e00
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 0 deletions.
71 changes: 71 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "webinertia/webinertia-session",
"description": "Provides concrete factories for Laminas session",
"license": "BSD-3-Clause",
"keywords": [
"webinertia",
"webinertia-mvc",
"laminas",
"laminas-mvc"
],
"homepage": "https://github.com/webinertia/webinertia-session/discussions",
"authors": [
{
"name": "Joey Smith",
"email": "jsmith@webinertia.net",
"homepage": "https://webinertia.net/"
}
],
"suggest": {
"laminas/laminas-component-installer" : "^3.0, required for configuration injection"
},
"require": {
"php": "~8.1.0 || ~8.2.0",
"laminas/laminas-session": "^2.16.0"
},
"require-dev": {
"laminas/laminas-coding-standard": "^2.4.0",
"phpunit/phpunit": "10.2.2",
"psalm/plugin-phpunit": "^0.18.4",
"phpstan/phpstan": "1.10.19",
"laminas/laminas-test": "^4.8",
"phpstan/extension-installer": "^1.2",
"slam/phpstan-laminas-framework": "^1.4"
},
"extra": {
"laminas": {
"component": "Webinertia\\Session",
"config-provider": "Webinertia\\Session\\ConfigProvider"
}
},
"autoload": {
"psr-4": {
"Webinertia\\Session\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"WebinertiaTest\\SessionTest\\": "test/"
}
},
"scripts": {
"check": [
"@cs-check",
"@phpstan",
"@test"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"static-analysis": "psalm --shepherd --stats",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"phpstan": "php vendor/bin/phpstan analyse --xdebug"
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
}
}
6 changes: 6 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"local>webinertia/webinertia:renovate-config"
]
}
21 changes: 21 additions & 0 deletions src/ConfigFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Webinertia\Session;

use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\Session\Config\SessionConfig;
use Psr\Container\ContainerInterface;

class ConfigFactory implements FactoryInterface
{
/** @param string $requestedName */
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): SessionConfig
{
$config = $container->get('config')['session_config'] ?? [];
$class = $requestedName();
$class->setOptions($config);
return $class;
}
}
51 changes: 51 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Webinertia\Session;

use Laminas\Session\Config\ConfigInterface;
use Laminas\Session\ManagerInterface;
use Laminas\Session\Storage\SessionArrayStorage;
use Laminas\Session\Validator\RemoteAddr;
use Laminas\Session\Validator\HttpUserAgent;

final class ConfigProvider
{
public function getDependencyConfig(): array
{
return [
'factories' => [
ConfigInterface::class => ConfigFactory::class,
ManagerInterface::class => SessionManagerFactory::class,
],
];
}

public function getSessionConfig(): array
{
return [
'use_cookies' => true,
];
}

public function getSessionContainerConfig(): array
{
return [Container::class];
}

public function getSessionStorageConfig(): array
{
return [
'type' => SessionArrayStorage::class,
];
}

public function getSessionValidatorConfig(): array
{
return [
RemoteAddr::class,
HttpUserAgent::class,
];
}
}
13 changes: 13 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Webinertia\Session;

use Laminas\Session\AbstractContainer;

class Container extends AbstractContainer
{
/** @var string $context */
protected $context;
}
19 changes: 19 additions & 0 deletions src/ContainerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Webinertia\Session;

use Webinertia\Session\Container;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\Session\SessionManager;
use Psr\Container\ContainerInterface;

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));
}
}
20 changes: 20 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Webinertia\Session;

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(),
];
}
}
40 changes: 40 additions & 0 deletions src/SaveHandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Webinertia\Session;

use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\TableGateway\TableGateway;
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
*/
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',
];
return new DbTableGateway(
new TableGateway($config['sessions_table_name'], $container->get(AdapterInterface::class)),
new DbTableGatewayOptions($dbOptions)
);
}
}
14 changes: 14 additions & 0 deletions src/SessionContainerAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Webinertia\Session;

use Webinertia\Session\Container;

interface SessionContainerAwareInterface
{
public function setSessionContainer(Container $container);

public function getSessionContainer(): Container;
}
23 changes: 23 additions & 0 deletions src/SessionContainerAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Webinertia\Session;

use Webinertia\Session\Container;

trait SessionContainerAwareTrait
{
/** @var Container $sessionContainer */
protected $sessionContainer;

public function setSessionContainer(Container $container)
{
$this->sessionContainer = $container;
}

public function getSessionContainer(): Container
{
return $this->sessionContainer;
}
}
27 changes: 27 additions & 0 deletions src/SessionManagerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Webinertia\Session;

use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\Session\Config\ConfigInterface;
use Laminas\Session\Config\SessionConfig;
use Laminas\Session\SessionManager;
use Psr\Container\ContainerInterface;

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'] ?? [];

// $sessionConfig = ! empty($config['config_class']) ? new $config['config_class']() : new SessionConfig();
// $sessionConfig->setOptions($config);
return new $requestedName(
$container->has(ConfigInterface::class) ? $container->get(ConfigInterface::class) : new SessionConfig()
);
}
}

0 comments on commit c510e00

Please sign in to comment.