Improve tree api

This commit is contained in:
Stéphane Goetz 2015-07-16 11:08:16 +02:00 committed by Stéphane Goetz
parent 290f52d181
commit a044b4b0ca
5 changed files with 61 additions and 26 deletions

BIN
daux.phar Normal file → Executable file

Binary file not shown.

View File

@ -53,7 +53,7 @@ abstract class MarkdownPage extends SimplePage
public static function fromFile(Content $file, $params) public static function fromFile(Content $file, $params)
{ {
$page = new static($file->title, file_get_contents($file->getPath())); $page = new static($file->getTitle(), $file->getContent());
$page->setFile($file); $page->setFile($file);
$page->setParams($params); $page->setParams($params);

View File

@ -56,7 +56,7 @@ class MarkdownPage extends \Todaymade\Daux\Format\Base\MarkdownPage
$entry_page['View Documentation'] = $params['base_page'] . $params['entry_page']->getUrl(); $entry_page['View Documentation'] = $params['base_page'] . $params['entry_page']->getUrl();
} }
} elseif ($params['file_uri'] === 'index') { } elseif ($params['file_uri'] === 'index') {
$entry_page[$params['entry_page']->title] = $params['base_page'] . $params['entry_page']->getUrl(); $entry_page[$params['entry_page']->getTitle()] = $params['base_page'] . $params['entry_page']->getUrl();
} }
$page = [ $page = [

View File

@ -4,7 +4,7 @@ use Todaymade\Daux\DauxHelper;
class Content extends Entry class Content extends Entry
{ {
public $title; protected $content;
public function __construct($path = '', $parents = array()) public function __construct($path = '', $parents = array())
{ {
@ -12,6 +12,20 @@ class Content extends Entry
$this->value = $this->uri; $this->value = $this->uri;
} }
public function getContent()
{
if (!$this->content) {
$this->content = file_get_contents($this->getPath());
}
return $this->content;
}
public function setContent($content)
{
$this->content = $content;
}
protected function getFilename($file) protected function getFilename($file)
{ {

View File

@ -15,40 +15,28 @@ abstract class Entry
public function __construct($path = '', $parents = array()) public function __construct($path = '', $parents = array())
{ {
if (!isset($path) || $path == '' || !file_exists($path)) { $this->setPath($path);
return; $this->setParents($parents);
}
$this->local_path = $path;
$this->parents = $parents;
$this->last_modified = filemtime($path);
$this->name = DauxHelper::pathinfo($path)['filename'];
$this->title = $this->getTitleInternal($this->name);
$this->uri = $this->getUrlInternal($this->getFilename($path));
$this->index_page = false;
} }
public function getName() public function getName()
{ {
return $this->name; return $this->name;
} }
public function setUri($uri) public function setName($name)
{ {
$this->uri = $uri; $this->name = $name;
} }
public function getUri() public function getUri()
{ {
return $this->uri; return $this->uri;
} }
public function getUrl() public function setUri($uri)
{ {
$url = ''; $this->uri = $uri;
foreach ($this->parents as $node) {
$url .= $node->uri . '/';
}
$url .= $this->uri;
return $url;
} }
public function getIndexPage() public function getIndexPage()
@ -101,16 +89,39 @@ abstract class Entry
{ {
return $this->title; return $this->title;
} }
public function setTitle($title)
{
$this->title = $title;
}
public function getParents() public function getParents()
{ {
return $this->parents; return $this->parents;
} }
public function setParents($parents)
{
$this->parents = $parents;
}
public function getPath() public function getPath()
{ {
return $this->local_path; return $this->local_path;
} }
public function setPath($path)
{
if (!isset($path) || $path == '' || !file_exists($path)) {
return;
}
$this->local_path = $path;
$this->last_modified = filemtime($path);
$this->name = DauxHelper::pathinfo($path)['filename'];
$this->title = $this->getTitleInternal($this->name);
$this->uri = $this->getUrlInternal($this->getFilename($path));
$this->index_page = false;
}
public function write($content) public function write($content)
{ {
@ -121,6 +132,16 @@ abstract class Entry
file_put_contents($this->local_path, $content); file_put_contents($this->local_path, $content);
return true; return true;
} }
public function getUrl()
{
$url = '';
foreach ($this->parents as $node) {
$url .= $node->uri . '/';
}
$url .= $this->uri;
return $url;
}
protected function getFilename($file) protected function getFilename($file)
{ {