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
<?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\Processors\Pre;
use Maslosoft\Staple\Interfaces\RendererAwareInterface;
/**
* ##Cascading Data Json Extractor##
*
* This extracts json data from content folder. Additionally it traverse folders
* above, check for json data and merge it all from top to bottom.
*
* Data files closer to current view have higher priority and will overwrite
* data from files closer to the root of website.
*
* By default it's name is `data.json`
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class CascadingDataJsonExtractor
{
public $filename = 'data.json';
public function decorate(RendererAwareInterface $owner, &$content, $data)
{
}
public function getData(RendererAwareInterface $owner, $filename, $view)
{
$data = [];
$rootPath = realpath($owner->getRootPath());
$path = dirname($filename);
$parts = explode('/', $path);
foreach ($parts as $part)
{
$filePath = sprintf('%s/%s', $path, $this->filename);
if (file_exists($filePath))
{
$data = array_merge((array) json_decode(file_get_contents($filePath), false), $data);
}
$path = realpath($path . '/../');
if ($path === $rootPath)
{
break;
}
}
return $data;
}
}
API documentation generated by ApiGen