|
|
|
@@ -2,18 +2,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
namespace Rmphp\Content;
|
|
|
|
namespace Rmphp\Content;
|
|
|
|
|
|
|
|
|
|
|
|
use Rmphp\Foundation\Exceptions\AppException;
|
|
|
|
use Rmphp\Foundation\Exceptions\AppError;
|
|
|
|
use Rmphp\Foundation\TemplateInterface;
|
|
|
|
use Rmphp\Foundation\TemplateInterface;
|
|
|
|
|
|
|
|
|
|
|
|
class Content implements TemplateInterface {
|
|
|
|
class Content implements TemplateInterface {
|
|
|
|
|
|
|
|
|
|
|
|
private array $content = [];
|
|
|
|
|
|
|
|
private ContentData $data;
|
|
|
|
|
|
|
|
private ContentData $dataGlobal;
|
|
|
|
|
|
|
|
private string $basePath = "";
|
|
|
|
|
|
|
|
private string $template;
|
|
|
|
|
|
|
|
private string $subtemplatePath;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Content constructor.
|
|
|
|
* Content constructor.
|
|
|
|
* @param string $template
|
|
|
|
* @param string $template
|
|
|
|
@@ -22,128 +15,123 @@ class Content implements TemplateInterface {
|
|
|
|
if(!empty($template)) $this->setTemplate($template);
|
|
|
|
if(!empty($template)) $this->setTemplate($template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/** @inheritDoc */
|
|
|
|
* @param string $template
|
|
|
|
|
|
|
|
* @param array $resource
|
|
|
|
|
|
|
|
* @return TemplateInterface
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function setTemplate(string $template, array $resource = []) : TemplateInterface {
|
|
|
|
public function setTemplate(string $template, array $resource = []) : TemplateInterface {
|
|
|
|
if(empty($this->data)) $this->data = new ContentData();
|
|
|
|
|
|
|
|
foreach ($resource as $resKey => $resVal){
|
|
|
|
foreach ($resource as $resKey => $resVal){
|
|
|
|
$this->data->{$resKey} = $resVal;
|
|
|
|
$this->{$resKey} = $resVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->basePath = dirname(__DIR__, 4);
|
|
|
|
ContentData::$basePath = dirname(__DIR__, 4);
|
|
|
|
$this->template = $this->basePath.'/'.$template;
|
|
|
|
ContentData::$template = ContentData::$basePath.$template;
|
|
|
|
return $this;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
|
|
|
public function setSubtemplatePath(string $subtemplatePath = "") : TemplateInterface {
|
|
|
|
|
|
|
|
ContentData::$subtemplatePath = ContentData::$basePath.rtrim($subtemplatePath, '/');
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
|
|
|
public function getSubtemplatePath(): string {
|
|
|
|
|
|
|
|
return ContentData::$subtemplatePath;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param string $subtemplatePath
|
|
|
|
* @param array $aliases
|
|
|
|
* @return TemplateInterface
|
|
|
|
* @return TemplateInterface
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function setSubtemplePath(string $subtemplatePath) : TemplateInterface {
|
|
|
|
public function setSubtemplatePathAlias(array $aliases = []) : TemplateInterface {
|
|
|
|
$this->subtemplatePath = $this->basePath.'/'.$subtemplatePath;
|
|
|
|
foreach($aliases as $alias => $subtemplatePath){
|
|
|
|
|
|
|
|
ContentData::$subtemplatePathAlias[$alias] = ContentData::$basePath.rtrim($subtemplatePath, '/');
|
|
|
|
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function getSubtemplatePathAlias() : array {
|
|
|
|
|
|
|
|
return ContentData::$subtemplatePathAlias;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param string $subtemplate
|
|
|
|
* @return string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function getSubtemplePath(): string {
|
|
|
|
public function getFullSubtemplatePath(string $subtemplate) : string {
|
|
|
|
return $this->subtemplatePath;
|
|
|
|
if (preg_match("'^[%@#](\w+?)(/.+)'", $subtemplate, $match)){
|
|
|
|
|
|
|
|
if(empty(ContentData::$subtemplatePathAlias)) throw new AppError("SubtemplatePathAliases is not defined");
|
|
|
|
|
|
|
|
if(empty(ContentData::$subtemplatePathAlias[$match[1]])) throw new AppError("Aliase '$match[1]' is not defined");
|
|
|
|
|
|
|
|
if(!file_exists(ContentData::$subtemplatePathAlias[$match[1]].$match[2])) throw new AppError("Subtemplate ".ContentData::$subtemplatePathAlias[$match[1]].$match[2]. " is not found");
|
|
|
|
|
|
|
|
return ContentData::$subtemplatePathAlias[$match[1]].$match[2];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (empty(ContentData::$subtemplatePath)) throw new AppError("SubtemplatePath is not defined");
|
|
|
|
|
|
|
|
if (!file_exists(ContentData::$subtemplatePath.$subtemplate)) throw new AppError("Subtemplate ".ContentData::$subtemplatePath.$subtemplate. " is not found");
|
|
|
|
|
|
|
|
return ContentData::$subtemplatePath.$subtemplate;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param string $point
|
|
|
|
* @inheritDoc
|
|
|
|
* @param string $string
|
|
|
|
|
|
|
|
* @throws AppException
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function addValue(string $point, string $string) : void {
|
|
|
|
public function addValue(string $point, string $string) : void {
|
|
|
|
if (empty($point)) throw new AppException("Empty point");
|
|
|
|
if (empty($point)) throw new AppError("Empty point");
|
|
|
|
if (empty($this->subtemplatePath))throw new AppException("SubtemplatePath is not defined");
|
|
|
|
ContentData::$content[$point][] = $string;
|
|
|
|
$this->content[$point][] = $string;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param string $point
|
|
|
|
* @inheritDoc
|
|
|
|
* @param string $string
|
|
|
|
|
|
|
|
* @throws AppException
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function setValue(string $point, string $string) : void {
|
|
|
|
public function setValue(string $point, string $string) : void {
|
|
|
|
unset($this->content[$point]);
|
|
|
|
unset(ContentData::$content[$point]);
|
|
|
|
$this->addValue($point, $string);
|
|
|
|
$this->addValue($point, $string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param string $point
|
|
|
|
* @inheritDoc
|
|
|
|
* @param string $subTempl
|
|
|
|
|
|
|
|
* @param array $resource
|
|
|
|
|
|
|
|
* @throws AppException
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function addSubtemple(string $point, string $subTempl, array $resource = []) : void {
|
|
|
|
public function addSubtemplate(string $point, string $subtemplate, array $resource = []) : void {
|
|
|
|
if (empty($this->subtemplatePath))throw new AppException("SubtemplatePath is not defined");
|
|
|
|
if (empty($point)) throw new AppError("Empty point");
|
|
|
|
if (empty($point)) throw new AppException("Empty point");
|
|
|
|
if (empty($subtemplate)) throw new AppError("Subtemplate is empty");
|
|
|
|
if (empty($subTempl) || !file_exists($this->subtemplatePath."/".$subTempl)) throw new AppException($this->subtemplatePath."/".$subTempl. " is not found");
|
|
|
|
$inc = $this->getFullSubtemplatePath($subtemplate);
|
|
|
|
if(empty($this->data)) $this->data = new ContentData();
|
|
|
|
|
|
|
|
foreach ($resource as $resKey => $resVal){
|
|
|
|
foreach ($resource as $resKey => $resVal){
|
|
|
|
$this->data->{$resKey} = $resVal;
|
|
|
|
$this->{$resKey} = $resVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ob_start(); include $this->subtemplatePath."/".$subTempl; $this->content[$point][] = ob_get_contents(); ob_end_clean();
|
|
|
|
ob_start(); include $inc; ContentData::$content[$point][] = ob_get_contents(); ob_end_clean();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param string $point
|
|
|
|
* @inheritDoc
|
|
|
|
* @param string $subTempl
|
|
|
|
|
|
|
|
* @param array $resource
|
|
|
|
|
|
|
|
* @throws AppException
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function setSubtemple(string $point, string $subTempl, array $resource = []) : void {
|
|
|
|
public function setSubtemplate(string $point, string $subtemplate, array $resource = []) : void {
|
|
|
|
unset($this->content[$point]);
|
|
|
|
unset(ContentData::$content[$point]);
|
|
|
|
$this->addSubtemple($point, $subTempl, $resource);
|
|
|
|
$this->addSubtemplate($point, $subtemplate, $resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param array $globals
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function setGlobals(array $globals = []) : void {
|
|
|
|
|
|
|
|
$this->dataGlobal = new ContentData();
|
|
|
|
|
|
|
|
foreach ($globals as $resKey => $resVal){
|
|
|
|
|
|
|
|
$this->dataGlobal->{$resKey} = $resVal;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param string $incFile
|
|
|
|
|
|
|
|
* @param array $resource
|
|
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
|
|
* @throws AppException
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function inc(string $incFile, array $resource = []) : string {
|
|
|
|
public function inc(string $incFile, array $resource = []) : string {
|
|
|
|
if(empty($this->data)) $this->data = new ContentData();
|
|
|
|
$inc = $this->getFullSubtemplatePath($incFile);
|
|
|
|
foreach ($resource as $resKey => $resVal){
|
|
|
|
foreach ($resource as $resKey => $resVal){
|
|
|
|
$this->data->{$resKey} = $resVal;
|
|
|
|
$this->{$resKey} = $resVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(empty($incFile) || !file_exists($this->subtemplatePath."/".$incFile)) throw new AppException("Empty inc file");
|
|
|
|
ob_start(); include $inc; $out = ob_get_contents(); ob_end_clean();
|
|
|
|
ob_start(); include $this->subtemplatePath."/".$incFile; $out = ob_get_contents(); ob_end_clean();
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/** @inheritDoc */
|
|
|
|
* @param string $point
|
|
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function getPoint(string $point) : string {
|
|
|
|
public function getPoint(string $point) : string {
|
|
|
|
if (empty($point) || empty($this->content[$point])) return "";
|
|
|
|
if (empty($point) || empty(ContentData::$content[$point])) return "";
|
|
|
|
return implode("", $this->content[$point]);
|
|
|
|
return implode("", ContentData::$content[$point]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/** @inheritDoc */
|
|
|
|
* @return string
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function getResponse(): string {
|
|
|
|
public function getResponse(): string {
|
|
|
|
if (empty($this->template) || !file_exists($this->template)) return '';
|
|
|
|
if (empty(ContentData::$template) || !file_exists(ContentData::$template)) throw new AppError("Invalid template file");
|
|
|
|
ob_start(); include $this->template; $out = ob_get_contents(); ob_end_clean();
|
|
|
|
ob_start(); include ContentData::$template; $out = ob_get_contents(); ob_end_clean();
|
|
|
|
return $out;
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|