GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 3c5463...84f915 )
by Hong
16:33
created

Container::initContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Di
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Di;
16
17
use Phossa2\Config\Config;
18
use Phossa2\Di\Message\Message;
19
use Phossa2\Di\Factory\Factory;
20
use Phossa2\Di\Resolver\Resolver;
21
use Phossa2\Di\Traits\ScopeTrait;
22
use Phossa2\Di\Traits\ArrayAccessTrait;
23
use Phossa2\Shared\Base\ObjectAbstract;
24
use Phossa2\Config\Traits\WritableTrait;
25
use Interop\Container\ContainerInterface;
26
use Phossa2\Di\Interfaces\ScopeInterface;
27
use Phossa2\Di\Exception\RuntimeException;
28
use Phossa2\Di\Exception\NotFoundException;
29
use Phossa2\Di\Traits\ContainerHelperTrait;
30
use Phossa2\Di\Traits\InstanceFactoryTrait;
31
use Phossa2\Di\Traits\ExtendedContainerTrait;
32
use Phossa2\Config\Interfaces\ConfigInterface;
33
use Phossa2\Config\Interfaces\WritableInterface;
34
use Phossa2\Shared\Delegator\DelegatorAwareTrait;
35
use Phossa2\Di\Interfaces\ExtendedContainerInterface;
36
use Phossa2\Shared\Delegator\DelegatorAwareInterface;
37
38
/**
39
 * Container
40
 *
41
 * A writable, array accessable, delegator-aware and extended instance container.
42
 *
43
 * - writable:
44
 *
45
 *   ```php
46
 *   $container->set('cache', $cache);
47
 *   ```
48
 *
49
 * - array accessable:
50
 *
51
 *   ```php
52
 *   // get
53
 *   $cache = $container['cache'];
54
 *
55
 *   // set/replace
56
 *   $container['cache'] = $anotherCache;
57
 *   ```
58
 *
59
 * - delegator-aware: lookup dependent instances in the delegator
60
 *
61
 *   ```php
62
 *   $delegator->addRegistry($container);
63
 *   ```
64
 *
65
 * - extended container
66
 *
67
 *   ```php
68
 *   // get new instance
69
 *   $newCache = $container->one('cache');
70
 *
71
 *   // run callables
72
 *   $container->run(['${#logger}', 'warning'], ['A warning message from ${user}']);
73
 *   ```
74
 *
75
 * @package Phossa2\Di
76
 * @author  Hong Zhang <[email protected]>
77
 * @see     ObjectAbstract
78
 * @see     ScopeInterface
79
 * @see     ContainerInterface
80
 * @see     ExtendedContainerInterface
81
 * @see     DelegatorAwareInterface
82
 * @see     WritableInterface
83
 * @see     \ArrayAccess
84
 * @version 2.0.0
85
 * @since   2.0.0 added
86
 */
