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

Commit 11042e9

Browse files
committed
first version
1 parent daba838 commit 11042e9

File tree

6 files changed

+316
-0
lines changed

6 files changed

+316
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
composer.phar
2+
composer.lock
3+
/vendor/
4+
.idea/
5+
build/

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "deimos/fast-run",
3+
"license": "MIT",
4+
"authors": [
5+
{
6+
"name": "rez1dent3",
7+
"email": "maksim.babichev95@gmail.com"
8+
}
9+
],
10+
"require": {
11+
"deimos/request": "~0.0",
12+
"deimos/router": "~0.0"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"Deimos\\": "src/"
17+
}
18+
}
19+
}

demo/.htaccess

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RewriteEngine On
2+
RewriteBase /
3+
RewriteCond %{REQUEST_FILENAME} !-f
4+
RewriteRule .* demo/app.php [PT,L,QSA]

demo/app.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
include_once dirname(__DIR__) . '/vendor/autoload.php';
4+
5+
class Run
6+
{
7+
8+
public static function test1()
9+
{
10+
return 'hello';
11+
}
12+
13+
public function test2()
14+
{
15+
return 'hello1';
16+
}
17+
18+
}
19+
20+
$fast = new \Deimos\FastRun\FastRun();
21+
22+
// call static method
23+
$fast->get('/demo/test0', 'Run::test1');
24+
25+
// call static method
26+
$fast->get('/demo/test1', ['Run', 'test1']);
27+
28+
// call method
29+
$fast->get('/demo/test2', ['Run', 'test2']);
30+
31+
$fast->get('/demo/<type>', function (\Deimos\Request\Request $request)
32+
{
33+
return $request->attributes();
34+
}, ['token' => sha1(mt_rand())]);
35+
36+
$fast->method(['POST'], ['/demo/<type>/<id>', ['token' => sha1(mt_rand())]], function (\Deimos\Request\Request $request)
37+
{
38+
return $request->attributes();
39+
});
40+
41+
$fast->all('/demo/<type>/<id>', function (\Deimos\Request\Request $request)
42+
{
43+
return $request->attributes();
44+
});
45+
46+
$fast->error(function (\Deimos\Request\Request $request, \Exception $e)
47+
{
48+
return [
49+
'query' => $request->query(),
50+
'message' => $e->getMessage()
51+
];
52+
});
53+
54+
echo $fast->dispatch();

src/FastRun/Builder.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Deimos\FastRun;
4+
5+
use Deimos\Helper\Helper;
6+
use Deimos\Request\Request;
7+
8+
class Builder extends \Deimos\Builder\Builder
9+
{
10+
11+
/**
12+
* @var array
13+
*/
14+
protected $mapClass = [
15+
'helper' => Helper::class,
16+
'request' => Request::class
17+
];
18+
19+
/**
20+
* @return Helper
21+
*/
22+
public function helper()
23+
{
24+
return $this->once(function ()
25+
{
26+
$class = $this->mapClass['helper'];
27+
28+
return new $class($this);
29+
}, __METHOD__);
30+
}
31+
32+
/**
33+
* @return Request
34+
*/
35+
public function request()
36+
{
37+
return $this->once(function ()
38+
{
39+
$class = $this->mapClass['request'];
40+
41+
return new $class($this->helper());
42+
}, __METHOD__);
43+
}
44+
45+
}

