Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
75.00% covered (warning)
75.00%
3 / 4
CRAP
50.00% covered (danger)
50.00%
1 / 2
ScopedDiContainer
80.00% covered (warning)
80.00%
8 / 10
66.67% covered (warning)
66.67%
2 / 3
8.51
0.00% covered (danger)
0.00%
0 / 1
 __construct
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
5.02
 add
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 disposeImpl
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
LocalScopeDiItem
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\DI;
6
7use PeServer\Core\DI\DiContainer;
8use PeServer\Core\DI\DiItem;
9use PeServer\Core\DI\DiRegisterContainer;
10
11/**
12 * 限定的DIコンテナ実装。
13 */
14class ScopedDiContainer extends DiRegisterContainer implements IScopedDiContainer
15{
16    /**
17     * 生成。
18     *
19     * @param DiContainer $sourceContainer 元になるDIコンテナ。
20     */
21    public function __construct(DiContainer $sourceContainer)
22    {
23        foreach ($sourceContainer->mapping as $key => $item) {
24            if ($item->lifecycle === DiItem::LIFECYCLE_SINGLETON && !$item->hasSingletonValue()) {
25                $this->mapping[$key] = new LocalScopeDiItem($item);
26                continue;
27            }
28            $this->mapping[$key] = $item;
29        }
30    }
31
32    #region IScopedDiContainer
33
34    public function add(string $id, DiItem $item): void
35    {
36        parent::add($id, new LocalScopeDiItem($item));
37    }
38
39    protected function disposeImpl(): void
40    {
41        foreach ($this->mapping as $item) {
42            if ($item instanceof LocalScopeDiItem) {
43                $item->dispose();
44            }
45        }
46
47        $this->mapping = [];
48    }
49
50    #endregion
51}
52
53/**
54 * `ScopedDiContainer` で削除対象になるアイテム。
55 */
56//phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
57final class LocalScopeDiItem extends DiItem
58{
59    public function __construct(DiItem $source)
60    {
61        parent::__construct($source->lifecycle, $source->type, $source->data);
62    }
63}