blast-project/utils-bundle

BlastCoreBundle的各种实用工具

安装量: 5,386

依赖项: 8

建议者: 0

安全: 0

星标: 1

关注者: 4

分叉: 0

开放问题: 0

类型:symfony-bundle

0.6.4 2017-11-03 14:59 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:04:10 UTC


README

Build Status Coverage Status License

Latest Stable Version Latest Unstable Version Total Downloads

功能

Blast选择

待编写的文档

Blast钩子

本捆绑包引入了一个基本的钩子管理功能。

您可以在视图中定义任何想要的钩子。

在视图中声明钩子目标位置

{# myTemplate.html.twig #}

<div>
    <h1>Here my custom hook</h1>
    {{ blast_hook('my.custom.hook', {'someParameters': myVar}) }}
</div>

钩子可以不使用任何参数进行声明。如果这样,"钩子块"在"handleParameters"方法参数中不会定义任何参数(变量$hookParameters将是一个空数组)。

声明您的钩子类

此类将通过设置视图参数来管理钩子内容的渲染(充当控制器)

<?php

namespace MyBundle\Hook\MyCustomHook;

use Blast\UtilsBundle\Hook\AbstractHook;

class MyCustomHookExample extends AbstractHook
{
    protected $hookName = 'my.custom.hook';
    protected $template = 'MyBundle:Hook:my_custom_hook_example.html.twig';

    public function handleParameters($hookParameters)
    {
        $this->templateParameters = [
            'someViewParameter' => 'a value that will be passed to the twig view'
        ];
    }
}

注意:您可以在属性AbstractHook::hookName中获取当前钩子名称(在服务定义中配置),以及在AbstractHook::template中配置的模板。

将钩子类注册为服务

    my_bundle.hook.my_custom_hook_example:
        class: MyBundle\Hook\MyCustomHook\MyCustomHookExample
        tags:
            - { name: blast.hook, hook: my.custom.hook, template: MyBundle:Hook:my_custom_hook_example.html.twig }

钩子配置设置在服务标签中

  • name:服务标签名称(必须是blast.hook
  • hook:将要渲染的"块"的目标钩子
  • template:"块"的twig模板

请不要忘记使用标签blast.hook来注册您的服务作为钩子

创建您的钩子模板

{# MyBundle:Hook:my_custom_hook_example.html.twig #}

<p>
    Here's my first custom hook,  with a view var : {{ someViewParameter }} !
</p>

然后,您应该有这个渲染内容

<div>
    <h1>Here my custom hook</h1>
    <p>
        Here's my first custom hook,  with a view var : a value that will be passed to the twig view !
    </p>
</div>

Blast自定义过滤器

在config.yml中启用此功能

# app/config/config.yml
blast_utils:
    features:
        customFilters:
            enabled: true

可选地,您可以定义自己的customFilter实体,如下设置(不要忘记设置相关的存储库以覆盖createNewCustomFilter方法)

# app/config/config.yml
blast_utils:
    features:
        customFilters:
            enabled: true
            class: MyBundle\Entity\MyCustomFilter

您只需在应用程序config.yml中设置您的User类实体(有关更多信息,请参阅https://symfony.ac.cn/doc/current/doctrine/resolve_target_entity.html

# app/config/config.yml
doctrine:
    # ...
    orm:
        # ...
        resolve_target_entities:
            Blast\CoreBundle\Model\UserInterface: MyBundle\Entity\MyUser

如果您正在使用Sylius,则设置doctrine.orm resolve_target_entities密钥将不起作用,因为Sylius已经使用了此系统。您可以在SyliusResource配置中声明您的Interface / Entity替换

# app/config/config.yml
sylius_resource:
    resources:
        blast.utils: # this is an arbitrary key
            classes:
                model: MyBundle\Entity\MyUser
                interface: Blast\CoreBundle\Model\UserInterface

Blast用户界面

为了设置与utils实体的User映射,使用Interface进行映射。

有2种方式来配置将要替换UserInterface的实际类

使用Sylius

通过资源声明将替换模型接口的类

sylius_resource:
    resources:
        blast.utils:
            classes:
                model: MyBundle\Entity\MyRealUser
                interface: Blast\CoreBundle\Model\UserInterface

使用Syfony的Doctrine目标实体解析器

doctrine:
    # ...
    orm:
        #...
        resolve_target_entities:
            Blast\CoreBundle\Model\UserInterface: MyBundle\Entity\MyRealUser