定制 drupal/plugin_topsort 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

drupal/plugin_topsort

最新稳定版本:1.0.0

Composer 安装命令:

composer require drupal/plugin_topsort

包简介

A mechanism for ordering plugin definitions using a directed acyclic graph.

README 文档

README

A mechanism for ordering plugin definitions using a directed acyclic graph.

It is intended for developers who are writing custom plugins, where the plugin definitions need to be "in order". Typically, these are plugins which transform data in some way, where the order of the transformations matters.

Usage

1. Add before/after properties to plugin definitions

A directed acyclic graph can be defined by way of before and after properties in the plugin definition:

#[\Attribute(\Attribute::TARGET_CLASS)]
final class MyPlugin extends Plugin {

  public function __construct(
    string $id,
    public readonly array $before = [],
    public readonly array $after = [],
    ?string $deriver = NULL,
  ) {
    parent::__construct($id, $deriver);
  }

}

How to interpret these properties:

  • A plugin's before property can be thought of as "this plugin must run before the listed plugins".
  • A plugin's after property can be thought of as "this plugin must run after the listed plugins".

For example, the order of the following plugins may be enforced like this:

#[MyPlugin('first', before: ['second'])]
#[MyPlugin('second')]
#[MyPlugin('third', after: ['second'])]

You do not need to specify both before and after constraints to form a dependency - either will suffice.

A LogicException is thrown if the constraints imply a circular dependency.

2. Add the trait to the plugin manager

  1. Use the PluginManagerTopologicalSortTrait in your plugin manager:
use \Drupal\plugin_top_sort\PluginManagerTopologicalSortTrait;

class MyPluginManager extends DefaultPluginManager {

  use PluginManagerTopologicalSortTrait;

  ...

}

The trait provides a findDefinitions() function that looks like this:

public function findDefinitions(): array {
  $definitions = parent::findDefinitions();
  // sort definitions
  return $definitions;
}

Sorting happens after discovery, and after any alter hook has been called.

3. Plugin manager behavior.

The array returned by the plugin manager's getDefinitions() function will be sorted in a way that respects the directed acyclic graph.

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2026-05-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固