|
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\CallableRepository; |
|
27
|
|
|
|
|
28
|
|
|
class CallableDir extends RequestPlugin |
|
29
|
|
|
{ |
|
30
|
|
|
use \Jaxon\Utils\Traits\Translator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The callable repository |
|
34
|
|
|
* |
|
35
|
|
|
* @var CallableRepository |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $repository = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* True if the Composer autoload is enabled |
|
41
|
|
|
* |
|
42
|
|
|
* @var boolean |
|
43
|
|
|
*/ |
|
44
|
|
|
private $bAutoloadEnabled = true; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The Composer autoloader |
|
48
|
|
|
* |
|
49
|
|
|
* @var Autoloader |
|
50
|
|
|
*/ |
|
51
|
|
|
private $xAutoloader = null; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The class constructor |
|
55
|
|
|
* |
|
56
|
|
|
* @param CallableRepository $repository |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct(CallableRepository $repository) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->repository = $repository; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return the name of this plugin |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getName() |
|
69
|
|
|
{ |
|
70
|
|
|
return Jaxon::CALLABLE_DIR; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Use the Composer autoloader |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function useComposerAutoloader() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->bAutoloadEnabled = true; |
|
81
|
|
|
$this->xAutoloader = require(__DIR__ . '/../../../../autoload.php'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Disable the autoloader in the library |
|
86
|
|
|
* |
|
87
|
|
|
* The user shall provide an alternative autoload system. |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function disableAutoload() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->bAutoloadEnabled = false; |
|
94
|
|
|
$this->xAutoloader = null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Register a callable class |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $sType The type of request handler being registered |
|
101
|
|
|
* @param string $sDirectory The name of the class being registered |
|
102
|
|
|
* @param array|string $aOptions The associated options |
|
103
|
|
|
* |
|
104
|
|
|
* @return boolean |
|
105
|
|
|
*/ |
|
106
|
|
|
public function register($sType, $sDirectory, $aOptions) |
|
107
|
|
|
{ |
|
108
|
|
|
if($sType != $this->getName()) |
|
109
|
|
|
{ |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if(!is_string($sDirectory) || !is_dir($sDirectory)) |
|
114
|
|
|
{ |
|
115
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
|
116
|
|
|
} |
|
117
|
|
|
if(is_string($aOptions)) |
|
118
|
|
|
{ |
|
119
|
|
|
$aOptions = ['namespace' => $aOptions]; |
|
120
|
|
|
} |
|
121
|
|
|
if(!is_array($aOptions)) |
|
122
|
|
|
{ |
|
123
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$sDirectory = rtrim(trim($sDirectory), DIRECTORY_SEPARATOR); |
|
127
|
|
|
if(!is_dir($sDirectory)) |
|
128
|
|
|
{ |
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
$aOptions['directory'] = $sDirectory; |
|
132
|
|
|
|
|
133
|
|
|
$sNamespace = key_exists('namespace', $aOptions) ? $aOptions['namespace'] : ''; |
|
134
|
|
|
if(!($sNamespace = trim($sNamespace, ' \\'))) |
|
135
|
|
|
{ |
|
136
|
|
|
$sNamespace = ''; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// $sSeparator = key_exists('separator', $aOptions) ? $aOptions['separator'] : '.'; |
|
140
|
|
|
// // Only '.' and '_' are allowed to be used as separator. Any other value is ignored and '.' is used instead. |
|
141
|
|
|
// if(($sSeparator = trim($sSeparator)) != '_') |
|
142
|
|
|
// { |
|
143
|
|
|
// $sSeparator = '.'; |
|
144
|
|
|
// } |
|
145
|
|
|
|
|
146
|
|
|
// Change the keys in $aOptions to have "\" as separator |
|
147
|
|
|
$_aOptions = []; |
|
148
|
|
|
foreach($aOptions as $sName => $aOption) |
|
149
|
|
|
{ |
|
150
|
|
|
$sName = trim(str_replace('.', '\\', $sName), ' \\'); |
|
151
|
|
|
$_aOptions[$sName] = $aOption; |
|
152
|
|
|
} |
|
153
|
|
|
$aOptions = $_aOptions; |
|
154
|
|
|
|
|
155
|
|
|
if(($sNamespace)) |
|
156
|
|
|
{ |
|
157
|
|
|
// Register the dir with PSR4 autoloading |
|
158
|
|
|
if(($this->xAutoloader)) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->xAutoloader->setPsr4($sNamespace . '\\', $sDirectory); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$this->repository->addNamespace($sNamespace, $aOptions); |
|
164
|
|
|
} |
|
165
|
|
|
else |
|
166
|
|
|
{ |
|
167
|
|
|
// Use underscore as separator, so there's no need to deal with namespace |
|
168
|
|
|
// when generating javascript code. |
|
169
|
|
|
$aOptions['separator'] = '_'; |
|
170
|
|
|
$aOptions['autoload'] = $this->bAutoloadEnabled; |
|
171
|
|
|
|
|
172
|
|
|
$this->repository->addDirectory($sDirectory, $aOptions); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return true; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Generate a hash for the registered callable objects |
|
180
|
|
|
* |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
public function generateHash() |
|
184
|
|
|
{ |
|
185
|
|
|
return ''; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Generate client side javascript code for the registered callable objects |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getScript() |
|
194
|
|
|
{ |
|
195
|
|
|
return ''; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Check if this plugin can process the incoming Jaxon request |
|
200
|
|
|
* |
|
201
|
|
|
* @return boolean |
|
202
|
|
|
*/ |
|
203
|
|
|
public function canProcessRequest() |
|
204
|
|
|
{ |
|
205
|
|
|
return false; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Process the incoming Jaxon request |
|
210
|
|
|
* |
|
211
|
|
|
* @return boolean |
|
212
|
|
|
*/ |
|
213
|
|
|
public function processRequest() |
|
214
|
|
|
{ |
|
215
|
|
|
return false; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|