Passed
Push — master ( 6da3e4...40d084 )
by Thierry
03:15
created

PluginManager::addCodeGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 2
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\App\I18n\Translator;
25
use Jaxon\Di\Container;
26
use Jaxon\Exception\SetupException;
27
use Jaxon\Plugin\CallableRegistryInterface;
28
use Jaxon\Plugin\Code\CodeGenerator;
29
use Jaxon\Plugin\CodeGeneratorInterface;
30
use Jaxon\Plugin\Request\CallableClass\CallableClassPlugin;
31
use Jaxon\Plugin\Request\CallableDir\CallableDirPlugin;
32
use Jaxon\Plugin\Request\CallableFunction\CallableFunctionPlugin;
33
use Jaxon\Plugin\RequestHandlerInterface;
34
use Jaxon\Plugin\RequestPlugin;
35
use Jaxon\Plugin\Response\DataBag\DataBagPlugin;
36
use Jaxon\Plugin\Response\Dialog\DialogPlugin;
37
use Jaxon\Plugin\Response\JQuery\JQueryPlugin;
38
use Jaxon\Plugin\ResponsePlugin;
39
use Jaxon\Plugin\ResponsePluginInterface;
40
use Jaxon\Response\Response;
41
42
use function class_implements;
43
use function in_array;
44
45
class PluginManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class PluginManager
Loading history...
46
{
47
    /**
48
     * @var Container
49
     */
50
    protected $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
51
52
    /**
53
     * @var CodeGenerator
54
     */
55
    private $xCodeGenerator;
56
57
    /**
58
     * @var Translator
59
     */
60
    protected $xTranslator;
61
62
    /**
63
     * Request plugins, indexed by name
64
     *
65
     * @var array<CallableRegistryInterface>
66
     */
67
    private $aRegistryPlugins = [];
68
69
    /**
70
     * Request handlers, indexed by name
71
     *
72
     * @var array<RequestHandlerInterface>
73
     */
74
    private $aRequestHandlers = [];
75
76
    /**
77
     * Response plugins, indexed by name
78
     *
79
     * @var array<ResponsePluginInterface>
80
     */
81
    private $aResponsePlugins = [];
82
83
    /**
84
     * The constructor
85
     *
86
     * @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...
87
     * @param CodeGenerator $xCodeGenerator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
88
     * @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...
89
     */
90
    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...
91
    {
92
        $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...
93
        $this->xCodeGenerator = $xCodeGenerator;
94
        $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...
95
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
96
97
    /**
98
     * Get the request plugins
99
     *
100
     * @return array<RequestPlugin>
101
     */
102
    public function getRequestHandlers(): array
103
    {
104
        return $this->aRequestHandlers;
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
106
107
    /**
108
     * Register a plugin
109
     *
110
     * Below is a table for priorities and their description:
111
     * - 0 to 999: Plugins that are part of or extensions to the jaxon core
112
     * - 1000 to 8999: User created plugins, typically, these plugins don't care about order
113
     * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list
114
     *
115
     * @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...
116
     * @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...
117
     * @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...
118
     *
119
     * @return void
120
     * @throws SetupException
121
     */
122
    public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000)
123
    {
124
        $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...
125
        $aInterfaces = class_implements($sClassName);
126
        if(in_array(CodeGeneratorInterface::class, $aInterfaces))
127
        {
128
            $this->xCodeGenerator->addCodeGenerator($sClassName, $nPriority);
129
            $bIsUsed = true;
130
        }
131
        if(in_array(CallableRegistryInterface::class, $aInterfaces))
132
        {
133
            $this->aRegistryPlugins[$sPluginName] = $sClassName;
134
            $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...
135
        }
136
        if(in_array(RequestHandlerInterface::class, $aInterfaces))
137
        {
138
            $this->aRequestHandlers[$sPluginName] = $sClassName;
139
            $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...
140
        }
141
        if(in_array(ResponsePluginInterface::class, $aInterfaces))
142
        {
143
            $this->aResponsePlugins[$sPluginName] = $sClassName;
144
            $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...
145
        }
146
147
        if(!$bIsUsed)
148
        {
149
            // The class is invalid.
150
            $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]);
151
            throw new SetupException($sMessage);
152
        }
153
154
        // Register the plugin in the DI container, if necessary
155
        if(!$this->di->has($sClassName))
156
        {
157
            $this->di->auto($sClassName);
158
        }
159
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
160
161
    /**
162
     * Find the specified response plugin by name and return a reference to it if one exists
163
     *
164
     * @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...
165
     * @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...
166
     *
167
     * @return ResponsePlugin|null
168
     */
169
    public function getResponsePlugin(string $sName, ?Response $xResponse = null): ?ResponsePlugin
170
    {
171
        if(!isset($this->aResponsePlugins[$sName]))
172
        {
173
            return null;
174
        }
175
        $xPlugin = $this->di->g($this->aResponsePlugins[$sName]);
0 ignored issues
show
Bug introduced by
$this->aResponsePlugins[$sName] of type Jaxon\Plugin\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

175
        $xPlugin = $this->di->g(/** @scrutinizer ignore-type */ $this->aResponsePlugins[$sName]);
Loading history...
176
        if(($xResponse))
177
        {
178
            $xPlugin->setResponse($xResponse);
179
        }
180
        return $xPlugin;
181
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
182
183
    /**
184
     * Register a function or callable class
185
     *
186
     * Call the request plugin with the $sType defined as name.
187
     *
188
     * @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...
189
     * @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...
190
     * @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...
191
     *
192
     * @return void
193
     * @throws SetupException
194
     */
195
    public function registerCallable(string $sType, string $sCallable, $xOptions = [])
196
    {
197
        if(isset($this->aRegistryPlugins[$sType]) &&
198
            ($xPlugin = $this->di->g($this->aRegistryPlugins[$sType])))
0 ignored issues
show
Bug introduced by
$this->aRegistryPlugins[$sType] of type Jaxon\Plugin\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

198
            ($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...
199
        {
200
            $xPlugin->register($sType, $sCallable, $xPlugin->checkOptions($sCallable, $xOptions));
201
            return;
202
        }
203
        throw new SetupException($this->xTranslator->trans('errors.register.plugin',
204
            ['name' => $sType, 'callable' => $sCallable]));
205
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
206
207
    /**
208
     * Register the Jaxon request plugins
209
     *
210
     * @return void
211
     * @throws SetupException
212
     */
213
    public function registerPlugins()
214
    {
215
        // Request plugins
216
        $this->registerPlugin(CallableClassPlugin::class, Jaxon::CALLABLE_CLASS, 101);
217
        $this->registerPlugin(CallableFunctionPlugin::class, Jaxon::CALLABLE_FUNCTION, 102);
218
        $this->registerPlugin(CallableDirPlugin::class, Jaxon::CALLABLE_DIR, 103);
219
220
        // Response plugins
221
        $this->registerPlugin(JQueryPlugin::class, JQueryPlugin::NAME, 700);
222
        $this->registerPlugin(DataBagPlugin::class, DataBagPlugin::NAME, 700);
223
        $this->registerPlugin(DialogPlugin::class, DialogPlugin::NAME, 750);
224
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
225
}
226