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.
Completed
Push — master ( 322edd...18d049 )
by Hong
03:32
created

Container   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 17
Bugs 1 Features 1
Metric Value
wmc 17
c 17
b 1
f 1
lcom 1
cbo 9
dl 0
loc 193
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A get() 0 15 3
A has() 0 9 2
A set() 0 16 2
A one() 0 8 1
A run() 0 8 1
A resolve() 0 5 1
A setDelegator() 0 6 1
A isWritable() 0 4 1
A registerSelf() 0 11 3
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\ContainerTrait;
22
use Phossa2\Di\Traits\ArrayAccessTrait;
23
use Phossa2\Shared\Base\ObjectAbstract;
24
use Interop\Container\ContainerInterface;
25
use Phossa2\Di\Interfaces\ScopeInterface;
26
use Phossa2\Di\Exception\RuntimeException;
27
use Phossa2\Di\Exception\NotFoundException;
28
use Phossa2\Config\Interfaces\ConfigInterface;
29
use Phossa2\Shared\Reference\DelegatorInterface;
30
use Phossa2\Config\Interfaces\WritableInterface;
31
use Phossa2\Di\Interfaces\FactoryAwareInterface;
32
use Phossa2\Di\Interfaces\ResolverAwareInterface;
33
use Phossa2\Shared\Reference\DelegatorAwareTrait;
34
use Phossa2\Di\Interfaces\ExtendedContainerInterface;
35
use Phossa2\Shared\Reference\DelegatorAwareInterface;
36
37
/**
38
 * Container
39
 *
40
 * A writable, array accessable, delegator-aware and extended instance container.
41
 *
42
 * - writable:
43
 *
44
 *   ```php
45
 *   $container->set('cache', $cache);
46
 *   ```
47
 *
48
 * - array accessable:
49
 *
50
 *   ```php
51
 *   // get
52
 *   $cache = $container['cache'];
53
 *
54
 *   // set/replace
55
 *   $container['cache'] = $anotherCache;
56
 *   ```
57
 *
58
 * - delegator-aware: lookup dependent instances in the delegator
59
 *
60
 *   ```php
61
 *   $delegator->addRegistry($container);
62
 *   ```
63
 *
64
 * - extended container
65
 *
66
 *   ```php
67
 *   // get new instance
68
 *   $newCache = $container->one('cache');
69
 *
70
 *   // run callables
71
 *   $container->run(['${#logger}', 'warning'], ['A warning message from ${user}']);
72
 *   ```
73
 *
74
 * @package Phossa2\Di
75
 * @author  Hong Zhang <[email protected]>
76
 * @see     ObjectAbstract
77
 * @see     ScopeInterface
78
 * @see     ContainerInterface
79
 * @see     ResolverAwareInterface
80
 * @see     FactoryAwareInterface
81
 * @see     ExtendedContainerInterface
82
 * @see     DelegatorAwareInterface
83
 * @see     WritableInterface
84
 * @see     \ArrayAccess
85
 * @version 2.0.0
86
 * @since   2.0.0 added
87
 */
