定制 thinktomorrow/vine 二次开发

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

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

thinktomorrow/vine

Composer 安装命令:

composer require thinktomorrow/vine

包简介

Generate a tree from an adjacent database structure

README 文档

README

Thinktomorrow\Vine is a PHP package designed to manage and manipulate adjacent tree-structured models. It provides a powerful interface for building and querying collections of hierarchical data, with methods to traverse, sort, and manipulate node collections in a flexible way.

It's key features are:

  • Tree Traversal: Map and traverse nodes with recursion.
  • Manipulation: Add, remove, or merge nodes.
  • Flattening & Inflating: Convert hierarchical data to flat lists and vice versa.
  • Customizable Queries: Find nodes based on specific attributes.

IMPORTANT An adjacent tree structure is assumed, where each node has an id and parent_id attribute.

Installation

Install the package via composer:

composer require thinktomorrow/vine

Basic Usage

1. Creating a Node Collection

You can create a node collection from an array of nodes.

use Thinktomorrow\Vine\NodeCollection;
use Thinktomorrow\Vine\Node;

// Assuming Node is a model implementing the Node interface
$nodes = [
    new Node(['id' => 1, 'name' => 'Parent']),
    new Node(['id' => 2, 'name' => 'Child', 'parent_id' => 1])
];

$collection = NodeCollection::fromArray($nodes);

2. Traversing Nodes

The package allows you to iterate over nodes recursively.

$collection->eachRecursive(function($node) {
    echo $node->getName();  // Access node attributes
});

3. Flattening a Tree

To get a flat array of all nodes:

$flatNodes = $collection->flatten()->toArray();

4. Inflating a Flattened Collection

You can restore a flattened collection back to its tree structure:

$inflatedCollection = $collection->inflate();

5. Finding Nodes

To find nodes by specific attributes:

$node = $collection->find('id', 1); // Find a node with id = 1

Or find multiple nodes by a set of values:

$nodes = $collection->findMany('id', [1, 2, 3]); // Find nodes with ids 1, 2, and 3

6. Adding and Merging Nodes

You can add or merge nodes into the collection:

$newNode = new Node(['id' => 3, 'name' => 'New Child']);
$collection->add($newNode); // Add a new node

$otherCollection = NodeCollection::fromArray([...]); // Another node collection
$collection->merge($otherCollection); // Merge collections

7. Sorting Nodes

Nodes can be sorted by any attribute:

$sortedCollection = $collection->sort('name'); // Sort by 'name' attribute

8. Removing Nodes

To remove nodes by attribute or condition:

$collection = $collection->remove(function($node) {
    return $node->getId() == 2; // Remove node with id 2
});

Example: Full Usage

use Thinktomorrow\Vine\NodeCollection;
use Thinktomorrow\Vine\Node;

$nodes = [
    new Node(['id' => 1, 'name' => 'Root']),
    new Node(['id' => 2, 'name' => 'Child 1', 'parent_id' => 1]),
    new Node(['id' => 3, 'name' => 'Child 2', 'parent_id' => 1])
];

$collection = NodeCollection::fromArray($nodes);

// Add a node
$collection->add(new Node(['id' => 4, 'name' => 'New Child', 'parent_id' => 2]));

// Flatten, sort, and remove
$flatNodes = $collection->flatten()->sort('name')->remove(function($node) {
    return $node->getName() === 'Child 2';
});

// Output as array
print_r($flatNodes->toArray());

thinktomorrow/vine 适用场景与选型建议

thinktomorrow/vine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42.57k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 05 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 thinktomorrow/vine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 thinktomorrow/vine 我们能提供哪些服务?
定制开发 / 二次开发

基于 thinktomorrow/vine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 42.57k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 13
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-17