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

nette / di / 4405606637

pending completion
4405606637

push

github

David Grudl
more self explanatory message for factory and service mismatch (closes #199) (#284)

8 of 8 new or added lines in 1 file covered. (100.0%)

2118 of 2238 relevant lines covered (94.64%)

0.95 hits per line

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

97.44
/src/DI/Definitions/ServiceDefinition.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
use Nette\DI\ServiceCreationException;
14

15

16
/**
17
 * Definition of standard service.
18
 *
19
 * @property string|null $class
20
 * @property Statement $factory
21
 * @property Statement[] $setup
22
 */
23
final class ServiceDefinition extends Definition
24
{
25
        private Statement $creator;
26

27
        /** @var Statement[] */
28
        private array $setup = [];
29

30

31
        public function __construct()
32
        {
33
                $this->creator = new Statement(null);
1✔
34
        }
1✔
35

36

37
        public function getDescriptor(): string
38
        {
39
                $entity = $this->getEntity();
1✔
40
                if ($entity && $this->isAnonymous()) {
1✔
41
                        return 'Service ' . (is_string($entity) ? "of type $entity" : Nette\DI\Helpers::entityToString($entity));
1✔
42
                }
43

44
                return parent::getDescriptor();
1✔
45
        }
46

47

48
        public function setType(?string $type): static
1✔
49
        {
50
                return parent::setType($type);
1✔
51
        }
52

53

54
        /**
55
         * Alias for setCreator()
56
         */
57
        public function setFactory(string|array|Definition|Reference|Statement $factory, array $args = []): static
1✔
58
        {
59
                return $this->setCreator($factory, $args);
1✔
60
        }
61

62

63
        /**
64
         * Alias for getCreator()
65
         */
66
        public function getFactory(): Statement
67
        {
68
                return $this->getCreator();
×
69
        }
70

71

72
        public function setCreator(string|array|Definition|Reference|Statement $creator, array $args = []): static
1✔
73
        {
74
                $this->creator = $creator instanceof Statement
1✔
75
                        ? $creator
1✔
76
                        : new Statement($creator, $args);
1✔
77
                return $this;
1✔
78
        }
79

80

81
        public function getCreator(): Statement
82
        {
83
                return $this->creator;
1✔
84
        }
85

86

87
        public function getEntity(): string|array|Definition|Reference|null
88
        {
89
                return $this->creator->getEntity();
1✔
90
        }
91

92

93
        public function setArguments(array $args = []): static
1✔
94
        {
95
                $this->creator->arguments = $args;
1✔
96
                return $this;
1✔
97
        }
98

99

100
        public function setArgument($key, $value): static
101
        {
102
                $this->creator->arguments[$key] = $value;
1✔
103
                return $this;
1✔
104
        }
105

106

107
        /**
108
         * @param  Statement[]  $setup
109
         */
110
        public function setSetup(array $setup): static
1✔
111
        {
112
                foreach ($setup as $v) {
1✔
113
                        if (!$v instanceof Statement) {
1✔
114
                                throw new Nette\InvalidArgumentException('Argument must be Nette\DI\Definitions\Statement[].');
×
115
                        }
116
                }
117

118
                $this->setup = $setup;
1✔
119
                return $this;
1✔
120
        }
121

122

123
        /** @return Statement[] */
124
        public function getSetup(): array
125
        {
126
                return $this->setup;
1✔
127
        }
128

129

130
        public function addSetup(string|array|Definition|Reference|Statement $entity, array $args = []): static
1✔
131
        {
132
                $this->setup[] = $entity instanceof Statement
1✔
133
                        ? $entity
1✔
134
                        : new Statement($entity, $args);
1✔
135
                return $this;
1✔
136
        }
137

138

139
        public function resolveType(Nette\DI\Resolver $resolver): void
1✔
140
        {
141
                if (!$this->getEntity()) {
1✔
142
                        if (!$this->getType()) {
1✔
143
                                throw new ServiceCreationException('Factory and type are missing in definition of service.');
1✔
144
                        }
145

146
                        $this->setCreator($this->getType(), $this->creator->arguments ?? []);
1✔
147

148
                } elseif (!$this->getType()) {
1✔
149
                        $type = $resolver->resolveEntityType($this->creator);
1✔
150
                        if (!$type) {
1✔
151
                                throw new ServiceCreationException('Unknown service type, specify it or declare return type of factory method.');
1✔
152
                        }
153

154
                        $this->setType($type);
1✔
155
                        $resolver->addDependency(new \ReflectionClass($type));
1✔
156
                }
157

158
                // auto-disable autowiring for aliases
159
                if ($this->getAutowired() === true && $this->getEntity() instanceof Reference) {
1✔
160
                        $this->setAutowired(false);
1✔
161
                }
162
        }
1✔
163

164

165
        public function complete(Nette\DI\Resolver $resolver): void
1✔
166
        {
167
                $entity = $this->creator->getEntity();
1✔
168
                if ($entity instanceof Reference && !$this->creator->arguments && !$this->setup) {
1✔
169
                        $ref = $resolver->normalizeReference($entity);
1✔
170
                        $this->setCreator([new Reference(Nette\DI\ContainerBuilder::ThisContainer), 'getService'], [$ref->getValue()]);
1✔
171
                }
172

173
                $this->creator = $resolver->completeStatement($this->creator);
1✔
174

175
                foreach ($this->setup as &$setup) {
1✔
176
                        if (
177
                                is_string($setup->getEntity())
1✔
178
                                && strpbrk($setup->getEntity(), ':@?\\') === false
1✔
179
                        ) { // auto-prepend @self
180
                                $setup = new Statement([new Reference(Reference::Self), $setup->getEntity()], $setup->arguments);
1✔
181
                        }
182

183
                        $setup = $resolver->completeStatement($setup, true);
1✔
184
                }
185
        }
1✔
186

187

188
        public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
1✔
189
        {
190
                $entity = $this->creator->getEntity();
1✔
191
                $code = $generator->formatStatement($this->creator) . ";\n";
1✔
192
                if (!$this->setup) {
1✔
193
                        $method->setBody('return ' . $code);
1✔
194
                        return;
1✔
195
                }
196

197
                $code = '$service = ' . $code;
1✔
198
                foreach ($this->setup as $setup) {
1✔
199
                        $code .= $generator->formatStatement($setup) . ";\n";
1✔
200
                }
201

202
                $code .= 'return $service;';
1✔
203
                $method->setBody($code);
1✔
204
        }
1✔
205

206

207
        public function __clone()
208
        {
209
                parent::__clone();
1✔
210
                $this->creator = unserialize(serialize($this->creator));
1✔
211
                $this->setup = unserialize(serialize($this->setup));
1✔
212
        }
1✔
213
}
214

215

216
class_exists(Nette\DI\ServiceDefinition::class);
1✔
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