olderProject.php.bak

<?php

namespace Taeluf\ProjectViewer;

class Project {

    protected $dir;
    public $name;
    protected $branchList;
    protected $docsDir;
    protected $defaultBranch;
    protected $branch;

    public function __construct($projectDir, $options=[]){

        $dir = realpath($projectDir);
        $this->dir = $dir;
        $this->name = basename($dir);
        if (!is_dir($dir))return;
        $this->branchList = array_filter(scandir($dir),
            function($file) use ($dir){
                if ($file=='.'||$file=='..'||!is_dir($dir.'/'.$file))return;
                return $file;
            }
        );

        $this->defaultBranch = $options['default_branch'];
        $this->branch = $this->defaultBranch;

    }

    public function path($url){
        $file = str_replace(['///','//'], '/', $this->docsDir().'/'.$url);
        return $file;
    }

    public function docsDir(){
        $dir = str_replace('//', '/', $this->dir.'/'.$this->branch.'/0-docs/' );
        return $dir;
    }

    public function filesFor($url=''){
        $dir = str_replace(['///','//'], '/', $this->docsDir().'/'.$url.'/');
        $files = scandir($dir);
        $indexFile = "README.md";
        $list = [];
        foreach ($files as $f){
            if ($f=='.'||$f=='..'||$f==$indexFile)continue;
            $list[] = $f;
        }
        return $list;
    }
    public function fileFor($url=''){
        $file = str_replace(['///','//'], '/', $this->docsDir().'/'.$url);
        if (is_dir($file))$file .='/';
        return $file;
    }
}