Skip to content

Commit

Permalink
Fix build order of layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibaud Fabre committed Feb 13, 2015
1 parent 090ddb5 commit 564aeee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/Elements/ErrorHandlingLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

namespace Aztech\Layers\Elements;

use Aztech\Layers\Layer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Aztech\Layers\Layer;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Symfony\Component\HttpFoundation\Response;

class ErrorHandlingLayer implements Layer
class ErrorHandlingLayer implements Layer, LoggerAwareInterface
{

use LoggerAwareTrait;

private $controller;

public function __construct(Layer $controller)
{
$this->controller = $controller;
$this->logger = new NullLogger();
}

public function __invoke(Request $request)
Expand All @@ -22,9 +30,9 @@ public function __invoke(Request $request)

return $controller($request);
} catch (\Exception $ex) {
error_log($ex);
$this->logger->error($ex);

throw new HttpException(500, $ex->getMessage());
return new Response($ex->getMessage(), 500);
}
}
}
6 changes: 4 additions & 2 deletions src/LayeredControllerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public function build($nextLayer, array $keys)
throw new \InvalidArgumentException('Controller must be a callable.');
}

$nextLayer = $this->wrapLayerIfNecessary($nextLayer);
$controller = $this->wrapLayerIfNecessary($nextLayer);

$keys = array_reverse($keys);

foreach ($keys as $keyValue) {
$key = is_array($keyValue) ? reset($keyValue) : $keyValue;
$arguments = is_array($keyValue) ? array_slice($keyValue, 1, count($keyValue) - 1, true) : [];

$controller = $this->builders[$key]->buildLayer($nextLayer, $arguments);
$controller = $this->builders[$key]->buildLayer($controller, $arguments);
}

return $controller;
Expand Down

0 comments on commit 564aeee

Please sign in to comment.