lagdo/symfony-facades

Call Symfony services using facades.

v2.3.1 2024-09-27 20:02 UTC

This package is auto-updated.

Last update: 2024-11-04 17:24:21 UTC


README

Build Status Scrutinizer Code Quality StyleCI codecov

Latest Stable Version Total Downloads License

Facades for Symfony services

With this package, Symfony services can be called using facades, with static method syntax.

It is a simpler alternative to passing services as parameters in the constructors of other classes, or using lazy services. It will be especially interesting in the case when a class depends on many services, but calls some of them only occasionally.

Installation

Install the package with composer.

composer require lagdo/symfony-facades

Register the Lagdo\Symfony\Facades\FacadesBundle bundle in the config/bundles.php file.

Usage

A facade inherits from the Lagdo\Symfony\Facades\AbstractFacade abstract class, and implements the getServiceIdentifier() method, which must return the id of the corresponding service in the Symfony service container.

namespace App\Facades;

use App\Services\MyService;
use Lagdo\Symfony\Facades\AbstractFacade;

class MyFacade extends AbstractFacade
{
    /**
     * @inheritDoc
     */
    protected static function getServiceIdentifier(): string
    {
        return MyService::class;
    }
}

The methods of the App\Services\MyService service can now be called using the App\Facades\MyFacade facade, like this.

class TheService
{
    public function theMethod()
    {
        MyFacade::myMethod();
    }
}

Instead of this.

class TheService
{
    /**
     * @var MyService
     */
    protected $myService;

    public function __construct(MyService $myService)
    {
        $this->myService = $myService;
    }

    public function theMethod()
    {
        $this->myService->myMethod();
    }
}

Using a service locator

The above facade will work only for services that are declared as public.

In order to call private services with facades, a service locator with id lagdo.facades.service_locator must be declared in the config/services.yaml file. See the Symfony service locators documentation.

The private services that need to be accessed with a facade must be passed as arguments to the service locator. For each argument, the key is the service id in the facade, while the value is the service id in the container.

In the following example, the Twig service is passed to the service locator.

    lagdo.facades.service_locator:
        public: true
        class: Symfony\Component\DependencyInjection\ServiceLocator
        tags: ['container.service_locator']
        arguments:
            -
                Twig\Environment: '@twig'

A facade can then be defined for the Twig service.

namespace App\Facades;

use Lagdo\Symfony\Facades\AbstractFacade;

class View extends AbstractFacade
{
    /**
     * @inheritdoc
     */
    protected static function getServiceIdentifier(): string
    {
        return \Twig\Environment::class;
    }
}

Templates can now be rendered using the facade.

use App\Facades\View;

class TheService
{
    public function theMethod()
    {
        ...
        $html = View::render($template, $vars);
        ...
    }
}

The lagdo.facades.service tag

Starting from version 2.3.0, the private services that need to be accessed with a facade can be tagged with lagdo.facades.service.

These services will then be automatically passed to the service locator, together with those received as arguments.

In the following example, the App\Services\TaggedService service will be passed to the service locator.

    lagdo.facades.service_locator:
        public: true
        class: Symfony\Component\DependencyInjection\ServiceLocator
        tags: ['container.service_locator']
        arguments:
            -
                Twig\Environment: '@twig'

    App\Services\TaggedService:
        tags: [lagdo.facades.service]
        class: App\Services\TaggedService

A facade can then be defined for the service.

namespace App\Facades;

use App\Services\TaggedService;
use Lagdo\Symfony\Facades\AbstractFacade;

class TaggedServiceFacade extends AbstractFacade
{
    /**
     * @inheritdoc
     */
    protected static function getServiceIdentifier(): string
    {
        return TaggedService::class;
    }
}

Getting the service instance

Starting from version 2.0.0, the instance() method returns the instance of the service.

class TheService
{
    public function theMethod()
    {
        /**
         * @var MyService $service
         */
        $service = MyFacade::instance();
        $service->myMethod();
    }
}

The ServiceInstance trait

By default, each call to a facade method will also call the Symfony service container.

Starting from version 2.2.0, the service instance can be saved in the facade after the first call to the Symfony service container, using the ServiceInstance trait. The next calls with return the service instance without calling the Symfony service container.

namespace App\Facades;

use App\Services\MyService;
use Lagdo\Symfony\Facades\AbstractFacade;
use Lagdo\Symfony\Facades\ServiceInstance;

class MyFacade extends AbstractFacade
{
    use ServiceInstance;

    /**
     * @inheritDoc
     */
    protected static function getServiceIdentifier(): string
    {
        return MyService::class;
    }
}

The Symfony service container is called only once in this example code.

    MyFacade::myMethod1(); // Calls the Symfony service container
    MyFacade::myMethod2(); // Doesn't call the Symfony service container
    MyFacade::myMethod1(); // Doesn't call the Symfony service container

Provided facades

This package includes facades for some Symfony services.

Logger

The logger service must be passed to the service locator in the config/services.yaml file.

    lagdo.facades.service_locator:
        public: true
        class: Symfony\Component\DependencyInjection\ServiceLocator
        tags: ['container.service_locator']
        arguments:
            -
                Psr\Log\LoggerInterface: '@logger'

Messages can now be logged using the facade.

use Lagdo\Symfony\Facades\Log;

Log::info($message, $vars);

View

The twig service must be passed to the service locator in the config/services.yaml file.

    lagdo.facades.service_locator:
        public: true
        class: Symfony\Component\DependencyInjection\ServiceLocator
        tags: ['container.service_locator']
        arguments:
            -
                Twig\Environment: '@twig'

Views can now be rendered using the facade.

use Lagdo\Symfony\Facades\View;

$html = View::render($template, $vars);

Contribute

  • Issue Tracker: github.com/lagdo/symfony-facades/issues
  • Source Code: github.com/lagdo/symfony-facades

License

The package is licensed under the 3-Clause BSD license.