1
|
|
|
<?php |
2
|
|
|
/* Divine CMS - Open source CMS for widespread use. |
3
|
|
|
Copyright (c) 2019 Mykola Burakov ([email protected]) |
4
|
|
|
|
5
|
|
|
See SOURCE.txt for other and additional information. |
6
|
|
|
|
7
|
|
|
This file is part of Divine CMS. |
8
|
|
|
|
9
|
|
|
This program is free software: you can redistribute it and/or modify |
10
|
|
|
it under the terms of the GNU General Public License as published by |
11
|
|
|
the Free Software Foundation, either version 3 of the License, or |
12
|
|
|
(at your option) any later version. |
13
|
|
|
|
14
|
|
|
This program is distributed in the hope that it will be useful, |
15
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
GNU General Public License for more details. |
18
|
|
|
|
19
|
|
|
You should have received a copy of the GNU General Public License |
20
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
21
|
|
|
|
22
|
|
|
namespace Divine\Engine\Core; |
23
|
|
|
|
24
|
|
|
class Action |
25
|
|
|
{ |
26
|
|
|
private $id; |
27
|
|
|
private $route; |
28
|
|
|
private $method = 'index'; |
29
|
|
|
|
30
|
|
|
public function __construct($route) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$this->id = $route; |
33
|
|
|
|
34
|
|
|
$parts = explode('/', preg_replace('/[^a-zA-Z0-9_\/]/', '', (string) $route)); |
35
|
|
|
|
36
|
|
|
// Break apart the route |
37
|
|
|
while ($parts) { |
|
|
|
|
38
|
|
|
$file = SR_APPLICATION . 'controller/' . implode('/', $parts) . '.php'; |
39
|
|
|
|
40
|
|
|
if (is_file($file)) { |
41
|
|
|
$this->route = implode('/', $parts); |
42
|
|
|
|
43
|
|
|
break; |
44
|
|
|
} else { |
45
|
|
|
$this->method = array_pop($parts); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getId() |
51
|
|
|
{ |
52
|
|
|
return $this->id; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function execute($registry, array $args = array()) |
56
|
|
|
{ |
57
|
|
|
// Stop any magical methods being called |
58
|
|
|
if (substr($this->method, 0, 2) == '__') { |
59
|
|
|
return new \Exception('Error: Calls to magic methods are not allowed!'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$file = SR_APPLICATION . 'controller/' . $this->route . '.php'; |
63
|
|
|
$class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $this->route); |
64
|
|
|
|
65
|
|
|
// Initialize the class |
66
|
|
|
if (is_file($file)) { |
67
|
|
|
include_once($file); |
68
|
|
|
|
69
|
|
|
$controller = new $class($registry); |
70
|
|
|
} else { |
71
|
|
|
return new \Exception('Error: Could not call ' . $this->route . '/' . $this->method . '!'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$reflection = new \ReflectionClass($class); |
75
|
|
|
|
76
|
|
|
if ($reflection->hasMethod($this->method) && $reflection->getMethod($this->method)->getNumberOfRequiredParameters() <= count($args)) { |
77
|
|
|
return call_user_func_array(array($controller, $this->method), $args); |
78
|
|
|
} else { |
79
|
|
|
return new \Exception('Error: Could not call ' . $this->route . '/' . $this->method . '!'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|