Skip to content

Commit 6c50bbc

Browse files
BooleanTypeLeroid
authored andcommitted
Marker interface; arguments.
Subclasses implement a marker interface. Using arguments: [...] instead of the variable key binding in services.yaml.
1 parent 526101c commit 6c50bbc

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

service_container/factories.rst

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,23 @@ Usage example
308308
The following example is intended to show how to create and use a factory method in Symfony framework.
309309
Suppose you want to realize the factory method pattern for services, that describe two delivery methods - DHL and UPS.
310310

311+
Firstly, define marker interface for delivery methods classes::
312+
313+
// src/Deliveries/DeliveryInterface.php
314+
namespace App\Deliveries;
315+
316+
interface DeliveryInterface
317+
{
318+
}
319+
311320
Services (subclasses) definition::
312321

313322
// src/Deliveries/DHL.php
314323
namespace App\Deliveries;
324+
325+
use App\Deliveries\DeliveryInterface;
315326

316-
class DHL
327+
class DHL implements DeliveryInterface
317328
{
318329
public $costLabel;
319330
@@ -324,8 +335,10 @@ Services (subclasses) definition::
324335
325336
// src/Deliveries/UPS.php
326337
namespace App\Deliveries;
338+
339+
use App\Deliveries\DeliveryInterface;
327340

328-
class UPS
341+
class UPS implements DeliveryInterface
329342
{
330343
public $costLabel;
331344
@@ -338,12 +351,19 @@ Factory definition::
338351

339352
// src/Factories/DeliveryFactory.php
340353
namespace App\Factories;
354+
355+
use App\Deliveries\DeliveryInterface;
356+
use RuntimeException;
341357
342358
abstract class DeliveryFactory
343359
{
344360
public static function create($deliveryMethod)
345361
{
346362
$delivery = new $deliveryMethod;
363+
if ( ! $delivery instanceof DeliveryInterface) {
364+
throw new RuntimeException(sprintf('%1$s should implement %2$s.', $deliveryMethod, DeliveryInterface::class));
365+
}
366+
347367
$delivery->costLabel = 'Delivery cost is: ';
348368
349369
return $delivery;
@@ -366,12 +386,10 @@ Next, use settings similar to those in the sections above. These settings allow
366386
367387
App\Deliveries\DHL:
368388
factory: ['@App\Factories\DeliveryFactory', create]
369-
arguments:
370-
$deliveryMethod: 'App\Deliveries\DHL'
389+
arguments: ['App\Deliveries\DHL']
371390
App\Deliveries\UPS:
372391
factory: ['@App\Factories\DeliveryFactory', create]
373-
arguments:
374-
$deliveryMethod: 'App\Deliveries\UPS'
392+
arguments: ['App\Deliveries\UPS']
375393
376394
377395
.. code-block:: xml
@@ -388,11 +406,11 @@ Next, use settings similar to those in the sections above. These settings allow
388406
389407
<service id="App\Deliveries\DHL">
390408
<factory service="App\Factories\DeliveryFactory" method="create"/>
391-
<argument key="$deliveryMethod">App\Deliveries\DHL</argument>
409+
<argument>App\Deliveries\DHL</argument>
392410
</service>
393411
<service id="App\Deliveries\UPS">
394412
<factory service="App\Factories\DeliveryFactory" method="create"/>
395-
<argument key="$deliveryMethod">App\Deliveries\UPS</argument>
413+
<argument>App\Deliveries\UPS</argument>
396414
</service>
397415
</services>
398416
</container>
@@ -411,11 +429,11 @@ Next, use settings similar to those in the sections above. These settings allow
411429
412430
$services->set(DHL::class)
413431
->factory([ref(DeliveryFactory::class), 'create'])
414-
->arg('$deliveryMethod', 'App\Deliveries\DHL')
432+
->args(['App\Deliveries\DHL'])
415433
;
416434
$services->set(UPS::class)
417435
->factory([ref(DeliveryFactory::class), 'create'])
418-
->arg('$deliveryMethod', 'App\Deliveries\UPS')
436+
->args(['App\Deliveries\UPS'])
419437
;
420438
};
421439

0 commit comments

Comments
 (0)