Completed
Push — master ( f42a52...0a9499 )
by Thierry
02:50 queued 01:16
created

CallableFunction   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2

6 Methods

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