Skip to content

Commit 9729dd6

Browse files
committed
readme.md: updated code
1 parent 0a8500a commit 9729dd6

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

readme.md

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ We have the object representing email:
5050
```php
5151
class Mail
5252
{
53-
public $subject;
54-
public $message;
53+
public string $subject;
54+
public string $message;
5555
}
5656
```
5757

@@ -60,7 +60,7 @@ An object which can send emails:
6060
```php
6161
interface Mailer
6262
{
63-
function send(Mail $mail, $to);
63+
function send(Mail $mail, string $to): void;
6464
}
6565
```
6666

@@ -69,7 +69,7 @@ A support for logging:
6969
```php
7070
interface Logger
7171
{
72-
function log($message);
72+
function log(string $message): void;
7373
}
7474
```
7575

@@ -78,23 +78,25 @@ And finally, a class that provides sending newsletters:
7878
```php
7979
class NewsletterManager
8080
{
81-
private $mailer;
82-
private $logger;
81+
private Mailer $mailer;
82+
private Logger $logger;
8383

84-
function __construct(Mailer $mailer, Logger $logger)
84+
public function __construct(Mailer $mailer, Logger $logger)
8585
{
8686
$this->mailer = $mailer;
8787
$this->logger = $logger;
8888
}
8989

90-
function distribute(array $recipients)
90+
public function distribute(array $recipients): void
9191
{
9292
$mail = new Mail;
93-
...
93+
$mail->subject = '...';
94+
$mail->message = '...';
95+
9496
foreach ($recipients as $recipient) {
9597
$this->mailer->send($mail, $recipient);
9698
}
97-
$this->logger->log(...);
99+
$this->logger->log('...');
98100
}
99101
}
100102
```
@@ -106,22 +108,22 @@ Also, we have a ability to implement own `Logger` or `Mailer`, like this:
106108
```php
107109
class SendMailMailer implements Mailer
108110
{
109-
function send(Mail $mail, $to)
111+
public function send(Mail $mail, string $to): void
110112
{
111113
mail($to, $mail->subject, $mail->message);
112114
}
113115
}
114116

115117
class FileLogger implements Logger
116118
{
117-
private $file;
119+
private string $file;
118120

119-
function __construct($file)
121+
public function __construct(string $file)
120122
{
121123
$this->file = $file;
122124
}
123125

124-
function log($message)
126+
public function log(string $message): void
125127
{
126128
file_put_contents($this->file, $message . "\n", FILE_APPEND);
127129
}
@@ -135,26 +137,26 @@ Container for our application might look like this:
135137
```php
136138
class Container
137139
{
138-
private $logger;
139-
private $mailer;
140+
private ?Logger $logger;
141+
private ?Mailer $mailer;
140142

141-
function getLogger()
143+
public function getLogger(): Logger
142144
{
143-
if (!$this->logger) {
145+
if (!isset($this->logger)) {
144146
$this->logger = new FileLogger('log.txt');
145147
}
146148
return $this->logger;
147149
}
148150

149-
function getMailer()
151+
public function getMailer(): Mailer
150152
{
151-
if (!$this->mailer) {
153+
if (!isset($this->mailer)) {
152154
$this->mailer = new SendMailMailer;
153155
}
154156
return $this->mailer;
155157
}
156158

157-
function createNewsletterManager()
159+
public function createNewsletterManager(): NewsletterManager
158160
{
159161
return new NewsletterManager($this->getMailer(), $this->getLogger());
160162
}
@@ -213,7 +215,7 @@ The container will be generated only once and the code is stored in cache (in di
213215
During development it is useful to activate auto-refresh mode which automatically regenerate the container when any class or configuration file is changed. Just in the constructor `ContainerLoader` append `true` as the second argument:
214216

215217
```php
216-
$loader = new Nette\DI\ContainerLoader(__DIR__ . '/temp', true);
218+
$loader = new Nette\DI\ContainerLoader(__DIR__ . '/temp', autoRebuild: true);
217219
```
218220

219221

@@ -244,17 +246,17 @@ Class of the service:
244246
```php
245247
class NewsletterManager
246248
{
247-
private $anotherService;
249+
private AnotherService $anotherService;
248250

249-
public function setAnotherService(AnotherService $service)
251+
public function setAnotherService(AnotherService $service): void
250252
{
251253
$this->anotherService = $service;
252254
}
253255

254256
...
255257
```
256258

257-
We can also add the `inject: yes` directive. This directive will enable automatic call of `inject*` methods and passing dependencies to public variables with `@inject` annotations:
259+
We can also add the `inject: yes` directive. This directive will enable automatic call of `inject*` methods and passing dependencies to public variables with #[Inject] attribute:
258260

259261
```neon
260262
services:
@@ -266,21 +268,23 @@ services:
266268
Dependency `Service1` will be passed by calling the `inject*` method, dependency `Service2` will be assigned to the `$service2` variable:
267269

268270
```php
271+
use Nette\DI\Attributes\Inject;
272+
269273
class FooClass
270274
{
271-
private $service1;
275+
private Service1 $service1;
272276

273277
// 1) inject* method:
274278

275-
public function injectService1(Service1 $service)
279+
public function injectService1(Service1 $service): void
276280
{
277281
$this->service1 = $service1;
278282
}
279283

280-
// 2) Assign to the variable with the @inject annotation:
284+
// 2) Assign to the variable with the #[Inject] attribute:
281285

282-
/** @inject @var Service2 */
283-
public $service2;
286+
#[Inject]
287+
public Service2 $service2;
284288
}
285289
```
286290

@@ -291,17 +295,14 @@ However, this method is not ideal, because the variable must be declared as publ
291295
Factories
292296
---------
293297

294-
We can use factories generated from an interface. The interface must declare the returning type in the `@return` annotation of the method. Nette will generate a proper implementation of the interface.
298+
We can use factories generated from an interface. The interface must declare the returning type of the method. Nette will generate a proper implementation of the interface.
295299

296300
The interface must have exactly one method named `create`. Our factory interface could be declared in the following way:
297301

298302
```php
299-
interface IBarFactory
303+
interface BarFactory
300304
{
301-
/**
302-
* @return Bar
303-
*/
304-
public function create();
305+
function create(): Bar;
305306
}
306307
```
307308

@@ -310,7 +311,7 @@ The `create` method will instantiate an `Bar` with the following definition:
310311
```php
311312
class Bar
312313
{
313-
private $logger;
314+
private Logger $logger;
314315

315316
public function __construct(Logger $logger)
316317
{
@@ -323,15 +324,15 @@ The factory will be registered in the `config.neon` file:
323324

324325
```neon
325326
services:
326-
- IBarFactory
327+
- BarFactory
327328
```
328329

329330
Nette will check if the declared service is an interface. If yes, it will also generate the corresponding implementation of the factory. The definition can be also written in a more verbose form:
330331

331332
```neon
332333
services:
333334
barFactory:
334-
implement: IBarFactory
335+
implement: BarFactory
335336
```
336337

337338
This full definition allows us to declare additional configuration of the object using the `arguments` and `setup` sections, similarly as for all other services.
@@ -341,14 +342,14 @@ In our code, we only have to obtain the factory instance and call the `create` m
341342
```php
342343
class Foo
343344
{
344-
private $barFactory;
345+
private BarFactory $barFactory;
345346

346-
function __construct(IBarFactory $barFactory)
347+
function __construct(BarFactory $barFactory)
347348
{
348349
$this->barFactory = $barFactory;
349350
}
350351

351-
function bar()
352+
function bar(): void
352353
{
353354
$bar = $this->barFactory->create();
354355
}

0 commit comments

Comments
 (0)