87
class Container extends ObjectAbstract implements ContainerInterface, ScopeInterface, WritableInterface, \ArrayAccess, DelegatorAwareInterface, ExtendedContainerInterface
88
{
89
    use ScopeTrait,
90
        WritableTrait,
91
        ArrayAccessTrait,
92
        DelegatorAwareTrait,
93
        ContainerHelperTrait,
94
        InstanceFactoryTrait,
95
        ExtendedContainerTrait;
96
97
    /**
98
     * Inject a Phossa2\Config\Config
99
     *
100
     * ```php
101
     * $configData = [
102
     *     // container class
103
     *     'di.class' => 'Phossa2\\Di\\Container',
104
     *
105
     *     // container service definitions
106
     *     'di.service' => [
107
     *         // ...
108
     *     ],
109
     *
110
     *     // init methods to run after container created
111
     *     'di.init' => [
112
     *         'default' => [],
113
     *         'mystuff' => [ ... ],
114
     *     ],
115
     * ];
116
     *
117
     * // instantiate $config
118
     * $config = new Config(null, null, $configData);
119
     *
120
     * // instantiate container
121
     * $container = new $config['di.class']($config);
122
     * ```
123
     *
124
     * @param  ConfigInterface $config inject the config instance
125
     * @param  string $baseNode container's starting node in $config
126
     * @access public
127
     */
128
    public function __construct(
129
        ConfigInterface $config = null,
130
        /*# string */ $baseNode = 'di'
131
    ) {
132
        // set resolver
133
        $this->setResolver(
134
            new Resolver($this, $config ?: new Config(), $baseNode)
135
        );
136
137
        // set factory
138
        $this->setFactory(new Factory($this->getResolver()));
139
140
        // run methods in 'di.init'
141
        $this->initContainer();
142
    }
143
144
    // ContainerInterface related
145
146
    /**
147
     * Extensions to the Interop\Container\ContainerInterface
148
     *
149
     * - Accepting second param as object constructor arguments
150
     * - Accpeting $id with scope appended, e.g. 'cache@myScope'
151
     *
152
     * {@inheritDoc}
153
     */
154
    public function get($id)
155
    {
156
        if ($this->has($id)) {
157
            $args = func_num_args() > 1 ? func_get_arg(1) : [];
158
            $this->resolve($args);
159
            return $this->getInstance($id, $args);
160
        } else {
161
            throw new NotFoundException(
162
                Message::get(Message::DI_SERVICE_NOTFOUND, $id),
163
                Message::DI_SERVICE_NOTFOUND
164
            );
165
        }
166
    }
167
168
    /**
169
     * Extensions to the Interop\Container\ContainerInterface
170
     *
171
     * - Accpeting $id with scope appended, e.g. 'cache@myScope'
172
     *
173
     * {@inheritDoc}
174
     */
175
    public function has($id)
176
    {
177
        if (is_string($id)) {
178
            return $this->getResolver()->hasService(
179
                $this->idWithoutScope($id)
180
            );
181
        }
182
        return false;
183
    }
184
185
    // WritableInterface related
186
187
    /**
188
     * {@inheritDoc}
189
     */
190
    public function set(/*# string */ $id, $value)
191
    {
192
        if ($this->isWritable()) {
193
            list($rawId, $scope) = $this->splitId($id);
194
195
            $this->getResolver()->setService(
196
                $rawId,
197
                '' === $scope ? $value : $this->scopedData($value, $scope)
198
            );
199
            return $this;
200
        } else {
201
            throw new RuntimeException(
202
                Message::get(Message::DI_CONTAINER_READONLY, $id),
203
                Message::DI_CONTAINER_READONLY
204
            );
205
        }
206
    }
207
208
    /**
209
     * Override 'isWritable()' in 'Phossa2\Config\Traits\WritableTrait'
210
     *
211
     * Container's writability is depend on its resolver
212
     *
213
     * {@inheritDoc}
214
     */
215
    public function isWritable()/*# : bool */
216
    {
217
        return $this->getResolver()->isWritable();
218
    }
219
220
    /**
221
     * Override 'setWritable()' in 'Phossa2\Config\Traits\WritableTrait'
222
     *
223
     * Container's writability is depend on its resolver
224
     *
225
     * {@inheritDoc}
226
     */
227
    public function setWritable($writable)/*# : bool */
228
    {
229
        return $this->getResolver()->setWritable((bool) $writable);
230
    }
231
232
    /**
233
     * execute init methods defined in 'di.init' node
234
     *
235
     * @return $this
236
     * @throws RuntimeException if anything goes wrong
237
     * @access protected
238
     */
239
    protected function initContainer()
240
    {
241
        $initNode = $this->getResolver()->getSectionId('', 'init');
242
243
        if ($this->getResolver()->has($initNode)) {
244
            $this->getFactory()->executeMethodBatch(
245
                $this->getResolver()->get($initNode)
246
            );
247
        }
248
        return $this;
249
    }
250
}
251