Passed
Push — master ( 0602c9...8f96ba )
by Thierry
02:02
created

Container::make()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 24
rs 9.6111
c 0
b 0
f 0
cc 5
nc 8
nop 1
1
<?php
2
3
/**
4
 * Container.php - Jaxon data container
5
 *
6
 * Provide container service for Jaxon utils class instances.
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 Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
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...
14
15
namespace Jaxon\Container;
16
17
use Jaxon\Jaxon;
18
use Pimple\Container as PimpleContainer;
19
use Pimple\Exception\UnknownIdentifierException;
20
use Psr\Container\ContainerInterface;
21
use Psr\Container\ContainerExceptionInterface;
22
use Psr\Container\NotFoundExceptionInterface;
23
24
use Closure;
25
use ReflectionClass;
26
use ReflectionException;
27
28
use function realpath;
29
30
class Container extends PimpleContainer
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Container
Loading history...
31
{
32
    use Traits\AppTrait;
33
    use Traits\RequestTrait;
34
    use Traits\ResponseTrait;
35
    use Traits\PluginTrait;
36
    use Traits\ConfigTrait;
37
    use Traits\CallableTrait;
38
    use Traits\RegisterTrait;
39
    use Traits\ViewTrait;
40
    use Traits\UtilTrait;
41
    use Traits\SessionTrait;
42
43
    /**
44
     * The Dependency Injection Container
45
     *
46
     * @var ContainerInterface
47
     */
48
    private $appContainer = null;
49
50
    /**
51
     * The class constructor
52
     *
53
     * @param Jaxon $jaxon
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
54
     * @param array $aOptions The default options
55
     */
56
    public function __construct(Jaxon $jaxon, array $aOptions)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
57
    {
58
        parent::__construct();
59
60
        $sTranslationDir = realpath(__DIR__ . '/../../translations');
61
        $sTemplateDir = realpath(__DIR__ . '/../../templates');
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...
62
        // Translation directory
63
        $this->val('jaxon.core.dir.translation', $sTranslationDir);
64
        // Template directory
65
        $this->val('jaxon.core.dir.template', $sTemplateDir);
66
        // Library options
67
        $this->val('jaxon.core.options', $aOptions);
68
69
        $this->val(Jaxon::class, $jaxon);
70
71
        $this->registerAll();
72
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
73
74
    /**
75
     * Register the values into the container
76
     *
77
     * @return void
78
     */
79
    private function registerAll()
80
    {
81
        $this->registerApp();
82
        $this->registerRequests();
83
        $this->registerResponses();
84
        $this->registerPlugins();
85
        $this->registerConfigs();
86
        $this->registerCallables();
87
        $this->registerViews();
88
        $this->registerUtils();
89
        $this->registerSessions();
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
91
92
    /**
93
     * Get the container provided by the integrated framework
94
     *
95
     * @return ContainerInterface
96
     */
97
    public function getAppContainer()
98
    {
99
        return $this->appContainer;
100
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
101
102
    /**
103
     * Set the container provided by the integrated framework
104
     *
105
     * @param ContainerInterface  $container     The container implementation
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
106
     *
107
     * @return void
108
     */
109
    public function setAppContainer(ContainerInterface $container)
110
    {
111
        $this->appContainer = $container;
112
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
113
114
    /**
115
     * Check if a class is defined in the container
116
     *
117
     * @param string                $sClass             The full class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 16 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
118
     *
119
     * @return bool
120
     */
121
    public function has($sClass)
122
    {
123
        if($this->appContainer != null && $this->appContainer->has($sClass))
124
        {
125
            return true;
126
        }
127
        return $this->offsetExists($sClass);
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
131
     * Get a class instance
132
     *
133
     * @param string                $sClass             The full class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 16 found
Loading history...
134
     *
135
     * @return mixed
136
     */
137
    public function g($sClass)
138
    {
139
        return $this->offsetGet($sClass);
140
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
141
142
    /**
143
     * Get a class instance
144
     *
145
     * @param string                $sClass             The full class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 16 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
146
     *
147
     * @return mixed
148
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
149
     * @throws ContainerExceptionInterface Error while retrieving the entry.
150
     * @throws UnknownIdentifierException If the identifier is not defined
151
     */
152
    public function get($sClass)
153
    {
154
        if($this->appContainer != null && $this->appContainer->has($sClass))
155
        {
156
            return $this->appContainer->get($sClass);
157
        }
158
        return $this->offsetGet($sClass);
159
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
160
161
    /**
162
     * Save a closure in the container
163
     *
164
     * @param string                $sClass             The full class name
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 13 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 16 found
Loading history...
165
     * @param Closure               $xClosure           The closure
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 11 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 15 found
Loading history...
166
     *
167
     * @return void
168
     */
169
    public function set($sClass, Closure $xClosure)
170
    {
171
        $this->offsetSet($sClass, $xClosure);
172
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
173
174
    /**
175
     * Save a value in the container
176
     *
177
     * @param string                $sKey               The key
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 15 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 16 found
Loading history...
178
     * @param mixed                 $xValue             The value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 17 found
Loading history...
179
     *
180
     * @return void
181
     */
182
    public function val($sKey, $xValue)
183
    {
184
        $this->offsetSet($sKey, $xValue);
185
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
186
187
    /**
188
     * Set an alias in the container
189
     *
190
     * @param string                $sAlias             The alias name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 16 found
Loading history...
191
     * @param string                $sClass             The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 16 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
192
     *
193
     * @return void
194
     */
195
    public function alias($sAlias, $sClass)
196
    {
197
        $this->set($sAlias, function($c) use ($sClass) {
198
            return $c->get($sClass);
199
        });
200
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
201
202
    /**
203
     * Create an instance of a class, getting the contructor parameters from the DI container
204
     *
205
     * @param string|ReflectionClass $xClass The class name or the reflection class
206
     *
207
     * @return object|null
208
     * @throws ReflectionException
209
     * @throws NotFoundExceptionInterface
210
     * @throws ContainerExceptionInterface
211
     * @throws UnknownIdentifierException
212
     */
213
    public function make($xClass)
214
    {
215
        if(is_string($xClass))
216
        {
217
            // Create the reflection class instance
218
            $xClass = new ReflectionClass($xClass);
219
        }
220
        if(!($xClass instanceof ReflectionClass))
0 ignored issues
show
introduced by
$xClass is always a sub-type of ReflectionClass.
Loading history...
221
        {
222
            return null;
223
        }
224
        // Use the Reflection class to get the parameters of the constructor
225
        if(($constructor = $xClass->getConstructor()) === null)
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
226
        {
227
            return $xClass->newInstance();
228
        }
229
        $parameters = $constructor->getParameters();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 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...
230
        $parameterInstances = [];
231
        foreach($parameters as $parameter)
232
        {
233
            // Get the parameter instance from the DI
234
            $parameterInstances[] = $this->get($parameter->getClass()->getName());
235
        }
236
        return $xClass->newInstanceArgs($parameterInstances);
237
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
238
239
    /**
240
     * Create an instance of a class by automatically fetching the dependencies from the constructor.
241
     *
242
     * @param string                $sClass             The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 16 found
Loading history...
243
     *
244
     * @return void
245
     */
246
    public function auto($sClass)
247
    {
248
        $this->set($sClass, function($c) use ($sClass) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

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

248
        $this->set($sClass, function(/** @scrutinizer ignore-unused */ $c) use ($sClass) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
249
            return $this->make($sClass);
250
        });
251
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
252
}
253