Completed
Push — master ( 5c3876...859b6d )
by Thierry
01:35
created

CallableFunction::generateRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Request\Support;
22
23
class CallableFunction
24
{
25
    use \Jaxon\Features\Config;
26
    use \Jaxon\Features\Template;
27
28
    /**
29
     * The name of the corresponding javascript function
30
     *
31
     * @var string
32
     */
33
    private $sJsFunction;
34
35
    /**
36
     * A string or an array which defines the function to be registered
37
     *
38
     * @var string|array
39
     */
40
    private $xCallableFunction;
41
42
    /**
43
     * The path and file name of the include file where the function is defined
44
     *
45
     * @var string
46
     */
47
    private $sInclude;
48
49
    /**
50
     * An associative array containing call options that will be sent
51
     * to the browser curing client script generation
52
     *
53
     * @var array
54
     */
55
    private $aConfiguration;
56
57
    public function __construct($sCallableFunction)
58
    {
59
        $this->aConfiguration = [];
60
        $this->sJsFunction = $sCallableFunction;
61
        $this->xCallableFunction = $sCallableFunction;
62
    }
63
64
    /**
65
     * Get the name of the function being referenced
66
     *
67
     * @return string
68
     */
69
    public function getName()
70
    {
71
        return $this->sJsFunction;
72
    }
73
74
    /**
75
     * Set call options for this instance
76
     *
77
     * @param string        $sName                The name of the configuration option
78
     * @param string        $sValue               The value of the configuration option
79
     *
80
     * @return void
81
     */
82
    public function configure($sName, $sValue)
83
    {
84
        switch($sName)
85
        {
86
        case 'class': // The user function is a method in the given class
87
            $this->xCallableFunction = [$sValue, $this->xCallableFunction];
88
            break;
89
        case 'alias':
90
            $this->sJsFunction = $sValue;
91
            break;
92
        case 'include':
93
            $this->sInclude = $sValue;
94
            break;
95
        default:
96
            $this->aConfiguration[$sName] = $sValue;
97
            break;
98
        }
99
    }
100
101
    /**
102
     * Generate the javascript function stub that is sent to the browser on initial page load
103
     *
104
     * @return string
105
     */
106
    public function getScript()
107
    {
108
        $sPrefix = $this->getOption('core.prefix.function');
109
        $sJsFunction = $this->getName();
110
111
        return $this->render('jaxon::support/function.js', [
112
            'sPrefix' => $sPrefix,
113
            'sAlias' => $sJsFunction,
114
            'sFunction' => $sJsFunction, // sAlias is the same as sFunction
115
            'aConfig' => $this->aConfiguration,
116
        ]);
117
    }
118
119
    /**
120
     * Call the registered user function, including an external file if needed
121
     * and passing along the specified arguments
122
     *
123
     * @param array         $aArgs                The function arguments
124
     *
125
     * @return void
126
     */
127
    public function call($aArgs = [])
128
    {
129
        if(($this->sInclude))
130
        {
131
            require_once $this->sInclude;
132
        }
133
134
        // If the function is an alias for a class method, then instanciate the class
135
        if(is_array($this->xCallableFunction) && is_string($this->xCallableFunction[0]))
136
        {
137
            $sClassName = $this->xCallableFunction[0];
138
            $this->xCallableFunction[0] = new $sClassName;
139
        }
140
141
        return call_user_func_array($this->xCallableFunction, $aArgs);
142
    }
143
}
144