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

nette / di / 6739035766

02 Nov 2023 10:44PM UTC coverage: 52.037% (-41.8%) from 93.846%
6739035766

push

github

dg
x

1009 of 1939 relevant lines covered (52.04%)

0.52 hits per line

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

38.46
/src/DI/CompilerExtension.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;
11

12
use Nette;
13

14

15
/**
16
 * Configurator compiling extension.
17
 */
18
abstract class CompilerExtension
19
{
20
        use Nette\SmartObject;
21

22
        /** @var Compiler */
23
        protected $compiler;
24

25
        /** @var string */
26
        protected $name;
27

28
        /** @var array|object */
29
        protected $config = [];
30

31
        /** @var Nette\PhpGenerator\Closure */
32
        protected $initialization;
33

34

35
        /** @return static */
36
        public function setCompiler(Compiler $compiler, string $name)
1✔
37
        {
38
                $this->initialization = new Nette\PhpGenerator\Closure;
1✔
39
                $this->compiler = $compiler;
1✔
40
                $this->name = $name;
1✔
41
                return $this;
1✔
42
        }
43

44

45
        /**
46
         * @param  array|object  $config
47
         * @return static
48
         */
49
        public function setConfig($config)
50
        {
51
                if (!is_array($config) && !is_object($config)) {
1✔
52
                        throw new Nette\InvalidArgumentException;
×
53
                }
54

55
                $this->config = $config;
1✔
56
                return $this;
1✔
57
        }
58

59

60
        /**
61
         * Returns extension configuration.
62
         * @return array|object
63
         */
64
        public function getConfig()
65
        {
66
                return $this->config;
×
67
        }
68

69

70
        /**
71
         * Returns configuration schema.
72
         */
73
        public function getConfigSchema(): Nette\Schema\Schema
74
        {
75
                return is_object($this->config)
1✔
76
                        ? Nette\Schema\Expect::from($this->config)
×
77
                        : Nette\Schema\Expect::array();
1✔
78
        }
79

80

81
        /**
82
         * Checks whether $config contains only $expected items and returns combined array.
83
         * @throws Nette\InvalidStateException
84
         * @deprecated  use getConfigSchema()
85
         */
86
        public function validateConfig(array $expected, ?array $config = null, ?string $name = null): array
87
        {
88
                if (func_num_args() === 1) {
×
89
                        return $this->config = $this->validateConfig($expected, $this->config);
×
90
                }
91

92
                if ($extra = array_diff_key((array) $config, $expected)) {
×
93
                        $name = $name ? str_replace('.', "\u{a0}›\u{a0}", $name) : $this->name;
×
94
                        $hint = Nette\Utils\Helpers::getSuggestion(array_keys($expected), key($extra));
×
95
                        throw new Nette\DI\InvalidConfigurationException(sprintf(
×
96
                                "Unknown configuration option '%s\u{a0}›\u{a0}%s'",
×
97
                                $name,
98
                                $hint ? key($extra) : implode("', '{$name}\u{a0}›\u{a0}", array_keys($extra))
×
99
                        ) . ($hint ? ", did you mean '{$name}\u{a0}›\u{a0}{$hint}'?" : '.'));
×
100
                }
101

102
                return Nette\Schema\Helpers::merge($config, $expected);
×
103
        }
104

105

106
        public function getContainerBuilder(): ContainerBuilder
107
        {
108
                return $this->compiler->getContainerBuilder();
1✔
109
        }
110

111

112
        /**
113
         * Reads configuration from file.
114
         */
115
        public function loadFromFile(string $file): array
116
        {
117
                $loader = $this->createLoader();
×
118
                $res = $loader->load($file);
×
119
                $this->compiler->addDependencies($loader->getDependencies());
×
120
                return $res;
×
121
        }
122

123

124
        /**
125
         * Loads list of service definitions from configuration.
126
         * Prefixes its names and replaces @extension with name in definition.
127
         */
128
        public function loadDefinitionsFromConfig(array $configList): void
129
        {
130
                $res = [];
×
131
                foreach ($configList as $key => $config) {
×
132
                        $key = is_string($key) ? $this->name . '.' . $key : $key;
×
133
                        $res[$key] = Helpers::prefixServiceName($config, $this->name);
×
134
                }
135

136
                $this->compiler->loadDefinitionsFromConfig($res);
×
137
        }
138

139

140
        protected function createLoader(): Config\Loader
141
        {
142
                return new Config\Loader;
×
143
        }
144

145

146
        public function getInitialization(): Nette\PhpGenerator\Closure
147
        {
148
                return $this->initialization;
1✔
149
        }
150

151

152
        /**
153
         * Prepend extension name to identifier or service name.
154
         */
155
        public function prefix(string $id): string
156
        {
157
                return substr_replace($id, $this->name . '.', substr($id, 0, 1) === '@' ? 1 : 0, 0);
×
158
        }
159

160

161
        /**
162
         * Processes configuration data. Intended to be overridden by descendant.
163
         * @return void
164
         */
165
        public function loadConfiguration()
166
        {
167
        }
168

169

170
        /**
171
         * Adjusts DI container before is compiled to PHP class. Intended to be overridden by descendant.
172
         * @return void
173
         */
174
        public function beforeCompile()
175
        {
176
        }
1✔
177

178

179
        /**
180
         * Adjusts DI container compiled to PHP class. Intended to be overridden by descendant.
181
         * @return void
182
         */
183
        public function afterCompile(Nette\PhpGenerator\ClassType $class)
1✔
184
        {
185
        }
1✔
186
}
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