Несколько командных классов Symfony с одинаковыми зависимостями: избегайте повторения кода - PullRequest
0 голосов
/ 17 февраля 2019

В проекте Symfony 4 у меня есть две команды (и, возможно, еще больше), которые имеют одинаковые зависимости.В настоящее время у меня есть некоторый повтор кода в моем коде для двух команд, я знаю, что должен быть в состоянии сделать это более СУХИМЫМ и эффективным, но не уверен, как именно я должен организовать это.

Вот как яимейте их пока:

# config/services.yaml

parameters:
    api_client_id: '%env(API_CLIENT_ID)%'
    api_client_secret: '%env(API_CLIENT_SECRET)%'
    api_client_id_sandbox: '%env(API_CLIENT_ID_SANDBOX)%'
    api_client_secret_sandbox: '%env(API_CLIENT_SECRET_SANDBOX)%'
    api_env: '%env(API_ENV)%'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones


    # Registering 3rd Party API Manager.
    app.api_service_factory:
        class: App\Service\APIServiceFactory
        arguments:
            - "%api_client_id%"
            - "%api_client_secret%"
            - "%api_client_id_sandbox%"
            - "%api_client_secret_sandbox%"
            - "%api_env%"  


    # Collect promotions command.
    App\Command\CollectPromotionsCommand:
        arguments:
            - "@app.api_service_factory"
        tags:
            - { name: 'console.command', command: 'app:collect-promotions' }


    # Processes the reserved topups orders.
    App\Command\ProcessReservedTopupsCommand:
        arguments:
            - "@app.api_service_factory"
        tags:
            - { name: 'console.command', command: 'app:process-reserved-topups' }

<?php
// src/Command/CollectPromotionsCommand.php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use App\Service\APIServiceFactory;
use Doctrine\ORM\EntityManagerInterface;

class CollectPromotionsCommand extends Command
{

  /**
   * @var APIServiceFactory
   */
  protected $apiServiceFactory;

  /**
   * @var EntityManagerInterface
   */
  protected $em;

  protected static $defaultName = 'app:collect-promotions';

  public function __construct(APIServiceFactory $apiServiceFactory, EntityManagerInterface $em) {
    $this->apiServiceFactory  = $apiServiceFactory;
    $this->em                         = $em;

    parent::__construct();
  }

// ...


}

<?php
// src/Command/ProcessReservedTopupsCommand.php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use App\Service\APIServiceFactory;
use Doctrine\ORM\EntityManagerInterface;

class ProcessReservedTopupsCommand extends Command
{

  /**
   * @var APIServiceFactory
   */
  protected $apiServiceFactory;

  /**
   * @var EntityManagerInterface
   */
  protected $em;

  protected static $defaultName = 'app:collect-promotions';

  public function __construct(APIServiceFactory $apiServiceFactory, EntityManagerInterface $em) {
    $this->apiServiceFactory  = $apiServiceFactory;
    $this->em                         = $em;

    parent::__construct();
  }

// ...


}

Любая помощь, чтобы заставить этот код выглядеть более приличным, очень ценится.

1 Ответ

0 голосов
/ 18 февраля 2019

Personnaly Я бы не стал этого делать, потому что предпочитаю ясность, даже если это подразумевает небольшое дублирование.

Но если вам интересно, как избежать дублирования кода в конструкторах и настройках ваших serivces, вот ответ: https://symfony.com/doc/current/service_container/parent_services.html. Речь идет о введении базового класса для ваших команд.Symfony также позволит вам избежать дублирования в конфигурации.

Ваш пример будет выглядеть так:

class BaseCommand extends Command
{
  /**
   * @var APIServiceFactory
   */
  protected $apiServiceFactory;

  /**
   * @var EntityManagerInterface
   */
  protected $em;

  public function __construct(APIServiceFactory $apiServiceFactory, EntityManagerInterface $em) {
    $this->apiServiceFactory  = $apiServiceFactory;
    $this->em = $em;

    parent::__construct();
  }

  // ...
}

и

class CollectPromotionsCommand extends BaseCommand
{
    // ...
}

и

class ProcessReservedTopupsCommand extends BaseCommand
{
    // ...
}

Конфигурация:

# Base API command depending on API.
App\Command\BaseCommand:
    abstract: true
    arguments:
        - "@app.api_service_factory"

# Collect promotions command.
App\Command\CollectPromotionsCommand:
    parent: App\Command\BaseCommand
    tags:
        - { name: 'console.command', command: 'app:collect-promotions' }


# Processes the reserved topups orders.
App\Command\ProcessReservedTopupsCommand:
    parent: App\Command\BaseCommand
    tags:
        - { name: 'console.command', command: 'app:process-reserved-topups' }
...