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 ( 15625b...b1045e )
by Hong
02:42
created

Container::isAuto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\ArrayAccessTrait;
22
use Phossa2\Shared\Base\ObjectAbstract;
23
use Phossa2\Config\Traits\WritableTrait;
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\Di\Traits\InstanceFactoryTrait;
29
use Phossa2\Config\Interfaces\ConfigInterface;
30
use Phossa2\Config\Interfaces\WritableInterface;
31
use Phossa2\Shared\Delegator\DelegatorAwareTrait;
32
use Phossa2\Di\Interfaces\ExtendedContainerInterface;
33
use Phossa2\Shared\Delegator\DelegatorAwareInterface;
34
35
/**
36
 * Container
37
 *
38
 * A writable, array accessable, delegator-aware and extended instance container.
39
 *
40
 * - writable:
41
 *
42
 *   ```php
43
 *   $container->set('cache', $cache);
44
 *   ```
45
 *
46
 * - array accessable:
47
 *
48
 *   ```php
49
 *   // get
50
 *   $cache = $container['cache'];
51
 *
52
 *   // set/replace
53
 *   $container['cache'] = $anotherCache;
54
 *   ```
55
 *
56
 * - delegator-aware: lookup dependent instances in the delegator
57
 *
58
 *   ```php
59
 *   $delegator->addContainer($container);
60
 *   ```
61
 *
62
 * - extended container
63
 *
64
 *   ```php
65
 *   // get new instance
66
 *   $newCache = $container->one('cache');
67
 *
68
 *   // run callables
69
 *   $container->run(['${#logger}', 'warning'], ['A warning message from ${user}']);
70
 *   ```
71
 *
72
 * @package Phossa2\Di
73
 * @author  Hong Zhang <[email protected]>
74
 * @see     ObjectAbstract
75
 * @see     ScopeInterface
76
 * @see     ContainerInterface
77
 * @see     ExtendedContainerInterface
78
 * @see     DelegatorAwareInterface
79
 * @see     WritableInterface
80
 * @see     \ArrayAccess
81
 * @version 2.0.0
82
 * @since   2.0.0 added
83
 */
