├── .github └── workflows │ └── qa.yaml ├── README.md ├── composer.json └── src └── StaticRouter.php /.github/workflows/qa.yaml: -------------------------------------------------------------------------------- 1 | name: QA 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - master 7 | 8 | push: 9 | branches: 10 | - master 11 | - v* 12 | 13 | jobs: 14 | tests: 15 | runs-on: ubuntu-latest 16 | strategy: 17 | fail-fast: false 18 | matrix: 19 | php: ['7.1', '7.2', '7.3', '7.4', '8.0'] 20 | 21 | name: PHP ${{ matrix.php }} 22 | 23 | steps: 24 | - uses: actions/checkout@v2 25 | - uses: shivammathur/setup-php@v2 26 | with: 27 | php-version: ${{ matrix.php }} 28 | extensions: Sjson 29 | coverage: none 30 | 31 | - run: composer install --no-interaction --no-progress 32 | - run: vendor/bin/phpstan analyse --level max src 33 | - run: vendor/bin/tester -C tests/cases 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Nextras StaticRouter 2 | ==================== 3 | 4 | [![Build Status](https://github.com/nextras/static-router/workflows/QA/badge.svg?branch=master)](https://github.com/nextras/static-router/actions?query=workflow%3AQA+branch%3Amaster) 5 | [![Downloads this Month](https://img.shields.io/packagist/dm/nextras/static-router.svg?style=flat)](https://packagist.org/packages/nextras/static-router) 6 | [![Stable Version](https://img.shields.io/packagist/v/nextras/static-router.svg?style=flat)](https://packagist.org/packages/nextras/static-router) 7 | 8 | 9 | ### Installation 10 | 11 | Add to your composer.json: 12 | 13 | ``` 14 | "require": { 15 | "nextras/static-router": "~2.0" 16 | } 17 | ``` 18 | 19 | 20 | ### Example 21 | 22 | ```php 23 | use Nextras\Routing\StaticRouter; 24 | 25 | $router = new StaticRouter(['Homepage:default' => 'index.php'], StaticRouter::ONE_WAY); 26 | 27 | $router = new StaticRouter([ 28 | 'Homepage:default' => '', 29 | 'Auth:signIn' => 'sign-in', 30 | 'Auth:signOut' => 'sign-out', 31 | 'Auth:signUp' => 'sign-up', 32 | ]); 33 | ``` 34 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nextras/static-router", 3 | "type": "library", 4 | "description": "Simple static router for Nette Framework", 5 | "keywords": ["nette", "router", "nextras"], 6 | "homepage": "https://github.com/nextras/static-router", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Nextras Community", 11 | "homepage": "https://github.com/nextras/static-router/graphs/contributors" 12 | } 13 | ], 14 | "support": { 15 | "issues": "https://github.com/nextras/static-router/issues" 16 | }, 17 | "require": { 18 | "php": ">=7.1", 19 | "nette/http": "~3.0", 20 | "nette/routing": "~3.0" 21 | }, 22 | "require-dev": { 23 | "nette/tester": "~2.0", 24 | "phpstan/phpstan": "^0.12" 25 | }, 26 | "autoload": { 27 | "psr-4": { "Nextras\\Routing\\": "src/" } 28 | }, 29 | "extra": { 30 | "branch-alias": { 31 | "dev-master": "2.0.x-dev" 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/StaticRouter.php: -------------------------------------------------------------------------------- 1 | (Presenter:action => slug) */ 16 | private $tableOut; 17 | 18 | /** @var int */ 19 | private $flags; 20 | 21 | /** @var UrlScript */ 22 | private $lastRefUrl; 23 | 24 | /** @var string */ 25 | private $lastBaseUrl; 26 | 27 | 28 | /** 29 | * @param array $routingTable Presenter:action => slug 30 | * @param int $flags Router::ONE_WAY 31 | */ 32 | public function __construct(array $routingTable, int $flags = 0) 33 | { 34 | $this->tableOut = $routingTable; 35 | $this->flags = $flags; 36 | } 37 | 38 | 39 | /** 40 | * Maps HTTP request to a Request object. 41 | * @return array 42 | */ 43 | public function match(HttpRequest $httpRequest): ?array 44 | { 45 | $slug = rtrim($httpRequest->getUrl()->getRelativePath(), '/'); 46 | foreach ($this->tableOut as $destination2 => $slug2) { 47 | if ($slug === rtrim($slug2, '/')) { 48 | $destination = (string) $destination2; 49 | break; 50 | } 51 | } 52 | 53 | if (!isset($destination)) { 54 | return null; 55 | } 56 | 57 | $params = $httpRequest->getQuery(); 58 | $pos = strrpos($destination, ':'); 59 | $params['presenter'] = substr($destination, 0, (int) $pos); 60 | $params['action'] = substr($destination, $pos + 1); 61 | 62 | return $params; 63 | } 64 | 65 | 66 | /** 67 | * Constructs absolute URL from Request object. 68 | * @param array $params 69 | */ 70 | public function constructUrl(array $params, UrlScript $refUrl): ?string 71 | { 72 | if ($this->flags & self::ONE_WAY) { 73 | return null; 74 | } 75 | 76 | if (!isset($params['action'], $params['presenter']) || !is_string($params['action']) || !is_string($params['presenter'])) { 77 | return null; 78 | } 79 | 80 | $key = $params['presenter'] . ':' . $params['action']; 81 | if (!isset($this->tableOut[$key])) { 82 | return null; 83 | } 84 | 85 | if ($this->lastRefUrl !== $refUrl) { 86 | $this->lastBaseUrl = $refUrl->getBaseUrl(); 87 | $this->lastRefUrl = $refUrl; 88 | } 89 | 90 | unset($params['presenter'], $params['action']); 91 | $slug = $this->tableOut[$key]; 92 | $query = (($tmp = http_build_query($params)) ? '?' . $tmp : ''); 93 | $url = $this->lastBaseUrl . $slug . $query; 94 | 95 | return $url; 96 | } 97 | } 98 | --------------------------------------------------------------------------------