Passed
Push — master ( bf2aee...a6dc35 )
by Thierry
02:22
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
cc 5
eloc 11
nc 8
nop 1
dl 0
loc 24
rs 9.6111
c 0
b 0
f 0
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\Di;
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\CallableTrait;
37
    use Traits\RegisterTrait;
38
    use Traits\ViewTrait;
39
    use Traits\UtilTrait;
40
    use Traits\SessionTrait;
41
42
    /**
43
     * The Dependency Injection Container
44
     *
45
     * @var ContainerInterface
46
     */
47
    private $appContainer = null;
48
49
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $jaxon should have a doc-comment as per coding-style.
Loading history...
50
     * The class constructor
51
     */
52
    public function __construct(Jaxon $jaxon)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
53
    {
54
        parent::__construct();
55
56
        // Save the Jaxon and Container instances
57
        $this->val(Jaxon::class, $jaxon);
58
        $this->val(Container::class, $this);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
59
        // Template directory
60
        $sTemplateDir = realpath(__DIR__ . '/../../templates');
61
        $this->val('jaxon.core.dir.template', $sTemplateDir);
62
        // Translation directory
63
        $sTranslationDir = realpath(__DIR__ . '/../../translations');
64
        $this->val('jaxon.core.dir.translation', $sTranslationDir);
65
66
        $this->registerAll();
67
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
68
69
    /**
70
     * Register the values into the container
71
     *
72
     * @return void
73
     */
74
    private function registerAll()
75
    {
76
        $this->registerApp();
77
        $this->registerRequests();
78
        $this->registerResponses();
79
        $this->registerPlugins();
80
        $this->registerCallables();
81
        $this->registerViews();
82
        $this->registerUtils();
83
        $this->registerSessions();
84
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
85
86
    /**
87
     * Get the container provided by the integrated framework
88
     *
89
     * @return ContainerInterface
90
     */
91
    public function getAppContainer(): ?ContainerInterface
92
    {
93
        return $this->appContainer;
94
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
95
96
    /**
97
     * Set the container provided by the integrated framework
98
     *
99
     * @param ContainerInterface $xContainer    The container implementation
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
100
     *
101
     * @return void
102
     */
103
    public function setAppContainer(ContainerInterface $xContainer)
104
    {
105
        $this->appContainer = $xContainer;
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * Check if a class is defined in the container
110
     *
111
     * @param string $sClass    The full class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
112
     *
113
     * @return bool
114
     */
115
    public function h(string $sClass): bool
116
    {
117
        return $this->offsetExists($sClass);
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
119
120
    /**
121
     * Check if a class is defined in the container
122
     *
123
     * @param string $sClass    The full class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
124
     *
125
     * @return bool
126
     */
127
    public function has(string $sClass): bool
128
    {
129
        if($this->appContainer != null && $this->appContainer->has($sClass))
130
        {
131
            return true;
132
        }
133
        return $this->offsetExists($sClass);
134
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
135
136
    /**
137
     * Get a class instance
138
     *
139
     * @param string $sClass    The full class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
140
     *
141
     * @return mixed
142
     */
143
    public function g(string $sClass)
144
    {
145
        return $this->offsetGet($sClass);
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * Get a class instance
150
     *
151
     * @param string $sClass    The full class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
152
     *
153
     * @return mixed
154
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
155
     * @throws ContainerExceptionInterface Error while retrieving the entry.
156
     * @throws UnknownIdentifierException If the identifier is not defined
157
     */
158
    public function get(string $sClass)
159
    {
160
        if($this->appContainer != null && $this->appContainer->has($sClass))
161
        {
162
            return $this->appContainer->get($sClass);
163
        }
164
        return $this->offsetGet($sClass);
165
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
166
167
    /**
168
     * Save a closure in the container
169
     *
170
     * @param string $sClass    The full class name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
171
     * @param Closure $xClosure    The closure
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
172
     *
173
     * @return void
174
     */
175
    public function set(string $sClass, Closure $xClosure)
176
    {
177
        $this->offsetSet($sClass, $xClosure);
178
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
179
180
    /**
181
     * Save a value in the container
182
     *
183
     * @param string $sKey    The key
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
184
     * @param mixed $xValue    The value
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...
185
     *
186
     * @return void
187
     */
188
    public function val(string $sKey, $xValue)
189
    {
190
        $this->offsetSet($sKey, $xValue);
191
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
192
193
    /**
194
     * Set an alias in the container
195
     *
196
     * @param string $sAlias    The alias name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
197
     * @param string $sClass    The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
198
     *
199
     * @return void
200
     */
201
    public function alias(string $sAlias, string $sClass)
202
    {
203
        $this->set($sAlias, function($c) use ($sClass) {
204
            return $c->get($sClass);
205
        });
206
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
207
208
    /**
209
     * Create an instance of a class, getting the contructor parameters from the DI container
210
     *
211
     * @param string|ReflectionClass $xClass    The class name or the reflection class
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
212
     *
213
     * @return object|null
214
     * @throws ReflectionException
215
     * @throws NotFoundExceptionInterface
216
     * @throws ContainerExceptionInterface
217
     * @throws UnknownIdentifierException
218
     */
219
    public function make($xClass)
220
    {
221
        if(is_string($xClass))
222
        {
223
            // Create the reflection class instance
224
            $xClass = new ReflectionClass($xClass);
225
        }
226
        if(!($xClass instanceof ReflectionClass))
0 ignored issues
show
introduced by
$xClass is always a sub-type of ReflectionClass.
Loading history...
227
        {
228
            return null;
229
        }
230
        // Use the Reflection class to get the parameters of the constructor
231
        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...
232
        {
233
            return $xClass->newInstance();
234
        }
235
        $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...
236
        $parameterInstances = [];
237
        foreach($parameters as $parameter)
238
        {
239
            // Get the parameter instance from the DI
240
            $parameterInstances[] = $this->get($parameter->getClass()->getName());
241
        }
242
        return $xClass->newInstanceArgs($parameterInstances);
243
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
244
245
    /**
246
     * Create an instance of a class by automatically fetching the dependencies in the constructor.
247
     *
248
     * @param string $sClass    The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
249
     *
250
     * @return void
251
     */
252
    public function auto(string $sClass)
253
    {
254
        $this->set($sClass, function() use ($sClass) {
255
            return $this->make($sClass);
256
        });
257
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
258
}
259