84
class Container extends ObjectAbstract implements ContainerInterface, ScopeInterface, WritableInterface, \ArrayAccess, DelegatorAwareInterface, ExtendedContainerInterface
85
{
86
    use WritableTrait,
87
        ArrayAccessTrait,
88
        DelegatorAwareTrait,
89
        InstanceFactoryTrait;
90
91
    /**
92
     * Inject a Phossa2\Config\Config
93
     *
94
     * ```php
95
     * $configData = [
96
     *     // container class
97
     *     'di.class' => 'Phossa2\\Di\\Container',
98
     *
99
     *     // container service definitions
100
     *     'di.service' => [
101
     *         // ...
102
     *     ],
103
     *
104
     *     // init methods to run after container created
105
     *     'di.init' => [
106
     *         'default' => [],
107
     *         'mystuff' => [ ... ],
108
     *     ],
109
     * ];
110
     *
111
     * // instantiate $config
112
     * $config = new Config(null, null, $configData);
113
     *
114
     * // instantiate container
115
     * $container = new $config['di.class']($config);
116
     * ```
117
     *
118
     * @param  ConfigInterface $config inject the config instance
119
     * @param  string $baseNode container's starting node in $config
120
     * @access public
121
     */
122
    public function __construct(
123
        ConfigInterface $config = null,
124
        /*# string */ $baseNode = 'di'
125
    ) {
126
        // set resolver
127
        $this->setResolver(
128
            new Resolver($this, $config ?: new Config(), $baseNode)
129
        );
130
131
        // set factory
132
        $this->setFactory(new Factory($this->getResolver()));
133
134
        // run methods in 'di.init'
135
        $this->initContainer();
136
    }
137
138
    // ContainerInterface related
139
140
    /**
141
     * Extensions to the Interop\Container\ContainerInterface
142
     *
143
     * - Accepting second param as object constructor arguments
144
     * - Accpeting $id with scope appended, e.g. 'cache@myScope'
145
     *
146
     * {@inheritDoc}
147
     */
148
    public function get($id)
149
    {
150
        if ($this->has($id)) {
151
            $args = func_num_args() > 1 ? func_get_arg(1) : [];
152
            $this->resolve($args);
153
            return $this->getInstance($id, $args);
154
        } else {
155
            throw new NotFoundException(
156
                Message::get(Message::DI_SERVICE_NOTFOUND, $id),
157
                Message::DI_SERVICE_NOTFOUND
158
            );
159
        }
160
    }
161
162
    /**
163
     * Extensions to the Interop\Container\ContainerInterface
164
     *
165
     * - Accpeting $id with scope appended, e.g. 'cache@myScope'
166
     *
167
     * {@inheritDoc}
168
     */
169
    public function has($id)
170
    {
171
        if (is_string($id)) {
172
            return $this->getResolver()->hasService(
173
                $this->idWithoutScope($id)
174
            );
175
        }
176
        return false;
177
    }
178
179
    // ExtendedContainerInterface
180
181
    /**
182
     * {@inheritDoc}
183
     */
184
    public function one(/*# string */ $id, array $arguments = [])
185
    {
186
        return $this->get(
187
            $this->scopedId($id, ScopeInterface::SCOPE_SINGLE), $arguments
188
        );
189
    }
190
191
    /**
192
     * {@inheritDoc}
193
     */
194
    public function run($callable, array $arguments = [])
195
    {
196
        $this->resolve($callable);
197
        $this->resolve($arguments);
198
199
        return $this->getFactory()->executeCallable($callable, $arguments);
200
    }
201
202
    /**
203
     * {@inheritDoc}
204
     */
205
    public function param(/*# string */ $name, $value)
206
    {
207
        $this->getResolver()->set((string) $name, $value);
208
        return $this;
209
    }
210
211
    // AutoWiringInterface related
212
213
    /**
214
     * {@inheritDoc}
215
     */
216
    public function auto(/*# bool */ $flag = true)
217
    {
218
        $this->getResolver()->auto($flag);
219
        return $this;
220
    }
221
222
    /**
223
     * {@inheritDoc}
224
     */
225
    public function isAuto()/*# : bool */
226
    {
227
        return $this->getResolver()->isAuto();
228
    }
229
230
    // ReferenceResolveInterface related
231
232
    /**
233
     * {@inheritDoc}
234
     */
235
    public function resolve(&$toResolve)
236
    {
237
        $this->getResolver()->resolve($toResolve);
238
        return $this;
239
    }
240
241
    // ScopeInterface related
242
243
    /**
244
     * @inheritDoc
245
     */
246
    public function share(/*# bool */ $shared = true)
247
    {
248
        $this->default_scope = (bool) $shared ? self::SCOPE_SHARED :
249
            self::SCOPE_SINGLE;
250
        return $this;
251
    }
252
253
    // WritableInterface related
254
255
    /**
256
     * {@inheritDoc}
257
     */
258
    public function set(/*# string */ $id, $value)
259
    {
260
        if ($this->isWritable()) {
261
            list($rawId, $scope) = $this->splitId($id);
262
263
            $this->getResolver()->setService(
264
                $rawId,
265
                '' === $scope ? $value : $this->scopedData($value, $scope)
266
            );
267
            return $this;
268
        } else {
269
            throw new RuntimeException(
270
                Message::get(Message::DI_CONTAINER_READONLY, $id),
271
                Message::DI_CONTAINER_READONLY
272
            );
273
        }
274
    }
275
276
    /**
277
     * Override 'isWritable()' in 'Phossa2\Config\Traits\WritableTrait'
278
     *
279
     * Container's writability is depend on its resolver
280
     *
281
     * {@inheritDoc}
282
     */
283
    public function isWritable()/*# : bool */
284
    {
285
        return $this->getResolver()->isWritable();
286
    }
287
288
    /**
289
     * Override 'setWritable()' in 'Phossa2\Config\Traits\WritableTrait'
290
     *
291
     * Container's writability is depend on its resolver
292
     *
293
     * {@inheritDoc}
294
     */
295
    public function setWritable($writable)/*# : bool */
296
    {
297
        return $this->getResolver()->setWritable((bool) $writable);
298
    }
299
}
300