Passed
Push — master ( e82656...5198fb )
by Thierry
03:06
created

PluginManager::getPackageOptions()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 5
nop 1
dl 0
loc 19
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * PluginManager.php - Jaxon plugin registry
5
 *
6
 * Register Jaxon plugins and callables.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
10
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
11
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
12
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
20
21
namespace Jaxon\Plugin\Manager;
22
23
use Jaxon\Jaxon;
24
use Jaxon\Di\Container;
25
use Jaxon\Exception\SetupException;
26
use Jaxon\Plugin\Code\CodeGenerator;
27
use Jaxon\Plugin\Contract\CallableRegistryInterface;
28
use Jaxon\Plugin\Contract\CodeGeneratorInterface;
29
use Jaxon\Plugin\Contract\RequestHandlerInterface;
30
use Jaxon\Plugin\Contract\ResponsePluginInterface;
31
use Jaxon\Plugin\RequestPlugin;
32
use Jaxon\Plugin\ResponsePlugin;
33
use Jaxon\Request\Plugin\CallableClass\CallableClassPlugin;
34
use Jaxon\Request\Plugin\CallableClass\CallableDirPlugin;
35
use Jaxon\Request\Plugin\CallableFunction\CallableFunctionPlugin;
36
use Jaxon\Response\Plugin\DataBag\DataBagPlugin;
37
use Jaxon\Response\Plugin\JQuery\JQueryPlugin;
38
use Jaxon\Response\Response;
39
use Jaxon\Ui\Dialogs\MessageInterface;
40
use Jaxon\Ui\Dialogs\QuestionInterface;
41
use Jaxon\Utils\Translation\Translator;
42
43
use function class_implements;
44
use function in_array;
45
46
class PluginManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class PluginManager
Loading history...
47
{
48
    /**
49
     * @var Container
50
     */
51
    protected $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
52
53
    /**
54
     * @var Translator
55
     */
56
    protected $xTranslator;
57
58
    /**
59
     * The code generator
60
     *
61
     * @var CodeGenerator
62
     */
63
    private $xCodeGenerator;
64
65
    /**
66
     * Request plugins, indexed by name
67
     *
68
     * @var array<CallableRegistryInterface>
69
     */
70
    private $aRegistryPlugins = [];
71
72
    /**
73
     * Request handlers, indexed by name
74
     *
75
     * @var array<RequestHandlerInterface>
76
     */
77
    private $aRequestHandlers = [];
78
79
    /**
80
     * Response plugins, indexed by name
81
     *
82
     * @var array<ResponsePluginInterface>
83
     */
84
    private $aResponsePlugins = [];
85
86
    /**
87
     * The constructor
88
     *
89
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
90
     * @param CodeGenerator $xCodeGenerator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
91
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
92
     */
93
    public function __construct(Container $di, CodeGenerator $xCodeGenerator, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
94
    {
95
        $this->di = $di;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
96
        $this->xCodeGenerator = $xCodeGenerator;
97
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
98
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
99
100
    /**
101
     * Get the request plugins
102
     *
103
     * @return array<RequestPlugin>
104
     */
105
    public function getRequestHandlers(): array
106
    {
107
        return $this->aRequestHandlers;
108
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
109
110
    /**
111
     * Register a plugin
112
     *
113
     * Below is a table for priorities and their description:
114
     * - 0 to 999: Plugins that are part of or extensions to the jaxon core
115
     * - 1000 to 8999: User created plugins, typically, these plugins don't care about order
116
     * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list
117
     *
118
     * @param string $sClassName    The plugin class
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
119
     * @param string $sPluginName    The plugin name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
120
     * @param integer $nPriority    The plugin priority, used to order the plugins
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
121
     *
122
     * @return void
123
     * @throws SetupException
124
     */
125
    public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000)
126
    {
127
        $bIsUsed = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
128
        $aInterfaces = class_implements($sClassName);
129
        if(in_array(CodeGeneratorInterface::class, $aInterfaces))
130
        {
131
            $this->xCodeGenerator->addGenerator($sClassName, $nPriority);
132
            $bIsUsed = true;
133
        }
134
        if(in_array(CallableRegistryInterface::class, $aInterfaces))
135
        {
136
            $this->aRegistryPlugins[$sPluginName] = $sClassName;
137
            $bIsUsed = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 30 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
138
        }
139
        if(in_array(RequestHandlerInterface::class, $aInterfaces))
140
        {
141
            $this->aRequestHandlers[$sPluginName] = $sClassName;
142
            $bIsUsed = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 30 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
143
        }
144
        if(in_array(ResponsePluginInterface::class, $aInterfaces))
145
        {
146
            $this->aResponsePlugins[$sPluginName] = $sClassName;
147
            $bIsUsed = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 30 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
148
        }
149
150
        // This plugin implements the Message interface
151
        if(in_array(MessageInterface::class, $aInterfaces))
152
        {
153
            $this->di->getDialog()->setMessage($sClassName);
154
            $bIsUsed = true;
155
        }
156
        // This plugin implements the Question interface
157
        if(in_array(QuestionInterface::class, $aInterfaces))
158
        {
159
            $this->di->getDialog()->setQuestion($sClassName);
160
            $bIsUsed = true;
161
        }
162
163
        if(!$bIsUsed)
164
        {
165
            // The class is invalid.
166
            $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]);
167
            throw new SetupException($sMessage);
168
        }
169
170
        // Register the plugin in the DI container, if necessary
171
        if(!$this->di->has($sClassName))
172
        {
173
            $this->di->auto($sClassName);
174
        }
175
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
176
177
    /**
178
     * Find the specified response plugin by name and return a reference to it if one exists
179
     *
180
     * @param string $sName    The name of the plugin
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
181
     * @param Response|null $xResponse    The response to attach the plugin to
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
182
     *
183
     * @return ResponsePlugin|null
184
     */
185
    public function getResponsePlugin(string $sName, ?Response $xResponse = null): ?ResponsePlugin
186
    {
187
        if(!isset($this->aResponsePlugins[$sName]))
188
        {
189
            return null;
190
        }
191
        $xPlugin = $this->di->g($this->aResponsePlugins[$sName]);
0 ignored issues
show
Bug introduced by
$this->aResponsePlugins[$sName] of type Jaxon\Plugin\Contract\ResponsePluginInterface is incompatible with the type string expected by parameter $sClass of Jaxon\Di\Container::g(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

191
        $xPlugin = $this->di->g(/** @scrutinizer ignore-type */ $this->aResponsePlugins[$sName]);
Loading history...
192
        if(($xResponse))
193
        {
194
            $xPlugin->setResponse($xResponse);
195
        }
196
        return $xPlugin;
197
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
198
199
    /**
200
     * Register a function or callable class
201
     *
202
     * Call the request plugin with the $sType defined as name.
203
     *
204
     * @param string $sType    The type of request handler being registered
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
205
     * @param string $sCallable    The callable entity being registered
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
206
     * @param array|string $xOptions    The associated options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
207
     *
208
     * @return void
209
     * @throws SetupException
210
     */
211
    public function registerCallable(string $sType, string $sCallable, $xOptions = [])
212
    {
213
        if(isset($this->aRegistryPlugins[$sType]) &&
214
            ($xPlugin = $this->di->g($this->aRegistryPlugins[$sType])))
0 ignored issues
show
Bug introduced by
$this->aRegistryPlugins[$sType] of type Jaxon\Plugin\Contract\CallableRegistryInterface is incompatible with the type string expected by parameter $sClass of Jaxon\Di\Container::g(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

214
            ($xPlugin = $this->di->g(/** @scrutinizer ignore-type */ $this->aRegistryPlugins[$sType])))
Loading history...
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
215
        {
216
            $xPlugin->register($sType, $sCallable, $xPlugin->checkOptions($sCallable, $xOptions));
217
            return;
218
        }
219
        throw new SetupException($this->xTranslator->trans('errors.register.plugin',
220
            ['name' => $sType, 'callable' => $sCallable]));
221
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
222
223
    /**
224
     * Register the Jaxon request plugins
225
     *
226
     * @return void
227
     * @throws SetupException
228
     */
229
    public function registerPlugins()
230
    {
231
        // Request plugins
232
        $this->registerPlugin(CallableClassPlugin::class, Jaxon::CALLABLE_CLASS, 101);
233
        $this->registerPlugin(CallableFunctionPlugin::class, Jaxon::CALLABLE_FUNCTION, 102);
234
        $this->registerPlugin(CallableDirPlugin::class, Jaxon::CALLABLE_DIR, 103);
235
236
        // Register the JQuery response plugin
237
        $this->registerPlugin(JQueryPlugin::class, JQueryPlugin::NAME, 700);
238
        // Register the DataBag response plugin
239
        $this->registerPlugin(DataBagPlugin::class, DataBagPlugin::NAME, 700);
240
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
241
}
242