Skip to content

Commit

Permalink
Conform Interop middlewares into Slytherin middleware (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin authored Mar 17, 2024
1 parent 8e46c75 commit c464e15
Show file tree
Hide file tree
Showing 22 changed files with 94 additions and 87 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.phpunit.result.cache
build
composer.lock
docs
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to `Weasley` will be documented in this file.

## [0.6.5](https://github.com/rougin/weasley/compare/v0.6.4...v0.6.5) - Unreleased

### Changed
- Conformed HTTP middlewares from `http-interop/http-middleware` to `rougin/slytherin`'s own middleware

### Fixed
- Unit tests in running `SessionIntegration`

## [0.6.4](https://github.com/rougin/weasley/compare/v0.6.3...v0.6.4) - 2023-11-16

### Changed
Expand Down
2 changes: 1 addition & 1 deletion src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Console extends Application
{
const VERSION = '0.6.4';
const VERSION = '0.6.5';

/**
* Instantiates the console application.
Expand Down
12 changes: 6 additions & 6 deletions src/Middleware/CrossOriginHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Rougin\Weasley\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Rougin\Slytherin\Middleware\MiddlewareInterface;
use Rougin\Slytherin\Http\Response;

/**
Expand Down Expand Up @@ -46,15 +46,15 @@ public function __construct(array $allowed = null, array $methods = null)
* Process an incoming server request and return a response, optionally
* delegating to the next middleware component to create the response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Interop\Http\ServerMiddleware\DelegateInterface $delegate
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
$options = $request->getMethod() === 'OPTIONS';

$response = $options ? new Response : $delegate->process($request);
$response = $options ? new Response : $handler->handle($request);

$response = $response->withHeader(self::ORIGIN, $this->allowed);

Expand Down
12 changes: 6 additions & 6 deletions src/Middleware/JsonHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Rougin\Weasley\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Rougin\Slytherin\Middleware\MiddlewareInterface;

/**
* JSON Headers Middleware
Expand All @@ -18,13 +18,13 @@ class JsonHeaders implements MiddlewareInterface
* Process an incoming server request and return a response, optionally
* delegating to the next middleware component to create the response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Interop\Http\ServerMiddleware\DelegateInterface $delegate
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
$response = $delegate->process($request);
$response = $handler->handle($request);

$new = $response->withHeader('Content-Type', 'application/json');

Expand Down
12 changes: 6 additions & 6 deletions src/Middleware/SpoofFormMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Rougin\Weasley\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Rougin\Slytherin\Middleware\MiddlewareInterface;

/**
* Spoof Form Method Middleware
Expand Down Expand Up @@ -33,11 +33,11 @@ public function __construct($key = null)
* Process an incoming server request and return a response, optionally
* delegating to the next middleware component to create the response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Interop\Http\ServerMiddleware\DelegateInterface $delegate
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
$parsed = $request->getParsedBody();

Expand All @@ -47,7 +47,7 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele
$request = $request->withMethod($method);
}

return $delegate->process($request);
return $handler->handle($request);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Middleware/TransformRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Rougin\Weasley\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Rougin\Slytherin\Middleware\MiddlewareInterface;

/**
* Transform Request Middleware
Expand All @@ -18,11 +18,11 @@ class TransformRequest implements MiddlewareInterface
* Process an incoming server request and return a response, optionally
* delegating to the next middleware component to create the response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Interop\Http\ServerMiddleware\DelegateInterface $delegate
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
$get = $this->map($request->getQueryParams());

Expand All @@ -34,7 +34,7 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele

$post = $request->withParsedBody($post);

return $delegate->process($post);
return $handler->handle($post);
}

/**
Expand Down
20 changes: 9 additions & 11 deletions src/Session/SessionIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function define(ContainerInterface $container, Configuration $config)
{
$name = $config->get('session.cookies', 'weasley_session');

list($container, $handler) = $this->handler($container, $config);
$container->set(self::HANDLER, $handler = $this->handler($config));

if (! $cookie = $config->get("app.http.cookies.$name", null))
{
Expand All @@ -50,27 +50,25 @@ public function define(ContainerInterface $container, Configuration $config)
/**
* Returns the specified SessionHandlerInterface.
*
* @param \Rougin\Slytherin\Container\ContainerInterface $container
* @param \Rougin\Slytherin\Integration\Configuration $config
* @param \Rougin\Slytherin\Integration\Configuration $config
* @return \SessionHandlerInterface
*/
protected function handler(ContainerInterface $container, Configuration $config)
protected function handler(Configuration $config)
{
$default = array('file' => 'Rougin\Weasley\Session\FileSessionHandler');

$handlers = $config->get('session.handlers', $default);
$items = array('file' => new FileSessionHandler);

$handler = $handlers[$config->get('session.driver', 'file')];
$handlers = $config->get('session.handlers', $items);

$instance = $container->get($handler);
$driver = $config->get('session.driver', 'file');

return array($container->set(self::HANDLER, $instance), $instance);
return $handlers[$driver];
}

/**
* Generates a random string.
* NOTE: Should be in a single function (or class).
*
* Generates a random string.
*
* @param integer $length
* @return string
*/
Expand Down
12 changes: 6 additions & 6 deletions src/Templates/Middleware.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace $NAMESPACE;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Rougin\Slytherin\Middleware\MiddlewareInterface;

/**
* $CLASS
Expand All @@ -18,14 +18,14 @@ class $CLASS implements MiddlewareInterface
* Process an incoming server request and return a response, optionally
* delegating to the next middleware component to create the response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Interop\Http\ServerMiddleware\DelegateInterface $delegate
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
//

return $delegate->process($request);
return $handler->handle($request);
}
}
12 changes: 6 additions & 6 deletions tests/Fixture/Middleware/CheckPatchMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Rougin\Weasley\Fixture\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Rougin\Slytherin\Middleware\MiddlewareInterface;

/**
* "Check PATCH Method" Middleware
Expand All @@ -18,13 +18,13 @@ class CheckPatchMethod implements MiddlewareInterface
* Process an incoming server request and return a response, optionally
* delegating to the next middleware component to create the response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Interop\Http\ServerMiddleware\DelegateInterface $delegate
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
$response = $delegate->process($request);
$response = $handler->handle($request);

if ($request->getMethod() === 'PATCH') {
$key = 'Weasley-Has-PATCH-Method';
Expand Down
12 changes: 6 additions & 6 deletions tests/Fixture/Middleware/CheckQueryParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Rougin\Weasley\Fixture\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Rougin\Slytherin\Middleware\MiddlewareInterface;

/**
* "Check $_GET Parameters" Middleware
Expand All @@ -18,13 +18,13 @@ class CheckQueryParams implements MiddlewareInterface
* Process an incoming server request and return a response, optionally
* delegating to the next middleware component to create the response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Interop\Http\ServerMiddleware\DelegateInterface $delegate
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
$response = $delegate->process($request);
$response = $handler->handle($request);

$query = $request->getQueryParams();

Expand Down
12 changes: 6 additions & 6 deletions tests/Fixture/Middleware/CheckTrimmedString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Rougin\Weasley\Fixture\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Rougin\Slytherin\Middleware\MiddlewareInterface;

/**
* "Check Trimmed String" Middleware
Expand All @@ -18,13 +18,13 @@ class CheckTrimmedString implements MiddlewareInterface
* Process an incoming server request and return a response, optionally
* delegating to the next middleware component to create the response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Interop\Http\ServerMiddleware\DelegateInterface $delegate
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
$response = $delegate->process($request);
$response = $handler->handle($request);

$query = $request->getQueryParams();

Expand Down
8 changes: 4 additions & 4 deletions tests/Fixture/Middleware/FinalDelegate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rougin\Weasley\Fixture\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand All @@ -12,15 +12,15 @@
* @package Weasley
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class FinalDelegate implements DelegateInterface
class FinalDelegate implements HandlerInterface
{
/**
* @var \Psr\Http\Message\ResponseInterface
*/
protected $response;

/**
* Initializes the delegate instance.
* Initializes the handler instance.
*
* @param \Psr\Http\Message\ResponseInterface $response
*/
Expand All @@ -35,7 +35,7 @@ public function __construct(ResponseInterface $response)
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request)
{
return $this->response;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Fixture/Middleware/ReturnQueryParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Rougin\Weasley\Fixture\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;
use Rougin\Slytherin\Middleware\MiddlewareInterface;

/**
* "Return $_GET Parameters" Middleware
Expand All @@ -18,13 +18,13 @@ class ReturnQueryParams implements MiddlewareInterface
* Process an incoming server request and return a response, optionally
* delegating to the next middleware component to create the response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Interop\Http\ServerMiddleware\DelegateInterface $delegate
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
$response = $delegate->process($request);
$response = $handler->handle($request);

$query = $request->getQueryParams();

Expand Down
Loading

0 comments on commit c464e15

Please sign in to comment.