PluginTrait::registerPackage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * PluginTrait.php
5
 *
6
 * Plugin registration and code generation.
7
 *
8
 * @package jaxon-core
9
 * @author Jared White
10
 * @author J. Max Wilson
11
 * @author Thierry Feuzeu
12
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
13
 * @copyright 2022 Thierry Feuzeu <[email protected]>
14
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
15
 * @link https://github.com/jaxon-php/jaxon-core
16
 */
17
18
namespace Jaxon\App\Ajax\Traits;
19
20
use Jaxon\App\Ajax\Bootstrap;
21
use Jaxon\Exception\SetupException;
22
use Jaxon\Plugin\AbstractPackage;
23
use Jaxon\Plugin\Code\CodeGenerator;
24
use Jaxon\Plugin\Manager\PackageManager;
25
use Jaxon\Plugin\Manager\PluginManager;
26
use Jaxon\Plugin\ResponsePluginInterface;
27
use Jaxon\Utils\Http\UriException;
28
29
trait PluginTrait
30
{
31
    /**
32
     * @return PluginManager
33
     */
34
    abstract public function getPluginManager(): PluginManager;
35
36
    /**
37
     * @return PackageManager
38
     */
39
    abstract public function getPackageManager(): PackageManager;
40
41
    /**
42
     * @return CodeGenerator
43
     */
44
    abstract public function getCodeGenerator(): CodeGenerator;
45
46
    /**
47
     * @return Bootstrap
48
     */
49
    abstract protected function getBootstrap(): Bootstrap;
50
51
    /**
52
     * Register request handlers, including functions, callable classes and directories.
53
     *
54
     * @param string $sType    The type of request handler being registered
55
     *        Options include:
56
     *        - Jaxon::CALLABLE_FUNCTION: a function declared at global scope
57
     *        - Jaxon::CALLABLE_CLASS: a class who's methods are to be registered
58
     *        - Jaxon::CALLABLE_DIR: a directory containing classes to be registered
59
     * @param string $sName
60
     *        When registering a function, this is the name of the function
61
     *        When registering a callable class, this is the class name
62
     *        When registering a callable directory, this is the full path to the directory
63
     * @param array|string $xOptions    The related options
64
     *
65
     * @return void
66
     * @throws SetupException
67
     */
68
    public function register(string $sType, string $sName, $xOptions = []): void
69
    {
70
        $this->getPluginManager()->registerCallable($sType, $sName, $xOptions);
71
    }
72
73
    /**
74
     * Register a plugin
75
     *
76
     * Below is a table for priorities and their description:
77
     * - 0 to 999: Plugins that are part of or extensions to the jaxon core
78
     * - 1000 to 8999: User created plugins, typically, these plugins don't care about order
79
     * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list
80
     *
81
     * @param string $sClassName    The plugin class
82
     * @param string $sPluginName    The plugin name
83
     * @param integer $nPriority    The plugin priority, used to order the plugins
84
     *
85
     * @return void
86
     * @throws SetupException
87
     */
88
    public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000)
89
    {
90
        $this->getPluginManager()->registerPlugin($sClassName, $sPluginName, $nPriority);
91
    }
92
93
    /**
94
     * Register a package
95
     *
96
     * @param string $sClassName    The package class
97
     * @param array $xPkgOptions    The user provided package options
98
     *
99
     * @return void
100
     * @throws SetupException
101
     */
102
    public function registerPackage(string $sClassName, array $xPkgOptions = [])
103
    {
104
        $this->getPackageManager()->registerPackage($sClassName, $xPkgOptions);
105
    }
106
107
    /**
108
     * Find a response plugin by name or class name
109
     *
110
     * @template R of ResponsePluginInterface
111
     * @param class-string<R>|string $sName    The name or class of the plugin
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<R>|string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<R>|string.
Loading history...
112
     *
113
     * @return ($sName is class-string ? R : ResponsePluginInterface)|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment ($sName at position 1 could not be parsed: Unknown type name '$sName' at position 1 in ($sName.
Loading history...
114
     */
115
    public function plugin(string $sName): ResponsePluginInterface|null
116
    {
117
        return $this->getPluginManager()->getResponsePlugin($sName);
118
    }
119
120
    /**
121
     * Get a package instance
122
     *
123
     * @template P of AbstractPackage
124
     * @param class-string<P> $sClassName The package class name
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<P> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<P>.
Loading history...
125
     *
126
     * @return P|null
127
     */
128
    public function package(string $sClassName): ?AbstractPackage
129
    {
130
        return $this->getPackageManager()->getPackage($sClassName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getPackage...getPackage($sClassName) also could return the type Jaxon\Plugin\AbstractPackage which is incompatible with the documented return type Jaxon\App\Ajax\Traits\P|null.
Loading history...
131
    }
132
133
    /**
134
     * Get the HTML tags to include Jaxon javascript files into the page.
135
     *
136
     * @return string
137
     */
138
    public function getJs(): string
139
    {
140
        return $this->getCodeGenerator()->getJs();
141
    }
142
143
    /**
144
     * Get the HTML tags to include Jaxon javascript files into the page.
145
     *
146
     * @return string  the javascript code
147
     */
148
    public function js(): string
149
    {
150
        return $this->getCodeGenerator()->getJs();
151
    }
152
153
    /**
154
     * Get the HTML tags to include Jaxon CSS code and files into the page.
155
     *
156
     * @return string
157
     */
158
    public function getCss(): string
159
    {
160
        return $this->getCodeGenerator()->getCss();
161
    }
162
163
    /**
164
     * Get the HTML tags to include Jaxon CSS code and files into the page.
165
     *
166
     * @return string
167
     */
168
    public function css(): string
169
    {
170
        return $this->getCodeGenerator()->getCss();
171
    }
172
173
    /**
174
     * Returns the js header and wrapper code to be printed into the page
175
     *
176
     * The javascript code returned by this function depends on the plugins
177
     * that are included and the functions and classes that are registered.
178
     *
179
     * @param bool $bIncludeJs    Also get the js code
180
     * @param bool $bIncludeCss    Also get the css code
181
     *
182
     * @return string
183
     * @throws UriException
184
     */
185
    public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string
186
    {
187
        return $this->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss);
188
    }
189
190
    /**
191
     * Returns the js header and wrapper code to be printed into the page
192
     *
193
     * @param bool $bIncludeJs    Also get the js code
194
     * @param bool $bIncludeCss    Also get the css code
195
     *
196
     * @return string  the javascript code
197
     * @throws UriException
198
     */
199
    public function script(bool $bIncludeJs = false, bool $bIncludeCss = false): string
200
    {
201
        return $this->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss);
202
    }
203
}
204