Passed
Push — master ( 68c244...b86a9b )
by Thierry
02:28
created

Container::logger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 Closure;
18
use Jaxon\App\Session\SessionInterface;
19
use Jaxon\Jaxon;
20
use Pimple\Container as PimpleContainer;
21
use Pimple\Exception\UnknownIdentifierException;
22
use Psr\Container\ContainerExceptionInterface;
23
use Psr\Container\ContainerInterface;
24
use Psr\Container\NotFoundExceptionInterface;
25
use Psr\Log\LoggerAwareInterface;
26
use Psr\Log\LoggerAwareTrait;
27
use Psr\Log\LoggerInterface;
28
use Psr\Log\NullLogger;
29
use ReflectionClass;
30
use ReflectionException;
31
use function realpath;
32
33
class Container extends PimpleContainer implements LoggerAwareInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Container
Loading history...
34
{
35
    use LoggerAwareTrait;
36
37
    use Traits\AppTrait;
38
    use Traits\PsrTrait;
39
    use Traits\RequestTrait;
40
    use Traits\ResponseTrait;
41
    use Traits\PluginTrait;
42
    use Traits\CallableTrait;
43
    use Traits\RegisterTrait;
44
    use Traits\ViewTrait;
45
    use Traits\UtilTrait;
46
47
    /**
48
     * The Dependency Injection Container
49
     *
50
     * @var ContainerInterface
51
     */
52
    private $xContainer = null;
53
54
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $jaxon should have a doc-comment as per coding-style.
Loading history...
55
     * The class constructor
56
     */
57
    public function __construct(Jaxon $jaxon)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
58
    {
59
        parent::__construct();
60
61
        // Set the default logger
62
        $this->setLogger(new NullLogger());
63
64
        // Save the Jaxon and Container instances
65
        $this->val(Jaxon::class, $jaxon);
66
        $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...
67
        // Template directory
68
        $sTemplateDir = realpath(__DIR__ . '/../../templates');
69
        $this->val('jaxon.core.dir.template', $sTemplateDir);
70
        // Translation directory
71
        $sTranslationDir = realpath(__DIR__ . '/../../translations');
72
        $this->val('jaxon.core.dir.translation', $sTranslationDir);
73
74
        $this->registerAll();
75
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
76
77
    /**
78
     * Register the values into the container
79
     *
80
     * @return void
81
     */
82
    private function registerAll()
83
    {
84
        $this->registerApp();
85
        $this->registerPsr();
86
        $this->registerRequests();
87
        $this->registerResponses();
88
        $this->registerPlugins();
89
        $this->registerCallables();
90
        $this->registerViews();
91
        $this->registerUtils();
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * Get the logger
96
     *
97
     * @return LoggerInterface
98
     */
99
    public function logger(): LoggerInterface
100
    {
101
        return $this->logger;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->logger could return the type null which is incompatible with the type-hinted return Psr\Log\LoggerInterface. Consider adding an additional type-check to rule them out.
Loading history...
102
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
103
104
    /**
105
     * Set the container provided by the integrated framework
106
     *
107
     * @param ContainerInterface $xContainer    The container implementation
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
108
     *
109
     * @return void
110
     */
111
    public function setContainer(ContainerInterface $xContainer)
112
    {
113
        $this->xContainer = $xContainer;
114
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
115
116
    /**
117
     * Check if a class is defined in the container
118
     *
119
     * @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...
120
     *
121
     * @return bool
122
     */
123
    public function h(string $sClass): bool
124
    {
125
        return $this->offsetExists($sClass);
126
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
127
128
    /**
129
     * Check if a class is defined in the container
130
     *
131
     * @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...
132
     *
133
     * @return bool
134
     */
135
    public function has(string $sClass): bool
136
    {
137
        if($this->xContainer != null && $this->xContainer->has($sClass))
138
        {
139
            return true;
140
        }
141
        return $this->offsetExists($sClass);
142
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
143
144
    /**
145
     * Get a class instance
146
     *
147
     * @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...
148
     *
149
     * @return mixed
150
     */
151
    public function g(string $sClass)
152
    {
153
        return $this->offsetGet($sClass);
154
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
155
156
    /**
157
     * Get a class instance
158
     *
159
     * @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...
160
     *
161
     * @return mixed
162
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
163
     * @throws ContainerExceptionInterface Error while retrieving the entry.
164
     * @throws UnknownIdentifierException If the identifier is not defined
165
     */
166
    public function get(string $sClass)
167
    {
168
        if($this->xContainer != null && $this->xContainer->has($sClass))
169
        {
170
            return $this->xContainer->get($sClass);
171
        }
172
        return $this->offsetGet($sClass);
173
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
174
175
    /**
176
     * Save a closure in the container
177
     *
178
     * @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...
179
     * @param Closure $xClosure    The closure
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
180
     *
181
     * @return void
182
     */
183
    public function set(string $sClass, Closure $xClosure)
184
    {
185
        $this->offsetSet($sClass, $xClosure);
186
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
187
188
    /**
189
     * Save a value in the container
190
     *
191
     * @param string $sKey    The key
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
192
     * @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...
193
     *
194
     * @return void
195
     */
196
    public function val(string $sKey, $xValue)
197
    {
198
        $this->offsetSet($sKey, $xValue);
199
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
200
201
    /**
202
     * Set an alias in the container
203
     *
204
     * @param string $sAlias    The alias name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
205
     * @param string $sClass    The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
206
     *
207
     * @return void
208
     */
209
    public function alias(string $sAlias, string $sClass)
210
    {
211
        $this->set($sAlias, function($c) use ($sClass) {
212
            return $c->get($sClass);
213
        });
214
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
215
216
    /**
217
     * Create an instance of a class, getting the constructor parameters from the DI container
218
     *
219
     * @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...
220
     *
221
     * @return object|null
222
     * @throws ReflectionException
223
     * @throws NotFoundExceptionInterface
224
     * @throws ContainerExceptionInterface
225
     * @throws UnknownIdentifierException
226
     */
227
    public function make($xClass)
228
    {
229
        if(is_string($xClass))
230
        {
231
            // Create the reflection class instance
232
            $xClass = new ReflectionClass($xClass);
233
        }
234
        if(!($xClass instanceof ReflectionClass))
0 ignored issues
show
introduced by
$xClass is always a sub-type of ReflectionClass.
Loading history...
235
        {
236
            return null;
237
        }
238
        // Use the Reflection class to get the parameters of the constructor
239
        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...
240
        {
241
            return $xClass->newInstance();
242
        }
243
        $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...
244
        $parameterInstances = [];
245
        foreach($parameters as $parameter)
246
        {
247
            // Get the parameter instance from the DI
248
            $parameterInstances[] = $this->get($parameter->getClass()->getName());
249
        }
250
        return $xClass->newInstanceArgs($parameterInstances);
251
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
252
253
    /**
254
     * Create an instance of a class by automatically fetching the dependencies in the constructor.
255
     *
256
     * @param string $sClass    The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
257
     *
258
     * @return void
259
     */
260
    public function auto(string $sClass)
261
    {
262
        $this->set($sClass, function() use ($sClass) {
263
            return $this->make($sClass);
264
        });
265
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
266
267
    /**
268
     * Get the session manager
269
     *
270
     * @return SessionInterface
271
     */
272
    public function getSessionManager(): SessionInterface
273
    {
274
        return $this->g(SessionInterface::class);
275
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
276
277
    /**
278
     * Set the session manager
279
     *
280
     * @param Closure $xClosure    A closure to create the session manager instance
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
281
     *
282
     * @return void
283
     */
284
    public function setSessionManager(Closure $xClosure)
285
    {
286
        $this->set(SessionInterface::class, $xClosure);
287
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
288
}
289