Skip to content
This repository was archived by the owner on Aug 13, 2023. It is now read-only.

Commit 7bd851c

Browse files
authored
Merge pull request #4 from DeimosProject/dev
Global Upgrade
2 parents e20321c + 0bd8390 commit 7bd851c

17 files changed

Lines changed: 1221 additions & 497 deletions

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
build:
22
environment:
33
php:
4-
version: 5.6
4+
version: 7.0.8
55
tests:
66
override:
77
-

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: php
22
php:
3-
- '5.6'
43
- '7.0'
54
- '7.1'
65
- nightly

composer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@
1111
"authors": [
1212
{
1313
"name": "rez1dent3",
14-
"email": "maksim.babichev95@gmail.com"
14+
"email": "info@babichev.net"
1515
}
1616
],
1717
"require": {
18-
"deimos/route": "~0.1"
18+
"deimos/slice": "~1.0",
19+
"deimos/cache-helper": "~1.0"
1920
},
2021
"require-dev": {
21-
"phpunit/phpunit": "5.7.5"
22+
"phpunit/phpunit": "5.7.5",
23+
"deimos/config": "~2.0"
2224
},
2325
"autoload": {
2426
"psr-4": {
2527
"Deimos\\": "src/"
26-
}
28+
},
29+
"files": [
30+
"libs/helper.php"
31+
]
2732
}
2833
}

demo/global.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
return [
4+
5+
'http' => [
6+
7+
'type' => 'http',
8+
9+
'scheme' => 'http', // optional null
10+
'domain' => '(.+\.)example.com', // optional null
11+
12+
'resolver' => [
13+
14+
'cp' => [
15+
'type' => 'prefix',
16+
'path' => '(/<language:[a-z]{2}>)/cp',
17+
18+
'resolver' => [
19+
20+
'dashboard' => [
21+
'type' => 'pattern',
22+
'path' => '/dashboard',
23+
24+
'defaults' => [
25+
'controller' => 'dashboard'
26+
]
27+
],
28+
29+
'post' => [
30+
'type' => 'pattern',
31+
'path' => '/post(/<id>)',
32+
],
33+
34+
'posts' => [
35+
'type' => 'pattern',
36+
'path' => '/posts(/<id>(/<type>))',
37+
38+
'defaults' => [
39+
'type' => 'list',
40+
'controller' => 'posts'
41+
],
42+
43+
'methods' => ['GET']
44+
],
45+
46+
],
47+
48+
'defaults' => [
49+
'controller' => 'post',
50+
'processor' => 'cp',
51+
'action' => 'default',
52+
'language' => 'en'
53+
]
54+
]
55+
56+
],
57+
58+
'methods' => ['POST']
59+
60+
],
61+
62+
'admin' => [
63+
'type' => 'prefix',
64+
'path' => '/admin',
65+
66+
'resolver' => [
67+
68+
'dashboard' => [
69+
'type' => 'pattern',
70+
'path' => '/dashboard',
71+
72+
'defaults' => [
73+
'controller' => 'dashboard'
74+
]
75+
],
76+
77+
],
78+
],
79+
80+
'default' => [
81+
'type' => 'pattern',
82+
'path' => [
83+
'/<any:\w+>',
84+
[
85+
'any' => '.*'
86+
]
87+
],
88+
89+
'defaults' => [
90+
'controller' => 'hello',
91+
'action' => 'world',
92+
'any' => 'hello-world'
93+
],
94+
95+
]
96+
97+
];

demo/index.php

Lines changed: 20 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,23 @@
22

33
include_once __DIR__ . '/../vendor/autoload.php';
44

