interitty/component-model
Composer 安装命令:
composer require interitty/component-model
包简介
Extension of the standard Nette/Component Model by some other features that are specific for use in the Interitty projects.
关键字:
README 文档
README
Extension of the standard Nette/Component Model by some other features that are specific for use in the Interitty projects.
Requirements
- PHP >= 8.5
Installation
The best way to install interitty/component-model is using Composer:
composer require interitty/component-model
Then register the extension in the Nette config file:
# app/config/config.neon
extensions:
componentLocator: Interitty\ComponentModel\Nette\DI\ComponentLocatorExtension
In order to work comfortably with the ComponentLocator, there is a Interitty\ComponentModel\ComponentLocatorTrait
which can be used in the Presenter where it should be available.
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\ComponentModel\ComponentLocatorTrait;
use Nette\Application\UI\Presenter;
abstract class BasePresenter extends Presenter
{
use ComponentLocatorTrait;
}
Configuration
The Component locator can maintain and provides any component factory defined as a service, class, or interface.
The component will be available by the name used as a key of the factories array or by the name retrieved from
the service, class, or interface.
componentLocator:
factories:
# componentName: Namespaced\ComponentFactoryInterface
# componentName: Namespaced\ComponentFactory
# componentName: @ComponentFactoryService
# - Namespaced\ComponentFactoryInterface
# - Namespaced\ComponentFactory
# - @ComponentFactoryService
There is also by default enabled mechanism autoRegister that automatically collects all services of
Interitty\ComponentModel\ComponentFactoryInterface type and register them into the Component locator.
The name of the registered component is derived from the service name, the interface, or a class name. It can fall into
the duplicity of the component name because the namespace of the services is not reflected. Second and other services
with the same derived component name are simply skipped. To register them with another name simply use the previous
factories mechanism together.
componentLocator:
autoRegister: true
services:
- Namespaced\ComponentFactory
testFactory:
implement: Namespaced\ComponentFactoryInterface
By default, the generated constants are used for component names in the ComponentLocatorHelperTrait, which makes
the code even clearer and helps in faster navigation. However, some people still prefer to use the standard string.
The use of components in the generated output can be influenced in the settings just like the namespace or location
of the generated code.
# 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
Usage
Usually, the component still needs to be configured or connected to others. This can be done in the same way as before, by creating a specific factory method.
public function createComponentTestControl(string $name = 'test'): TestControl
{
$control = $this->createLocatedComponent($name);
assert($control instanceof TestControl);
// Any necessary settings
return $control;
}
When the generated ComponentLocatorHelperTrait helper is used, just overload the original factory method if necessary:
public function createComponentTestControl(string $name = 'test'): TestControl
{
$control = parent::createComponentTestControl($name);
// Any necessary settings
return $control;
}
Helper trait
To work with this Component locator, just use the the basic ComponentLocatorTrait which provides everything you need.
However, it's usually handy to be able to extend the component settings in their factories and have getters to access
them directly. For this purpose, the extension also has the ability to generate a helper ComponentLocatorHelperTrait
at build time of the Dependency Injection Container, which contains the following methods.
So just use this generated helper in the Presenter where the components are to be available instead of the basic one.
<?php
declare(strict_types=1);
namespace App\Presenters;
use Interitty\ComponentModel\ComponentLocatorHelperTrait;
use Nette\Application\UI\Presenter;
abstract class BasePresenter extends Presenter
{
use ComponentLocatorHelperTrait;
}
Suitable application settings for PHPStan
The extension itself can generate an auxiliary Trait and immediately load it to make it available to the rest of the application regardless of its name, namespace, or location. PHPStan, however, requires that it be loaded using an autoloader. To achieve this, you need to have the file generated to a namespace and an appropriate location that is identified by the autoloader.
To do this, just set the appropriate parameters in the neon configuration.
componentLocator:
helperNamespace: 'App\Controls' # Same namespace as the rest of application
tempDir: %appDir%/Controls
interitty/component-model 适用场景与选型建议
interitty/component-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 992 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 02 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「components」 「nette」 「interitty」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 interitty/component-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 interitty/component-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 interitty/component-model 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nette Framework adapter for I18n package
Web Font Loader gives you added control when using linked fonts via @font-face.
Build forms from schema
Asynchronous MQTT client built on React
Provides some additional features into the great Nette\DI container.
Form with datetime support and Bootstrap 4 form renderer
统计信息
- 总下载量: 992
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-05