|
|
|
|
@@ -3,9 +3,9 @@
|
|
|
|
|
namespace Rmphp\Content;
|
|
|
|
|
|
|
|
|
|
use Rmphp\Foundation\Exceptions\AppError;
|
|
|
|
|
use Rmphp\Foundation\Exceptions\AppException;
|
|
|
|
|
use Rmphp\Foundation\TemplateInterface;
|
|
|
|
|
|
|
|
|
|
#[\AllowDynamicProperties]
|
|
|
|
|
class Content implements TemplateInterface {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -16,51 +16,83 @@ class Content implements TemplateInterface {
|
|
|
|
|
if(!empty($template)) $this->setTemplate($template);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $template
|
|
|
|
|
* @param array $resource
|
|
|
|
|
* @return TemplateInterface
|
|
|
|
|
*/
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function setTemplate(string $template, array $resource = []) : TemplateInterface {
|
|
|
|
|
foreach ($resource as $resKey => $resVal){
|
|
|
|
|
$this->{$resKey} = $resVal;
|
|
|
|
|
}
|
|
|
|
|
ContentData::$basePath = dirname(__DIR__, 4);
|
|
|
|
|
ContentData::$template = ContentData::$basePath.'/'.$template;
|
|
|
|
|
ContentData::$template = ContentData::$basePath.$template;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $subtemplatePath
|
|
|
|
|
* @return TemplateInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setSubtemplePath(string $subtemplatePath) : TemplateInterface {
|
|
|
|
|
ContentData::$subtemplatePath = ContentData::$basePath.'/'.$subtemplatePath;
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function setSubtemplatePath(string $subtemplatePath = "") : TemplateInterface {
|
|
|
|
|
ContentData::$subtemplatePath = ContentData::$basePath.rtrim($subtemplatePath, '/');
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getSubtemplePath(): string {
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getSubtemplatePath(): string {
|
|
|
|
|
return ContentData::$subtemplatePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $point
|
|
|
|
|
* @param string $string
|
|
|
|
|
* @throws AppException
|
|
|
|
|
* @param array $aliases
|
|
|
|
|
* @return TemplateInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setSubtemplatePathAlias(array $aliases = []) : TemplateInterface {
|
|
|
|
|
foreach($aliases as $alias => $subtemplatePath){
|
|
|
|
|
ContentData::$subtemplatePathAlias[$alias] = ContentData::$basePath.rtrim($subtemplatePath, '/');
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getSubtemplatePathAlias() : array {
|
|
|
|
|
return ContentData::$subtemplatePathAlias;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $subtemplate
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getFullSubtemplatePath(string $subtemplate) : string {
|
|
|
|
|
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 array $data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function addData(array $data = []) : void {
|
|
|
|
|
foreach ($data as $dataKey => $dataVal){
|
|
|
|
|
$this->{$dataKey} = $dataVal;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function addValue(string $point, string $string) : void {
|
|
|
|
|
if (empty($point)) throw new AppException("Empty point");
|
|
|
|
|
if (empty(ContentData::$subtemplatePath))throw new AppException("SubtemplatePath is not defined");
|
|
|
|
|
if (empty($point)) throw new AppError("Empty point");
|
|
|
|
|
ContentData::$content[$point][] = $string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $point
|
|
|
|
|
* @param string $string
|
|
|
|
|
* @throws AppException
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function setValue(string $point, string $string) : void {
|
|
|
|
|
unset(ContentData::$content[$point]);
|
|
|
|
|
@@ -68,60 +100,47 @@ class Content implements TemplateInterface {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $point
|
|
|
|
|
* @param string $subTempl
|
|
|
|
|
* @param array $resource
|
|
|
|
|
* @throws AppException
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function addSubtemple(string $point, string $subTempl, array $resource = []) : void {
|
|
|
|
|
if (empty(ContentData::$subtemplatePath))throw new AppException("SubtemplatePath is not defined");
|
|
|
|
|
if (empty($point)) throw new AppException("Empty point");
|
|
|
|
|
if (empty($subTempl) || !file_exists(ContentData::$subtemplatePath."/".$subTempl)) throw new AppException(ContentData::$subtemplatePath."/".$subTempl. " is not found");
|
|
|
|
|
public function addSubtemplate(string $point, string $subtemplate, array $resource = []) : void {
|
|
|
|
|
if (empty($point)) throw new AppError("Empty point");
|
|
|
|
|
if (empty($subtemplate)) throw new AppError("Subtemplate is empty");
|
|
|
|
|
$inc = $this->getFullSubtemplatePath($subtemplate);
|
|
|
|
|
foreach ($resource as $resKey => $resVal){
|
|
|
|
|
$this->{$resKey} = $resVal;
|
|
|
|
|
}
|
|
|
|
|
ob_start(); include ContentData::$subtemplatePath."/".$subTempl; ContentData::$content[$point][] = ob_get_contents(); ob_end_clean();
|
|
|
|
|
ob_start(); include $inc; ContentData::$content[$point][] = ob_get_contents(); ob_end_clean();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $point
|
|
|
|
|
* @param string $subTempl
|
|
|
|
|
* @param array $resource
|
|
|
|
|
* @throws AppException
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function setSubtemple(string $point, string $subTempl, array $resource = []) : void {
|
|
|
|
|
public function setSubtemplate(string $point, string $subtemplate, array $resource = []) : void {
|
|
|
|
|
unset(ContentData::$content[$point]);
|
|
|
|
|
$this->addSubtemple($point, $subTempl, $resource);
|
|
|
|
|
$this->addSubtemplate($point, $subtemplate, $resource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $incFile
|
|
|
|
|
* @param array $resource
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws AppException
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function inc(string $incFile, array $resource = []) : string {
|
|
|
|
|
$inc = $this->getFullSubtemplatePath($incFile);
|
|
|
|
|
foreach ($resource as $resKey => $resVal){
|
|
|
|
|
$this->{$resKey} = $resVal;
|
|
|
|
|
}
|
|
|
|
|
if(empty($incFile) || !file_exists(ContentData::$subtemplatePath."/".$incFile)) throw new AppException("Empty inc file");
|
|
|
|
|
ob_start(); include ContentData::$subtemplatePath."/".$incFile; $out = ob_get_contents(); ob_end_clean();
|
|
|
|
|
ob_start(); include $inc; $out = ob_get_contents(); ob_end_clean();
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $point
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getPoint(string $point) : string {
|
|
|
|
|
if (empty($point) || empty(ContentData::$content[$point])) return "";
|
|
|
|
|
return implode("", ContentData::$content[$point]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws AppException
|
|
|
|
|
*/
|
|
|
|
|
/** @inheritDoc */
|
|
|
|
|
public function getResponse(): string {
|
|
|
|
|
if (empty(ContentData::$template) || !file_exists(ContentData::$template)) throw new AppError("Invalid template file");
|
|
|
|
|
ob_start(); include ContentData::$template; $out = ob_get_contents(); ob_end_clean();
|
|
|
|
|
|