adachsoft / file-tree-render
A flexible PHP library for rendering file system trees in various formats (Markdown, JSON, XML, HTML) with customizable metadata and filtering.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Forks: 0
pkg:composer/adachsoft/file-tree-render
Requires
- adachsoft/console-io: ^0.1.0
- adachsoft/filesystem: ^1.3
- adachsoft/normalized-safe-path: ^0.1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.91
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.4
This package is not auto-updated.
Last update: 2026-01-21 07:29:32 UTC
README
A flexible PHP library for rendering file system trees in various formats (Markdown, JSON, XML, HTML). It supports filtering, metadata enrichment (file size, permissions), and includes a CLI tool.
Features
- Multiple Output Formats: Markdown (ASCII tree), JSON, XML, HTML.
- Filtering: Whitelist file extensions and exclude specific directories.
- Metadata: Optionally display file sizes and permissions.
- Security: Uses
adachsoft/normalized-safe-pathto prevent path traversal. - CLI Tool: Includes a
render-treebinary for command-line usage. - Customizable: Fluent Builder pattern for easy configuration.
Installation
composer require adachsoft/file-tree-render
Usage
Basic Usage
use AdachSoft\FileTreeRender\FileTreeRenderBuilder;
$renderer = (new FileTreeRenderBuilder())
->withBasePath(__DIR__) // Set the root directory to scan
->build();
// Render the tree relative to the base path
echo $renderer->render('.');
Advanced Configuration
use AdachSoft\FileTreeRender\FileTreeRenderBuilder;
use AdachSoft\FileTreeRender\Renderer\JsonTreeRenderer;
$renderer = (new FileTreeRenderBuilder())
->withBasePath('/var/www/project')
// Only show these extensions
->withAllowedExtensions(['php', 'md', 'json'])
// Exclude these directories from recursion
->withExcludedDirectories(['vendor', 'node_modules', '.git'])
// Optionally show excluded items (without recursing into them)
->withShowExcludedDirectories(true)
->withShowExcludedFiles(true)
// Enable metadata addons
->withAddons(['filesize', 'permissions'])
// Use a specific renderer (default is Markdown)
->withRenderer(new JsonTreeRenderer())
->build();
echo $renderer->render('src');
Available Renderers
MarkdownTreeRenderer(Default): Generates a text-based tree suitable for Markdown files or console output.JsonTreeRenderer: Generates a JSON representation of the tree.XmlTreeRenderer: Generates an XML representation.HtmlTreeRenderer: Generates an HTML unordered list (<ul>).
CLI Tool
The package comes with a binary bin/render-tree that can be used directly from the command line.
# Basic usage (renders current directory in Markdown)
./bin/render-tree
# Render a specific subdirectory
./bin/render-tree src
# Output as JSON
./bin/render-tree src --format=json
# Disable metadata
./bin/render-tree --no-metadata
# Show excluded directories (e.g. vendor) but don't recurse into them
./bin/render-tree --show-excluded-dirs
Options:
path: Path relative to project root (default:.)format: One of:markdown,md,json,xml,html(default:markdown)--metadata=default: Enable default metadata providers (size, permissions)--metadata=none/--no-metadata: Disable metadata--show-excluded-dirs: Show excluded directories (without recursion)--show-excluded-files: Show excluded files--dump-metadata: Print a debug tree with raw node metadata
Testing
composer test
License
MIT