This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.php
179 lines (129 loc) · 3.97 KB
/
Action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* @package DF_Web
*/
require_once 'DF/Web/Routing/ActionArgs.php';
require_once 'DF/Web/Routing/ActionArgs/Any.php';
/**
*/
class DF_Web_Routing_Action {
public static $LOGGER = NULL;
protected $name = NULL;
/**
* @var DF_Web_Path
*/
protected $path = NULL;
protected $args = NULL;
protected $controller_path = NULL;
protected $controller_name = NULL;
/**
* Constructor.
*
* @param DF_Web_Routing_Config_Action $config
* @return DF_Web_Routing_Action
*/
public function __construct($config) {
if (! $config instanceof DF_Web_Routing_Config_Action) {
throw new DF_Error_InvalidArgumentException("config", $config, DF_Web_Routing_Config_Action);
}
$ctrl = $config->get_controller();
$this->controller_name = $ctrl->get_name();
$this->controller_path = $ctrl->get_path();
$this->name = $config->get_name();
$this->path = $config->get_path();
$this->args = $this->prepare_args($config);
$this->init_local($config);
}
/**
*
* @param DF_Web_Routing_Config_Action $config
*/
protected function init_local($config) {}
protected function prepare_args($config) {
$args = NULL;
if ($config->has('args')) {
$numargs = $config->get('args');
if (is_int($numargs)) {
$args = new DF_Web_Routing_ActionArgs($numargs);
}
else {
$args = new DF_Web_Routing_ActionArgs_Any();
}
}
return $args;
}
public function get_path_match() {
$stars = array();
$args = $this->args;
if ($args instanceof DF_Web_Routing_ActionArgs_Any) {
$stars[] = "**";
}
else {
for ($i=0; $i<$args->get_numargs(); $i++) {
$stars[] = "*";
}
}
$argspath = join("/", $stars);
$path = $this->get_path();
if (!strlen("$path")) {
$path = $this->controller_path;
}
elseif (!$path->is_absolute()) {
$path = $this->controller_path->append_path($path);
}
if ($argspath) {
$path = $path->append_path(DF_Web_Path::fromString($argspath));
}
if (!$path->is_absolute()) {
$root = DF_Web_Path::fromString("/");
$path = $root->append_path($path);
}
return $path;
}
public function get_controller_name() {
return $this->controller_name;
}
public function get_private_path() {
$name = $this->get_name();
$ctrlpath = $this->controller_path;
$private_path = "$name";
if (strlen($private_path)) {
$private_path = "/$private_path";
}
if ("$ctrlpath") {
$private_path = "/$ctrlpath$private_path";
}
return DF_Web_Path::fromString($private_path);
}
public function get_args() {
return $this->args;
}
public function get_name() {
return $this->name;
}
public function get_path() {
return $this->path;
}
public function get_method() {
$name = $this->name;
$method = "handle_$name";
return $method;
}
public function get_code_ref() {
$ctrl = $this->controller_name."";
$method = $this->get_method();
$ref = array($ctrl, $method);
return $ref;
}
public function __toString() {
$ctrl = $this->controller_name;
$method = $this->get_method();
$path_match = $this->get_path_match();
$name = $this->name;
$path = $this->path;
$args = $this->args;
$str = "$ctrl::$method Pathmatch: $path_match, Name: $name, Path: $path, Args: $args";
return $str;
}
}
DF_Web_Routing_Action::$LOGGER = DF_Web_Logger::logger('DF_Web_Routing_Action');