8
0
daux.io/libs/Tree/Directory.php

283 Zeilen
7.0 KiB
PHP

<?php namespace Todaymade\Daux\Tree;
use ArrayIterator;
use RuntimeException;
class Directory extends Entry implements \ArrayAccess, \IteratorAggregate
2015-04-23 00:32:30 +02:00
{
/** @var Entry[] */
protected $children = [];
/** @var Content */
protected $first_page;
2015-04-23 00:32:30 +02:00
public function sort()
{
2015-10-28 00:01:41 +01:00
// Separate the values into buckets to sort them separately
$buckets = [
'index' => [],
2015-10-28 00:01:41 +01:00
'numeric' => [],
'normal' => [],
'down_numeric' => [],
'down' => [],
];
foreach ($this->children as $key => $entry) {
$name = $entry->getName();
if ($name == 'index' || $name == '_index') {
$buckets['index'][$key] = $entry;
continue;
}
2016-07-27 21:32:51 +02:00
if ($name[0] == '-') {
2015-10-28 00:01:41 +01:00
if (is_numeric($name[1])) {
2016-07-27 21:32:51 +02:00
$exploded = explode('_', $name);
2015-10-28 00:01:41 +01:00
$buckets['down_numeric'][abs(substr($exploded[0], 1))][$key] = $entry;
continue;
}
$buckets['down'][$key] = $entry;
continue;
}
if (is_numeric($name[0])) {
2016-07-27 21:32:51 +02:00
$exploded = explode('_', $name);
2015-10-28 00:01:41 +01:00
$buckets['numeric'][abs($exploded[0])][$key] = $entry;
continue;
}
$buckets['normal'][$key] = $entry;
}
$final = [];
foreach ($buckets as $name => $bucket) {
if ($name == 'numeric' || $name == 'down_numeric') {
ksort($bucket);
foreach ($bucket as $sub_bucket) {
$final = $this->sortBucket($sub_bucket, $final);
}
} else {
$final = $this->sortBucket($bucket, $final);
}
}
$this->children = $final;
}
private function sortBucket($bucket, $final)
{
2016-07-27 21:32:51 +02:00
uasort($bucket, function (Entry $a, Entry $b) {
2015-10-28 00:01:41 +01:00
return strcasecmp($a->getName(), $b->getName());
});
foreach ($bucket as $key => $value) {
$final[$key] = $value;
}
return $final;
}
2015-07-18 21:45:38 +02:00
/**
* @return Entry[]
*/
public function getEntries()
{
return $this->children;
}
public function addChild(Entry $entry)
{
$this->children[$entry->getUri()] = $entry;
}
public function removeChild(Entry $entry)
{
unset($this->children[$entry->getUri()]);
}
/**
* @return \Todaymade\Daux\Config
*/
public function getConfig()
{
if (!$this->parent) {
2016-07-27 21:32:51 +02:00
throw new \RuntimeException('Could not retrieve configuration. Are you sure that your tree has a Root ?');
}
return $this->parent->getConfig();
}
/**
* @return Content|null
*/
public function getIndexPage()
{
$index_key = $this->getConfig()['index_key'];
if (isset($this->children[$index_key])) {
return $this->children[$index_key];
}
if ($this->getConfig()->shouldInheritIndex() && $first_page = $this->seekFirstPage()) {
return $first_page;
}
return null;
}
/**
* Seek the first available page from descendants
* @return Content|null
*/
2015-08-16 22:26:00 +02:00
public function seekFirstPage()
{
2016-07-27 21:32:51 +02:00
if ($this instanceof self) {
2015-08-16 22:26:00 +02:00
$index_key = $this->getConfig()['index_key'];
if (isset($this->children[$index_key])) {
return $this->children[$index_key];
}
foreach ($this->children as $node_key => $node) {
if ($node instanceof Content) {
return $node;
}
2016-07-27 21:32:51 +02:00
if ($node instanceof self
2015-08-16 22:26:00 +02:00
&& strpos($node->getUri(), '.') !== 0
2016-07-27 21:32:51 +02:00
&& $childNode = $node->seekFirstPage()) {
2015-08-16 22:26:00 +02:00
return $childNode;
}
}
}
2016-07-27 21:32:51 +02:00
2015-08-16 22:26:00 +02:00
return null;
}
/**
2015-07-20 15:59:52 +02:00
* @return Content|null
*/
public function getFirstPage()
{
if ($this->first_page) {
return $this->first_page;
}
// First we try to find a real page
foreach ($this->getEntries() as $node) {
if ($node instanceof Content) {
2015-07-19 01:16:04 +02:00
if ($this instanceof Root && $this->getIndexPage() == $node) {
// The homepage should not count as first page
continue;
}
$this->setFirstPage($node);
2016-07-27 21:32:51 +02:00
return $node;
}
}
// If we can't find one we check in the sub-directories
foreach ($this->getEntries() as $node) {
2016-07-27 21:32:51 +02:00
if ($node instanceof self && $page = $node->getFirstPage()) {
$this->setFirstPage($page);
2016-07-27 21:32:51 +02:00
return $page;
}
}
2015-07-20 15:59:52 +02:00
return null;
}
/**
* @param Content $first_page
*/
public function setFirstPage($first_page)
{
$this->first_page = $first_page;
}
2015-07-29 08:30:41 +02:00
/**
* Used when creating the navigation.
* Hides folders without showable content
*
* @return bool
*/
public function hasContent()
{
foreach ($this->getEntries() as $node) {
if ($node instanceof Content) {
return true;
2016-07-27 21:32:51 +02:00
} elseif ($node instanceof self) {
2015-07-29 08:30:41 +02:00
if ($node->hasContent()) {
return true;
}
}
}
return false;
}
public function dump()
{
$dump = parent::dump();
$dump['index'] = $this->getIndexPage() ? $this->getIndexPage()->getUrl() : '';
$dump['first'] = $this->getFirstPage() ? $this->getFirstPage()->getUrl() : '';
foreach ($this->getEntries() as $entry) {
$dump['children'][] = $entry->dump();
}
return $dump;
}
/**
* Whether a offset exists
* @param mixed $offset An offset to check for.
2016-07-27 21:32:51 +02:00
* @return bool true on success or false on failure.
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->children);
}
/**
* Offset to retrieve
* @param mixed $offset The offset to retrieve.
* @return Entry Can return all value types.
*/
public function offsetGet($offset)
{
return $this->children[$offset];
}
/**
* Offset to set
* @param mixed $offset The offset to assign the value to.
* @param Entry $value The value to set.
* @return void
*/
public function offsetSet($offset, $value)
{
if (!$value instanceof Entry) {
2016-07-27 21:32:51 +02:00
throw new RuntimeException('The value is not of type Entry');
}
$this->addChild($value);
}
/**
* Offset to unset
* @param string $offset the offset to unset
* @return void
*/
public function offsetUnset($offset)
{
unset($this->children[$offset]);
}
public function getIterator()
{
return new ArrayIterator($this->children);
}
}