-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
351 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
language: php | ||
|
||
php: | ||
- 7.0 | ||
- 7.1 | ||
|
||
before_script: | ||
- travis_retry composer self-update | ||
- travis_retry composer install --no-interaction --prefer-source --dev | ||
|
||
script: | ||
- vendor/bin/phpunit --coverage-clover=coverage.xml | ||
|
||
after_success: | ||
- bash <(curl -s https://codecov.io/bash) | ||
|
||
notifications: | ||
email: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Daniel Wendrich | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"name": "dwendrich/expressive-session-middleware", | ||
"description": "Session handling middleware for use with zend expressive 2.0 based on zend-session.", | ||
"keywords": [ | ||
"session handling", | ||
"psr-7", | ||
"zend", | ||
"expressive", | ||
"middleware" | ||
], | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Daniel Wendrich", | ||
"email": "daniel.wendrich@gmail.com" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"SessionMiddleware\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"SessionMiddlewareTest\\": "test/" | ||
} | ||
}, | ||
"require": { | ||
"php": "^7.0", | ||
"zendframework/zend-session": "^2.7", | ||
"zendframework/zend-servicemanager": "^3.3", | ||
"psr/http-message": "^1.0", | ||
"http-interop/http-middleware": "^0.4.1" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^6.0.8", | ||
"squizlabs/php_codesniffer": "^2.8.1" | ||
}, | ||
"scripts": { | ||
"check": [ | ||
"@cs-check", | ||
"@test" | ||
], | ||
"cs-check": "phpcs", | ||
"cs-fix": "phpcbf", | ||
"test": "phpunit", | ||
"test-coverage": "phpunit --color=never --coverage-text" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/** | ||
* For further information on zend session configuration options have a look at: | ||
* @see https://docs.zendframework.com/zend-session/config/#standard-config | ||
*/ | ||
return [ | ||
'session' => [ | ||
'config' => [ | ||
'options' => [ | ||
'name' => 'my_session_name', | ||
'cookie_httponly' => true, | ||
'use_cookies' => true, | ||
], | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="Expressive Skeleton coding standard"> | ||
<description>Expressive Skeleton coding standard</description> | ||
|
||
<!-- display progress --> | ||
<arg value="p"/> | ||
|
||
<!-- inherit rules from: --> | ||
<rule ref="PSR2"/> | ||
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/> | ||
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"> | ||
<properties> | ||
<property name="ignoreBlankLines" value="false"/> | ||
</properties> | ||
</rule> | ||
|
||
<!-- Paths to check --> | ||
<file>src</file> | ||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true"> | ||
<testsuites> | ||
<testsuite name="SessionMiddleware\\Test"> | ||
<directory>./test</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">./src</directory> | ||
<exclude> | ||
<file>./src/ConfigProvider.php</file> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace SessionMiddleware; | ||
|
||
use Zend\Session; | ||
use Zend\Session\ConfigProvider as ZendSessionConfigProvider; | ||
|
||
/** | ||
* Class ConfigProvider | ||
* | ||
* @package SessionMiddleware | ||
* @author Daniel Wendrich <daniel.wendrich@gmail.com> | ||
*/ | ||
class ConfigProvider | ||
{ | ||
public function __invoke() | ||
{ | ||
return [ | ||
'dependencies' => $this->getZendSessionDependencies(), | ||
'session' => $this->getSessionConfig(), | ||
]; | ||
} | ||
|
||
public function getSessionConfig() | ||
{ | ||
return [ | ||
'config' => [ | ||
'class' => Session\Config\SessionConfig::class, | ||
'options' => [ | ||
'name' => 'my_app', | ||
], | ||
], | ||
'storage' => Session\Storage\SessionArrayStorage::class, | ||
'validators' => [ | ||
Session\Validator\RemoteAddr::class, | ||
Session\Validator\HttpUserAgent::class | ||
], | ||
]; | ||
} | ||
|
||
public function getZendSessionDependencies() | ||
{ | ||
return (new ZendSessionConfigProvider())->getDependencyConfig(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace SessionMiddleware\Factory; | ||
|
||
use Interop\Container\ContainerInterface; | ||
use Interop\Container\Exception\ContainerException; | ||
use Zend\ServiceManager\Factory\FactoryInterface; | ||
use Zend\ServiceManager\Exception\ServiceNotCreatedException; | ||
use Zend\ServiceManager\Exception\ServiceNotFoundException; | ||
use Zend\Session\SessionManager; | ||
use SessionMiddleware\Middleware\SessionMiddleware; | ||
|
||
/** | ||
* Class SessionMiddlewareFactory | ||
* | ||
* @package SessionMiddleware\Factory | ||
* @author Daniel Wendrich <daniel.wendrich@gmail.com> | ||
*/ | ||
class SessionMiddlewareFactory implements FactoryInterface | ||
{ | ||
/** | ||
* Create an object | ||
* | ||
* @param ContainerInterface $container | ||
* @param string $requestedName | ||
* @param null|array $options | ||
* @return object | ||
* @throws ServiceNotFoundException if unable to resolve the service. | ||
* @throws ServiceNotCreatedException if an exception is raised when | ||
* creating a service. | ||
* @throws ContainerException if any other error occurs | ||
*/ | ||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | ||
{ | ||
$sessionManager = $container->get(SessionManager::class); | ||
return new SessionMiddleware($sessionManager); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace SessionMiddleware\Middleware; | ||
|
||
use Interop\Http\ServerMiddleware\DelegateInterface; | ||
use Interop\Http\ServerMiddleware\MiddlewareInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Zend\Session\SessionManager; | ||
|
||
/** | ||
* Class SessionMiddleware | ||
* | ||
* @package SessionMiddleware\Middleware | ||
* @author Daniel Wendrich <daniel.wendrich@gmail.com> | ||
*/ | ||
class SessionMiddleware implements MiddlewareInterface | ||
{ | ||
const REQUEST_ATTRIBUTE_KEY = 'session_manager'; | ||
|
||
/** | ||
* @var SessionManager | ||
*/ | ||
private $sessionManager; | ||
|
||
/** | ||
* SessionMiddleware constructor. | ||
* | ||
* @param SessionManager $sessionManager | ||
*/ | ||
public function __construct(SessionManager $sessionManager) | ||
{ | ||
$this->sessionManager = $sessionManager; | ||
} | ||
|
||
/** | ||
* Process an incoming server request and return a response, optionally delegating | ||
* to the next middleware component to create the response. | ||
* | ||
* @param ServerRequestInterface $request | ||
* @param DelegateInterface $delegate | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function process(ServerRequestInterface $request, DelegateInterface $delegate) | ||
{ | ||
// start session handling | ||
$this->sessionManager->start(); | ||
|
||
// pass on session manager as request attribute | ||
$request = $request->withAttribute(self::REQUEST_ATTRIBUTE_KEY, $this->sessionManager); | ||
|
||
// delegate request to next middleware in stack | ||
return $delegate->process($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace SessionMiddlewareTest\Factory; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SessionMiddleware\Factory\SessionMiddlewareFactory; | ||
use SessionMiddleware\Middleware\SessionMiddleware; | ||
use Zend\ServiceManager\ServiceManager; | ||
use Zend\Session\SessionManager; | ||
|
||
class SessionMiddlewareFactoryTest extends TestCase | ||
{ | ||
public function testCreateMiddleware() | ||
{ | ||
$container = $this->prophesize(ServiceManager::class); | ||
$sessionManager = $this->prophesize(SessionManager::class); | ||
|
||
$container->get(SessionManager::class)->willReturn( | ||
$sessionManager->reveal() | ||
); | ||
|
||
$factory = new SessionMiddlewareFactory(); | ||
$middleware = $factory->__invoke($container->reveal(), 'foo'); | ||
|
||
$this->assertInstanceOf(SessionMiddleware::class, $middleware); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace SessionMiddlewareTest\Middleware; | ||
|
||
use Interop\Http\ServerMiddleware\DelegateInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use SessionMiddleware\Middleware\SessionMiddleware; | ||
use Zend\Session\SessionManager; | ||
|
||
class SessionMiddlewareTest extends TestCase | ||
{ | ||
public function testSessionStartIsCalledAndRequestIsDelegated() | ||
{ | ||
$invoked = false; | ||
|
||
$request = $this->prophesize(ServerRequestInterface::class); | ||
$request->withAttribute( | ||
SessionMiddleware::REQUEST_ATTRIBUTE_KEY, | ||
Argument::type(SessionManager::class) | ||
)->willReturn( | ||
$request->reveal() | ||
); | ||
|
||
$response = $this->prophesize(ResponseInterface::class)->reveal(); | ||
$delegate = $this->prophesize(DelegateInterface::class); | ||
|
||
$delegate->process(Argument::any())->will(function () use(&$invoked, $response) { | ||
$invoked = true; | ||
return $response; | ||
}); | ||
|
||
$sessionManager = $this->prophesize(SessionManager::class); | ||
$sessionManager->start()->willReturn(true); | ||
|
||
// assert that $sessionManager->start() gets called | ||
$sessionManager->start()->shouldbeCalled(); | ||
|
||
// assert that $sessionManager instance is added as request attribute | ||
$request->withAttribute(Argument::cetera())->shouldBeCalled(); | ||
|
||
$middleware = new SessionMiddleware($sessionManager->reveal()); | ||
$return = $middleware->process($request->reveal(), $delegate->reveal()); | ||
|
||
$this->assertSame($response, $return); | ||
$this->assertTrue($invoked); | ||
} | ||
} |