mrsuh/tree-printer

1.0.0 2023-03-13 18:18 UTC

This package is not auto-updated.

Last update: 2024-11-20 01:47:01 UTC


README

Installation

composer require mrsuh/tree-printer

Usage

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Mrsuh\Tree\NodeInterface;
use Mrsuh\Tree\Printer;

class Node implements NodeInterface
{
    private string $name;
    private array  $children;

    public function __construct(string $name, array $children = [])
    {
        $this->name     = $name;
        $this->children = $children;
    }

    public function getChildren(): array
    {
        return $this->children;
    }

    public function __toString(): string
    {
        return $this->name;
    }
}

$tree = new Node('/etc', [
    new Node('adduser.conf'),
    new Node('apt', [
        new Node('apt.conf.d', [
            new Node('01autoremove'),
            new Node('70debconf')
        ]),
        new Node('auth.conf.d'),
        new Node('preferences.d'),
        new Node('trusted.gpg.d', [
            new Node('debian-archive-bullseye-automatic.gpg'),
            new Node('debian-archive-bullseye-security-automatic.gpg'),
            new Node('debian-archive-bullseye-stable.gpg'),
        ]),
    ]),
    new Node('bash.bashrc'),
    new Node('cron.d', [
        new Node('e2scrub_all'),
    ]),
    new Node('cron.daily', [
        new Node('apt-compat'),
        new Node('dpkg'),
    ])
]);

$printer = new Printer();
$printer->print($ast);
.
├── /etc
    ├── adduser.conf
    ├── apt
    │   ├── apt.conf.d
    │   │   ├── 01autoremove
    │   │   └── 70debconf
    │   ├── auth.conf.d
    │   ├── preferences.d
    │   └── trusted.gpg.d
    │       ├── debian-archive-bullseye-automatic.gpg
    │       ├── debian-archive-bullseye-security-automatic.gpg
    │       └── debian-archive-bullseye-stable.gpg
    ├── bash.bashrc
    ├── cron.d
    │   └── e2scrub_all
    └── cron.daily
        ├── apt-compat
        └── dpkg