andersundsehr / unleash
TYPO3 扩展,用于集成 getunleash.io 特性开关服务。
Requires
- php: ~8.2.0 || ~8.3.0
- typo3/cms-core: ^11.5 || ^12.4 || ^13.0
- typo3/cms-lowlevel: ^11.5 || ^12.4 || ^13.0
- unleash/client: ^2.5
Requires (Dev)
- pluswerk/grumphp-config: ^7.0.4
- saschaegerer/phpstan-typo3: ^1.10.1
- ssch/typo3-rector: ^2.6.4
README
TYPO3 扩展,用于集成 Unleash 特性开关服务。
安装
composer require andersundsehr/unleash
在扩展设置中设置配置
配置
需要 appUrl
以及在大多数情况下 authorization
(如果您使用托管 unleash 服务)
使用
TypoScript 条件
您可以使用自定义的 TypoScript 条件 unleash
来检查一个特性是否启用。
page.19 = TEXT page.19.value = Feature 0 is disabled<br> [unleash('feature0', false)] page.19.value = Feature 0 is enabled<br> [end] page.20 = TEXT page.20.value = Feature 1 is disabled<br> [unleash('feature1')] page.20.value = Feature 1 is enabled<br> [end] page.21 = TEXT page.21.value = Feature 2 is disabled<br> [unleash('feature2', true)] page.21.value = Feature 2 is enabled<br> [end]
如果您想检查 A/B/n 标志,您可以使用 unleashVariant
条件。
page.22 = TEXT [unleashVariant('feature3') === 'A'] page.22.value = Feature 3 is set to A enabled<br> [end] [unleashVariant('feature3') === 'B'] page.22.value = Feature 3 is set to B enabled<br> [end] [!unleashVariant('feature3')] page.22.value = Feature 3 is disabled<br> [end]
Fluid 视图助手
如果您想在您的 Fluid 模板中检查一个特性是否启用,您可以使用 unleash:isEnabled
视图助手。
{namespace unleash=Andersundsehr\Unleash\ViewHelpers} <unleash:isEnabled feature="my-feature" default="false"> <f:then> <p>Feature is enabled</p> </f:then> <f:else> <p>Feature is disabled</p> </f:else> </unleash:isEnabled> {unleash:isEnabled(feature: 'my-feature', default: false, then: 'Feature is enabled', else: 'Feature is disabled')}
如果您想检查 A/B/n 标志,您可以使用 unleash:getVariant
视图助手。
{namespace unleash=Andersundsehr\Unleash\ViewHelpers} <f:switch expression="{unleash:getVariant(feature: 'my-feature')}"> <f:case value="A"><f:render section="A"/></f:case> <f:case value="B"><f:render section="B"/></f:case> <f:defaultCase>If is disabled</f:defaultCase> </f:switch>
PHP
您可以将 Unleash
服务注入到您的类中,并像这样使用它
配置和上下文将已经为您设置好。
有关更详细的用法信息,请参阅 Unleash PHP SDK 文档
use Unleash\Client\Unleash; class Controller { public function __construct(private Unleash $unleash) {} public function index() { if ($this->unleash->isEnabled('my-feature')) { // do something } } }
或者您可以使用 GeneralUtility 获取 Unleash 实例
use TYPO3\CMS\Core\Utility\GeneralUtility; use Unleash\Client\Unleash; GeneralUtility::makeInstance(Unleash::class)->isEnabled('my-feature');
自定义上下文
使用此扩展,您可以将特性开关约束到特定用户、管理员或登录用户。
后端和前端
backendUser.isLoggedIn
backendUser.id
backendUser.username
backendUser.isAdmin
frontendUser.isLoggedIn
frontendUser.id
frontendUser.username
frontendUser.isAdmin
通过事件扩展
您可以使用以下事件来扩展扩展的功能。
UnleashBuilderBeforeBuildEvent
在构建 UnleashBuilder 之前分发的事件(->build()
)。
use Andersundsehr\Unleash\Event\UnleashBuilderBeforeBuildEvent; class UnleashBuilderBeforeBuildEventListener { public function __invoke(UnleashBuilderBeforeBuildEvent $event) { $event->builder = $event->builder ->withAppName('my-custom-app-name'); } }
UnleashCustomContextEvent
当创建 Unleash 上下文时分发此事件。
如果只想覆盖或添加 customContext 数据,请使用此功能
如果您想更改其他内容,请使用 UnleashContextCreatedEvent
use Andersundsehr\Unleash\Event\UnleashCustomContextEvent; class UnleashCustomContextEventListener { public function __invoke(UnleashCustomContextEvent $event) { $event->customContext['fair'] = 'value'; } }
UnleashContextCreatedEvent
在创建带有所有默认值的 UnleashContext 后立即分发的事件。
将会被多次调用,每次调用 ->isEnabled
或 ->getVariant
。
use Andersundsehr\Unleash\Event\UnleashContextCreatedEvent; class UnleashContextCreatedEventListener { public function __invoke(UnleashContextCreatedEvent $event) { $event->context->setHostname('my-custom-hostname'); } }
由 anders und sehr GmbH 制作
如果某些事情没有按预期工作 😮
或者您喜欢这个扩展 🥰 让我们得知。