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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
<?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;
use Maslosoft\EmbeDi\EmbeDi;
use Maslosoft\EmbeDi\Traits\FlyTrait;
use Maslosoft\Staple\Interfaces\PostProcessorInterface;
use Maslosoft\Staple\Interfaces\PreProcessorInterface;
use Maslosoft\Staple\Interfaces\RendererExtensionInterface;
use Maslosoft\Staple\Interfaces\RendererInterface;
use Maslosoft\Staple\Interfaces\RequestAwareInterface;
use Maslosoft\Staple\Interfaces\RequestInterface;
use Maslosoft\Staple\Processors\Post\TemplateApplier;
use Maslosoft\Staple\Processors\Pre\CascadingDataJsonExtractor;
use Maslosoft\Staple\Processors\Pre\DataJsonExtractor;
use Maslosoft\Staple\Processors\Pre\TagExtractor;
use Maslosoft\Staple\Processors\Pre\ViewJsonExtractor;
use Maslosoft\Staple\Renderers\ErrorRenderer;
use Maslosoft\Staple\Renderers\HtmlRenderer;
use Maslosoft\Staple\Renderers\MdRenderer;
use Maslosoft\Staple\Renderers\PassThroughRenderer;
use Maslosoft\Staple\Renderers\PhpMdRenderer;
use Maslosoft\Staple\Renderers\PhpRenderer;
use Maslosoft\Staple\Renderers\ThumbRenderer;
/**
* @method Staple fly() Get staple flyweight instance
*/
class Staple implements RequestAwareInterface
{
use FlyTrait;
const DefaultInstanceId = '.';
const BootstrapName = '_bootstrap.php';
const ContentPath = '_content';
const LayoutPath = '_layout';
/**
* Version number holder
* @var string
*/
private static $version;
/**
* Renderers configuration. Keys are file extensions,
* values are renderers class name or configuration supported by EmbeDi.
*
* For available configuration options see renderer class.
*
* Example configuration:
* ```
* public $renderers = [
* 'php' => PhpRenderer::class,
* 'php.md' => PhpMdRenderer::class,
* 'md' => [
* 'class' => MdRenderer::class,
* 'extension' => 'mkd'
* ],
* 'html' => [
* 'class' => HtmlRenderer::class,
* 'extension' => 'htm'
* ],
* ];
* ```
* @var mixed[]
*/
public $renderers = [
'php' => PhpRenderer::class,
'mkd.php' => PhpMdRenderer::class,
'md.php' => PhpMdRenderer::class,
'mkd' => MdRenderer::class,
'md' => MdRenderer::class,
'html' => HtmlRenderer::class,
'thumb.jpg' => ThumbRenderer::class,
'thumb.gif' => ThumbRenderer::class,
'thumb.png' => ThumbRenderer::class,
'jpg' => PassThroughRenderer::class,
'gif' => PassThroughRenderer::class,
'png' => PassThroughRenderer::class,
'svg' => PassThroughRenderer::class,
'pdf' => PassThroughRenderer::class,
];
public $preProcessors = [
DataJsonExtractor::class,
CascadingDataJsonExtractor::class,
ViewJsonExtractor::class,
TagExtractor::class
];
public $postProcessors = [
TemplateApplier::class
];
/**
* Absolute root path of website.
* This should be path where your main index file resides.
* @var string
*/
private $rootPath = '';
/**
* Relative path to content files. This defaults to `_content`
* @var string
*/
private $contentPath = self::ContentPath;
/**
* Relative path to layout files. This defaults to `_layout`
* @var string
*/
private $layoutPath = self::LayoutPath;
/**
* DI container
* @var EmbeDi
*/
private $di = null;
/**
*
* @param string $rootPath
*/
public function __construct($rootPath = '')
{
$this->di = EmbeDi::fly();
$this->setRootPath($rootPath);
}
/**
* Get Post processors
* @return PostProcessorInterface[]
*/
public function getPostProcessors()
{
$post = [];
foreach ($this->postProcessors as $config)
{
$post[] = $this->di->apply($config);
}
return $post;
}
/**
* Get Pre Processors
* @return PreProcessorInterface[]
*/
public function getPreProcessors()
{
$pre = [];
foreach ($this->preProcessors as $config)
{
$pre[] = $this->di->apply($config);
}
return $pre;
}
/**
* Set absolute root path
* @param string $path
* @return Staple
*/
public function setRootPath($path)
{
$this->rootPath = $path;
return $this;
}
/**
* Get root path. Will try to autodetect if empty.
* @return string
*/
public function getRootPath()
{
// Guess if empty
if (empty($this->rootPath))
{
$this->rootPath = __DIR__ . '../../../../';
}
return $this->rootPath;
}
public function getContentPath($absolute = false)
{
if ($absolute)
{
return sprintf('%s/%s', $this->getRootPath(), $this->contentPath);
}
return $this->contentPath;
}
public function setContentPath($contentPath)
{
$this->contentPath = $contentPath;
return $this;
}
public function getLayoutPath()
{
return $this->layoutPath;
}
public function setLayoutPath($layoutPath)
{
$this->layoutPath = $layoutPath;
return $this;
}
/**
* Get current staple version
* @return string
*/
public function getVersion()
{
if (null === self::$version)
{
self::$version = require __DIR__ . '/version.php';
}
return self::$version;
}
/**
* Get renderer
* @param string $fileName
* @return RendererInterface
*/
public function getRenderer($fileName)
{
if (empty($fileName))
{
return (new ErrorRenderer('404', 'File not found'))->setOwner($this);
}
$renderers = $this->renderers;
$keys = array_map('strlen', array_keys($renderers));
array_multisort($keys, SORT_DESC, $renderers);
foreach ($renderers as $extension => $config)
{
$matches = [];
$ext = preg_quote($extension);
if (preg_match("~$ext$~i", $fileName, $matches))
{
$renderer = $this->di->apply($config);
/* @var $renderer RendererInterface */
if ($renderer instanceof RendererExtensionInterface)
{
// Use $matches[0] here to ensure extension letters case
$renderer->setExtension($matches[0]);
}
$renderer->setOwner($this);
return $renderer;
}
}
new ErrorRenderer(500, sprintf('Unsupported file extension: `%s`', $ext));
}
/**
* Get supported file extensions, based on `renderers` configuration.
* @return string[]
*/
public function getExtensions()
{
return array_keys($this->renderers);
}
/**
* Handle request. This is entry point for staple.
* @param RequestInterface $request
* @return string
*/
public function handle(RequestInterface $request)
{
return $request->dispatch($this);
}
}
API documentation generated by ApiGen