|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* CallableDir.php - Jaxon callable dir plugin |
|
5
|
|
|
* |
|
6
|
|
|
* This class registers directories containing user defined callable classes, |
|
7
|
|
|
* and generates client side javascript code. |
|
8
|
|
|
* |
|
9
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
10
|
|
|
* @author Jared White |
|
|
|
|
|
|
11
|
|
|
* @author J. Max Wilson |
|
|
|
|
|
|
12
|
|
|
* @author Joseph Woolley |
|
|
|
|
|
|
13
|
|
|
* @author Steffen Konerow |
|
|
|
|
|
|
14
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
15
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
|
|
16
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
|
|
17
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
18
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
19
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
20
|
|
|
*/ |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
namespace Jaxon\Request\Plugin; |
|
23
|
|
|
|
|
24
|
|
|
use Jaxon\Jaxon; |
|
25
|
|
|
use Jaxon\Plugin\Request as RequestPlugin; |
|
26
|
|
|
use Jaxon\Request\Support\CallableRegistry; |
|
27
|
|
|
|
|
28
|
|
|
class CallableDir extends RequestPlugin |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
use \Jaxon\Features\Translator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The callable registrar |
|
34
|
|
|
* |
|
35
|
|
|
* @var CallableRegistry |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $xRegistry; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The class constructor |
|
41
|
|
|
* |
|
42
|
|
|
* @param CallableRegistry $xRegistry |
|
|
|
|
|
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(CallableRegistry $xRegistry) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->xRegistry = $xRegistry; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Return the name of this plugin |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getName() |
|
55
|
|
|
{ |
|
56
|
|
|
return Jaxon::CALLABLE_DIR; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Check the directory |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $sDirectory The path of teh directory being registered |
|
|
|
|
|
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
* @throws \Jaxon\Exception\Error |
|
66
|
|
|
*/ |
|
67
|
|
|
private function checkDirectory($sDirectory) |
|
68
|
|
|
{ |
|
69
|
|
|
if(!is_string($sDirectory)) |
|
70
|
|
|
{ |
|
71
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
|
72
|
|
|
} |
|
73
|
|
|
$sDirectory = rtrim(trim($sDirectory), '/\\'); |
|
|
|
|
|
|
74
|
|
|
if(!is_dir($sDirectory)) |
|
75
|
|
|
{ |
|
76
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
|
77
|
|
|
} |
|
78
|
|
|
return realpath($sDirectory); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Check the options |
|
83
|
|
|
* |
|
84
|
|
|
* @param array|string $aOptions The associated options |
|
|
|
|
|
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
* @throws \Jaxon\Exception\Error |
|
88
|
|
|
*/ |
|
89
|
|
|
private function checkOptions($aOptions) |
|
90
|
|
|
{ |
|
91
|
|
|
if(is_string($aOptions)) |
|
92
|
|
|
{ |
|
93
|
|
|
$aOptions = ['namespace' => $aOptions]; |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
if(!is_array($aOptions)) |
|
96
|
|
|
{ |
|
97
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Change the keys in $aOptions to have "\" as separator |
|
101
|
|
|
$_aOptions = []; |
|
102
|
|
|
foreach($aOptions as $sName => $aOption) |
|
103
|
|
|
{ |
|
104
|
|
|
$sName = trim(str_replace('.', '\\', $sName), ' \\'); |
|
|
|
|
|
|
105
|
|
|
$_aOptions[$sName] = $aOption; |
|
106
|
|
|
} |
|
107
|
|
|
return $_aOptions; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Register a callable class |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $sType The type of request handler being registered |
|
|
|
|
|
|
114
|
|
|
* @param string $sDirectory The path of teh directory being registered |
|
|
|
|
|
|
115
|
|
|
* @param array|string $aOptions The associated options |
|
|
|
|
|
|
116
|
|
|
* |
|
117
|
|
|
* @return boolean |
|
118
|
|
|
*/ |
|
119
|
|
|
public function register($sType, $sDirectory, $aOptions) |
|
120
|
|
|
{ |
|
121
|
|
|
$sType = trim($sType); |
|
|
|
|
|
|
122
|
|
|
if($sType != $this->getName()) |
|
123
|
|
|
{ |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$sDirectory = $this->checkDirectory($sDirectory); |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
$aOptions = $this->checkOptions($aOptions); |
|
|
|
|
|
|
130
|
|
|
$aOptions['directory'] = $sDirectory; |
|
131
|
|
|
|
|
132
|
|
|
$sNamespace = key_exists('namespace', $aOptions) ? $aOptions['namespace'] : ''; |
|
133
|
|
|
if(!($sNamespace = trim($sNamespace, ' \\'))) |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
|
|
$sNamespace = ''; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if(($sNamespace)) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->xRegistry->addNamespace($sNamespace, $aOptions); |
|
141
|
|
|
} |
|
142
|
|
|
else |
|
143
|
|
|
{ |
|
144
|
|
|
$this->xRegistry->addDirectory($sDirectory, $aOptions); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Generate a hash for the registered callable objects |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
public function generateHash() |
|
156
|
|
|
{ |
|
157
|
|
|
return ''; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Generate client side javascript code for the registered callable objects |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getScript() |
|
166
|
|
|
{ |
|
167
|
|
|
return ''; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Check if this plugin can process the incoming Jaxon request |
|
172
|
|
|
* |
|
173
|
|
|
* @return boolean |
|
174
|
|
|
*/ |
|
175
|
|
|
public function canProcessRequest() |
|
176
|
|
|
{ |
|
177
|
|
|
return false; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Process the incoming Jaxon request |
|
182
|
|
|
* |
|
183
|
|
|
* @return boolean |
|
184
|
|
|
*/ |
|
185
|
|
|
public function processRequest() |
|
186
|
|
|
{ |
|
187
|
|
|
return false; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|