88
class Container extends ObjectAbstract implements ContainerInterface, ResolverAwareInterface, FactoryAwareInterface, ScopeInterface, ExtendedContainerInterface, DelegatorAwareInterface, \ArrayAccess, WritableInterface
89
{
90
    use ContainerTrait, ArrayAccessTrait, DelegatorAwareTrait;
91
92
    /**
93
     * Inject a Phossa2\Config\Config
94
     *
95
     * ```php
96
     * $configData = [
97
     *     // container class
98
     *     'di.class' => 'Phossa2\\Di\\Container',
99
     *
100
     *     // container service definitions
101
     *     'di.service' => [
102
     *         // ...
103
     *     ],
104
     *
105
     *     // interface/classname mappings
106
     *     'di.mapping' => [
107
     *     ],
108
     *
109
     *     // init methods to run after container created
110
     *     'di.init' => [
111
     *         'default' => [],
112
     *         'mystuff' => [ ... ],
113
     *     ],
114
     * ];
115
     *
116
     * // instantiate $config
117
     * $config = new Config(null, null, $configData);
118
     *
119
     * // instantiate container
120
     * $container = new $config['di.class']($config);
121
     * ```
122
     *
123
     * @param  ConfigInterface $config inject the config instance
124
     * @param  string $baseNode container's starting node in $config
125
     * @access public
126
     */
127
    public function __construct(
128
        ConfigInterface $config = null,
129
        /*# string */ $baseNode = 'di'
130
    ) {
131
        $this->setResolver(new Resolver($this, $config ?: new Config(), $baseNode))
132
             ->setFactory(new Factory($this))
133
             ->registerSelf()
134
             ->initContainer();
135
    }
136
137
    /**
138
     * Extensions to the Interop\Container\ContainerInterface
139
     *
140
     * - Accepting second param as object constructor arguments
141
     * - Accpeting $id with scope appended, e.g. 'cache@myScope'
142
     *
143
     * {@inheritDoc}
144
     */
145
    public function get($id)
146
    {
147
        // not found
148
        if (!$this->has($id)) {
149
            throw new NotFoundException(
150
                Message::get(Message::DI_SERVICE_NOTFOUND, $id),
151
                Message::DI_SERVICE_NOTFOUND
152
            );
153
        }
154
155
        // get the instance, constructor args if any
156
        return $this->getInstance(
157
            $id, func_num_args() > 1 ? func_get_arg(1) : []
158
        );
159
    }
160
161
    /**
162
     * Extensions to the Interop\Container\ContainerInterface
163
     *
164
     * - Accpeting $id with scope appended, e.g. 'cache@myScope'
165
     *
166
     * {@inheritDoc}
167
     */
168
    public function has($id)
169
    {
170
        if (is_string($id)) {
171
            return $this->getResolver()->hasService(
172
                $this->idWithoutScope($id)
173
            );
174
        }
175
        return false;
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181
    public function set(/*# string */ $id, $value)
182
    {
183
        if ($this->isWritable()) {
184
            // set in service definition
185
            $this->getResolver()->setService(
186
                $this->idWithoutScope($id),
187
                $value
188
            );
189
            return $this;
190
        } else {
191
            throw new RuntimeException(
192
                Message::get(Message::DI_CONTAINER_READONLY, $id),
193
                Message::DI_CONTAINER_READONLY
194
            );
195
        }
196
    }
197
198
    /**
199
     * {@inheritDoc}
200
     */
201
    public function one(/*# string */ $id, array $arguments = [])
202
    {
203
        // set in single scope
204
        return $this->get(
205
            $this->scopedId($id, self::SCOPE_SINGLE),
206
            $arguments
207
        );
208
    }
209
210
    /**
211
     * {@inheritDoc}
212
     */
213
    public function run($callable, array $arguments = [])
214
    {
215
        // resolve any references in the callable(array) and arguments
216
        $this->resolve($callable);
217
        $this->resolve($arguments);
218
219
        return $this->getFactory()->executeCallable($callable, $arguments);
220
    }
221
222
    /**
223
     * {@inheritDoc}
224
     */
225
    public function resolve(&$toResolve)
226
    {
227
        $this->getResolver()->resolve($toResolve);
228
        return $this;
229
    }
230
231
    /**
232
     * - Overwrite `setDelegator()` from DelegatorAwareTrait
233
     * - Update resolver $object_resolver
234
     *
235
     * {@inheritDoc}
236
     */
237
    public function setDelegator(DelegatorInterface $delegator)
238
    {
239
        $this->delegator = $delegator;
240
        $this->getResolver()->setObjectResolver();
241
        return $this;
242
    }
243
244
    /**
245
     * {@inheritDoc}
246
     */
247
    public function isWritable()/*# : bool */
248
    {
249
        return false !== $this->writable;
250
    }
251
252
    /**
253
     * Register $this as 'di.service.container'
254
     *
255
     * - Later, $this can be referenced as '${#container}' anywhere
256
     *
257
     * - $skipCommon is to demonstrate skipping execute common methods for objects.
258
     *
259
     *   instead of just do
260
     *      `$container->set($name, $object)`
261
     *
262
     *   you may do
263
     *      $container->set($name, ['class' => $object, 'skip' => true]);
264
     *
265
     * @param  bool $skipCommon skip common methods normally after instantiation
266
     * @return $this
267
     * @access protected
268
     */
269
    protected function registerSelf(/*# bool */ $skipCommon = false)
270
    {
271
        $name = 'container';
272
        if (!$this->has($name) && $this->isWritable()) {
273
            $this->set(
274
                $name,
275
                ['class' => $this, 'skip' => $skipCommon]
276
            );
277
        }
278
        return $this;
279
    }
280
}
281