5-
$router = new \Deimos\Router\Router();
6-
7-
$router->setMethod('GET');
8-
9-
10-
//$route = new \Deimos\Route\Route(
11-
// ['(/<lang:[a-z]{2}>)/<controller>(/<action>(/<id:\d+>))'],
12-
// [
13-
// 'lang' => 'ru',
14-
// 'action' => 'default'
15-
// ]
16-
//);
17-
//$router->addRoute($route); // OR
18-
19-
$router->setRoutes([
20-
[
21-
'type' => 'pattern',
22-
'path' => [
23-
'(/<lang:[a-z]{2}>)/<controller>(/<action>(/<id:\d+>))',
24-
[
25-
'controller' => '[\w-<>!12]+'
26-
]
27-
],
28-
29-
'defaults' => [
30-
// 'lang' => 'ru',
31-
'action' => 'default'
32-
]
33-
],
34-
[
35-
'type' => 'prefix',
36-
'path' => '/get/',
37-
38-
'resolver' => [
39-
[
40-
'type' => 'pattern',
41-
'path' => 'image/<hash:[a-z0-9]+/[a-z0-9]+>/<frame:\d+><ext:\.(png|jpe?g)>',
42-
43-
'methods' => [
44-
'GET'
45-
],
46-
47-
'defaults' => [
48-
'action' => 'image',
49-
]
50-
],
51-
[
52-
'type' => 'pattern',
53-
'path' => 'file/<size>/<path:.*>',
54-
55-
'defaults' => [
56-
'action' => 'file',
57-
]
58-
],
59-
],
60-
61-
'defaults' => [
62-
'controller' => 'file',
63-
'runner' => 'pub',
64-
'notFound' => 'default'
65-
]
66-
]
67-
]);
68-
69-
var_dump($route = $router->getCurrentRoute('/get/image/ab/cd/0.png'), $route->attributes());
70-
var_dump($route = $router->getCurrentRoute('/hello-world'), $route->attributes());
71-
var_dump($route = $router->getCurrentRoute('/en/hello-world'), $route->attributes());
72-
die;
5+
$builder = new Deimos\Builder\Builder();
6+
$helper = new \Deimos\Helper\Helper($builder);
7+
$config = new \Deimos\Config\Config($helper, __DIR__);
8+
9+
$slice = $config->get('global');
10+
11+
$cache = new \Deimos\CacheHelper\SliceHelper(__DIR__ . '/cache');
12+
$router = new \Deimos\Router\Router($slice, $cache);
13+
14+
$route = $router->getCurrentRoute();
15+
16+
var_dump(
17+
\Deimos\Router\route($route),
18+
$route->getAttributes(),
19+
$route->getDefaults(),
20+
$route->getHttp(),
21+
$route->getPath(),
22+
$route->getRegex(),
23+
$route->getRegexPath()
24+
);

libs/helper.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Deimos\Router;
4+
5+
/**
6+
* @param string $variableName
7+
* @param mixed $default
8+
*
9+
* @return mixed
10+
*/
11+
function server($variableName, $default = null)
12+
{
13+
static $storage = [];
14+
15+
if (!isset($storage[$variableName]))
16+
{
17+
$storage[$variableName] =
18+
filter_input(INPUT_SERVER, $variableName) ??
19+
$default;
20+
}
21+
22+
return $storage[$variableName];
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
function method()
29+
{
30+
return server('REQUEST_METHOD');
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
function scheme()
37+
{
38+
$scheme = server('HTTP_CF_VISITOR'); // cloudFlare
39+
40+
if ($scheme)
41+
{
42+
/**
43+
* { scheme: "https" }
44+
*
45+
* @var string $scheme
46+
*/
47+
$scheme = json_decode($scheme);
48+
}
49+
50+
return $scheme['scheme'] ??
51+
server('HTTP_X_FORWARDED_PROTO') ??
52+
server('REQUEST_SCHEME');
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
function domain()
59+
{
60+
return server('HTTP_HOST');
61+
}
62+
63+
/**
64+
* @return string
65+
*/
66+
function path()
67+
{
68+
return server('REQUEST_URI');
69+
}
70+
71+
/**
72+
* @param Route $route
73+
* @param array $attributes
74+
*
75+
* @return string
76+
*/
77+
function route(Route $route, array $attributes = [])
78+
{
79+
$attributes = $route->getDefaults() + $attributes;
80+
81+
$path = preg_replace_callback('~\<(\w+)\>~', function ($matches) use ($attributes)
82+
{
83+
return $attributes[$matches[1]] ?? null;
84+
}, $route->getFilterPath());
85+
86+
return preg_replace('~(\(/\)|\(|\)|//)~', '', $path);
87+
}

0 commit comments

Comments
 (0)