Skip to content

Commit

Permalink
Merge 28350fa into 56a3e23
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jan 8, 2020
2 parents 56a3e23 + 28350fa commit c159cec
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 50 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CONTRIBUTING
To contribute, you can send pull requests with :

- Typo fix.
- Use [Zend Coding Standard](https://github.com/zendframework/zend-coding-standard).
- Use [Laminas Coding Standard](https://github.com/laminas/laminas-coding-standard).
- patch(es) need new/updated test(s).
- new feature(s) need test(s).

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ ExpressiveRedirectHandler
[![Coverage Status](https://coveralls.io/repos/samsonasik/ExpressiveRedirectHandler/badge.svg?branch=master)](https://coveralls.io/r/samsonasik/ExpressiveRedirectHandler)
[![Downloads](https://poser.pugx.org/samsonasik/expressive-redirect-handler/downloads)](https://packagist.org/packages/samsonasik/expressive-redirect-handler)

> This is README for version ^1.0 which only support ZF Expressive version 3 with php ^7.1.
> This is README for version ^2.0 which only support Mezzio version 3 with php ^7.1.
> This is README for version ^1.0, , you can read at [version 1.* readme](https://github.com/samsonasik/ExpressiveRedirectHandler/tree/1.x.x) which support ZF Expressive version 3 with php ^7.1.
> For version 0.*, you can read at [version 0.* readme](https://github.com/samsonasik/ExpressiveRedirectHandler/tree/0.x.x) which still ZF Expressive version 1 and 2 with php ^5.6|^7.0 support.
*ExpressiveRedirectHandler* is a package that contains [zend-expressive](https://github.com/zendframework/zend-expressive) middleware for handling redirect that fit with [zend-expressive-skeleton](https://github.com/zendframework/zend-expressive-skeleton) for following conditions:
*ExpressiveRedirectHandler* is a package that contains [Mezzio](https://github.com/mezzio/mezzio) middleware for handling redirect that fit with [mezzio-skeleton](https://github.com/mezzio/mezzio-skeleton) for following conditions:

1. When the given url to `RedirectResponse` is not registered in routing config
-------------------------------------------------------------------------------

For example, we use `RedirectResponse` instance in our Middleware:

```php
use Zend\Diactoros\Response\RedirectResponse;
use Laminas\Diactoros\Response\RedirectResponse;
// ...
$redirect = '/foo'; // may be a variable from GET
return new RedirectResponse($redirect);
```

if the passed `$redirect` as url is a valid and registered in the routes, it uses default `RedirectResponse` implementation, otherwise, it will redirect to default `default_url` registered in `config/autoload/expressive-redirect-handler.local.php`:
if the passed `$redirect` as url is a valid and registered in the routes, it uses default `RedirectResponse` implementation, otherwise, it will redirect to default `default_url` registered in `config/autoload/mezzio-redirect-handler.local.php`:

For example, we define:

Expand Down Expand Up @@ -87,6 +89,7 @@ Installation
------------

- Require via composer

```bash
composer require samsonasik/expressive-redirect-handler
```
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"require": {
"php": "^7.1",
"jeremykendall/php-domain-parser": "^5.0",
"zendframework/zend-diactoros": "^1.7.1"
"laminas/laminas-diactoros": "^1.7.1"
},
"require-dev": {
"laminas/laminas-coding-standard": "^2.0.0",
"mezzio/mezzio": "^3.0",
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^7.0",
"zendframework/zend-coding-standard": "^1.0",
"zendframework/zend-expressive": "^3.0"
"phpunit/phpunit": "^7.0"
},
"config": {
"process-timeout": 5000
Expand All @@ -45,6 +45,8 @@
"ExpressiveRedirectHandlerTest\\": "test/"
}
},
"minimum-stability": "alpha",
"prefer-stable": true,
"scripts": {
"cs-check": "phpcs",
"cs-fix": "phpcbf"
Expand Down
4 changes: 2 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<ruleset name="Zend Framework Coding Standard">
<rule ref="./vendor/zendframework/zend-coding-standard/ruleset.xml"/>
<ruleset name="Laminas Coding Standard">
<rule ref="./vendor/laminas/laminas-coding-standard/src/LaminasCodingStandard/ruleset.xml"/>

<!-- Paths to check -->
<file>src</file>
Expand Down
19 changes: 18 additions & 1 deletion src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
<?php

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

declare(strict_types=1);

namespace ExpressiveRedirectHandler;

class ConfigProvider
{
public function __invoke()
public function __invoke(): array
{
return [
'dependencies' => [
Expand Down
51 changes: 29 additions & 22 deletions src/Middleware/RedirectHandlerAction.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -19,17 +17,23 @@
* and is licensed under the MIT license.
*/

declare(strict_types=1);

namespace ExpressiveRedirectHandler\Middleware;

use InvalidArgumentException;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\RedirectResponse;
use Laminas\Diactoros\Uri;
use Mezzio\Router\RouterInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\RedirectResponse;
use Zend\Diactoros\Uri;
use Zend\Expressive\Router\RouterInterface;

use function in_array;
use function is_string;
use function sprintf;

class RedirectHandlerAction implements MiddlewareInterface
{
Expand All @@ -43,18 +47,19 @@ public function __construct(array $config, RouterInterface $router)
$this->router = $router;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);

if (isset($this->config['header_handler']['enable'])
if (
isset($this->config['header_handler']['enable'])
&& $this->config['header_handler']['enable'] === true
&& ! empty($this->config['header_handler']['headers'])
) {
$statusCode = $response->getStatusCode();
foreach ($this->config['header_handler']['headers'] as $code => $redirect) {
if (! \is_string($redirect)) {
throw new InvalidArgumentException(\sprintf(
if (! is_string($redirect)) {
throw new InvalidArgumentException(sprintf(
'redirect value for %s must be a string',
$code
));
Expand All @@ -69,39 +74,41 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $response;
}

$allow_not_routed_url = $this->config['allow_not_routed_url'] ?? false;
$exclude_urls = $this->config['options']['exclude_urls'] ?? [];
$exclude_hosts = $this->config['options']['exclude_hosts'] ?? [];
$allowNotRoutedURL = $this->config['allow_not_routed_url'] ?? false;
$excludeURLs = $this->config['options']['exclude_urls'] ?? [];
$excludeHosts = $this->config['options']['exclude_hosts'] ?? [];

$uriTarget = $response->getHeader('location')[0];
$uriTargetHost = (new Uri($uriTarget))->getHost();

if (true === $allow_not_routed_url ||
\in_array($uriTarget, $exclude_urls) ||
\in_array($uriTargetHost, $exclude_hosts)
if (
true === $allowNotRoutedURL ||
in_array($uriTarget, $excludeURLs) ||
in_array($uriTargetHost, $excludeHosts)
) {
return $response;
}

$default_url = $this->config['default_url'] ?? '/';
$defaultURL = $this->config['default_url'] ?? '/';
$currentPath = $request->getUri()->getPath();

$newUri = new Uri($uriTarget);
$request = $request->withUri($newUri);
$uriTargetPath = $newUri->getPath();
$match = $this->router->match($request);

if ($match->isFailure()
if (
$match->isFailure()
|| ($match->isSuccess()
&& $uriTargetPath === $currentPath
&& $uriTarget !== $default_url
&& $uriTargetPath !== $default_url
&& $uriTarget !== $defaultURL
&& $uriTargetPath !== $defaultURL
)
) {
return $response->withHeader('location', $default_url);
return $response->withHeader('location', $defaultURL);
}

if ($uriTarget === $default_url || $uriTargetPath === $default_url) {
if ($uriTarget === $defaultURL || $uriTargetPath === $defaultURL) {
return new Response();
}

Expand Down
16 changes: 8 additions & 8 deletions src/Middleware/RedirectHandlerActionFactory.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -19,21 +17,23 @@
* and is licensed under the MIT license.
*/

declare(strict_types=1);

namespace ExpressiveRedirectHandler\Middleware;

use Mezzio\Router\RouterInterface;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Router\RouterInterface;

class RedirectHandlerActionFactory
{
public function __invoke(ContainerInterface $container) : RedirectHandlerAction
public function __invoke(ContainerInterface $container): RedirectHandlerAction
{
$config = $container->get('config');
$expressive_redirect_handler_config = $config['expressive-redirect-handler'] ?? [];
$router = $container->get(RouterInterface::class);
$config = $container->get('config');
$expressiveRedirectHandlerConfig = $config['expressive-redirect-handler'] ?? [];
$router = $container->get(RouterInterface::class);

return new RedirectHandlerAction(
$expressive_redirect_handler_config,
$expressiveRedirectHandlerConfig,
$router
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/Middleware/RedirectHandlerActionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

use ExpressiveRedirectHandler\Middleware\RedirectHandlerAction;
use ExpressiveRedirectHandler\Middleware\RedirectHandlerActionFactory;
use Mezzio\Router\RouterInterface;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Router\RouterInterface;

class RedirectHandlerActionFactoryTest extends TestCase
{
Expand Down
14 changes: 7 additions & 7 deletions test/Middleware/RedirectHandlerActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
namespace ExpressiveRedirectHandlerTest\Middleware;

use ExpressiveRedirectHandler\Middleware\RedirectHandlerAction;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\RedirectResponse;
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\Uri;
use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
use Mezzio\Router\RouterInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\RedirectResponse;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;
use Zend\Expressive\Router\Route;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\Router\RouterInterface;

class RedirectHandlerActionTest extends TestCase
{
Expand Down

0 comments on commit c159cec

Please sign in to comment.