Skip to content

Commit

Permalink
Merge pull request #11 from koriym/2.x-lazy-load
Browse files Browse the repository at this point in the history
Load route script file on every request #8  for 2.x
  • Loading branch information
koriym authored Jun 8, 2018
2 parents 0f711d5 + 3139a3d commit c4d22b4
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 83 deletions.
10 changes: 5 additions & 5 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ return \PhpCsFixer\Config::create()
'method_separation' => true,
'modernize_types_casting' => true,
'native_function_casing' => true,
// 'native_function_invocation' => true,
'native_function_invocation' => true,
'new_with_braces' => false, //
'no_alias_functions' => true,
'no_blank_lines_after_class_opening' => true,
Expand Down Expand Up @@ -78,7 +78,7 @@ return \PhpCsFixer\Config::create()
'php_unit_dedicate_assert' => true,
'php_unit_fqcn_annotation' => true,
'php_unit_strict' => true,
// 'phpdoc_add_missing_param_annotation' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_align' => true,
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
Expand All @@ -87,7 +87,7 @@ return \PhpCsFixer\Config::create()
'phpdoc_no_alias_tag' => ['property-read' => 'property', 'property-write' => 'property', 'type' => 'var'],
'phpdoc_no_empty_return' => true,
'phpdoc_no_package' => true,
// 'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_order' => true,
'phpdoc_return_self_reference' => true,
'phpdoc_scalar' => true,
Expand All @@ -108,7 +108,7 @@ return \PhpCsFixer\Config::create()
'self_accessor' => true,
'short_scalar_cast' => true,
// 'silenced_deprecation_error' => true,
// 'simplified_null_return' => true,
'simplified_null_return' => true,
// 'single_blank_line_before_namespace' => true,
'single_quote' => true,
'space_after_semicolon' => true,
Expand All @@ -125,6 +125,6 @@ return \PhpCsFixer\Config::create()
->setFinder(
PhpCsFixer\Finder::create()
->exclude('tests/Fake')
->exclude('var')
->exclude('fake-app/var')
->in(__DIR__)
)->setLineEnding("\n");
13 changes: 7 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: php
sudo: false
php:
- 7.0
- 7
- 7.1
- 7.2

cache:
directories:
- vendor
Expand All @@ -16,9 +15,11 @@ env:
before_script:
- composer self-update
- composer update $DEPENDENCIES
- if [[ $TRAVIS_PHP_VERSION = '7.2' ]]; then COVERAGE="--coverage-clover=coverage.clover"; else phpenv config-rm xdebug.ini; fi
- if [[ $TRAVIS_PHP_VERSION = '7.2' ]]; then composer require --dev phpstan/phpstan-shim friendsofphp/php-cs-fixer; fi
script:
- ./vendor/bin/phpunit --coverage-clover=coverage.clover;
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then wget http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar && php php-cs-fixer-v2.phar fix --config=.php_cs -v --dry-run --using-cache=no --path-mode=intersection `git diff --name-only --diff-filter=ACMRTUXB $TRAVIS_COMMIT_RANGE`; fi

- ./vendor/bin/phpunit $COVERAGE;
- if [[ $TRAVIS_PHP_VERSION = '7.2' ]]; then vendor/bin/php-cs-fixer fix --verbose --diff --dry-run --config=.php_cs; fi
- if [[ $TRAVIS_PHP_VERSION = '7.2' ]]; then vendor/bin/phpstan analyse -l 2 -c phpstan.neon src tests --no-progress --no-interaction; fi
after_script:
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
- if [[ $TRAVIS_PHP_VERSION = '7.2' ]]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
},
"autoload-dev": {
"psr-4": {
"BEAR\\Package\\": "tests/",
"FakeVendor\\HelloWorld\\": "tests/Fake/fake-app/src"
}
},
"scripts" :{
"test": ["@cs", "phpunit"],
"scripts": {
"test": ["phpunit"],
"tests": ["@cs", "phpmd src,tests text ./phpmd.xml", "phpstan analyse -l 4 src tests -c phpstan.neon --no-progress", "@test"],
"coverage": ["php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"],
"cs": ["php-cs-fixer fix -v --dry-run", "phpcs --standard=./phpcs.xml src"],
"cs-fix": ["php-cs-fixer fix -v", "phpcbf src"]
"cs": ["php-cs-fixer fix -v --dry-run", "phpcs --standard=phpcs.xml src;"],
"cs-fix": ["php-cs-fixer fix -v", "phpcbf src tests"]
}
}
2 changes: 1 addition & 1 deletion phpmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<rule ref="rulesets/naming.xml/BooleanGetMethodName" />
<!--unusedcode-->
<rule ref="rulesets/unusedcode.xml/UnusedFormalParameter" />
<rule ref="rulesets/unusedcode.xml/UnusedLocalVariable" />
<!--<rule ref="rulesets/unusedcode.xml/UnusedLocalVariable" />-->
<rule ref="rulesets/unusedcode.xml/UnusedPrivateField" />
<rule ref="rulesets/unusedcode.xml/UnusedPrivateMethod" />
<!--controversial-->
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
ignoreErrors:
- '#Undefined variable#'
26 changes: 19 additions & 7 deletions src/Provide/Router/AuraRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
namespace BEAR\Package\Provide\Router;

