|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* CallableFunction.php - Jaxon user function |
|
5
|
|
|
* |
|
6
|
|
|
* This class stores a reference to a user defined function which can be called from the client via an Jaxon request |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @author Jared White |
|
|
|
|
|
|
10
|
|
|
* @author J. Max Wilson |
|
|
|
|
|
|
11
|
|
|
* @author Joseph Woolley |
|
|
|
|
|
|
12
|
|
|
* @author Steffen Konerow |
|
|
|
|
|
|
13
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
14
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
|
|
15
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
|
|
16
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
17
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
18
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
19
|
|
|
*/ |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
namespace Jaxon\Plugin\Request\CallableFunction; |
|
22
|
|
|
|
|
23
|
|
|
class CallableFunction |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The name of the function in the ajax call |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $sFunction; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The name of the generated javascript function |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $sJsFunction; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* A string or an array which defines the registered PHP function |
|
41
|
|
|
* |
|
42
|
|
|
* @var string|array |
|
43
|
|
|
*/ |
|
44
|
|
|
private $xPhpFunction; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The path and file name of the include file where the function is defined |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $sInclude; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* An associative array containing call options that will be sent |
|
55
|
|
|
* to the browser curing client script generation |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
private $aOptions = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The constructor |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $sFunction |
|
|
|
|
|
|
65
|
|
|
* @param string $sJsFunction |
|
|
|
|
|
|
66
|
|
|
* @param string $sPhpFunction |
|
|
|
|
|
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct(string $sFunction, string $sJsFunction, string $sPhpFunction) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$this->sFunction = $sFunction; |
|
|
|
|
|
|
71
|
|
|
$this->sJsFunction = $sJsFunction; |
|
72
|
|
|
$this->xPhpFunction = $sPhpFunction; |
|
73
|
|
|
} |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the name of the function being referenced |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getName(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->sFunction; |
|
83
|
|
|
} |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get name of the corresponding javascript function |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getJsName(): string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->sJsFunction; |
|
93
|
|
|
} |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the config options of the function being referenced |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getOptions(): array |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->aOptions; |
|
103
|
|
|
} |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Set call options for this instance |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $sName The name of the configuration option |
|
|
|
|
|
|
109
|
|
|
* @param string $sValue The value of the configuration option |
|
|
|
|
|
|
110
|
|
|
* |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
public function configure(string $sName, string $sValue) |
|
114
|
|
|
{ |
|
115
|
|
|
switch($sName) |
|
116
|
|
|
{ |
|
117
|
|
|
case 'class': // The user function is a method in the given class |
|
118
|
|
|
$this->xPhpFunction = [$sValue, $this->xPhpFunction]; |
|
119
|
|
|
break; |
|
120
|
|
|
case 'include': |
|
121
|
|
|
$this->sInclude = $sValue; |
|
122
|
|
|
break; |
|
123
|
|
|
default: |
|
124
|
|
|
$this->aOptions[$sName] = $sValue; |
|
125
|
|
|
break; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Call the registered user function, including an external file if needed |
|
131
|
|
|
* and passing along the specified arguments |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $aArgs The function arguments |
|
|
|
|
|
|
134
|
|
|
* |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
*/ |
|
137
|
|
|
public function call(array $aArgs = []) |
|
138
|
|
|
{ |
|
139
|
|
|
if(($this->sInclude)) |
|
140
|
|
|
{ |
|
141
|
|
|
require_once $this->sInclude; |
|
142
|
|
|
} |
|
143
|
|
|
// If the function is an alias for a class method, then instantiate the class |
|
144
|
|
|
if(is_array($this->xPhpFunction) && is_string($this->xPhpFunction[0])) |
|
145
|
|
|
{ |
|
146
|
|
|
$sClassName = $this->xPhpFunction[0]; |
|
|
|
|
|
|
147
|
|
|
$this->xPhpFunction[0] = new $sClassName; |
|
148
|
|
|
} |
|
149
|
|
|
return call_user_func_array($this->xPhpFunction, $aArgs); |
|
150
|
|
|
} |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|