3 Methods to Create Service using Factory in Symfony 7

3 Methods to Create Service using Factory in Symfony 7

In some applications, we may need to use the factory design pattern for creating an instance of the class. In Symfony framework, the service container can invoke the method of factory class to create the desired service.

This tutorial provides 3 methods how to create service using factory in Symfony 7 application.

For demonstration purposes, let's say we have the following simple class:

src/Service/DataEncryptionService.php

<?php

namespace App\Service;

class DataEncryptionService
{
}

The following command will be used to test whether a service was registered in the container after creating service using factory:

php bin/console debug:container DataEncryptionService

Method 1 - Static factory

Create a factory class with static method that returns an instance of the service:

src/Factory/DataEncryptionServiceFactory.php

<?php

namespace App\Factory;

use App\Service\DataEncryptionService;

class DataEncryptionServiceFactory
{
    public static function createService(): DataEncryptionService
    {
        return new DataEncryptionService();
    }
}

Register service in configuration file as follows:

config/services.yaml

services:
    # ...

    App\Service\DataEncryptionService:
        factory: ['App\Factory\DataEncryptionServiceFactory', 'createService']

Output example of debug:container command:

---------------- ------------------------------------------  
  Option           Value                                    
---------------- ------------------------------------------
 Service ID       App\Service\DataEncryptionService
 ...
 Factory Class    App\Factory\DataEncryptionServiceFactory
 Factory Method   createService                              
---------------- ------------------------------------------

Method 2 - Non-static factory

Create a factory class with regular method that returns an instance of the service:

src/Factory/DataEncryptionServiceFactory.php

<?php

namespace App\Factory;

use App\Service\DataEncryptionService;

class DataEncryptionServiceFactory
{
    public function createService(): DataEncryptionService
    {
        return new DataEncryptionService();
    }
}

In the configuration file, register service and factory itself as follows:

config/services.yaml

services:
    # ...

    App\Factory\DataEncryptionServiceFactory: ~

    App\Service\DataEncryptionService:
        factory: ['@App\Factory\DataEncryptionServiceFactory', 'createService']

Method 3 - Invokable factory

Create a factory class with __invoke magic method that returns an instance of the service:

src/Factory/DataEncryptionServiceFactory.php

<?php

namespace App\Factory;

use App\Service\DataEncryptionService;

class DataEncryptionServiceFactory
{
    public function __invoke(): DataEncryptionService
    {
        return new DataEncryptionService();
    }
}

Next, register service and factory itself in configuration file:

config/services.yaml

services:
    # ...

    App\Factory\DataEncryptionServiceFactory: ~

    App\Service\DataEncryptionService:
        factory: '@App\Factory\DataEncryptionServiceFactory'

Leave a Comment

Cancel reply

Your email address will not be published.