Skip to content

Commit

Permalink
Merge branch 'migrate-laminas'
Browse files Browse the repository at this point in the history
* migrate-laminas:
  rename to mezzio for config
  cs
  kahlan config
  migrate to laminas
  • Loading branch information
samsonasik committed Jan 10, 2020
2 parents 79cd77f + 9179bfd commit 2ee59e9
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 85 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
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ ForceHttpsModule
Introduction
------------

ForceHttpsModule is a configurable module for force https in your [ZF Mvc](https://zendframework.github.io/tutorials/getting-started/overview/) and [ZF Expressive](https://zendframework.github.io/zend-expressive/) Application.
ForceHttpsModule is a configurable module for force https in your [Laminas Mvc](https://docs.laminas.dev/tutorials/) and [Mezzio](https://docs.mezzio.dev/mezzio/) Application.

> This is README for version ^2.0 which only support ZF3 and ZF Expressive version 3 with php ^7.1.
> This is README for version ^3.0 which only support Laminas Mvc version 3 and Mezzio version 3 with php ^7.1.
> For version ^2.0, you can read at [version 2 readme](https://github.com/samsonasik/ForceHttpsModule/tree/2.x.x) which only support ZF3 and ZF Expressive version 3 with php ^7.1.
> For version 1, you can read at [version 1 readme](https://github.com/samsonasik/ForceHttpsModule/tree/1.x.x) which still support ZF2 and ZF Expressive version 1 and 2 with php ^5.6|^7.0 support.
Expand All @@ -39,7 +41,7 @@ composer require samsonasik/force-https-module

**2. Copy config**

***a. For [ZF3 Mvc](https://zendframework.github.io/tutorials/getting-started/overview/) application, copy `force-https-module.local.php.dist` config to your local's autoload and configure it***
***a. For [Laminas Mvc](https://docs.laminas.dev/tutorials/) application, copy `force-https-module.local.php.dist` config to your local's autoload and configure it***

| source | destination |
|------------------------------------------------------------------------------|---------------------------------------------|
Expand All @@ -51,23 +53,23 @@ Or run copy command:
cp vendor/samsonasik/force-https-module/config/force-https-module.local.php.dist config/autoload/force-https-module.local.php
```

***b. For [ZF Expressive](https://zendframework.github.io/zend-expressive/) application, copy `expressive-force-https-module.local.php.dist` config to your local's autoload and configure it***
***b. For [Mezzio](https://docs.mezzio.dev/mezzio/) application, copy `mezzio-force-https-module.local.php.dist` config to your local's autoload and configure it***

| source | destination |
|------------------------------------------------------------------------------|---------------------------------------------|
| vendor/samsonasik/force-https-module/config/expressive-force-https-module.local.php.dist | config/autoload/expressive-force-https-module.local.php |
| vendor/samsonasik/force-https-module/config/mezzio-force-https-module.local.php.dist | config/autoload/mezzio-force-https-module.local.php |

Or run copy command:

```sh
cp vendor/samsonasik/force-https-module/config/expressive-force-https-module.local.php.dist config/autoload/expressive-force-https-module.local.php
cp vendor/samsonasik/force-https-module/config/mezzio-force-https-module.local.php.dist config/autoload/mezzio-force-https-module.local.php
```

When done, you can modify your local config:

```php
<?php
// config/autoload/force-https-module.local.php or config/autoload/expressive-force-https-module.local.php
// config/autoload/force-https-module.local.php or config/autoload/mezzio-force-https-module.local.php
return [
'force-https-module' => [
'enable' => true,
Expand Down Expand Up @@ -97,7 +99,7 @@ return [

**3. Lastly, enable it**

***a. For ZF Mvc application***
***a. For Laminas Mvc application***

```php
// config/modules.config.php or config/application.config.php
Expand All @@ -107,9 +109,9 @@ return [
],
```

***b. For ZF Expressive application***
***b. For Mezzio application***

For [zend-expressive-skeleton](https://github.com/zendframework/zend-expressive-skeleton) ^3.0, you need to open `config/pipeline.php` and add:
For [mezzio-skeleton](https://github.com/mezzio/mezzio-skeleton) ^3.0, you need to open `config/pipeline.php` and add:

```php
$app->pipe(ForceHttpsModule\Middleware\ForceHttps::class);
Expand Down
21 changes: 11 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "samsonasik/force-https-module",
"type": "library",
"description": "Force Https Module",
"description": "Force Https Module for Laminas Mvc and Mezzio application",
"keywords": [
"zf2",
"zf3",
"laminas3",
"expressive",
"middleware",
"force",
Expand All @@ -27,20 +26,20 @@
"require": {
"php": "^7.1",
"webmozart/assert": "^1.4",
"zendframework/zend-console": "^2.5"
"laminas/laminas-console": "^2.5"
},
"conflict": {
"zendframework/zend-expressive": "<3.0",
"zendframework/zend-mvc": "<3.0"
"mezzio/mezzio": "<3.0",
"laminas/laminas-mvc": "<3.0"
},
"require-dev": {
"kahlan/kahlan": "^4.0",
"php-coveralls/php-coveralls": "^2.1",
"phpstan/phpstan": "^0.10.8",
"phpstan/phpstan-webmozart-assert": "^0.10.0",
"zendframework/zend-coding-standard": "^1.0",
"zendframework/zend-expressive": "^3.0",
"zendframework/zend-mvc": "^3.0"
"laminas/laminas-coding-standard": "^2.0",
"mezzio/mezzio": "^3.0",
"laminas/laminas-mvc": "^3.0"
},
"config": {
"bin-dir": "bin",
Expand All @@ -64,5 +63,7 @@
"scripts": {
"cs-check": "phpcs",
"cs-fix": "phpcbf"
}
},
"minimum-stability": "alpha",
"prefer-stable": true
}
3 changes: 3 additions & 0 deletions kahlan-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

file_put_contents('vendor/laminas/laminas-zendframework-bridge/src/autoload.php', '<?php');
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
14 changes: 7 additions & 7 deletions spec/Listener/ForceHttpsSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use Kahlan\Plugin\Double;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\EventManager\EventManagerInterface;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\MvcEvent;
use Zend\Router\RouteMatch;
use Zend\Uri\Uri;
use Laminas\Console\Console;
use Laminas\EventManager\EventManagerInterface;
use Laminas\Http\PhpEnvironment\Request;
use Laminas\Http\PhpEnvironment\Response;
use Laminas\Mvc\MvcEvent;
use Laminas\Router\RouteMatch;
use Laminas\Uri\Uri;

describe('ForceHttps', function () {

Expand Down
2 changes: 1 addition & 1 deletion spec/Middleware/ForceHttpsFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use ForceHttpsModule\Middleware\ForceHttpsFactory;
use Kahlan\Plugin\Double;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Router\RouterInterface;
use Mezzio\Router\RouterInterface;

describe('ForceHttpsFactory', function () {

Expand Down
6 changes: 3 additions & 3 deletions spec/Middleware/ForceHttpsSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Psr\Http\Message\UriInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Expressive\Router\Route;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\Router\RouterInterface;
use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
use Mezzio\Router\RouterInterface;

describe('ForceHttps', function () {

Expand Down
36 changes: 17 additions & 19 deletions src/HttpsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace ForceHttpsModule;

use Psr\Http\Message\ResponseInterface;
use Laminas\Router\RouteMatch;
use Mezzio\Router\RouteResult;
use Webmozart\Assert\Assert;
use Zend\Expressive\Router\RouteResult;
use Zend\Http\PhpEnvironment\Response;
use Zend\Router\RouteMatch;

use function in_array;
use function strpos;
use function substr_replace;

trait HttpsTrait
{
Expand All @@ -18,7 +20,7 @@ trait HttpsTrait
/** @var bool */
private $alreadyHasWwwPrefix;

private function isSchemeHttps(string $uriScheme) : bool
private function isSchemeHttps(string $uriScheme): bool
{
return $uriScheme === 'https';
}
Expand All @@ -28,7 +30,7 @@ private function isSchemeHttps(string $uriScheme) : bool
*
* @param RouteMatch|RouteResult|null $match
*/
private function isGoingToBeForcedToHttps($match = null) : bool
private function isGoingToBeForcedToHttps($match = null): bool
{
if ($match === null || ($match instanceof RouteResult && $match->isFailure())) {
return $this->config['allow_404'] ?? false;
Expand All @@ -39,7 +41,7 @@ private function isGoingToBeForcedToHttps($match = null) : bool
}

Assert::notNull($match);
if (! \in_array($match->getMatchedRouteName(), $this->config['force_specific_routes'])) {
if (! in_array($match->getMatchedRouteName(), $this->config['force_specific_routes'])) {
return false;
}

Expand All @@ -50,9 +52,8 @@ private function isGoingToBeForcedToHttps($match = null) : bool
* Check if Setup Strict-Transport-Security need to be skipped.
*
* @param RouteMatch|RouteResult|null $match
*
*/
private function isSkippedHttpStrictTransportSecurity(string $uriScheme, $match = null) : bool
private function isSkippedHttpStrictTransportSecurity(string $uriScheme, $match = null): bool
{
return ! $this->isSchemeHttps($uriScheme) ||
! $this->isGoingToBeForcedToHttps($match) ||
Expand All @@ -65,43 +66,40 @@ private function isSkippedHttpStrictTransportSecurity(string $uriScheme, $match
/**
* Add www. prefix when use add_www_prefix = true
*/
private function withWwwPrefixWhenRequired(string $httpsRequestUri) : string
private function withWwwPrefixWhenRequired(string $httpsRequestUri): string
{
$this->needsWwwPrefix = $this->config['add_www_prefix'] ?? false;
$this->alreadyHasWwwPrefix = \strpos($httpsRequestUri, 'www.', 8) === 8;
$this->alreadyHasWwwPrefix = strpos($httpsRequestUri, 'www.', 8) === 8;

if (! $this->needsWwwPrefix || $this->alreadyHasWwwPrefix) {
return $httpsRequestUri;
}

return \substr_replace($httpsRequestUri, 'www.', 8, 0);
return substr_replace($httpsRequestUri, 'www.', 8, 0);
}

/**
* Remove www. prefix when use remove_www_prefix = true
* It only works if previous's config 'add_www_prefix' => false
*/
private function withoutWwwPrefixWhenNotRequired(string $httpsRequestUri) : string
private function withoutWwwPrefixWhenNotRequired(string $httpsRequestUri): string
{
if ($this->needsWwwPrefix) {
return $httpsRequestUri;
}

$removeWwwPrefix = $this->config['remove_www_prefix'] ?? false;
$removeWwwPrefix = $this->config['remove_www_prefix'] ?? false;
if (! $removeWwwPrefix || ! $this->alreadyHasWwwPrefix) {
return $httpsRequestUri;
}

return \substr_replace($httpsRequestUri, '', 8, 4);
return substr_replace($httpsRequestUri, '', 8, 4);
}

/**
* Get Final Request Uri with configured with or without www prefix
*
* @param string $httpsRequestUri
* @return string
*/
private function getFinalhttpsRequestUri(string $httpsRequestUri) : string
private function getFinalhttpsRequestUri(string $httpsRequestUri): string
{
$httpsRequestUri = $this->withWwwPrefixWhenRequired($httpsRequestUri);
$httpsRequestUri = $this->withoutWwwPrefixWhenNotRequired($httpsRequestUri);
Expand Down
40 changes: 22 additions & 18 deletions src/Listener/ForceHttps.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@
namespace ForceHttpsModule\Listener;

use ForceHttpsModule\HttpsTrait;
use Zend\Console\Console;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\MvcEvent;
use Zend\Router\RouteMatch;
use Laminas\Console\Console;
use Laminas\EventManager\AbstractListenerAggregate;
use Laminas\EventManager\EventManagerInterface;
use Laminas\Http\PhpEnvironment\Request;
use Laminas\Http\PhpEnvironment\Response;
use Laminas\Mvc\MvcEvent;
use Laminas\Router\RouteMatch;

use function sprintf;

class ForceHttps extends AbstractListenerAggregate
{
use HttpsTrait;

/**
* @var array
*/
/** @var array */
private $config;

public function __construct(array $config)
{
$this->config = $config;
}

public function attach(EventManagerInterface $events, $priority = 1) : void
/**
* @param int $priority
*/
public function attach(EventManagerInterface $events, $priority = 1): void
{
if (Console::isConsole() || ! $this->config['enable']) {
return;
Expand All @@ -37,10 +41,10 @@ public function attach(EventManagerInterface $events, $priority = 1) : void
}

private function setHttpStrictTransportSecurity(
string $uriScheme,
Response $response,
string $uriScheme,
Response $response,
?RouteMatch $match
) : Response {
): Response {
if ($this->isSkippedHttpStrictTransportSecurity($uriScheme, $match)) {
return $response;
}
Expand All @@ -63,14 +67,14 @@ private function setHttpStrictTransportSecurity(
/**
* Force Https Scheme handle.
*/
public function forceHttpsScheme(MvcEvent $e) : void
public function forceHttpsScheme(MvcEvent $e): void
{
/** @var \Zend\Http\PhpEnvironment\Request $request */
$request = $e->getRequest();
/** @var Request $request */
$request = $e->getRequest();
/** @var Response $response */
$response = $e->getResponse();
$response = $e->getResponse();

$uri = $request->getUri();
$uri = $request->getUri();
/** @var string $uriScheme*/
$uriScheme = $uri->getScheme();

Expand Down
2 changes: 1 addition & 1 deletion src/Listener/ForceHttpsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class ForceHttpsFactory
{
public function __invoke(ContainerInterface $container) : ForceHttps
public function __invoke(ContainerInterface $container): ForceHttps
{
$config = $container->get('config');
$forceHttpsConfig = $config['force-https-module'] ?? ['enable' => false];
Expand Down
Loading

0 comments on commit 2ee59e9

Please sign in to comment.