tourze/user-tag-bundle
最新稳定版本:0.0.1
Composer 安装命令:
composer require tourze/user-tag-bundle
包简介
用户标签系统,支持智能规则和SQL规则的标签分配与管理
README 文档
README
A comprehensive user tagging system for Symfony applications, supporting static tags, smart tags, and SQL-based tags with category management.
Table of Contents
Features
- Static Tags: Manual assignment of tags to users
- Smart Tags: Automatic tag assignment based on JSON rules and cron expressions
- SQL Tags: Dynamic tag assignment using custom SQL queries
- Category Management: Hierarchical organization of tags
- Assignment Tracking: Complete audit trail of tag assignments
- EasyAdmin Integration: Ready-to-use admin interface
- JSON-RPC API: RESTful API for tag management
Installation
composer require tourze/user-tag-bundle
Configuration
The bundle works out of the box with minimal configuration. Services are auto-configured through services.yaml.
Quick Start
1. Register the Bundle
// config/bundles.php return [ // ... UserTagBundle\UserTagBundle::class => ['all' => true], ];
2. Configure Database
Run migrations to create the required tables:
bin/console doctrine:migrations:migrate
3. Basic Usage
use UserTagBundle\Entity\Tag; use UserTagBundle\Entity\Category; use UserTagBundle\Enum\TagType; // Create a category $category = new Category(); $category->setName('Customer Status'); $entityManager->persist($category); // Create a static tag $tag = new Tag(); $tag->setName('VIP Customer') ->setType(TagType::StaticTag) ->setCategory($category) ->setDescription('High-value customers'); $entityManager->persist($tag); // Create a smart tag with JSON rules $smartTag = new Tag(); $smartTag->setName('Active User') ->setType(TagType::SmartTag) ->setCategory($category); $smartRule = new SmartRule(); $smartRule->setTag($smartTag) ->setCronStatement('0 0 * * *') // Daily at midnight ->setJsonStatement([ 'conditions' => [ ['field' => 'last_login', 'operator' => '>=', 'value' => '7 days ago'] ] ]); $entityManager->persist($smartTag); $entityManager->persist($smartRule); $entityManager->flush(); // Create a SQL-based tag $sqlTag = new Tag(); $sqlTag->setName('Recent Buyers') ->setType(TagType::SqlTag) ->setCategory($category); $sqlRule = new SqlRule(); $sqlRule->setTag($sqlTag) ->setSqlStatement(" SELECT user_id FROM orders WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY) GROUP BY user_id HAVING COUNT(*) >= 3 "); $entityManager->persist($sqlTag); $entityManager->persist($sqlRule); $entityManager->flush();
4. Using JSON-RPC API
// Get user tags $procedure = new GetUserTagList(); $result = $procedure->execute(); // Assign tag to user $assignProcedure = new AssignTagToBizUser(); $assignProcedure->setParameters([ 'tagId' => $tag->getId(), 'bizUserId' => $userId ]); $assignProcedure->execute();
Advanced Usage
Custom Tag Types
You can extend the system with custom tag types by implementing the TagInterface:
use UserTagBundle\Enum\TagType; // Create a custom tag type class CustomTagType extends TagType { public const CustomType = 'custom'; }
Event Listeners
The bundle dispatches events that you can listen to:
use UserTagBundle\Event\BeforeAddTagEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class TagEventSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ BeforeAddTagEvent::class => 'onBeforeAddTag', ]; } public function onBeforeAddTag(BeforeAddTagEvent $event): void { // Custom logic before tag assignment $user = $event->getUser(); $tag = $event->getTag(); // Example: Validate business rules if (!$this->validateTagAssignment($user, $tag)) { throw new \InvalidArgumentException('Tag assignment not allowed'); } } }
Smart Tag Rules
Configure complex JSON rules for smart tags:
$smartRule->setJsonStatement([ 'conditions' => [ [ 'field' => 'user.orders.count', 'operator' => '>=', 'value' => 10 ], [ 'field' => 'user.totalSpent', 'operator' => '>', 'value' => 1000 ] ], 'logic' => 'AND' // or 'OR' ]);
SQL Tag Examples
Dynamic tags using SQL queries:
$sqlRule->setSqlStatement(" SELECT user_id FROM orders WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY) GROUP BY user_id HAVING COUNT(*) >= 3 ");
API Reference
JSON-RPC Procedures
Tag Management
CreateSingleUserTag- Create a new tagUpdateSingleUserTag- Update existing tagDeleteSingleUserTag- Delete a tagGetUserTagList- List all tags
Category Management
CreateSingleUserTagCategory- Create a new categoryUpdateSingleUserTagCategory- Update existing categoryDeleteSingleUserTagCategory- Delete a categoryGetUserTagCategories- List all categories
Tag Assignment
AssignTagToBizUser- Assign tag to userUnassignTagToBizUser- Remove tag from userGetAssignTagsByBizUserId- Get user's assigned tagsAdminGetAssignLogsByTag- Get assignment history
Entities
Tag- Main tag entity with support for different typesCategory- Hierarchical category systemSmartRule- JSON-based rules for smart tagsSqlRule- SQL-based rules for dynamic tagsAssignLog- Assignment tracking and audit trail
Requirements
- PHP 8.1+
- Symfony 6.4+
- Doctrine ORM 3.0+
- Doctrine DBAL 3.0+
Required Packages
symfony/framework-bundledoctrine/ormdoctrine/doctrine-bundletourze/doctrine-timestamp-bundletourze/doctrine-snowflake-bundletourze/arrayable
License
This bundle is released under the MIT License. See the LICENSE file for details.
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-13