use Aura\Router\Exception\RouteNotFound;
use Aura\Router\Map;
use Aura\Router\Route;
use Aura\Router\RouterContainer;
use BEAR\Sunday\Annotation\DefaultSchemeHost;
use BEAR\Sunday\Extension\Router\RouterInterface;
use BEAR\Sunday\Extension\Router\RouterMatch;
use Ray\Di\Di\Named;
use Zend\Diactoros\ServerRequest;

class AuraRouter implements RouterInterface
Expand Down Expand Up @@ -46,15 +48,31 @@ class AuraRouter implements RouterInterface
*/
private $routerContainer;

/**
* @var string
*/
private $routerFile;

/**
* @DefaultSchemeHost("schemeHost")
* @Named("routerFile=aura_router_file")
*/
public function __construct(RouterContainer $routerContainer, string $schemeHost, HttpMethodParamsInterface $httpMethodParams)
public function __construct(RouterContainer $routerContainer, string $schemeHost, HttpMethodParamsInterface $httpMethodParams, string $routerFile)
{
$this->routerContainer = $routerContainer;
$this->matcher = $routerContainer->getMatcher();
$this->schemeHost = $schemeHost;
$this->httpMethodParams = $httpMethodParams;
$this->routerFile = $routerFile;
}

public function __wakeup()
{
$this->routerContainer = new RouterContainer;
/* @global Map $map */
$map = $this->routerContainer->getMap();

require $this->routerFile;
}

