2015-04-22 18:24:10 +02:00
|
|
|
<?php namespace Todaymade\Daux\Tree;
|
|
|
|
|
2015-08-16 21:19:55 +02:00
|
|
|
use SplFileInfo;
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
abstract class Entry
|
|
|
|
{
|
2015-07-17 23:38:06 +02:00
|
|
|
/** @var string */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $title;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $name;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2015-04-23 00:32:30 +02:00
|
|
|
protected $uri;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
/** @var Directory */
|
|
|
|
protected $parent;
|
|
|
|
|
2015-08-16 21:19:55 +02:00
|
|
|
/** @var SplFileInfo */
|
|
|
|
protected $info;
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/** @var string */
|
2015-07-18 20:52:14 +02:00
|
|
|
protected $path;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/**
|
2015-07-18 20:52:14 +02:00
|
|
|
* @param string $uri
|
2015-08-16 21:19:55 +02:00
|
|
|
* @param SplFileInfo $info
|
2015-07-17 23:38:06 +02:00
|
|
|
*/
|
2015-08-16 21:19:55 +02:00
|
|
|
public function __construct(Directory $parent, $uri, SplFileInfo $info = null)
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-18 20:52:14 +02:00
|
|
|
$this->setUri($uri);
|
|
|
|
$this->setParent($parent);
|
|
|
|
|
2015-08-16 21:19:55 +02:00
|
|
|
if ($info !== null) {
|
|
|
|
$this->info = $info;
|
|
|
|
$this->path = $info->getPathname();
|
2015-07-18 20:52:14 +02:00
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
2015-07-17 23:38:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getUri()
|
|
|
|
{
|
|
|
|
return $this->uri;
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
2015-07-16 11:08:16 +02:00
|
|
|
public function setUri($uri)
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-18 20:52:14 +02:00
|
|
|
if ($this->parent) {
|
|
|
|
$this->parent->removeChild($this);
|
|
|
|
}
|
|
|
|
|
2015-07-16 11:08:16 +02:00
|
|
|
$this->uri = $uri;
|
2015-07-18 20:52:14 +02:00
|
|
|
|
|
|
|
if ($this->parent) {
|
|
|
|
$this->parent->addChild($this);
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-09 19:21:16 +01:00
|
|
|
public function getTitle(): ?string
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
2019-11-28 23:32:33 +01:00
|
|
|
public function setTitle(string $title)
|
2015-07-17 23:38:06 +02:00
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-18 20:52:14 +02:00
|
|
|
* @return Directory
|
2015-07-17 23:38:06 +02:00
|
|
|
*/
|
2019-11-28 23:32:33 +01:00
|
|
|
public function getParent(): ?Directory
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-18 20:52:14 +02:00
|
|
|
return $this->parent;
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
2020-04-22 22:24:52 +02:00
|
|
|
* Return all parents starting with the root.
|
2015-07-18 20:52:14 +02:00
|
|
|
*
|
2015-07-18 21:23:02 +02:00
|
|
|
* @return Directory[]
|
2015-07-17 23:38:06 +02:00
|
|
|
*/
|
2015-07-18 20:52:14 +02:00
|
|
|
public function getParents()
|
2015-07-17 23:38:06 +02:00
|
|
|
{
|
2015-07-18 20:52:14 +02:00
|
|
|
$parents = [];
|
|
|
|
if ($this->parent && !$this->parent instanceof Root) {
|
|
|
|
$parents = $this->parent->getParents();
|
|
|
|
$parents[] = $this->parent;
|
|
|
|
}
|
2015-07-17 23:38:06 +02:00
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
return $parents;
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2015-07-17 23:38:06 +02:00
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
protected function setParent(Directory $parent)
|
2015-07-17 23:38:06 +02:00
|
|
|
{
|
2015-07-18 20:52:14 +02:00
|
|
|
if ($this->parent) {
|
|
|
|
$this->parent->removeChild($this);
|
2015-07-16 11:08:16 +02:00
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
$parent->addChild($this);
|
|
|
|
$this->parent = $parent;
|
2015-07-16 11:08:16 +02:00
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-01-08 08:17:33 +01:00
|
|
|
public function getPath(): ?string
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-18 20:52:14 +02:00
|
|
|
return $this->path;
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 00:18:34 +02:00
|
|
|
/**
|
2020-04-22 22:24:52 +02:00
|
|
|
* Get the path to the file from the root of the documentation.
|
2016-08-17 00:18:34 +02:00
|
|
|
*/
|
2019-11-28 23:32:33 +01:00
|
|
|
public function getRelativePath(): string
|
2016-08-17 00:18:34 +02:00
|
|
|
{
|
|
|
|
$root = $this;
|
2016-09-26 20:54:06 +02:00
|
|
|
while ($root->getParent() != null) {
|
2016-08-17 00:18:34 +02:00
|
|
|
$root = $root->getParent();
|
|
|
|
}
|
|
|
|
|
2016-09-26 20:54:06 +02:00
|
|
|
return substr($this->path, strlen($root->getPath()) + 1);
|
2016-08-17 00:18:34 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 23:32:33 +01:00
|
|
|
public function getFileinfo(): SplFileInfo
|
2015-08-16 21:19:55 +02:00
|
|
|
{
|
|
|
|
return $this->info;
|
|
|
|
}
|
|
|
|
|
2019-11-28 23:32:33 +01:00
|
|
|
public function getUrl(): string
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-18 20:52:14 +02:00
|
|
|
$url = '';
|
|
|
|
|
|
|
|
if ($this->getParent() && !$this->getParent() instanceof Root) {
|
|
|
|
$url = $this->getParent()->getUrl() . '/' . $url;
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
2015-07-18 20:52:14 +02:00
|
|
|
|
|
|
|
$url .= $this->getUri();
|
2016-07-27 21:32:51 +02:00
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
return $url;
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
public function dump()
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-18 20:52:14 +02:00
|
|
|
return [
|
|
|
|
'title' => $this->getTitle(),
|
|
|
|
'type' => get_class($this),
|
|
|
|
'name' => $this->getName(),
|
|
|
|
'uri' => $this->getUri(),
|
|
|
|
'url' => $this->getUrl(),
|
2016-07-27 21:32:51 +02:00
|
|
|
'path' => $this->path,
|
2015-07-18 20:52:14 +02:00
|
|
|
];
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
2017-01-09 18:29:52 +01:00
|
|
|
|
2020-04-22 21:55:53 +02:00
|
|
|
public function isHotPath(Entry $node = null)
|
|
|
|
{
|
2017-01-09 18:29:52 +01:00
|
|
|
return $this->parent->isHotPath($node ?: $this);
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|