oldProject.php.bak

<?php

namespace Taeluf\ProjectViewer;

class Project {

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

    protected $config;

    public function __construct($dir){

        if (!is_dir($dir)){
            throw new \Exception("Cannot create project because path '{$dir}' is not a directory. ");
        }
        $dir = realpath($dir);
        $config = new Config($dir);
        $name = $config->name;
        $defaultBranch = $config->defaultBranch;


        $this->defaultBranch = $defaultBranch;
        $this->branchList = $config->branches;
        $this->config = $config;
        $this->name = $name;
        $this->dir = $dir;

    }
    public function branches(){
        return array_keys($this->branchList);
    }

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

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

        return $this->dir.'/'.$branch.'/'.$this->config->docsDir($branch).'/'.$url;
    }

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