Skip to content

Commit

Permalink
Move "Server" back to "Middleware"
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Dec 5, 2023
1 parent ea84717 commit 13d14d2
Show file tree
Hide file tree
Showing 37 changed files with 291 additions and 315 deletions.
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ parameters:
- src
excludePaths:
analyse:
- src/Server/Handlers
- src/Server/Interop.php
- src/Middleware/Handlers
- src/Middleware/Interop.php
14 changes: 7 additions & 7 deletions src/Component/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Container\VanillaContainer;
use Rougin\Slytherin\Debug\ErrorHandlerInterface;
use Rougin\Slytherin\Routing\DispatcherInterface as RouteDispatcher;
use Rougin\Slytherin\Server\DispatchInterface as MiddlewareDispatcher;
use Rougin\Slytherin\Middleware\DispatcherInterface as Middleware;
use Rougin\Slytherin\Routing\DispatcherInterface as Routing;
use Rougin\Slytherin\System;

/**
Expand Down Expand Up @@ -67,7 +67,7 @@ public function getDispatcher()
* @param \Rougin\Slytherin\Routing\DispatcherInterface $dispatcher
* @return self
*/
public function setDispatcher(RouteDispatcher $dispatcher)
public function setDispatcher(Routing $dispatcher)
{
$this->set(System::DISPATCHER, $dispatcher);

Expand Down Expand Up @@ -206,13 +206,13 @@ public function setHttpResponse(ResponseInterface $response)
*
* Gets the middleware.
*
* @return \Rougin\Slytherin\Server\DispatchInterface|null
* @return \Rougin\Slytherin\Middleware\DispatcherInterface|null
*/
public function getMiddleware()
{
if (! $this->has(System::MIDDLEWARE)) return null;

/** @var \Rougin\Slytherin\Server\DispatchInterface */
/** @var \Rougin\Slytherin\Middleware\DispatcherInterface */
return $this->get(System::MIDDLEWARE);
}

Expand All @@ -221,10 +221,10 @@ public function getMiddleware()
*
* Sets the middleware.
*
* @param \Rougin\Slytherin\Server\DispatchInterface $middleware
* @param \Rougin\Slytherin\Middleware\DispatcherInterface $middleware
* @return self
*/
public function setMiddleware(MiddlewareDispatcher $middleware)
public function setMiddleware(Middleware $middleware)
{
$this->set(System::MIDDLEWARE, $middleware);

Expand Down
6 changes: 3 additions & 3 deletions src/Server/Callback.php → src/Middleware/Callback.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rougin\Slytherin\Server;
namespace Rougin\Slytherin\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -39,8 +39,8 @@ public function __construct($middleware, ResponseInterface $response = null)
/**
* Processes an incoming server request and return a response.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Server\HandlerInterface $handler
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, HandlerInterface $handler)
Expand Down
2 changes: 0 additions & 2 deletions src/Middleware/Delegate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Rougin\Slytherin\Middleware;

use Rougin\Slytherin\System\Handler;

/**
* Calls the callback with a specified HTTP request.
* NOTE: To be removed in v1.0.0. Use "Handler" instead.
Expand Down
142 changes: 135 additions & 7 deletions src/Middleware/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,145 @@

namespace Rougin\Slytherin\Middleware;

use Rougin\Slytherin\Server\Dispatch;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Http\Response;

/**
* Dispatcher
*
* A simple implementation of a middleware dispatcher.
*
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
* @author Rasmus Schultz <rasmus@mindplay.dk>
*/
class Dispatcher extends Dispatch
class Dispatcher implements DispatcherInterface
{
/**
* @var \Rougin\Slytherin\Middleware\MiddlewareInterface[]
*/
protected $stack = array();

/**
* @var \Psr\Http\Message\ResponseInterface|null
*/
protected $response = null;

/**
* @param mixed[] $stack
*/
public function __construct($stack = array())
{
if ($stack) $this->setStack($stack);
}

/**
* @return \Rougin\Slytherin\Middleware\MiddlewareInterface[]
*/
public function getStack()
{
return $this->stack;
}

/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
*/
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
$stack = (array) $this->getStack();

$handler = new Handler($stack, $handler);

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

/**
* @param mixed $middleware
* @return self
*/
public function push($middleware)
{
if (! is_array($middleware))
{
$item = $this->transform($middleware);

array_push($this->stack, $item);

return $this;
}

foreach ($middleware as $item) $this->push($item);

return $this;
}

/**
* @param mixed[] $stack
* @return self
*/
public function setStack($stack)
{
$result = array();

foreach ($stack as $item)
{
$result[] = $this->transform($item);
}

$this->stack = $result;

return $this;
}

/**
* NOTE: To be removed in v1.0.0. Use $this->getStack() instead.
*
* @return \Rougin\Slytherin\Middleware\MiddlewareInterface[]
*/
public function stack()
{
return $this->getStack();
}

/**
* @param mixed $middleware
* @return \Rougin\Slytherin\Middleware\MiddlewareInterface
*/
protected function transform($middleware)
{
if ($middleware instanceof MiddlewareInterface)
{
return $middleware;
}

// Set empty response for double pass middlewares ---
if (! $this->response)
{
$this->response = new Response;
}
// --------------------------------------------------

if ($this->isCallable($middleware))
{
/** @var callable $middleware */
return new Callback($middleware, $this->response);
}

return new Wrapper($middleware);
}

/**
* @param mixed $item
* @return boolean
*/
protected function isCallable($item)
{
/** @var object|string $item */
$method = method_exists($item, '__invoke');

$callable = is_callable($item);

$object = is_object($item);

$closure = (! $object) || $item instanceof \Closure;

return ($method || $callable) && $closure;
}
}
20 changes: 17 additions & 3 deletions src/Middleware/DispatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Rougin\Slytherin\Middleware;

use Rougin\Slytherin\Server\DispatchInterface;

/**
* Dispatcher Interface
*
Expand All @@ -12,6 +10,22 @@
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
interface DispatcherInterface extends DispatchInterface
interface DispatcherInterface extends MiddlewareInterface
{
/**
* @return \Rougin\Slytherin\Middleware\MiddlewareInterface[]
*/
public function getStack();

/**
* @param mixed $middleware
* @return self
*/
public function push($middleware);

/**
* @param mixed[] $stack
* @return self
*/
public function setStack($stack);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rougin\Slytherin\Server;
namespace Rougin\Slytherin\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down
12 changes: 6 additions & 6 deletions src/Server/Handler.php → src/Middleware/Handler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rougin\Slytherin\Server;
namespace Rougin\Slytherin\Middleware;

use Psr\Http\Message\ServerRequestInterface;

Expand All @@ -12,7 +12,7 @@
class Handler implements HandlerInterface
{
/**
* @var \Rougin\Slytherin\Server\HandlerInterface
* @var \Rougin\Slytherin\Middleware\HandlerInterface
*/
protected $default;

Expand All @@ -22,13 +22,13 @@ class Handler implements HandlerInterface
protected $index = 0;

/**
* @var \Rougin\Slytherin\Server\MiddlewareInterface[]
* @var \Rougin\Slytherin\Middleware\MiddlewareInterface[]
*/
protected $stack;

/**
* @param \Rougin\Slytherin\Server\MiddlewareInterface[] $stack
* @param \Rougin\Slytherin\Server\HandlerInterface $default
* @param \Rougin\Slytherin\Middleware\MiddlewareInterface[] $stack
* @param \Rougin\Slytherin\Middleware\HandlerInterface $default
*/
public function __construct(array $stack, HandlerInterface $default)
{
Expand Down Expand Up @@ -65,7 +65,7 @@ public function handle(ServerRequestInterface $request)
}

/**
* @return \Rougin\Slytherin\Server\HandlerInterface
* @return \Rougin\Slytherin\Middleware\HandlerInterface
*/
protected function next()
{
Expand Down
9 changes: 7 additions & 2 deletions src/Middleware/HandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rougin\Slytherin\Middleware;

use Rougin\Slytherin\Server\HandlerInterface as Slytherin;
use Psr\Http\Message\ServerRequestInterface;

/**
* Handler Interface
Expand All @@ -12,6 +12,11 @@
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
interface HandlerInterface extends Slytherin
interface HandlerInterface
{
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return \Psr\Http\Message\ResponseInterface
*/
public function handle(ServerRequestInterface $request);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Rougin\Slytherin\Server\Handlers;
namespace Rougin\Slytherin\Middleware\Handlers;

use Interop\Http\Middleware\DelegateInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Server\HandlerInterface;
use Rougin\Slytherin\Middleware\HandlerInterface;

/**
* @package Slytherin
Expand All @@ -15,12 +15,12 @@
class Handler030 implements DelegateInterface
{
/**
* @var \Rougin\Slytherin\Server\HandlerInterface
* @var \Rougin\Slytherin\Middleware\HandlerInterface
*/
protected $handler;

/**
* @param \Rougin\Slytherin\Server\HandlerInterface $handler
* @param \Rougin\Slytherin\Middleware\HandlerInterface $handler
*/
public function __construct(HandlerInterface $handler)
{
Expand Down
Loading

0 comments on commit 13d14d2

Please sign in to comment.