grand-media/compiler-extension

v2.1.1 2017-02-06 10:27 UTC

This package is auto-updated.

Last update: 2024-09-29 04:36:57 UTC


README

目前我没有时间、精力和金钱来维护这个项目。但如果您依赖这个项目,并且希望成为赞助者或进一步开发它,请毫不犹豫地与我联系。否则,我无法保证这个仓库的美好未来... :)

增强型 Nette 框架 CompilerExtension

Build Status

如果您有一个结构更复杂的项目,包含许多捆绑包(DIC 扩展),那么您可能需要设置很多东西,这可能相当困难。但这个扩展不是这样的。您所需要做的只是使用 Adeira\ConfigurableExtensionsExtension 代替默认的 ExtensionsExtension,如下所示(可能是在 bootstrap.php 中)

$configurator->defaultExtensions['extensions'] = \Adeira\ConfigurableExtensionsExtension::class;

这个新的扩展将负责您的捆绑包中的配置文件。接下来,如果您想为扩展使用自定义配置,只需使用 provideConfig 方法

<?php

namespace App\Articles\DI;

class ArticlesExtension extends \Adeira\CompilerExtension
{

  public function provideConfig()
  {
    return __DIR__ . '/config.neon';
  }

  public function beforeCompile()
  {
    $this->setMapping(['Articles' => 'App\Articles\*Module\Presenters\*Presenter']);
  }

}

您不需要扩展 Adeira\CompilerExtension,但有一个有用的助手 setMapping()(它只是设置自定义展示者映射)。但是 provideConfig 方法也可以与 Nette\DI\CompilerExtension 的后代一起工作。为什么这很有趣? 想象一下您在应用程序中有这样的配置(通过 Nette\DI\Compiler::addConfig 在引导中添加)

parameters:
  key1: value1
  key2: value2

services:
  - DefaultService
  named: Tests\Service

extensions:
  ext2: CustomExtension2

ext2:
  ext_key1: ext_value1
  ext_key2: ext_value2

application:
  mapping:
    *: *

现在您将使用 provideConfig 方法添加您的 DIC 扩展中的另一个配置

parameters:
  key2: overridden
  key3: value3

services:
  - Tests\TestService
  named: Service2

ext2:
  ext_key2: overridden
  ext_key3: ext_value3

latte:
  macros:
    - App\Grid\Latte\Macros

结果是什么?现在有三个全局参数

parameters:
  key1: value1
  key2: overridden
  key3: value3

如您所见,您的自定义 DIC 扩展具有优先级。扩展参数(ext2)的行为完全相同。那么服务呢?正如您所期望的,将会有三个服务

- DefaultService
named: Service2
- Tests\TestService

接下来是最有趣的部分。如果您有大量的扩展,使用自定义配置文件是个好主意(它简单易懂)。但从 neon 文件中获取扩展配置可能很难。在 Nette\DI\CompilerExtension 后代类中,您可以通过简单地调用 $this->getConfig() 来获取与扩展相关的配置,但在 neon 中没有等效的方法。这个扩展为此情况添加了特殊的语法。从之前的例子中,有三个与 ext2 扩展相关的选项

ext2:
  ext_key1: ext_value1
  ext_key2: overridden
  ext_key3: ext_value3

要获取第二个参数到服务中,请使用此方法

services:
  - Tests\TestService(%%ext_key2%%)

请记住,这仅在使用通过 provideConfig 方法添加的自定义配置时才可行。它不会在引导文件中添加的配置(通过 Nette\DI\Compiler::addConfig)中工作。这是因为只有在扩展的情况下,才可以从正确的扩展部分(在这种情况下为 ext2.ext_key2)获取键。

实验性功能

这些功能现在默认不启用(但未来可能默认启用)。要启用实验性功能,现在您必须以不同的方式注册此扩展

 $configurator->defaultExtensions['extensions'] = [\Adeira\ConfigurableExtensionsExtension::class, [TRUE]]; // Become superhero!

目前有一个名为 GroupedNeonAdapter 的功能。它允许您使用分组语法在 NEON 中编写服务定义。之前

graphql:
  types:
  - Adeira\Connector\Devices\Infrastructure\Delivery\API\GraphQL\Type\WeatherStationRecordType
  - Adeira\Connector\Devices\Infrastructure\Delivery\API\GraphQL\Type\WeatherStationsConnectionType
  - Adeira\Connector\Devices\Infrastructure\Delivery\API\GraphQL\Type\WeatherStationsEdgeType
  - Adeira\Connector\Devices\Infrastructure\Delivery\API\GraphQL\Type\WeatherStationType

之后

graphql:
  types:
    - Adeira\Connector\Devices\Infrastructure\Delivery\API\GraphQL\Type\( # namespace must end with backslash
      WeatherStationRecordType
      WeatherStationsConnectionType
      WeatherStationsEdgeType
      WeatherStationType
    )

此功能是可选的,并且仅在通过 provideConfig 方法提供的 NEON 文件中工作。所有类都必须匿名注册。如果不可能,请勿使用此功能。