tourze / user-tag-bundle
用户标签系统,支持智能规则和SQL规则的标签分配与管理
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/user-tag-bundle
Requires
- doctrine/collections: ^2.3
- doctrine/data-fixtures: ^2.0
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/doctrine-fixtures-bundle: ^4.0
- doctrine/orm: ^3.0
- doctrine/persistence: ^4.1
- easycorp/easyadmin-bundle: ^4
- knplabs/knp-menu: ^3.7
- monolog/monolog: ^3.1
- nesbot/carbon: ^2.72 || ^3
- psr/log: ^3|^2|^1
- symfony/config: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/doctrine-bridge: ^7.3
- symfony/event-dispatcher: ^7.3
- symfony/event-dispatcher-contracts: ^3
- symfony/expression-language: ^7.3
- symfony/form: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/routing: ^7.3
- symfony/security-bundle: ^7.3
- symfony/security-core: ^7.3
- symfony/security-http: ^7.3
- symfony/serializer: ^7.3
- symfony/service-contracts: ^3.6
- symfony/yaml: ^7.3
- tourze/arrayable: 1.*
- tourze/bundle-dependency: 1.*
- tourze/catalog-bundle: 1.0.*
- tourze/doctrine-helper: 1.0.*
- tourze/doctrine-ip-bundle: 1.1.*
- tourze/doctrine-snowflake-bundle: 1.1.*
- tourze/doctrine-timestamp-bundle: 1.1.*
- tourze/doctrine-track-bundle: 1.1.*
- tourze/doctrine-user-bundle: 1.0.*
- tourze/easy-admin-extra-bundle: 1.0.*
- tourze/easy-admin-menu-bundle: 1.0.*
- tourze/enum-extra: 1.0.*
- tourze/json-rpc-core: 1.0.*
- tourze/json-rpc-lock-bundle: 1.0.*
- tourze/json-rpc-log-bundle: 1.0.*
- tourze/json-rpc-paginator-bundle: 1.0.*
- tourze/symfony-aop-lock-bundle: 1.0.*
- tourze/symfony-dependency-service-loader: 1.0.*
- tourze/user-id-bundle: 1.0.*
- tourze/user-service-contracts: 1.1.*
- tourze/user-tag-contracts: 1.0.*
- yiisoft/json: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2025-11-13 18:15:04 UTC
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.