1 | <?php |
||
8 | class Http |
||
9 | { |
||
10 | private $app; |
||
11 | |||
12 | 3 | public function __construct(Application $app) |
|
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 |
|
76 | } |
||
77 |