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
|
1 |
|
public function __construct(Application $app)
|
13
|
|
|
{
|
14
|
1 |
|
$this->app = $app;
|
15
|
1 |
|
}
|
16
|
|
|
|
17
|
1 |
|
public function process($uri)
|
18
|
|
|
{
|
19
|
1 |
|
list($controller, $method) = $this->getChain($uri);
|
20
|
1 |
|
$className = "Controllers\\".ucfirst($controller);
|
21
|
1 |
|
$class = $this->app->get(Filesystem::class)->completeClassName($className);
|
22
|
1 |
|
if(!class_exists($class)) {
|
23
|
|
|
$frameworkClass = $this->app->get(Framework::class)->completeClassName($className);
|
24
|
|
|
}
|
25
|
|
|
|
26
|
1 |
|
if(!class_exists($class)) {
|
27
|
|
|
if(!class_exists($frameworkClass)) {
|
|
|
|
|
28
|
|
|
throw new LogicException("No class for $controller $controller, [$class, $frameworkClass]");
|
29
|
|
|
}
|
30
|
|
|
$class = $frameworkClass;
|
31
|
|
|
}
|
32
|
|
|
|
33
|
1 |
|
$url = '';
|
34
|
1 |
|
if(!method_exists($class, $method)) {
|
35
|
1 |
|
if(!method_exists($class, '__process')) {
|
36
|
|
|
return "$controller/$method not found";
|
37
|
|
|
}
|
38
|
1 |
|
$url = substr($uri, strlen($controller)+2);
|
39
|
1 |
|
$method = '__process';
|
40
|
|
|
}
|
41
|
|
|
|
42
|
1 |
|
$container = $this->app->get(Container::class);
|
43
|
1 |
|
$result = $container->call([$container->get($class), $method], ['url' => $url]);
|
44
|
|
|
|
45
|
1 |
|
if(is_array($result) || is_object($result)) {
|
46
|
|
|
return json_encode($result);
|
47
|
|
|
} else {
|
48
|
1 |
|
return $result;
|
49
|
|
|
}
|
50
|
|
|
}
|
51
|
|
|
|
52
|
1 |
|
public function getChain($uri)
|
53
|
|
|
{
|
54
|
1 |
|
list($clean) = explode('?', $uri);
|
55
|
1 |
|
$chain = explode('/', $clean);
|
56
|
1 |
|
foreach($chain as $k => $v) {
|
57
|
1 |
|
if(!$v) {
|
58
|
1 |
|
unset($chain[$k]);
|
59
|
|
|
}
|
60
|
|
|
}
|
61
|
|
|
|
62
|
1 |
|
$chain = array_values($chain);
|
63
|
|
|
|
64
|
1 |
|
if(!count($chain)) {
|
65
|
1 |
|
$chain[] = 'index';
|
66
|
|
|
}
|
67
|
|
|
|
68
|
1 |
|
if(count($chain) == 1) {
|
69
|
1 |
|
$chain[] = 'index';
|
70
|
|
|
}
|
71
|
|
|
|
72
|
1 |
|
return $chain;
|
73
|
|
|
}
|
74
|
|
|
}
|
75
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: