• 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

48.89
/src/DI/Definitions/Definition.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\Definitions;
11

12
use Nette;
13

14

15
/**
16
 * Definition used by ContainerBuilder.
17
 */
18
abstract class Definition
19
{
20
        use Nette\SmartObject;
21

22
        /** @var string|null */
23
        private $name;
24

25
        /** @var string|null  class or interface name */
26
        private $type;
27

28
        /** @var array */
29
        private $tags = [];
30

31
        /** @var bool|string[] */
32
        private $autowired = true;
33

34
        /** @var callable|null */
35
        private $notifier;
36

37

38
        /**
39
         * @return static
40
         * @internal  This is managed by ContainerBuilder and should not be called by user
41
         */
42
        final public function setName(string $name)
1✔
43
        {
44
                if ($this->name) {
1✔
45
                        throw new Nette\InvalidStateException('Name already has been set.');
×
46
                }
47

48
                $this->name = $name;
1✔
49
                return $this;
1✔
50
        }
51

52

53
        final public function getName(): ?string
54
        {
55
                return $this->name;
1✔
56
        }
57

58

59
        /** @return static */
60
        protected function setType(?string $type)
1✔
61
        {
62
                if ($this->autowired && $this->notifier && $this->type !== $type) {
1✔
63
                        ($this->notifier)();
1✔
64
                }
65

66
                if ($type === null) {
1✔
67
                        $this->type = null;
1✔
68
                } elseif (!class_exists($type) && !interface_exists($type)) {
1✔
69
                        throw new Nette\InvalidArgumentException(sprintf(
×
70
                                "Service '%s': Class or interface '%s' not found.",
×
71
                                $this->name,
×
72
                                $type
73
                        ));
74
                } else {
75
                        $this->type = Nette\DI\Helpers::normalizeClass($type);
1✔
76
                }
77

78
                return $this;
1✔
79
        }
80

81

82
        final public function getType(): ?string
83
        {
84
                return $this->type;
1✔
85
        }
86

87

88
        /** @return static */
89
        final public function setTags(array $tags)
1✔
90
        {
91
                $this->tags = $tags;
1✔
92
                return $this;
1✔
93
        }
94

95

96
        final public function getTags(): array
97
        {
98
                return $this->tags;
1✔
99
        }
100

101

102
        /**
103
         * @param  mixed  $attr
104
         * @return static
105
         */
106
        final public function addTag(string $tag, $attr = true)
107
        {
108
                $this->tags[$tag] = $attr;
×
109
                return $this;
×
110
        }
111

112

113
        /** @return mixed */
114
        final public function getTag(string $tag)
115
        {
116
                return $this->tags[$tag] ?? null;
×
117
        }
118

119

120
        /**
121
         * @param  bool|string|string[]  $state
122
         * @return static
123
         */
124
        final public function setAutowired($state = true)
125
        {
126
                if ($this->notifier && $this->autowired !== $state) {
×
127
                        ($this->notifier)();
×
128
                }
129

130
                $this->autowired = is_string($state) || is_array($state)
×
131
                        ? (array) $state
×
132
                        : (bool) $state;
×
133
                return $this;
×
134
        }
135

136

137
        /** @return bool|string[] */
138
        final public function getAutowired()
139
        {
140
                return $this->autowired;
1✔
141
        }
142

143

144
        /** @return static */
145
        public function setExported(bool $state = true)
146
        {
147
                return $this->addTag('nette.exported', $state);
×
148
        }
149

150

151
        public function isExported(): bool
152
        {
153
                return (bool) $this->getTag('nette.exported');
×
154
        }
155

156

157
        public function __clone()
158
        {
159
                $this->notifier = $this->name = null;
×
160
        }
161

162

163
        /********************* life cycle ****************d*g**/
164

165

166
        abstract public function resolveType(Nette\DI\Resolver $resolver): void;
167

168

169
        abstract public function complete(Nette\DI\Resolver $resolver): void;
170

171

172
        abstract public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void;
173

174

175
        final public function setNotifier(?callable $notifier): void
1✔
176
        {
177
                $this->notifier = $notifier;
1✔
178
        }
1✔
179

180

181
        /********************* deprecated stuff from former ServiceDefinition ****************d*g**/
182

183

184
        /** @deprecated Use setType() */
185
        public function setClass(?string $type)
186
        {
187
                return $this->setType($type);
×
188
        }
189

190

191
        /** @deprecated Use getType() */
192
        public function getClass(): ?string
193
        {
194
                return $this->getType();
×
195
        }
196

197

198
        /** @deprecated Use '$def instanceof Nette\DI\Definitions\ImportedDefinition' */
199
        public function isDynamic(): bool
200
        {
201
                trigger_error(sprintf('Service %s: %s() is deprecated, use "instanceof ImportedDefinition".', $this->getName(), __METHOD__), E_USER_DEPRECATED);
×
202
                return false;
×
203
        }
204

205

206
        /** @deprecated Use Nette\DI\Definitions\FactoryDefinition or AccessorDefinition */
207
        public function getImplement(): ?string
208
        {
209
                trigger_error(sprintf('Service %s: %s() is deprecated.', $this->getName(), __METHOD__), E_USER_DEPRECATED);
×
210
                return null;
×
211
        }
212

213

214
        /** @deprecated Use getAutowired() */
215
        public function isAutowired()
216
        {
217
                return $this->autowired;
×
218
        }
219
}
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