Maslosoft Staple API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/staple
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @link http://maslosoft.com/staple/
*/
namespace Maslosoft\Staple\Helpers;
use Maslosoft\Staple\Interfaces\NavigableInterface;
use Maslosoft\Staple\Interfaces\ProcessorAwareInterface;
use Maslosoft\Staple\Interfaces\RendererAwareInterface;
use Maslosoft\Staple\Models\RequestItem;
use Maslosoft\Staple\Request\HttpRequest;
use Maslosoft\Staple\Staple;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use UnexpectedValueException;
/**
* SiteWalker
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class SiteWalker extends AbstractWalker
{
/**
* Scanning depth
* @var int
*/
private $depth = -1;
/**
* Starting dir, relative to base website path
*
* @param string $dir
* @return SiteWalker
*/
public function start($dir, $depth = -1)
{
$url = (new HttpRequest())->getPath();
$this->path = realpath($this->path . DIRECTORY_SEPARATOR . trim($dir, '/\\'));
$urlParts = preg_split('~/~', trim($url, '/'));
$pathParts = preg_split('~/~', trim($this->path, '/'));
$relativeUrl = '/' . implode('/', array_intersect($pathParts, $urlParts));
$this->relativePath = $relativeUrl;
$this->depth = (int) $depth;
if ($this->depth === 0)
{
throw new UnexpectedValueException('Parameter `depth` cannot be equall zero');
}
return $this;
}
/**
*
* @return static
*/
public function scan()
{
$this->item->url = '/';
$this->scanFiles($this->path, $this->item);
return $this;
}
private function scanFiles($path, $parent)
{
$finder = new Finder();
$finder->in($path);
$finder->files();
$finder->ignoreDotFiles(true);
$finder->depth(0);
$finder->sortByName();
$finder->followLinks();
$items = [];
$index = null;
foreach ($finder as $entity)
{
/* @var $entity SplFileInfo */
$name = $entity->getFilename();
// Skip items starting with underscore
if (preg_match('~^_~', $name))
{
continue;
}
// Skip items starting with dot
if (preg_match('~^\.~', $name))
{
continue;
}
$isIndex = false;
if (preg_match('~^index\.~', basename($name)))
{
$isIndex = true;
}
$item = new RequestItem;
$path = $entity->getPathname();
$url = $this->relativePath . str_replace($this->path, '', $path);
if ($isIndex)
{
$url = dirname($url) . '/';
$index = $item;
$this->scanFolders(dirname($path), $index);
}
$renderer = $this->staple->getRenderer($path);
// Skip assets
if (!$renderer instanceof NavigableInterface)
{
continue;
}
$data = (object) (new PreProcessor)->getData($this, $path, $entity->getBasename());
$item->url = $url;
$item->title = !empty($data->title) ? $data->title : '* ' . ucfirst(basename($url));
$parent->items[] = $item;
}
return $index;
}
private function scanFolders($path, $parent)
{
$finder = new Finder();
$finder->in($path);
$finder->directories();
$finder->ignoreDotFiles(true);
$finder->depth(0);
$finder->sortByName();
foreach ($finder as $entity)
{
/* @var $entity SplFileInfo */
$name = $entity->getFilename();
// Skip items starting with underscore
if (preg_match('~^_~', $name))
{
continue;
}
// Skip items starting with dot
if (preg_match('~^\.~', $name))
{
continue;
}
$this->scanFiles($entity->getPathname(), $parent);
}
}
/**
* Get root item of pages
* @return RequestItem
*/
public function get()
{
return $this->item;
}
}
API documentation generated by ApiGen