diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 88ee7cf..0000000 --- a/.travis.yml +++ /dev/null @@ -1,19 +0,0 @@ -language: php - -php: - - 7.2 - - 7.3 - -dist: xenial - -cache: - directories: - - $HOME/.composer/cache - -before_script: - - composer self-update - - composer update --no-interaction --prefer-dist --no-progress - - cd $TRAVIS_BUILD_DIR - -script: - - composer check-all diff --git a/LICENSE b/LICENSE index 30d7b83..fa19b85 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2019 odan +Copyright (c) 2021 odan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 667994c..b2e277c 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,11 @@ A session handler for PHP [![Build Status](https://travis-ci.org/odan/session.svg?branch=master)](https://travis-ci.org/odan/session) [![Code Coverage](https://scrutinizer-ci.com/g/odan/session/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/odan/session/?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/odan/session/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/odan/session/?branch=master) -[![Total Downloads](https://img.shields.io/packagist/dt/odan/session.svg)](https://packagist.org/packages/odan/session) +[![Total Downloads](https://img.shields.io/packagist/dt/odan/session.svg)](https://packagist.org/packages/odan/session/stats) ## Requirements -* PHP 7.2+ +* PHP 7.2+ or 8.0+ ## Installation @@ -24,7 +24,7 @@ composer require odan/session ```php use Odan\Session\PhpSession; -// Set session options before we start +// Set session options before you start the session // You can use all the standard PHP session configuration options // https://secure.php.net/manual/en/session.configuration.php @@ -81,7 +81,7 @@ $session->clear(); // Generate a new session ID $session->regenerateId(); -// Clears all session data and regenerates session ID +// Clears all session $session->destroy(); // Get the current session ID @@ -155,14 +155,15 @@ $session = new MemorySession(); #### Configuration -Add your application-specific settings. - -In this example we store all settings in a PHP file. +Add your application-specific settings: ```php // config/settings.php return [ + + // ... + 'session' => [ 'name' => 'webapp', 'cache_expire' => 0, @@ -172,89 +173,64 @@ return [ ]; ``` -For this example we use the [leage/container](https://github.com/thephpleague/container) package. +For this example we use the [PHP-DI](http://php-di.org/) package. -Add the PSR-15 middleware factory: +Add the container definitions as follows: ```php -// config/container.php +delegate(new ReflectionContainer()); - -$container->share('settings', static function () { - return require __DIR__ . '/settings.php'; -}); +return [ + // ... -$container->share(SessionInterface::class, static function (Container $container) { - $settings = $container->get('settings'); - $session = new PhpSession(); - $session->setOptions((array)$settings['session']); + SessionInterface::class => function (ContainerInterface $container) { + $settings = $container->get('settings'); + $session = new PhpSession(); + $session->setOptions((array)$settings['session']); - return $session; -})->addArgument($container); + return $session; + }, -$container->share(SessionMiddleware::class, static function (Container $container) { - return new SessionMiddleware($container->get(SessionInterface::class)); -})->addArgument($container); + SessionMiddleware::class => function (ContainerInterface $container) { + return new SessionMiddleware($container->get(SessionInterface::class)); + }, +]; ``` ##### Registering middleware routes -For this example we use the [league/route](https://github.com/thephpleague/container) package. - Register middleware for all routes: ```php -// config/middleware.php - -use League\Route\Router; use Odan\Session\SessionMiddleware; -$router = $container->get(Router::class); - -$router->lazyMiddleware(SessionMiddleware::class); - -return $router; +$app->add(SessionMiddleware::class); ``` Register middleware for a routing group: ```php -// config/routes.php - -use League\Route\Router; use Odan\Session\SessionMiddleware; +use Slim\Routing\RouteCollectorProxy; -$router = $container->get(Router::class); - -$router->group('/users', static function (RouteGroup $group): void { - $group->post('/login', \App\Action\UserLoginSubmitAction::class); -})->lazyMiddleware(SessionMiddleware::class); - -return $router; +// Protect the whole group +$app->group('/admin', function (RouteCollectorProxy $group) { + // ... +})->add(SessionMiddleware::class); ``` Register middleware for a single route: ```php -// config/routes.php - -use League\Route\Router; use Odan\Session\SessionMiddleware; -$router = $container->get(Router::class); - -$router->get('/users', \App\Action\HomeIndexAction::class) - ->lazyMiddleware(SessionMiddleware::class); - -return $router; +$app->post('/example', \App\Action\ExampleAction::class) + ->add(SessionMiddleware::class); ``` ### Slim 3 framework integration @@ -280,7 +256,6 @@ Add the session factory: ```php use Odan\Session\PhpSession; use Odan\Session\SessionInterface; -use Odan\Session\SessionMiddleware; use Psr\Container\ContainerInterface as Container; $container[SessionInterface::class] = function (Container $container) { @@ -335,5 +310,9 @@ $app->group('/users', function () { ## Similar packages +* https://github.com/laminas/laminas-session * https://symfony.com/doc/current/components/http_foundation/sessions.html -* https://github.com/auraphp/Aura.Session + +## License + +The MIT License (MIT). Please see [License File](LICENSE) for more information. diff --git a/composer.json b/composer.json index 572b66a..1be9b35 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "php": "^7.2", + "php": "^7.2 || ^8.0", "psr/http-message": "^1.0", "psr/http-server-handler": "^1.0", "psr/http-server-middleware": "^1.0" @@ -23,7 +23,7 @@ "require-dev": { "overtrue/phplint": "^1.1", "phpstan/phpstan-shim": "^0.11", - "phpunit/phpunit": "^8.0", + "phpunit/phpunit": "^8.0 || ^9.0", "slim/slim": "^3.12", "squizlabs/php_codesniffer": "^3.4" },