<?php
namespace Tlf\Provi;
class Project {
// return [
// 'url'=>$_url,
// 'parts'=>$parts,
// 'vendor'=>$vendor,
// 'project'=>$project_name,
// 'view_source'=>$view_source,
// 'branch_name'=>$branch,
// 'file'=>$rel_file,
// ];
// }
//
/**
* Whether this request is for a source file or a documentation file
*/
public bool $view_source;
public string $url;
/**
* The unparsed parts of the url, without the base url, separated by forward slash `/`
*/
public array $parts;
/** the vendor name */
public string $vendor;
/** the branch name */
public string $branch_name;
/** The relative file path */
public string $file;
/** the branch name specified in the url */
public ?string $url_branch;
/**
* The root directory of this project. This is the same dir that a `.git/` folder would be stored in.
*/
public string $dir;
/**
* The full path to the file being requested
*/
public string $path;
/** the project name */
public string $name;
public function __construct($dir, $info){
$this->dir = $dir;
$this->name = $info['project'];
unset($info['project']);
foreach ($info as $key=>$value){
$this->$key = $value;
}
$this->file = '/'.implode('/',array_slice($info['parts'],2));
if ($info['view_source']){
$this->path = $this->dir.$this->file;
/** @todo implement branch pathing */
$this->branchDir = $this->dir;
} else {
$this->path = $this->dir.'/'.$this->findDocsDir().$this->file;
/** @todo implement branch pathing */
$this->branchDir = $this->dir.'/'.$this->findDocsDir();
}
$this->path = str_replace(['///','//'],'/',$this->path);
$this->branch_name = $this->url_branch ?? $this->getDefaultBranchName();
$this->docsDir = $this->findDocsDir();
$this->branch_url = '/'.implode('/', array_slice($info['parts'],0,2));
}
public function findDocsDir(){
return 'doc';
}
public function urlForPath($relPath=null){
$relPath = $relPath ?? $this->file;
$path = $this->branch_url.$relPath;
return $path;
}
public function fileForPath($relPath=null){
$relPath = $relPath ?? $this->file;
$path = $this->branchDir.$relPath;
if (!file_exists($path))return null;
return $path;
}
public function isPathActive($relPath){
$url = str_replace(['///','//'],'/',$relPath.'/');
if (substr($this->file,0,strlen($url))==$url){
return true;
}
return false;
}
public function filesForPath($relPath=null){
$relPath = $relPath ?? $this->file;
$path = $this->branchDir.$relPath;
if (!is_dir($path))$path = dirname($path);
$files = scandir($path);
/** @todo assess whether I need to filter out the index file */
$indexFile = "README.md";
$list = [];
foreach ($files as $f){
if ($f=='.'||$f=='..'||$f==$indexFile)continue;
$list[] = $f;
}
return $list;
}
/**
* @param $relPath the relative path to the file
*/
public function srcUrl($relPath=''){
$project = $this->name.'-src';
if ($this->url_branch != null)$project .= ':'.$this->url_branch;
$parts = [
$this->base_url,
$this->vendor,
$project,
$relPath
];
return str_replace(['///','//'],'/', '/'.implode('/',$parts) );
}
public function docsUrl($relPath=''){
$project = $this->name;
if ($this->url_branch != null)$project .= ':'.$this->url_branch;
$parts = [
$this->base_url,
$this->vendor,
$project,
$relPath
];
return str_replace(['///','//'],'/','/'.implode('/',$parts) );
}
/**
*
* @todo actually implement this
*/
public function getDefaultBranchName(){
return 'main';
}
/**
* @todo actually implement branches lookup
*/
public function branches(){
return [];
}
}