/**
Expand Down Expand Up @@ -95,12 +113,6 @@ public function generate($name, $data)

/**
* Return resource request
*
* @param array $globals
* @param array $server
* @param Route $route
*
* @return RouterMatch
*/
private function getRouterMatch(array $globals, array $server, Route $route) : RouterMatch
{
Expand Down
15 changes: 6 additions & 9 deletions src/Provide/Router/AuraRouterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ class AuraRouterProvider implements ProviderInterface
/**
* @DefaultSchemeHost("schemeHost")
*/
public function __construct(AbstractAppMeta $appMeta, $schemeHost, RouterContainer $routerContainer)
public function __construct(AbstractAppMeta $appMeta, string $schemeHost, RouterContainer $routerContainer)
{
$this->schemeHost = $schemeHost;
$this->appMeta = $appMeta;
$this->routerContainer = $routerContainer;
}

/**
* @param Map $router
*
* @Inject
* @Named("router=aura_map,routerFile=aura_router_file")
*/
public function setRouter(Map $router, $routerFile = null)
public function setRouter(string $routerFile = null)
{
$this->router = $router;
$this->routerFile = ($routerFile === null) ? $this->appMeta->appDir . '/var/conf/aura.route.php' : $routerFile;
if (! \file_exists($this->routerFile)) {
throw new InvalidRouterFilePathException($this->routerFile);
}
}

/**
Expand All @@ -66,12 +66,9 @@ public function setRouter(Map $router, $routerFile = null)
public function get()
{
$map = $this->routerContainer->getMap(); // global
if (! file_exists($this->routerFile)) {
throw new InvalidRouterFilePathException($this->routerFile);
}

include $this->routerFile;

return new AuraRouter($this->routerContainer, $this->schemeHost, new HttpMethodParams);
return new AuraRouter($this->routerContainer, $this->schemeHost, new HttpMethodParams, $this->routerFile);
}
}
Empty file.
1 change: 0 additions & 1 deletion tests/Fake/fake-app/var/conf/aura.route.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

/* @var $map \Aura\Router\Map */

$map->route('/blog', '/blog/{id}');
$map->route('/user', '/user/{name}')->tokens(['name' => '[a-z]+']);
31 changes: 0 additions & 31 deletions tests/Fake/fake-app/var/www/index.php

This file was deleted.

Empty file removed tests/Provide/Router/
Empty file.
14 changes: 7 additions & 7 deletions tests/Provide/Router/AuraRouterModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ public function testRoute(AuraRouter $auraRouter)
{
$globals = [
'_GET' => [],
'_POST' => ['title' => 'hello']
'_POST' => []
];
$server = [
'REQUEST_METHOD' => 'POST',
'REQUEST_URI' => 'http://localhost/blog/PC6001?query=value#fragment'
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => 'http://localhost/user/bear'
];
$request = $auraRouter->match($globals, $server);
$this->assertSame('post', $request->method);
$this->assertSame('page://self/blog', $request->path);
$this->assertSame(['id' => 'PC6001', 'title' => 'hello'], $request->query);
$this->assertSame('get', $request->method);
$this->assertSame('page://self/user', $request->path);
$this->assertSame(['name' => 'bear'], $request->query);
}

/**
Expand Down Expand Up @@ -88,7 +88,7 @@ public function testRouterFileNotExsits()
$module = (new AuraRouterModule('__INVALID', new AppModule));
$module->install(new AppMetaModule(new AppMeta('FakeVendor\HelloWorld')));
$injector = new Injector($module);
$router = $injector->getInstance(RouterInterface::class);
$injector->getInstance(RouterInterface::class);
}

public function testRouterFileExsits()
Expand Down
41 changes: 31 additions & 10 deletions tests/Provide/Router/AuraRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AuraRouterTest extends TestCase
/**
* @var Map
*/
private $matcher;
private $map;

/**
* @var AuraRouter
Expand All @@ -26,13 +26,16 @@ public function setUp()
{
parent::setUp();
$routerContainer = new RouterContainer;
$this->matcher = $routerContainer->getMap();
$this->auraRouter = new AuraRouter($routerContainer, 'page://self', new HttpMethodParams);
$map = $routerContainer->getMap();
$this->map = $map;
$routerFile = \dirname(__DIR__, 2) . '/Fake/fake-app/var/conf/aura.route.php';
require $routerFile;
$this->auraRouter = new AuraRouter($routerContainer, 'page://self', new HttpMethodParams, $routerFile);
}

public function testMatch()
{
$this->matcher->route('/blog', '/blog/{id}');
$this->map->route('/blog', '/blog/{id}');
$globals = [
'_GET' => [],
'_POST' => ['title' => 'hello']
Expand All @@ -49,7 +52,7 @@ public function testMatch()

public function testMatchInvalidToken()
{
$this->matcher->route('/blog', '/blog/{id}')->tokens(['id' => '\d+']);
$this->map->route('/blog', '/blog/{id}')->tokens(['id' => '\d+']);
$globals = [
'_GET' => [],
'_POST' => []
Expand All @@ -64,7 +67,7 @@ public function testMatchInvalidToken()

public function testMatchValidToken()
{
$this->matcher->route('/blog', '/blog/{id}')->tokens(['id' => '\d+']);
$this->map->route('/blog', '/blog/{id}')->tokens(['id' => '\d+']);
$globals = [
'_GET' => [],
'_POST' => ['title' => 'hello']
Expand All @@ -80,7 +83,7 @@ public function testMatchValidToken()

public function testMethodOverrideField()
{
$this->matcher->route('/blog', '/blog/{id}');
$this->map->route('/blog', '/blog/{id}');
$globals = [
'_POST' => [AuraRouter::METHOD_FILED => 'PUT', 'title' => 'hello'],
'_GET' => []
Expand All @@ -96,7 +99,7 @@ public function testMethodOverrideField()

public function testMethodOverrideHeader()
{
$this->matcher->route('/blog', '/blog/{id}');
$this->map->route('/blog', '/blog/{id}');
$globals = [
'_POST' => [AuraRouter::METHOD_FILED => 'PUT'],
'_GET' => []
Expand All @@ -113,7 +116,7 @@ public function testMethodOverrideHeader()

public function testNotMatch()
{
$this->matcher->route('/blog', '/blog/{id}');
$this->map->route('/blog', '/blog/{id}');
$globals = [
'_POST' => [],
'_GET' => []
Expand Down Expand Up @@ -142,7 +145,7 @@ public function testInvalidPath()

public function testGenerate()
{
$this->matcher->route('/calendar', '/calendar/{year}/{month}');
$this->map->route('/calendar', '/calendar/{year}/{month}');
$uri = $this->auraRouter->generate('/calendar', ['year' => '8', 'month' => '1']);
$this->assertSame('/calendar/8/1', $uri);
}
Expand All @@ -152,4 +155,22 @@ public function testGenerateFailed()
$uri = $this->auraRouter->generate('/_invalid_', ['year' => '8', 'month' => '1']);
$this->assertFalse($uri);
}

public function testSerialize()
{
/** @var AuraRouter $router */
$router = \unserialize(\serialize($this->auraRouter));
$globals = [
'_GET' => [],
'_POST' => []
];
$server = [
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => 'http://localhost/user/bear'
];
$request = $router->match($globals, $server);
$this->assertSame('get', $request->method);
$this->assertSame('page://self/user', $request->path);
$this->assertSame(['name' => 'bear'], $request->query);
}
}
2 changes: 1 addition & 1 deletion tests/Provide/Router/aura.route.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* @license http://opensource.org/licenses/MIT MIT
*/

/* @var $map \Aura\Router\Map */
/* @var \Aura\Router\Map $map */

$map->route('/user', '/user/{id}');
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
*
* @license http://opensource.org/licenses/MIT MIT
*/
$loader = require dirname(__DIR__) . '/vendor/autoload.php';
$loader = require \dirname(__DIR__) . '/vendor/autoload.php';
/* @var $loader \Composer\Autoload\ClassLoader */
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader([$loader, 'loadClass']);

0 comments on commit c4d22b4

Please sign in to comment.