Completed
Push — master ( 3d0920...259888 )
by Hong
03:03
created

ContainerTrait::initContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Di
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Di\Util;
13
14
use Phoole\Config\ConfigInterface;
15
use Psr\Container\ContainerInterface;
16
use Phoole\Base\Reference\ReferenceTrait;
17
18
/**
19
 * ContainerTrait
20
 *
21
 * @package Phoole\Di
22
 */
23
trait ContainerTrait
24
{
25
    use FactoryTrait;
26
    use ReferenceTrait;
27
    
28
    /**
29
     * for configuration lookup
30
     *
31
     * @var ConfigInterface
32
     */
33
    protected $config;
34
35
    /**
36
     * delegator for object lookup
37
     *
38
     * @var ContainerInterface
39
     */
40
    protected $delegator;
41
42
    /**
43
     * object pool
44
     *
45
     * @var array
46
     */
47
    protected $objects;
48
49
    /**
50
     * service prefix
51
     *
52
     * @var string
53
     */
54
    protected $prefix = 'di.service.';
55
56
    /**
57
     * common prefix
58
     *
59
     * @var string
60
     */
61
    protected $common = 'di.common';
62
63
    /**
64
     * Reload all service definitions
65
     *
66
     * @return void
67
     */
68
    protected function reloadAll(): void
69
    {
70
        $this->objects = [];
71
72
        // some predefined objects
73
        $this->objects['config'] = $this->config;
74
        $this->objects['container'] = $this->delegator;
75
76
        // do the job
77
        $settings = &($this->config->getTree())->get('');
78
        $this->deReference($settings);
79
    }
80
81
    /**
82
     * Get the instance
83
     *
84
     * @param  string $id
85
     * @return object
86
     */
87
    protected function getInstance(string $id): object
88
    {
89
        // get new object
90
        if ('@' === substr($id, -1)) {
91
            return $this->newInstance($id);
92
        }
93
94
        // check the pool for shared object
95
        if (!isset($this->objects[$id])) {
96
            $this->objects[$id] = $this->newInstance($id);
97
        }
98
        return $this->objects[$id];
99
    }
100
101
    /**
102
     * creaet a new instance
103
     *
104
     * @param  string $id
105
     * @return object
106
     */
107
    protected function newInstance(string $id): object
108
    {
109
        $def = $this->config->get($this->getRawId($id));
110
        $obj = $this->fabricate($def);
111
        $this->executeCommon($obj);
112
        return $obj;
113
    }
114
115
    /**
116
     * Try find a service in the definition
117
     *
118
     * @param  string $id
119
     * @return bool
120
     */
121
    protected function hasDefinition(string $id): bool
122
    {
123
        return $this->config->has($this->getRawId($id));
124
    }
125
126
    /**
127
     * get the raw id as defined in $config
128
     *
129
     * @param  string $id
130
     * @return string
131
     */
132
    protected function getRawId(string $id): string
133
    {
134
        return $this->prefix . explode('@', $id, 2)[0];
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140
    protected function getReference(string $name)
141
    {
142
        return $this->delegator->get($name);
143
    }
144
145
    /**
146
     * execute common methods for newed objects
147
     *
148
     * @param  object $object
149
     * @return void
150
     */
151
    protected function executeCommon(object $object): void
152
    {
153
        if ($this->config->has($this->common)) {
154
            $args = [ $object ];
155
            foreach ($this->config->get($this->common) as $pair) {
156
                $tester = $pair[0]; // test function
157
                $runner = $pair[1]; // callable with $object as arguments
158
                if ($tester($object, $this)) {
159
                    $this->executeCallable($runner, $args);
160
                }
161
            }
162
        }
163
    }
164
}
165