tourze / text-manage-bundle
文本格式化管理
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.1
- league/commonmark: ^2.6.1
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- twig/twig: ^3.13|^4.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-04-26 08:24:21 UTC
README
TextManageBundle 是一个 Symfony Bundle,提供文本格式化和处理的功能。
功能
- 纯文本处理
- Markdown 格式化
- Twig 模板解析
安装
composer require tourze/text-manage-bundle
在 config/bundles.php
中注册 Bundle:
return [ // ... Tourze\TextManageBundle\TextManageBundle::class => ['all' => true], ];
使用方法
基本用法
use Tourze\TextManageBundle\Service\TextFormatter; class MyService { public function __construct( private readonly TextFormatter $textFormatter ) { } public function process(): string { // 基本文本处理 return $this->textFormatter->formatText('Hello World'); } }
Twig 语法处理
// 传入参数解析 Twig 语法 $params = ['name' => 'John']; $text = 'Hello {{ name }}!'; $result = $textFormatter->formatText($text, $params); // 返回 "Hello John!" // 复杂 Twig 语法 $params = ['items' => ['apple', 'banana', 'orange']]; $text = '{% for item in items %}{{ item }}{% if not loop.last %}, {% endif %}{% endfor %}'; $result = $textFormatter->formatText($text, $params); // 返回 "apple, banana, orange"
Markdown 处理
// Markdown 格式化 $text = '# Title\n\n**Bold text**'; $result = $textFormatter->formatText($text); // 返回纯文本,移除 HTML 标签
装饰器模式
本包使用装饰器模式实现了文本格式化处理的链式调用:
PlainFormatter
- 基本文本处理MarkdownFormatter
- Markdown 格式化(装饰 TextFormatter)TwigFormatter
- Twig 模板解析(装饰 TextFormatter)
单元测试
./vendor/bin/phpunit packages/text-manage-bundle/tests