• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / di / 5772360309

pending completion
5772360309

push

github

dg
Resolver: added new special functions constant() and getProperty()

17 of 17 new or added lines in 2 files covered. (100.0%)

2093 of 2215 relevant lines covered (94.49%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.24
/src/DI/Extensions/DIExtension.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\DI\Extensions;
11

12
use Nette;
13
use Tracy;
14

15

16
/**
17
 * DI extension.
18
 */
19
final class DIExtension extends Nette\DI\CompilerExtension
20
{
21
        public array $exportedTags = [];
22
        public array $exportedTypes = [];
23
        private bool $debugMode;
24
        private float $time;
25

26

27
        public function __construct(bool $debugMode = false)
1✔
28
        {
29
                $this->debugMode = $debugMode;
1✔
30
                $this->time = microtime(true);
1✔
31

32
                $this->config = new class {
1✔
33
                        public ?bool $debugger = null;
34

35
                        /** @var string[] */
36
                        public array $excluded = [];
37
                        public ?string $parentClass = null;
38
                        public object $export;
39
                };
40
                $this->config->export = new class {
1✔
41
                        public bool $parameters = true;
42

43
                        /** @var string[]|bool|null */
44
                        public array|bool|null $tags = true;
45

46
                        /** @var string[]|bool|null */
47
                        public array|bool|null $types = true;
48
                };
49
        }
1✔
50

51

52
        public function loadConfiguration()
53
        {
54
                $builder = $this->getContainerBuilder();
1✔
55
                $builder->addExcludedClasses($this->config->excluded);
1✔
56
        }
1✔
57

58

59
        public function beforeCompile()
60
        {
61
                if (!$this->config->export->parameters) {
1✔
62
                        $this->getContainerBuilder()->parameters = [];
1✔
63
                }
64
        }
1✔
65

66

67
        public function afterCompile(Nette\PhpGenerator\ClassType $class)
1✔
68
        {
69
                if ($this->config->parentClass) {
1✔
70
                        $class->setExtends($this->config->parentClass);
1✔
71
                }
72

73
                $this->restrictTags($class);
1✔
74
                $this->restrictTypes($class);
1✔
75

76
                if (
77
                        $this->debugMode &&
1✔
78
                        ($this->config->debugger ?? $this->getContainerBuilder()->getByType(Tracy\Bar::class))
1✔
79
                ) {
80
                        $this->enableTracyIntegration();
×
81
                }
82
        }
1✔
83

84

85
        private function restrictTags(Nette\PhpGenerator\ClassType $class): void
1✔
86
        {
87
                $option = $this->config->export->tags;
1✔
88
                if ($option === true) {
1✔
89
                } elseif ($option === false) {
1✔
90
                        $class->removeProperty('tags');
1✔
91
                } elseif ($prop = $class->getProperties()['tags'] ?? null) {
1✔
92
                        $prop->setValue(array_intersect_key($prop->getValue(), $this->exportedTags + array_flip((array) $option)));
1✔
93
                }
94
        }
1✔
95

96

97
        private function restrictTypes(Nette\PhpGenerator\ClassType $class): void
1✔
98
        {
99
                $option = $this->config->export->types;
1✔
100
                if ($option === true) {
1✔
101
                        return;
1✔
102
                }
103

104
                $prop = $class->getProperty('wiring');
1✔
105
                $prop->setValue(array_intersect_key(
1✔
106
                        $prop->getValue(),
1✔
107
                        $this->exportedTypes + (is_array($option) ? array_flip($option) : []),
1✔
108
                ));
109
        }
1✔
110

111

112
        private function enableTracyIntegration(): void
113
        {
114
                Nette\Bridges\DITracy\ContainerPanel::$compilationTime = $this->time;
×
115
                $this->initialization->addBody($this->getContainerBuilder()->formatPhp('?;', [
×
116
                        new Nette\DI\Definitions\Statement('@Tracy\Bar::addPanel', [new Nette\DI\Definitions\Statement(Nette\Bridges\DITracy\ContainerPanel::class)]),
×
117
                ]));
118
        }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc