Passed
Push — master ( 6f7a23...2cf817 )
by Thierry
02:29
created

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