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 ( b1cdd5...322edd )
by Hong
03:50
created

Container::setDelegator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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
     * @param  bool $writable able to inject object to the container
126
     * @access public
127
     */
128
    public function __construct(
129
        ConfigInterface $config = null,
130
        /*# string */ $baseNode = 'di',
131
        /*# bool */ $writable = true
132
    ) {
133
        if (null === $config) {
134
            $config = new Config();
135
        }
136
137
        if ($config instanceof WritableInterface) {
138
            $this->setWritable($writable);
139
            $config->setWritable($writable);
140
        }
141
142
        $this->setResolver(new Resolver($this, $config, $baseNode))
143
            ->setFactory(new Factory($this))->registerSelf()->initContainer();
144
    }
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
        // not found
157
        if (!$this->has($id)) {
158
            throw new NotFoundException(
159
                Message::get(Message::DI_SERVICE_NOTFOUND, $id),
160
                Message::DI_SERVICE_NOTFOUND
161
            );
162
        }
163
164
        // get the instance, constructor args if any
165
        return $this->getInstance(
166
            $id, func_num_args() > 1 ? func_get_arg(1) : []
167
        );
168
    }
169
170
    /**
171
     * Extensions to the Interop\Container\ContainerInterface
172
     *
173
     * - Accpeting $id with scope appended, e.g. 'cache@myScope'
174
     *
175
     * {@inheritDoc}
176
     */
177
    public function has($id)
178
    {
179
        if (is_string($id)) {
180
            return $this->getResolver()->hasService(
181
                $this->idWithoutScope($id)
182
            );
183
        }
184
        return false;
185
    }
186
187
    /**
188
     * {@inheritDoc}
189
     */
190
    public function set(/*# string */ $id, $value)
191
    {
192
        if ($this->isWritable()) {
193
            // set in service definition
194
            $this->getResolver()->setService(
195
                $this->idWithoutScope($id),
196
                $value
197
            );
198
            return $this;
199
        } else {
200
            throw new RuntimeException(
201
                Message::get(Message::DI_CONTAINER_READONLY, $id),
202
                Message::DI_CONTAINER_READONLY
203
            );
204
        }
205
    }
206
207
    /**
208
     * {@inheritDoc}
209
     */
210
    public function one(/*# string */ $id, array $arguments = [])
211
    {
212
        // set in single scope
213
        return $this->get(
214
            $this->scopedId($id, self::SCOPE_SINGLE),
215
            $arguments
216
        );
217
    }
218
219
    /**
220
     * {@inheritDoc}
221
     */
222
    public function run($callable, array $arguments = [])
223
    {
224
        // resolve any references in the callable(array) and arguments
225
        $this->resolve($callable);
226
        $this->resolve($arguments);
227
228
        return $this->getFactory()->executeCallable($callable, $arguments);
229
    }
230
231
    /**
232
     * {@inheritDoc}
233
     */
234
    public function resolve(&$toResolve)
235
    {
236
        $this->getResolver()->resolve($toResolve);
237
        return $this;
238
    }
239
240
    /**
241
     * - Overwrite `setDelegator()` from DelegatorAwareTrait
242
     * - Update resolver $object_resolver
243
     *
244
     * {@inheritDoc}
245
     */
246
    public function setDelegator(DelegatorInterface $delegator)
247
    {
248
        $this->delegator = $delegator;
249
        $this->getResolver()->setObjectResolver();
250
        return $this;
251
    }
252
253
    /**
254
     * Register $this as 'di.service.container'
255
     *
256
     * - Later, $this can be referenced as '${#container}' anywhere
257
     *
258
     * - $skipCommon is to demonstrate skipping execute common methods for objects.
259
     *
260
     *   instead of just do
261
     *      `$container->set($name, $object)`
262
     *
263
     *   you may do
264
     *      $container->set($name, ['class' => $object, 'skip' => true]);
265
     *
266
     * @param  bool $skipCommon skip common methods normally after instantiation
267
     * @return $this
268
     * @access protected
269
     */
270
    protected function registerSelf(/*# bool */ $skipCommon = false)
271
    {
272
        $name = 'container';
273
        if (!$this->has($name) && $this->isWritable()) {
274
            $this->set(
275
                $name,
276
                ['class' => $this, 'skip' => $skipCommon]
277
            );
278
        }
279
        return $this;
280
    }
281
}
282