src/FastRun/FastRun.php

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
3+
namespace Deimos\FastRun;
4+
5+
use Deimos\Router\Exceptions\NotFound;
6+
use Deimos\Router\Router;
7+
8+
/**
9+
* Class FastRun
10+
*
11+
* @package Deimos\FastRun
12+
*
13+
* @method $this get(string $route, $callback, array $defaults = [])
14+
* @method $this post(string $route, $callback, array $defaults = [])
15+
* @method $this put(string $route, $callback, array $defaults = [])
16+
* @method $this patch(string $route, $callback, array $defaults = [])
17+
* @method $this delete(string $route, $callback, array $defaults = [])
18+
*/
19+
class FastRun
20+
{
21+
22+
/**
23+
* @var Builder
24+
*/
25+
protected $builder;
26+
27+
/**
28+
* @var array
29+
*/
30+
protected $fastRun = [];
31+
32+
/**
33+
* @var callable
34+
*/
35+
protected $error;
36+
37+
/**
38+
* @var array
39+
*/
40+
protected $mapClass = [
41+
'builder' => Builder::class
42+
];
43+
44+
/**
45+
* @var array
46+
*/
47+
protected $allowMethod = ['get', 'post', 'put', 'patch', 'delete'];
48+
49+
/**
50+
* @return Builder
51+
*/
52+
protected function builder()
53+
{
54+
if (!$this->builder)
55+
{
56+
$class = $this->mapClass['builder'];
57+
58+
$this->builder = new $class();
59+
}
60+
61+
return $this->builder;
62+
}
63+
64+
/**
65+
* @param string $name
66+
* @param array $arguments
67+
*
68+
* @return $this
69+
*
70+
* @throws \BadFunctionCallException
71+
*/
72+
public function __call($name, $arguments)
73+
{
74+
if (!in_array($name, $this->allowMethod, true))
75+
{
76+
throw new \BadFunctionCallException($name);
77+
}
78+
79+
return $this->method(
80+
[strtoupper($name)],
81+
[
82+
$arguments[0],
83+
isset($arguments[2]) ? $arguments[2] : []
84+
],
85+
$arguments[1]
86+
);
87+
}
88+
89+
/**
90+
* @param array $methods
91+
* @param array $route
92+
* @param callable $callback
93+
*
94+
* @return $this
95+
*/
96+
public function method($methods, array $route, $callback)
97+
{
98+
$this->fastRun[$route[0]] = [
99+
'type' => 'pattern',
100+
'path' => $route[0],
101+
'defaults' => isset($route[1]) ? $route[1] : [],
102+
'methods' => $methods,
103+
'callback' => $callback,
104+
];
105+
106+
return $this;
107+
}
108+
109+
/**
110+
* @param string $route
111+
* @param $callback
112+
* @param array $defaults
113+
*
114+
* @return FastRun
115+
*/
116+
public function all($route, $callback, array $defaults = [])
117+
{
118+
return $this->method(null, [$route, $defaults], $callback);
119+
}
120+
121+
/**
122+
* @param callable $callback
123+
*
124+
* @return $this
125+
*/
126+
public function error($callback)
127+
{
128+
$this->error = $callback;
129+
130+
return $this;
131+
}
132+
133+
/**
134+
* @param $callback
135+
* @param $exception
136+
*
137+
* @return string
138+
*/
139+
protected function response($callback, $exception)
140+
{
141+
$request = $this->builder()->request();
142+
143+
$mixed = $callback($request, $exception);
144+
145+
if (is_array($mixed) || is_object($mixed))
146+
{
147+
return $this->builder()->helper()->json()->encode($mixed);
148+
}
149+
150+
return $mixed;
151+
}
152+
153+
/**
154+
* @return string
155+
*
156+
* @throws NotFound
157+
* @throws \Deimos\Route\Exceptions\PathNotFound
158+
* @throws \InvalidArgumentException
159+
*/
160+
public function dispatch()
161+
{
162+
$request = $this->builder()->request();
163+
164+
$router = new Router();
165+
$router->setRoutes($this->fastRun);
166+
167+
$request->setRouter($router);
168+
$exception = null;
169+
170+
try
171+
{
172+
$key = $request->route()->path();
173+
$data = $this->fastRun[$key];
174+
$callback = $data['callback'];
175+
}
176+
catch (NotFound $exception)
177+
{
178+
$callback = $this->error;
179+
180+
if (!$callback)
181+
{
182+
throw $exception;
183+
}
184+
}
185+
186+
return $this->response($callback, $exception);
187+
}
188+
189+
}

0 commit comments

Comments
 (0)