SAREhub/PHP_Commons

View on GitHub
examples/run-example

Summary

Maintainability
Test Coverage
#!/usr/bin/env php
<?php

require __DIR__ . '/ExampleCommons.php';

class ExampleListHelper
{

    public function printList($dir)
    {
        echo "EXAMPLES:\n";
        foreach ($this->getDirList($dir) as $path => $name) {
            $this->printPackage($path, $name);
        }
    }

    private function printPackage($path, $name)
    {
        echo "$name - " . $this->loadDescription($path) . "\n";
        foreach ($this->getDirList($path) as $path => $name) {
            $this->printTopic($path, $name);
        }
    }

    private function printTopic($path, $name)
    {
        echo " $name - " . $this->loadDescription($path) . "\n";
        foreach ($this->getDirList($path) as $path => $name) {
            $this->printExample($path, $name);
        }
    }

    private function printExample($path, $name)
    {
        echo "  $name - " . $this->loadDescription($path) . "\n";
    }

    private function loadDescription($path)
    {
        $descriptionPath = $path . '/description';
        return file_exists($descriptionPath) ? file_get_contents($descriptionPath) : 'empty';
    }

    private function getDirList($dir)
    {
        $it = new DirectoryIterator($dir);
        $list = [];
        /** @var DirectoryIterator $it */
        foreach ($it as $path) {
            if (!$path->isDot() && $path->isDir()) {
                $list[$path->getPathname()] = $path->getBasename();
            }
        }

        return $list;
    }
}

if (empty($argv[1])) {
    echo "USAGE: php run-example <examplePath>\n\n";
    $examplesList = new ExampleListHelper();
    $examplesList->printList(__DIR__);
    return;
}

$examplePath = __DIR__ . '/' . strtolower($argv[1]) . '/example.php';

if (file_exists($examplePath)) {
    require $examplePath;
} else {
    echo "example doesn't exists";
}