interitty / component-model
标准Nette/Component Model的扩展,增加了适用于Interitty项目的特定功能。
Requires
- php: ~8.3
- dg/composer-cleaner: ~2.2
- interitty/di: ~1.0
- nette/component-model: ~3.1
Requires (Dev)
- interitty/code-checker: ~1.0
- interitty/phpunit: ~1.0
- nette/application: ~3.2
- nette/bootstrap: ~3.2
- nette/caching: ~3.3
README
通过一些特定于Interitty项目的功能扩展了标准Nette/Component Model。
需求
- PHP >= 8.3
安装
安装 interitty/component-model 的最佳方式是使用 Composer。
composer require interitty/component-model
然后,在 Nette 配置文件 中注册扩展。
# app/config/config.neon
extensions:
componentLocator: Interitty\ComponentModel\Nette\DI\ComponentLocatorExtension
为了方便使用 ComponentLocator
,提供了一个可以在需要它的Presenter中使用的 Interitty\ComponentModel\ComponentLocatorTrait
。
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\ComponentModel\ComponentLocatorTrait;
use Nette\Application\UI\Presenter;
abstract class BasePresenter extends Presenter
{
use ComponentLocatorTrait;
}
配置
Component定位器可以维护和提供任何定义为服务、类或接口的组件工厂。组件将通过用作factories
数组键的名称或从服务、类或接口检索的名称来提供。
componentLocator:
factories:
# componentName: Namespaced\ComponentFactoryInterface
# componentName: Namespaced\ComponentFactory
# componentName: @ComponentFactoryService
# - Namespaced\ComponentFactoryInterface
# - Namespaced\ComponentFactory
# - @ComponentFactoryService
默认情况下还启用了autoRegister
机制,它自动收集所有Interitty\ComponentModel\ComponentFactoryInterface
类型的服务并将它们注册到Component定位器中。
已注册组件的名称来自服务名称、接口或类名称。由于服务的命名空间没有反映,可能会出现组件名称的重复。具有相同派生组件名称的第二个和其他服务将简单地跳过。要使用另一个名称注册它们,只需使用之前的factories
机制一起即可。
componentLocator:
autoRegister: true
services:
- Namespaced\ComponentFactory
testFactory:
implement: Namespaced\ComponentFactoryInterface
默认情况下,在ComponentLocatorHelperTrait
中使用生成的常量作为组件名称,这使得代码更加清晰,并有助于快速导航。然而,有些人仍然更喜欢使用标准的字符串。组件在生成的输出中的使用可以像生成的代码的命名空间或位置一样在设置中受到影响。
# Example of default component locator settings
componentLocator:
autoRegister: true # Can be `false` to disable automatic registration of any `ComponentFactoryInterface` compatible services
constants: true # Can be `false` to use string names
helperNamespace: 'Interitty\\ComponentModel' # Can be `null` to disable generating `ComponentLocatorHelperTrait` helper
tempDir: %tempDir% # Can be any writable directory
用法
通常,组件仍然需要配置或连接到其他组件。这可以通过创建特定的工厂方法完成,就像以前一样。
public function createComponentTestControl(string $name = 'test'): TestControl
{
$control = $this->createLocatedComponent($name);
assert($control instanceof TestControl);
// Any necessary settings
return $control;
}
当使用生成的ComponentLocatorHelperTrait
助手时,如果需要,只需覆盖原始的工厂方法即可。
public function createComponentTestControl(string $name = 'test'): TestControl
{
$control = parent::createComponentTestControl($name);
// Any necessary settings
return $control;
}
助手特性
要使用此Component定位器,只需使用提供所需一切的基本ComponentLocatorTrait
即可。然而,通常在它们的工厂中扩展组件设置并直接访问它们时很有用。为此,扩展在依赖注入容器的构建时生成一个助手ComponentLocatorHelperTrait
,其中包含以下方法。
所以只需在组件需要可用性的Presenter中使用此生成的助手而不是基本的一个。
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\ComponentModel\ComponentLocatorHelperTrait;
use Nette\Application\UI\Presenter;
abstract class BasePresenter extends Presenter
{
use ComponentLocatorHelperTrait;
}
适用于PHPStan的合适应用程序设置
扩展本身可以生成一个辅助特性并将其立即加载,使其无论其名称、命名空间或位置如何都可以用于整个应用程序。然而,PHPStan需要使用自动加载器加载它。为了实现这一点,需要将生成的文件放置在由自动加载器识别的命名空间和适当的位置中。
要完成这个操作,只需在neon配置中设置适当的参数即可。
componentLocator:
helperNamespace: 'App\Controls' # Same namespace as the rest of application
tempDir: %appDir%/Controls