Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Treewalker Laravel Package

lukascivil/treewalker

Laravel package for walking, searching, and manipulating tree structures with a simple API. Traverse nodes, run callbacks, and query descendants/ancestors efficiently—useful for nested categories, menus, and other hierarchical data.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer:

composer require lukascivil/treewalker

Then, import the core classes into your workflow. The main entry point is the TreeWalker class—create an instance with your tree data and configure how children are accessed (e.g., using ->childrenAccessor('subitems') if your data uses a custom key like subitems instead of children).
Your first use case is likely transforming a nested menu array into a flat list of breadcrumb strings:

use Lukascivil\TreeWalker\TreeWalker;

$walker = new TreeWalker($menuTree);
$breadcrumbs = $walker->walk(function ($node) {
    return $node['title'];
})->toArray();

Check the examples/ directory (if present in the repo) or read the PHPDoc for TreeWalker::walk(), map(), filter(), and reduce().

Implementation Patterns

  • Menu flattening or restructuring: Use walk() with a callback to collect paths or map() to inject computed fields (e.g., depth, isActive) while preserving tree shape.
  • Dynamic configuration processing: Parse nested config (e.g., CI pipelines, feature flags) by walking with filter() to exclude inactive sections and map() to normalize keys.
  • AST or schema transformations: Apply targeted edits to JSON-like structures (e.g., API docs, JSON schemas) by combining walk() with node-type checks:
    $walker->walk(function ($node) {
        if ($node['type'] === 'array') {
            $node['items'] ??= [];
        }
        return $node;
    });
    
  • Aggregation patterns: Use reduce() to compute summary stats (e.g., total leaf count, max depth, cumulative price) in a single pass.
  • Middleware-style traversal: Chain filter(), map(), and walk() for declarative pipelines—each operation returns a new TreeWalker for chaining.

Gotchas and Tips

  • Children accessor must be reliable: If your data mixes associative and indexed arrays or uses magic accessors (e.g., __get), define a robust childrenAccessor callback instead of relying on defaults.
  • No root node filtering by default: walk() processes the root node—wrap your callback in if ($depth > 0) if you want to skip it.
  • Return values must be scalar or array: The walk() callback should return transformed node data (array/object); non-serializable returns may cause unexpected behavior in downstream operations like toArray().
  • Shallow vs. deep clones: map() mutates references unless you explicitly clone nodes (return (array) $node; or clone $node). Use clone to avoid side effects.
  • Depth-first only: This package doesn’t support breadth-first traversal—implement custom logic if needed.
  • Lack of recent updates: Since the last release was in 2019, verify compatibility with modern PHP versions (tested up to PHP 7.4 likely) and consider vendoring or forking for critical projects.
  • Extensibility via static methods: The package supports static helpers like TreeWalker::flatten($tree)—use these for quick conversions without instantiation.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests