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 ( 706a33...19eae1 )
by Hong
02:54
created

Container::setWritable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
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 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\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, WritableTrait;
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  Config $config inject the config instance
124
     * @param  string $baseNode container's starting node in $config
125
     * @access public
126
     */
127
    public function __construct(
128
        Config $config = null,
129
        /*# string */ $baseNode = 'di'
130
    ) {
131
        $conf = $config ?: new Config();
132
133
        $this
134
            ->setResolver(new Resolver($this, $conf, $baseNode))
135
            ->setFactory(new Factory($this))
136
            ->registerObject('container', $this)
137
            ->registerObject('config', $conf)
138
            ->initContainer();
139
    }
140
141
    /**
142
     * Extensions to the Interop\Container\ContainerInterface
143
     *
144
     * - Accepting second param as object constructor arguments
145
     * - Accpeting $id with scope appended, e.g. 'cache@myScope'
146
     *
147
     * {@inheritDoc}
148
     */
149
    public function get($id)
150
    {
151
        // not found
152
        if (!$this->has($id)) {
153
            throw new NotFoundException(
154
                Message::get(Message::DI_SERVICE_NOTFOUND, $id),
155
                Message::DI_SERVICE_NOTFOUND
156
            );
157
        }
158
159
        // get the instance, constructor args if any
160
        return $this->getInstance(
161
            $id, func_num_args() > 1 ? func_get_arg(1) : []
162
        );
163
    }
164
165
    /**
166
     * Extensions to the Interop\Container\ContainerInterface
167
     *
168
     * - Accpeting $id with scope appended, e.g. 'cache@myScope'
169
     *
170
     * {@inheritDoc}
171
     */
172
    public function has($id)
173
    {
174
        if (is_string($id)) {
175
            return $this->getResolver()->hasService(
176
                $this->idWithoutScope($id)
177
            );
178
        }
179
        return false;
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185
    public function set(/*# string */ $id, $value)
186
    {
187
        if ($this->isWritable()) {
188
            // set in service definition
189
            $this->getResolver()->setService(
190
                $this->idWithoutScope($id),
191
                $value
192
            );
193
            return $this;
194
        } else {
195
            throw new RuntimeException(
196
                Message::get(Message::DI_CONTAINER_READONLY, $id),
197
                Message::DI_CONTAINER_READONLY
198
            );
199
        }
200
    }
201
202
    /**
203
     * {@inheritDoc}
204
     */
205
    public function one(/*# string */ $id, array $arguments = [])
206
    {
207
        // set in single scope
208
        return $this->get(
209
            $this->scopedId($id, self::SCOPE_SINGLE),
210
            $arguments
211
        );
212
    }
213
214
    /**
215
     * {@inheritDoc}
216
     */
217
    public function run($callable, array $arguments = [])
218
    {
219
        // resolve any references in the callable(array) and arguments
220
        $this->resolve($callable);
221
        $this->resolve($arguments);
222
223
        return $this->getFactory()->executeCallable($callable, $arguments);
224
    }
225
226
    /**
227
     * {@inheritDoc}
228
     */
229
    public function map(/*# string */ $from, $to)
230
    {
231
        $this->getResolver()->setMapping($from, $to);
232
        return $this;
233
    }
234
235
    /**
236
     * {@inheritDoc}
237
     */
238
    public function auto(/*# bool */ $on = true)
239
    {
240
        $this->getResolver()->autoWiring($on);
241
        return $this;
242
    }
243
244
    /**
245
     * {@inheritDoc}
246
     */
247
    public function param(/*# string */ $name, $value)
248
    {
249
        $this->getResolver()->set((string) $name, $value);
250
        return $this;
251
    }
252
253
    /**
254
     * {@inheritDoc}
255
     */
256
    public function resolve(&$toResolve)
257
    {
258
        $this->getResolver()->resolve($toResolve);
259
        return $this;
260
    }
261
262
    /**
263
     * Override `setDelegator()` from 'Phossa2\Shared\Reference\DelegatorAwareTrait'
264
     *
265
     * {@inheritDoc}
266
     */
267
    public function setDelegator(DelegatorInterface $delegator)
268
    {
269
        $this->delegator = $delegator;
270
271
        // this will make sure all dependencies will be looked up in the delegator
272
        $this->getResolver()->setObjectResolver();
273
274
        return $this;
275
    }
276
277
    /**
278
     * Override 'isWritable()' in 'Phossa2\Config\Traits\WritableTrait'
279
     *
280
     * Container's writability is depend on its resolver
281
     *
282
     * {@inheritDoc}
283
     */
284
    public function isWritable()/*# : bool */
285
    {
286
        return $this->getResolver()->isWritable();
287
    }
288
289
    /**
290
     * Override 'setWritable()' in 'Phossa2\Config\Traits\WritableTrait'
291
     *
292
     * Container's writability is depend on its resolver
293
     *
294
     * {@inheritDoc}
295
     */
296
    public function setWritable($writable)/*# : bool */
297
    {
298
        $this->getResolver()->setWritable((bool) $writable);
299
        return $this;
300
    }
301
302
    /**
303
     * Register object in 'di.service' with $name
304
     *
305
     * e.g. `$this->registerObject('container', $this)`
306
     *
307
     * - Later, $this can be referenced as '${#container}' anywhere
308
     *
309
     * - $object will skip execute common methods for created instances.
310
     *
311
     *   instead of just do
312
     *      `$container->set($name, $object)`
313
     *
314
     *   you may do
315
     *      $container->set($name, ['class' => $object, 'skip' => true]);
316
     *
317
     * @param  string $name name to register with
318
     * @param  object $object
319
     * @return $this
320
     * @access protected
321
     */
322
    protected function registerObject(/*# string */ $name, $object)
323
    {
324
        if (!$this->has($name) && $this->isWritable()) {
325
            $this->set($name, ['class' => $object, 'skip' => true]);
326
        }
327
        return $this;
328
    }
329
}
330