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

nette / di / 6739697931

03 Nov 2023 12:12AM UTC coverage: 93.859% (+0.005%) from 93.854%
6739697931

push

github

dg
ServiceDefinition: prepends @self to setup immediately

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

3 existing lines in 2 files now uncovered.

2262 of 2410 relevant lines covered (93.86%)

0.94 hits per line

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

98.68
/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
        /** @var Statement */
26
        private $creator;
27

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

31

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

37

38
        /** @return static */
39
        public function setType(?string $type)
1✔
40
        {
41
                return parent::setType($type);
1✔
42
        }
43

44

45
        /**
46
         * Alias for setCreator()
47
         * @param  string|array|Definition|Reference|Statement  $factory
48
         * @return static
49
         */
50
        public function setFactory($factory, array $args = [])
1✔
51
        {
52
                return $this->setCreator($factory, $args);
1✔
53
        }
54

55

56
        /**
57
         * Alias for getCreator()
58
         */
59
        public function getFactory(): Statement
60
        {
61
                return $this->getCreator();
1✔
62
        }
63

64

65
        /**
66
         * @param  string|array|Definition|Reference|Statement  $creator
67
         * @return static
68
         */
69
        public function setCreator($creator, array $args = [])
1✔
70
        {
71
                $this->creator = $creator instanceof Statement
1✔
72
                        ? $creator
1✔
73
                        : new Statement($creator, $args);
1✔
74
                return $this;
1✔
75
        }
76

77

78
        public function getCreator(): Statement
79
        {
80
                return $this->creator;
1✔
81
        }
82

83

84
        /** @return string|array|Definition|Reference|null */
85
        public function getEntity()
86
        {
87
                return $this->creator->getEntity();
1✔
88
        }
89

90

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

98

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

106

107
        /**
108
         * @param  Statement[]  $setup
109
         * @return static
110
         */
111
        public function setSetup(array $setup)
1✔
112
        {
113
                foreach ($setup as &$entity) {
1✔
114
                        if (!$entity instanceof Statement) {
1✔
UNCOV
115
                                throw new Nette\InvalidArgumentException('Argument must be Nette\DI\Definitions\Statement[].');
×
116
                        }
117
                        $entity = $this->prependSelf($entity);
1✔
118
                }
119

120
                $this->setup = $setup;
1✔
121
                return $this;
1✔
122
        }
123

124

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

131

132
        /**
133
         * @param  string|array|Definition|Reference|Statement  $entity
134
         * @return static
135
         */
136
        public function addSetup($entity, array $args = [])
1✔
137
        {
138
                $entity = $entity instanceof Statement
1✔
139
                        ? $entity
1✔
140
                        : new Statement($entity, $args);
1✔
141
                $this->setup[] = $this->prependSelf($entity);
1✔
142
                return $this;
1✔
143
        }
144

145

146
        public function resolveType(Nette\DI\Resolver $resolver): void
1✔
147
        {
148
                if (!$this->getEntity()) {
1✔
149
                        if (!$this->getType()) {
1✔
150
                                throw new ServiceCreationException('Factory and type are missing in definition of service.');
1✔
151
                        }
152

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

155
                } elseif (!$this->getType()) {
1✔
156
                        $type = $resolver->resolveEntityType($this->creator);
1✔
157
                        if (!$type) {
1✔
158
                                throw new ServiceCreationException('Unknown service type, specify it or declare return type of factory method.');
1✔
159
                        }
160

161
                        $this->setType($type);
1✔
162
                        $resolver->addDependency(new \ReflectionClass($type));
1✔
163
                }
164

165
                // auto-disable autowiring for aliases
166
                if ($this->getAutowired() === true && $this->getEntity() instanceof Reference) {
1✔
167
                        $this->setAutowired(false);
1✔
168
                }
169
        }
1✔
170

171

172
        public function complete(Nette\DI\Resolver $resolver): void
1✔
173
        {
174
                $entity = $this->creator->getEntity();
1✔
175
                if ($entity instanceof Reference && !$this->creator->arguments && !$this->setup) {
1✔
176
                        $ref = $resolver->normalizeReference($entity);
1✔
177
                        $this->setCreator([new Reference(Nette\DI\ContainerBuilder::ThisContainer), 'getService'], [$ref->getValue()]);
1✔
178
                }
179

180
                $this->creator = $resolver->completeStatement($this->creator);
1✔
181

182
                foreach ($this->setup as &$setup) {
1✔
183
                        $setup = $resolver->completeStatement($setup, true);
1✔
184
                }
185
        }
1✔
186

187

188
        private function prependSelf(Statement $setup): Statement
1✔
189
        {
190
                return is_string($setup->getEntity()) && strpbrk($setup->getEntity(), ':@?\\') === false
1✔
191
                        ? new Statement([new Reference(Reference::Self), $setup->getEntity()], $setup->arguments)
1✔
192
                        : $setup;
1✔
193
        }
194

195

196
        public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
1✔
197
        {
198
                $code = $generator->formatStatement($this->creator) . ";\n";
1✔
199
                if (!$this->setup) {
1✔
200
                        $method->setBody('return ' . $code);
1✔
201
                        return;
1✔
202
                }
203

204
                $code = '$service = ' . $code;
1✔
205
                foreach ($this->setup as $setup) {
1✔
206
                        $code .= $generator->formatStatement($setup) . ";\n";
1✔
207
                }
208

209
                $code .= 'return $service;';
1✔
210
                $method->setBody($code);
1✔
211
        }
1✔
212

213

214
        public function __clone()
215
        {
216
                parent::__clone();
1✔
217
                $this->creator = unserialize(serialize($this->creator));
1✔
218
                $this->setup = unserialize(serialize($this->setup));
1✔
219
        }
1✔
220
}
221

222

223
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