-
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
1 parent
f98d03a
commit 16f729d
Showing
8 changed files
with
292 additions
and
8 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,64 @@ | ||
<?php | ||
namespace Lawoole\Homer\Calling\Invokers; | ||
|
||
use Lawoole\Homer\Calling\Invocation; | ||
use Lawoole\Homer\Calling\Pipeline; | ||
|
||
class MiddlewareInvoker extends Invoker | ||
{ | ||
/** | ||
* The container implementation. | ||
* | ||
* @var \Illuminate\Contracts\Container\Container | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* The invoker to be called. | ||
* | ||
* @var \Lawoole\Homer\Calling\Invokers\Invoker | ||
*/ | ||
protected $invoker; | ||
|
||
/** | ||
* The middleware for the invoker. | ||
* | ||
* @var array | ||
*/ | ||
protected $middleware; | ||
|
||
/** | ||
* Create a concrete invoker instance. | ||
* | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @param string $interface | ||
* @param \Lawoole\Homer\Calling\Invokers\Invoker $invoker | ||
* @param array $middleware | ||
* @param array $options | ||
*/ | ||
public function __construct($container, $interface, $invoker, $middleware, array $options = []) | ||
{ | ||
parent::__construct($interface, $options); | ||
|
||
$this->container = $container; | ||
$this->invoker = $invoker; | ||
$this->middleware = $middleware; | ||
} | ||
|
||
/** | ||
* Do invoking and get the result. | ||
* | ||
* @param \Lawoole\Homer\Calling\Invocation $invocation | ||
* | ||
* @return \Lawoole\Homer\Calling\Result | ||
*/ | ||
protected function doInvoke(Invocation $invocation) | ||
{ | ||
return (new Pipeline($this->container)) | ||
->send($invocation) | ||
->through($this->middleware) | ||
->then(function ($invocation) { | ||
return $this->invoker->invoke($invocation); | ||
}); | ||
} | ||
} |
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,79 @@ | ||
<?php | ||
namespace Lawoole\Homer\Calling; | ||
|
||
use Closure; | ||
use Exception; | ||
use Illuminate\Contracts\Debug\ExceptionHandler; | ||
use Illuminate\Pipeline\Pipeline as BasePipeline; | ||
use Symfony\Component\Debug\Exception\FatalThrowableError; | ||
use Throwable; | ||
|
||
class Pipeline extends BasePipeline | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function prepareDestination(Closure $destination) | ||
{ | ||
return function ($passable) use ($destination) { | ||
try { | ||
return $destination($passable); | ||
} catch (Exception $e) { | ||
return $this->handleException($passable, $e); | ||
} catch (Throwable $e) { | ||
return $this->handleException($passable, new FatalThrowableError($e)); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function carry() | ||
{ | ||
return function ($stack, $pipe) { | ||
return function ($passable) use ($stack, $pipe) { | ||
try { | ||
$slice = parent::carry(); | ||
|
||
$callable = $slice($stack, $pipe); | ||
|
||
return $callable($passable); | ||
} catch (Exception $e) { | ||
return $this->handleException($passable, $e); | ||
} catch (Throwable $e) { | ||
return $this->handleException($passable, new FatalThrowableError($e)); | ||
} | ||
}; | ||
}; | ||
} | ||
|
||
/** | ||
* Handle the given exception. | ||
* | ||
* @param mixed $passable | ||
* @param \Exception $e | ||
* | ||
* @return mixed | ||
*/ | ||
protected function handleException($passable, Exception $e) | ||
{ | ||
$handler = $this->container->make(ExceptionHandler::class); | ||
|
||
$handler->report($e); | ||
|
||
return $this->createExceptionResult($e); | ||
} | ||
|
||
/** | ||
* Create an excepted calling result. | ||
* | ||
* @param \Throwable $e | ||
* | ||
* @return \Lawoole\Homer\Calling\Result | ||
*/ | ||
protected function createExceptionResult($e) | ||
{ | ||
return new Result(null, $e); | ||
} | ||
} |
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
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
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
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
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,73 @@ | ||
<?php | ||
namespace Lawoole\Homer; | ||
|
||
use Closure; | ||
|
||
class MiddlewareNameResolver | ||
{ | ||
/** | ||
* Resolve the middleware name to a class name(s) preserving passed parameters. | ||
* | ||
* @param string $name | ||
* @param array $nameMiddleware | ||
* @param array $middlewareGroups | ||
* | ||
* @return \Closure|string|array | ||
*/ | ||
public static function resolve($name, $nameMiddleware, $middlewareGroups) | ||
{ | ||
if ($name instanceof Closure) { | ||
return $name; | ||
} | ||
|
||
if (isset($nameMiddleware[$name]) && $nameMiddleware[$name] instanceof Closure) { | ||
return $nameMiddleware[$name]; | ||
} | ||
|
||
if (isset($middlewareGroups[$name])) { | ||
return static::parseMiddlewareGroup($name, $nameMiddleware, $middlewareGroups); | ||
} | ||
|
||
[$middleware, $parameters] = array_pad(explode(':', $name, 2), 2, null); | ||
|
||
if (isset($nameMiddleware[$middleware])) { | ||
$middleware = $nameMiddleware[$middleware]; | ||
} | ||
|
||
return $middleware.($parameters ? ':'.$parameters : ''); | ||
} | ||
|
||
/** | ||
* Parse the middleware group and format it for usage. | ||
* | ||
* @param string $name | ||
* @param array $nameMiddleware | ||
* @param array $middlewareGroups | ||
* | ||
* @return array | ||
*/ | ||
protected static function parseMiddlewareGroup($name, $nameMiddleware, $middlewareGroups) | ||
{ | ||
$results = []; | ||
|
||
foreach ($middlewareGroups[$name] as $middleware) { | ||
if (isset($middlewareGroups[$middleware])) { | ||
$results = array_merge($results, static::parseMiddlewareGroup( | ||
$middleware, $nameMiddleware, $middlewareGroups | ||
)); | ||
|
||
continue; | ||
} | ||
|
||
[$middleware, $parameters] = array_pad(explode(':', $middleware, 2), 2, null); | ||
|
||
if (isset($nameMiddleware[$middleware])) { | ||
$middleware = $nameMiddleware[$middleware]; | ||
} | ||
|
||
$results[] = $middleware.($parameters ? ':'.$parameters : ''); | ||
} | ||
|
||
return $results; | ||
} | ||
} |
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