|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* CallableDirPlugin.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\Plugin\Request\CallableClass; |
|
23
|
|
|
|
|
24
|
|
|
use Jaxon\Jaxon; |
|
25
|
|
|
use Jaxon\App\I18n\Translator; |
|
26
|
|
|
use Jaxon\Exception\SetupException; |
|
27
|
|
|
use Jaxon\Plugin\CallableRegistryInterface; |
|
28
|
|
|
use Jaxon\Plugin\PluginInterface; |
|
29
|
|
|
|
|
30
|
|
|
use function is_array; |
|
31
|
|
|
use function is_dir; |
|
32
|
|
|
use function is_string; |
|
33
|
|
|
use function realpath; |
|
34
|
|
|
use function rtrim; |
|
35
|
|
|
use function str_replace; |
|
36
|
|
|
use function trim; |
|
37
|
|
|
|
|
38
|
|
|
class CallableDirPlugin implements PluginInterface, CallableRegistryInterface |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* The callable registry |
|
42
|
|
|
* |
|
43
|
|
|
* @var CallableRegistry |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $xRegistry; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Translator |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $xTranslator; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The class constructor |
|
54
|
|
|
* |
|
55
|
|
|
* @param CallableRegistry $xRegistry |
|
|
|
|
|
|
56
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct(CallableRegistry $xRegistry, Translator $xTranslator) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$this->xRegistry = $xRegistry; |
|
|
|
|
|
|
61
|
|
|
$this->xTranslator = $xTranslator; |
|
62
|
|
|
} |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritDoc |
|
66
|
|
|
*/ |
|
|
|
|
|
|
67
|
|
|
public function getName(): string |
|
68
|
|
|
{ |
|
69
|
|
|
return Jaxon::CALLABLE_DIR; |
|
70
|
|
|
} |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Check the directory |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $sDirectory The path of teh directory being registered |
|
|
|
|
|
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
* @throws SetupException |
|
79
|
|
|
*/ |
|
80
|
|
|
private function checkDirectory(string $sDirectory): string |
|
81
|
|
|
{ |
|
82
|
|
|
$sDirectory = rtrim(trim($sDirectory), '/\\'); |
|
83
|
|
|
if(!is_dir($sDirectory)) |
|
84
|
|
|
{ |
|
85
|
|
|
throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
|
86
|
|
|
} |
|
87
|
|
|
return realpath($sDirectory); |
|
88
|
|
|
} |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
|
|
|
|
|
91
|
|
|
* @inheritDoc |
|
92
|
|
|
* @throws SetupException |
|
93
|
|
|
*/ |
|
|
|
|
|
|
94
|
|
|
public function checkOptions(string $sCallable, $xOptions): array |
|
95
|
|
|
{ |
|
96
|
|
|
if(is_string($xOptions)) |
|
97
|
|
|
{ |
|
98
|
|
|
$xOptions = ['namespace' => $xOptions]; |
|
99
|
|
|
} |
|
100
|
|
|
if(!is_array($xOptions)) |
|
101
|
|
|
{ |
|
102
|
|
|
throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
|
103
|
|
|
} |
|
104
|
|
|
// Check the directory |
|
105
|
|
|
$xOptions['directory'] = $this->checkDirectory($sCallable); |
|
106
|
|
|
// Check the namespace |
|
107
|
|
|
$sNamespace = $xOptions['namespace'] ?? ''; |
|
108
|
|
|
if(!($xOptions['namespace'] = trim($sNamespace, ' \\'))) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$xOptions['namespace'] = ''; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Change the keys in $xOptions to have "\" as separator |
|
114
|
|
|
$_aOptions = []; |
|
115
|
|
|
foreach($xOptions as $sName => $aOption) |
|
116
|
|
|
{ |
|
117
|
|
|
$sName = trim(str_replace('.', '\\', $sName), ' \\'); |
|
|
|
|
|
|
118
|
|
|
$_aOptions[$sName] = $aOption; |
|
119
|
|
|
} |
|
120
|
|
|
return $_aOptions; |
|
121
|
|
|
} |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
|
|
|
|
|
124
|
|
|
* @inheritDoc |
|
125
|
|
|
*/ |
|
|
|
|
|
|
126
|
|
|
public function register(string $sType, string $sCallable, array $aOptions): bool |
|
127
|
|
|
{ |
|
128
|
|
|
if(($aOptions['namespace'])) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->xRegistry->addNamespace($aOptions['namespace'], $aOptions); |
|
131
|
|
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
$this->xRegistry->addDirectory($aOptions['directory'], $aOptions); |
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
|
|
|
|
|
138
|
|
|
* @inheritDoc |
|
139
|
|
|
* @throws SetupException |
|
140
|
|
|
*/ |
|
|
|
|
|
|
141
|
|
|
public function getCallable(string $sCallable) |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->xRegistry->getCallableObject($sCallable); |
|
144
|
|
|
} |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|