|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pimf |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Pimf; |
|
10
|
|
|
|
|
11
|
|
|
use Pimf\Resolver\Exception as Bomb; |
|
12
|
|
|
use Pimf\Util\Character as Str; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Resolves the user requests to controller and action. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Pimf |
|
18
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class Resolver |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $controllerPath; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $controllerClass; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $repositoryPath; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var Request |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $request; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Router |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $router; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Request $request |
|
49
|
|
|
* @param string $repositoryPath |
|
50
|
|
|
* @param string $prefix |
|
51
|
|
|
* @param \Pimf\Router $router |
|
52
|
|
|
* |
|
53
|
|
|
* @throws \Pimf\Resolver\Exception If no controller found at the repository path |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(\Pimf\Request $request, $repositoryPath = '/Controller', $prefix = 'Pimf\\', $router) |
|
56
|
|
|
{ |
|
57
|
|
|
$controllerName = $request->fromGet()->get('controller'); |
|
58
|
|
|
$this->router = $router; |
|
59
|
|
|
|
|
60
|
|
|
if (Config::get('app.routeable') === true) { |
|
61
|
|
|
|
|
62
|
|
|
$target = $this->router->find(); |
|
63
|
|
|
|
|
64
|
|
|
if ($target instanceof \Pimf\Route\Target) { |
|
65
|
|
|
$controllerName = $target->getController(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (Sapi::isCli() && Config::get('environment') == 'production') { |
|
70
|
|
|
$controllerName = $request->fromCli()->get('controller'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (!$controllerName) { |
|
74
|
|
|
$controllerName = Config::get('app.default_controller'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->repositoryPath = $repositoryPath; |
|
78
|
|
|
$this->request = $request; |
|
79
|
|
|
$this->controllerClass = $prefix . 'Controller\\'; |
|
80
|
|
|
|
|
81
|
|
|
$basepath = $this->repositoryPath . '/'; |
|
82
|
|
|
$controller = ucfirst($controllerName); |
|
83
|
|
|
|
|
84
|
|
|
if (Str::isEvilPath($basepath . $controller)) { |
|
85
|
|
|
throw new Bomb('directory traversal attack is not funny!'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->controllerPath = $basepath . $controller . '.php'; |
|
89
|
|
|
|
|
90
|
|
|
if (!file_exists($this->controllerPath)) { |
|
91
|
|
|
throw new Bomb('no "' . $controller . '" controller found at the repository path'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param Environment $env |
|
97
|
|
|
* @param Logger $logger |
|
98
|
|
|
* @param EntityManager $em |
|
99
|
|
|
* |
|
100
|
|
|
* @return \Pimf\Controller\Base |
|
101
|
|
|
* @throws \Pimf\Resolver\Exception If no controller specified or no controller found at the repository. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function process(Environment $env, $em, Logger $logger) |
|
104
|
|
|
{ |
|
105
|
|
|
$path = str_replace($this->repositoryPath, '', $this->controllerPath); |
|
106
|
|
|
$name = str_replace('/', $this->controllerClass, $path); |
|
107
|
|
|
$controller = str_replace('.php', '', $name); |
|
108
|
|
|
|
|
109
|
|
|
if (!class_exists($controller)) { |
|
110
|
|
|
throw new Bomb('can not load class "' . $controller . '" from the repository'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return new $controller( |
|
114
|
|
|
$this->request, new Response($this->request->getMethod()), $logger, $em, $this->router, $env |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|