|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basis; |
|
4
|
|
|
|
|
5
|
|
|
use League\Container\Container; |
|
6
|
|
|
use LogicException; |
|
7
|
|
|
|
|
8
|
|
|
class Http |
|
9
|
|
|
{ |
|
10
|
|
|
private $app; |
|
11
|
|
|
|
|
12
|
3 |
|
public function __construct(Application $app) |
|
13
|
|
|
{ |
|
14
|
3 |
|
$this->app = $app; |
|
15
|
3 |
|
} |
|
16
|
|
|
|
|
17
|
3 |
|
public function process(string $uri): ?string |
|
18
|
|
|
{ |
|
19
|
3 |
|
list($controller, $method) = $this->getChain($uri); |
|
20
|
3 |
|
$className = "Controller\\".ucfirst($controller); |
|
21
|
3 |
|
$class = $this->app->get(Filesystem::class)->completeClassName($className); |
|
22
|
3 |
|
if (!class_exists($class)) { |
|
23
|
1 |
|
$frameworkClass = $this->app->get(Framework::class)->completeClassName($className); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
3 |
|
if (!class_exists($class)) { |
|
27
|
1 |
|
if (!isset($frameworkClass) || !class_exists($frameworkClass)) { |
|
28
|
|
|
if (isset($frameworkClass) && !class_exists($frameworkClass)) { |
|
29
|
|
|
throw new LogicException("No class for $controller $controller, [$class, $frameworkClass]"); |
|
30
|
|
|
} |
|
31
|
|
|
throw new LogicException("No class for $controller $controller - $class"); |
|
32
|
|
|
} |
|
33
|
1 |
|
$class = $frameworkClass; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
if (!method_exists($class, $method)) { |
|
37
|
1 |
|
if (!method_exists($class, '__process')) { |
|
38
|
|
|
return "$controller/$method not found"; |
|
39
|
|
|
} |
|
40
|
1 |
|
$url = substr($uri, strlen($controller)+2); |
|
41
|
1 |
|
$method = '__process'; |
|
42
|
|
|
} else { |
|
43
|
3 |
|
$url = substr($uri, (strlen($controller) + strlen($method) + 3)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
$container = $this->app->get(Container::class); |
|
47
|
3 |
|
$result = $container->call([$container->get($class), $method], ['url' => $url]); |
|
48
|
|
|
|
|
49
|
3 |
|
if (is_array($result) || is_object($result)) { |
|
50
|
1 |
|
return json_encode($result); |
|
51
|
|
|
} else { |
|
52
|
2 |
|
return $result; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
public function getChain(string $uri) : array |
|
57
|
|
|
{ |
|
58
|
3 |
|
list($clean) = explode('?', $uri); |
|
59
|
3 |
|
$chain = []; |
|
60
|
3 |
|
foreach (explode('/', $clean) as $k => $v) { |
|
61
|
3 |
|
if ($v) { |
|
62
|
3 |
|
$chain[] = $v; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
if (!count($chain)) { |
|
67
|
1 |
|
$chain[] = 'index'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
if (count($chain) == 1) { |
|
71
|
2 |
|
$chain[] = 'index'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
return $chain; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|