Completed
Push — master ( c61dd9...791f1f )
by Thierry
01:36
created

UserFunction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * UserFunction.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 UserFunction
27
{
28
    use \Jaxon\Utils\Traits\Config;
29
    use \Jaxon\Utils\Traits\Manager;
30
    use \Jaxon\Utils\Traits\Template;
31
    use \Jaxon\Utils\Traits\Translator;
32
33
    /**
34
     * An alias to use for this function
35
     *
36
     * This is useful when you want to call the same jaxon enabled function with
37
     * a different set of call options from what was already registered.
38
     *
39
     * @var string
40
     */
41
    private $sAlias;
42
43
    /**
44
     * A string or an array which defines the function to be registered
45
     *
46
     * @var string|array
47
     */
48
    private $sUserFunction;
49
50
    /**
51
     * The path and file name of the include file where the function is defined
52
     *
53
     * @var string
54
     */
55
    private $sInclude;
56
57
    /**
58
     * An associative array containing call options that will be sent
59
     * to the browser curing client script generation
60
     *
61
     * @var array
62
     */
63
    private $aConfiguration;
64
65
    public function __construct($sUserFunction)
66
    {
67
        $this->aConfiguration = [];
68
        $this->sAlias = '';
69
        $this->sUserFunction = $sUserFunction;
70
    }
71
72
    /**
73
     * Get the name of the function being referenced
74
     *
75
     * @return string
76
     */
77
    public function getName()
78
    {
79
        // Do not use sAlias here!
80
        if(is_array($this->sUserFunction))
81
        {
82
            return $this->sUserFunction[1];
83
        }
84
        return $this->sUserFunction;
85
    }
86
87
    /**
88
     * Set call options for this instance
89
     *
90
     * @param string        $sName                The name of the configuration option
91
     * @param string        $sValue                The value of the configuration option
92
     *
93
     * @return void
94
     */
95
    public function configure($sName, $sValue)
96
    {
97
        switch($sName)
98
        {
99
        case 'class': // The user function is a method in the given class
100
            $this->sUserFunction = [$sValue, $this->sUserFunction];
101
            break;
102
        case 'alias':
103
            $this->sAlias = $sValue;
104
            break;
105
        case 'include':
106
            $this->sInclude = $sValue;
107
            break;
108
        default:
109
            $this->aConfiguration[$sName] = $sValue;
110
            break;
111
        }
112
    }
113
114
    /**
115
     * Constructs and returns a <Jaxon\Request\Request> object which is capable of generating the javascript call to invoke this jaxon enabled function
116
     *
117
     * @return Jaxon\Request\Request
118
     */
119
    public function generateRequest()
120
    {
121
        $sAlias = (($this->sAlias) ? $this->sAlias : $this->getName());
122
        return new Request($sAlias, 'function');
123
    }
124
125
    /**
126
     * Generate the javascript function stub that is sent to the browser on initial page load
127
     *
128
     * @return string
129
     */
130
    public function getScript()
131
    {
132
        $sJaxonPrefix = $this->getOption('core.prefix.function');
133
        $sFunction = $this->getName();
134
        $sAlias = (($this->sAlias) ? $this->sAlias : $sFunction);
135
136
        return $this->render('jaxon::support/function.js', array(
137
            'sPrefix' => $sJaxonPrefix,
138
            'sAlias' => $sAlias,
139
            'sFunction' => $sFunction,
140
            'aConfig' => $this->aConfiguration,
141
        ));
142
    }
143
144
    /**
145
     * Call the registered user function, including an external file if needed
146
     * and passing along the specified arguments
147
     *
148
     * @param array         $aArgs                The function arguments
149
     *
150
     * @return void
151
     */
152
    public function call($aArgs = [])
153
    {
154
        if(($this->sInclude))
155
        {
156
            require_once $this->sInclude;
157
        }
158
        $response = call_user_func_array($this->sUserFunction, $aArgs);
159
        if(($response))
160
        {
161
            $this->getResponseManager()->append($response);
162
        }
163
    }
164
}
165