Skip to content

Commit

Permalink
Allow to definte the factories in the constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Oct 12, 2018
1 parent 9e9f8d1 commit 2d05ed3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed

- Use `phpstan` as a dev dependency to detect bugs

### Added

- Added `responseFactory` option to `__construct`

## [1.0.0] - 2018-09-23

### Added
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"phpunit/phpunit": "^6.0",
"zendframework/zend-diactoros": "^1.3",
"friendsofphp/php-cs-fixer": "^2.0",
"squizlabs/php_codesniffer": "^3.0"
"squizlabs/php_codesniffer": "^3.0",
"phpstan/phpstan": "^0.9.2|^0.10.3"
},
"autoload": {
"psr-4": {
Expand All @@ -42,6 +43,7 @@
"phpcs"
],
"cs-fix": "php-cs-fixer fix .",
"coverage": "phpunit --coverage-html=coverage"
"coverage": "phpunit --coverage-html=coverage",
"analyse": "phpstan analyse --no-progress --level 7 src"
}
}
11 changes: 9 additions & 2 deletions src/BasePathRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Middlewares;

use Middlewares\Utils\Factory;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand All @@ -26,14 +27,20 @@ class BasePathRouter implements MiddlewareInterface
*/
private $continueOnError = false;

/**
* @var ResponseFactoryInterface
*/
private $responseFactory;

/**
* @var string Attribute name for handler reference
*/
private $attribute = 'request-handler';

public function __construct(array $middlewares)
public function __construct(array $middlewares, ResponseFactoryInterface $responseFactory = null)
{
$this->middlewares = $middlewares;
$this->responseFactory = $responseFactory ?: Factory::getResponseFactory();

// Make sure the longest path prefixes are matched first
// (otherwise, a path /foo would always match, even when /foo/bar
Expand Down Expand Up @@ -100,7 +107,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $handler->handle($request);
}

return Factory::createResponse(404);
return $this->responseFactory->createResponse(404);
}

private function unprefixedRequest(ServerRequestInterface $request, string $prefix): ServerRequestInterface
Expand Down

0 comments on commit 2d05ed3

Please sign in to comment.