如何在symfony 5 bundle中重用代码?第5部分。配置

让我们谈谈如何停止项目之间的复制粘贴并将代码转移到可重复使用的插件包Symfony 5中。在实践中总结了一系列关于包的经验的文章,将从创建最小的包,重构演示应用程序到测试以及包发布周期的实践中进行。


一篇文章中,我们讨论了如何使用标签扩展主机应用程序中捆绑软件的功能。在本文中,我们添加了灵活性捆绑包:创建配置文件并定义几个参数。


  • DI容器参数及其重新定义
  • 捆绑包配置文件
  • 使用配置


如果您没有按顺序完成本教程,请从存储库下载该应用程序,然后切换到4-extend分支


有关在文件中安装和启动项目的说明README.md您可以在5配置分支中找到本文代码的最终版本


DI容器参数及其重新定义


捆绑包中已经有一个配置文件config/services.yaml,用于确定DI容器的服务配置。您可以在那里定义参数。


捆绑软件的任何参数和服务都可以在主机应用程序中覆盖。

我们可以使用它来允许用户自定义捆绑包的操作。例如,我们希望使所谓的软删除模式成为可能(当删除记录时未从数据库中删除记录,但将其标记为“已存档”时)。此功能已实现,但默认情况下已禁用。


为此,请在config/services.yaml捆绑软件中输入参数


parameters:
    bravik.calendar.enable_soft_delete: true

注意参数名称的格式venodor.package.parameter我们snake_case通过在点上添加前缀:供应商和包装的名称。参数是在整个应用程序的公共命名空间中定义的,使用前缀可以减少名称冲突的可能性。


EditorController . $enableSoftDelete, false:


public function __construct(
    EventRepository $eventRepository,
    bool $enableSoftDelete = false
) { 
    //... 
}

, services.yaml :


bravik\CalendarBundle\Controller\EditorController:
    arguments:
        $enableSoftDelete: '%bravik.calendar.enable_soft_delete%'

, -   «».  , ,     « »,   .  false,   ,   «-».


services.yaml -. , , !


, :


  1. -, . , - , «  ». , . , ,
  2. -, .

.



: , , , , .

, . Symfony .


config/packages/, , . . , .


.


load() DependencyInjection/CalendarExtension.php :


public function load(array $configs, ContainerBuilder $container) {}

, ContainerBuilder $configs. dd($configs) : .


config/packages/ calendar.yaml:


calendar:                     # extension key
  enable_soft_delete: false

, Symfony config/packages/ . CalendarExtension::load(), Extension-, Extension snake_case. , .


, $configs:


^ array:1 [▼
  0 => array:1 [▼
    "enable_soft_delete" => false
  ]
]

PHP , - . ?


, . packages test, prod dev .


, . , , framework , $configs. .


(extension key) Symfony , load(). .


. 1 , dd($configs) .


:


, ? , .


CalendarExtension Configuration:


<?php
namespace bravik\CalendarBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('calendar');

        $treeBuilder->getRootNode()
            ->children()
                ->booleanNode('enable_soft_delete')->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

TreeBuilder, , enable_soft_delete boolean.


TreeBuilder , , . , , .

, CalendarExtension::load() :


public function load(array $configs, ContainerBuilder $container)
{
    //...
    $configuration = new Configuration();
    $config = $this->processConfiguration($configuration, $configs);
}

processConfiguration() , Configuration, , , $config.


$config, .


, :


$container->setParameter(
    'bravik.calendar.enable_soft_delete',
    $config['enable_soft_delete']
);

, bravik.calendar.enable_soft_delete config/service.yaml.


   , «»   .


services.yaml bravik.calendar.enable_soft_delete. enable_soft_delete :


//$container->setParameter(
//    'bravik.calendar.enable_soft_delete',
//    $config['enable_soft_delete']
//);

$definition = $container->getDefinition(EditorController::class);
$definition->setArguments([
  '$enableSoftDelete' => $config['enable_soft_delete'],
]);


, , .


true/false :


Exception: Invalid type for path "calendar.enable_soft_delete". Expected boolean, but got string.

, ,


Exception: Undefined index: enable_soft_delete

: . . Configuration.


$treeBuilder->getRootNode()
    ->children()
        ->booleanNode('enable_soft_delete')
            ->defaultValue(false)
//            ->defaultFalse() //    booleanNode()
        ->end()
    ->end()
;

, , . :


$treeBuilder->getRootNode()
    ->children()
        ->booleanNode('enable_soft_delete')
            ->isRequired()
        ->end()
    ->end()
;

, . , deprecated.


. Symfony bin/console config:dump calendar, .


$treeBuilder->getRootNode()
    ->children()
        ->booleanNode('enable_soft_delete')
            ->isRequired()
            ->setDeprecated()
            ->info('Enables soft delete mode for articles. Articles would be marked as `archived` instead of deletion')
        ->end()
    ->end()
;

:


calendar:
    limits:
      per_day: 10
      per_month: 100

$treeBuilder->getRootNode()
    ->children()
        ->arrayNode('limits')
            ->addDefaultsIfNotSet()
            ->children()
                ->integerNode('per_day')
                    ->defaultValue(10)
                    ->validate()
                        ->ifTrue(function ($v) { return $v <= 0; })
                        ->thenInvalid('Number must be positive')
                    ->end()
                ->end()
                ->integerNode('per_month')
                    ->defaultValue(100)
                    ->validate()
                        ->ifTrue(function ($v) { return $v <= 0; })
                        ->thenInvalid('Number must be positive')
                    ->end()
                ->end()
            ->end()
        ->end()
    ->end()
    ;

. : .


Symfony , .

: .


c , . .


:


calendar:
    available_locales:
          locales:
              - { code: 'en', label: 'English' }
              - { code: 'ru', label: '' }

$treeBuilder->getRootNode()
    ->children()
        ->arrayNode('locales')
            ->addDefaultChildrenIfNoneSet()
            ->arrayPrototype()
                ->children()
                    ->scalarNode('code')
                        ->defaultValue('ru')
                    ->end()
                    ->scalarNode('label')
                        ->defaultValue('')
                    ->end()
                ->end()
            ->end()
        ->end()
    ->end()
;

-, .


. .



, , Symfony .


, — . , .


5-configuration.


Symfony .


:


1.部分最小包
2的一部分,我们拿出包中的代码和模板
第3部分集成与主机捆绑的:模板,样式,JS
第4部分接口扩展包
第5部分参数及配置
部分6.测试,该包内microapplication
第7发布周期,安装和更新


All Articles