ScriptPlugin::getHash()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * ScriptPlugin.php
5
 *
6
 * Adds Javascript selector and function call commands to the Jaxon response.
7
 *
8
 * @package jaxon-core
9
 * @copyright 2024 Thierry Feuzeu
10
 * @license https://opensource.org/licenses/MIT MIT License
11
 * @link https://github.com/jaxon-php/jaxon-core
12
 */
13
14
namespace Jaxon\Plugin\Response\Script;
15
16
use Jaxon\Plugin\AbstractResponsePlugin;
17
use Jaxon\Response\NodeResponse;
18
use Jaxon\Script\CallFactory;
19
use Jaxon\Script\Call\JqSelectorCall;
20
use Jaxon\Script\Call\JsObjectCall;
21
use Jaxon\Script\Call\JsSelectorCall;
22
use Jaxon\Script\JsExpr;
23
use Closure;
24
25
use function is_a;
26
27
class ScriptPlugin extends AbstractResponsePlugin
28
{
29
    /**
30
     * @const The plugin name
31
     */
32
    public const NAME = 'script';
33
34
    /**
35
     * The class constructor
36
     *
37
     * @param CallFactory $xFactory
38
     */
39
    public function __construct(private CallFactory $xFactory)
40
    {}
41
42
    /**
43
     * @return Closure
44
     */
45
    private function getCallback(): Closure
46
    {
47
        // The closure needs to capture the response object the script plugin is called with.
48
        $xResponse = $this->response();
49
        return function(JsExpr $xJsExpr) use($xResponse) {
50
            // Add the newly created expression to the response
51
            $aOptions = [
52
                'expr' => $xJsExpr,
53
                'context' => is_a($xResponse, NodeResponse::class) ?
54
                    ['component' => true] : [],
55
            ];
56
            $xResponse->addCommand('script.exec.expr', $aOptions)
57
                ->setOption('plugin', $this->getName());
58
        };
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function getName(): string
65
    {
66
        return self::NAME;
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function getHash(): string
73
    {
74
        // Use the version number as hash
75
        return '5.0.0';
76
    }
77
78
    /**
79
     * Create a JQuery selector expression, and link it to the current response.
80
     *
81
     * @param string $sPath    The jQuery selector path
82
     * @param mixed $xContext    A context associated to the selector
83
     *
84
     * @return JqSelectorCall
85
     */
86
    public function jq(string $sPath = '', $xContext = null): JqSelectorCall
87
    {
88
        return $this->xFactory->jq($sPath, $xContext, $this->getCallback());
89
    }
90
91
    /**
92
     * Create a Javascript object expression, and link it to the current response.
93
     *
94
     * @param string $sObject
95
     *
96
     * @return JsObjectCall
97
     */
98
    public function jo(string $sObject = ''): JsObjectCall
99
    {
100
        return $this->xFactory->jo($sObject, $this->getCallback());
101
    }
102
103
    /**
104
     * Create a Javascript element selector expression, and link it to the current response.
105
     *
106
     * @param string $sElementId
107
     *
108
     * @return JsSelectorCall
109
     */
110
    public function je(string $sElementId = ''): JsSelectorCall
111
    {
112
        return $this->xFactory->je($sElementId, $this->getCallback());
113
    }
114
}
115