Project.php

<?php

namespace Taeluf\ProjectViewer;

class Project {

    protected $vendorUrl;
    protected $dir;
    protected $url;

    protected $conf;

    protected $name;
    protected $description;
    protected $branchList;
    protected $defaultBranch;


    public function __construct($dir, $vendorUrl){
        $this->dir = $dir;
        $this->vendorUrl = $vendorUrl;
        $confPath = $dir;
        if (substr($confPath,-1)=='/')$confPath = substr($dir,0,-1);
        $confPath .= '.json';
        $conf = [];
        if (file_exists($confPath)){
            $json = file_get_contents($confPath);
            $conf = json_decode($json,true);
        }
        $this->conf = $conf;
        $this->config = new Config($dir);
        $this->name = $conf->name ?? basename($dir);
        $this->description = $conf->description ?? 'No description available...';
        $this->url = $this->normalizePath($this->vendorUrl.'/'.$this->name);
        $this->branchList = $conf->branches ?? null;
        if ($this->branchList==null){
            $files = scandir($this->dir);
            $branches = array_filter($files,function($f){return !($f=='.'||$f=='..');});
            $this->branchList = $branches;
        }
        $this->defaultBranch = $conf->default_branch ?? array_slice($this->branchList,0,1)[0];
        $this->branch = $this->defaultBranch;
    }
    public function branches(){
        return array_keys($this->branchList);
    }

    public function defaultBranch(){
        return $this->defaultBranch;
    }

    public function url(){
        //@TODO add paramaters for branch, type, and possibly the 'relative' path
        return $this->normalizePath(
            $this->url
        );
    }
    public function name(){
        return $this->name;
    }

    public function description(){
        //@TODO Figure this out
        return $this->description;
    }


    public function normalizePath($path){
        return str_replace(['///','//'], '/', $path);
    }


/**
 *
 *
 *
 *
 *
 * Old, presumably bad, code follows
 *
 *
 *
 *
 *
 */


    public function fileForRequest($branch, $mode, $url){
        if ($mode=='src'){
            return $this->dir.'/'.$branch.'/'.$url;
        }

        // var_dump($this->dir);
        // var_dump($this->dir);
        // print_r($this->config->docsDir($branch));
        // exit;
        $ret = $this->dir.'/'.$branch.'/'.$this->config->docsDir($branch).'/'.$url;

        // echo "<br>\n";
        // var_dump($ret);
        // echo "<br>\n";
        // exit;
        return $ret;
    }

    public function filesForRequest($branch, $mode, $url){
        $branchSubPath = $url;
        if ($mode=='src'){
            $path = $this->dir.'/'.$branch.'/'.$url;
        } else {
            $path = $this->dir.'/'.$branch.'/'.$this->config->docsDir($branch).'/'.$url;
            // var_dump($path);
            // exit;
        }
        if (!is_dir($path))$path = dirname($path);
        $files = scandir($path);
        $indexFile = "README.md";
        $list = [];
        foreach ($files as $f){
            if ($f=='.'||$f=='..'||$f==$indexFile)continue;
            $list[] = $f;
        }
        return $list;
    }
    public function urlForRequest($branch, $mode, $url){

        $file = $this->fileForRequest($branch,$mode,$url);

        $base = $this->name;
        if ($mode=='src')$base .= '-src';
        if ($branch!=$this->defaultBranch)$base .= ':'.$branch;
        $base .= '/';
        $tSlash = is_dir($file) ? '/' : '';
        return str_replace('//','/',$base.$url.$tSlash);
    }

    public function urlForVendorFile($branch, $mode, $file){
        $parts = explode('/',$file);
        if ($parts[0]=='')array_shift($parts);
        if ($parts[0]==$this->name)array_shift($parts);
        $base = $this->name; 
        if ($mode=='src')$base .= '-src';
        $branch = array_shift($parts);
        if ($branch!=$this->defaultBranch)$base .= ':'.$branch;
        $base .= '/';

        $remainder = implode('/',$parts);
        if ($mode=='docs'
            &&substr($remainder,0,$rmLen=strlen($this->config->docsDir($branch)))==$this->config->docsDir($branch)){
            $remainder = substr($remainder,$rmLen);
        }
        $base .= $remainder;
        // var_dump($file);
        // var_dump($remainder);
        // exit;
        // echo "\n\n";
        // var_dump($file);
        // echo "\n\n";
        // var_dump($base);exit;
        return str_replace('//','/',$base);
    }









    // 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;
    // }
}