18 Commits
1.1 ... 4.x

Author SHA1 Message Date
User
74a8b51047 20250514#1 2025-05-14 13:52:00 +03:00
User
1e8457567a 20250302#6 2025-03-02 21:51:53 +03:00
User
8e62519dfd 20250112#1 2025-01-12 22:44:11 +03:00
User
ed5b931e59 20240429#1 2024-04-29 19:47:40 +03:00
User
d09bd2836a 20240410#12 2024-04-10 03:49:52 +03:00
User
e96bc71eff 20240410#11 2024-04-10 03:46:37 +03:00
User
3b4836bb5e 20240410#10 2024-04-10 03:42:03 +03:00
User
94ada77e89 20240410#9 2024-04-10 03:39:40 +03:00
User
bbaf319dc8 20240410#7 2024-04-10 03:27:21 +03:00
User
763228a883 20240410#6 2024-04-10 03:21:30 +03:00
User
a576bf0638 20240410#5 2024-04-10 02:54:51 +03:00
User
4ed91f096c 20240410#3 2024-04-10 02:31:59 +03:00
User
45f4364c33 20240410#1 2024-04-10 02:23:57 +03:00
User
cf3a11cb62 20231008#3 2023-10-08 20:06:59 +03:00
User
7b3228d14e 20231008#2 2023-10-08 19:12:28 +03:00
User
5b967d6439 20231008#1 2023-10-08 18:57:09 +03:00
User
37d3884205 20231007#3 2023-10-07 17:55:16 +03:00
User
93246556e0 20231007#2 2023-10-07 16:52:39 +03:00
4 changed files with 87 additions and 96 deletions

View File

@@ -10,12 +10,12 @@ Stable version
composer require rmphp/content
```
```bash
composer require rmphp/content:"^1.1"
composer require rmphp/content:"^4.0"
```
Dev version contains the latest changes
```bash
composer require rmphp/content:"1.x-dev"
composer require rmphp/content:"4.x-dev"
```

View File

@@ -9,7 +9,7 @@
],
"require": {
"php": "^8.1",
"rmphp/foundation": "^1.0"
"rmphp/foundation": "^3.0"
},
"autoload": {
"psr-4": {

View File

@@ -3,18 +3,11 @@
namespace Rmphp\Content;
use Rmphp\Foundation\Exceptions\AppError;
use Rmphp\Foundation\Exceptions\AppException;
use Rmphp\Foundation\TemplateInterface;
#[\AllowDynamicProperties]
class Content implements TemplateInterface {
private array $content = [];
private ContentData $data;
private ContentData $dataGlobal;
private string $basePath = "";
private string $template;
private string $subtemplatePath;
/**
* Content constructor.
* @param string $template
@@ -23,129 +16,134 @@ 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 {
if(empty($this->data)) $this->data = new ContentData();
foreach ($resource as $resKey => $resVal){
$this->data->{$resKey} = $resVal;
$this->{$resKey} = $resVal;
}
$this->basePath = dirname(__DIR__, 4);
$this->template = $this->basePath.'/'.$template;
ContentData::$basePath = dirname(__DIR__, 4);
ContentData::$template = ContentData::$basePath.$template;
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
*/
public function setSubtemplePath(string $subtemplatePath) : TemplateInterface {
$this->subtemplatePath = $this->basePath.'/'.$subtemplatePath;
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 getSubtemplePath(): string {
return $this->subtemplatePath;
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;
}
}
/**
* @param string $point
* @param string $string
* @throws AppException
* @inheritDoc
*/
public function addValue(string $point, string $string) : void {
if (empty($point)) throw new AppException("Empty point");
if (empty($this->subtemplatePath))throw new AppException("SubtemplatePath is not defined");
$this->content[$point][] = $string;
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($this->content[$point]);
unset(ContentData::$content[$point]);
$this->addValue($point, $string);
}
/**
* @param string $point
* @param string $subTempl
* @param array $resource
* @throws AppException
* @inheritDoc
*/
public function addSubtemple(string $point, string $subTempl, array $resource = []) : void {
if (empty($this->subtemplatePath))throw new AppException("SubtemplatePath is not defined");
if (empty($point)) throw new AppException("Empty point");
if (empty($subTempl) || !file_exists($this->subtemplatePath."/".$subTempl)) throw new AppException($this->subtemplatePath."/".$subTempl. " is not found");
if(empty($this->data)) $this->data = new ContentData();
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->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
* @param string $subTempl
* @param array $resource
* @throws AppException
* @inheritDoc
*/
public function setSubtemple(string $point, string $subTempl, array $resource = []) : void {
unset($this->content[$point]);
$this->addSubtemple($point, $subTempl, $resource);
public function setSubtemplate(string $point, string $subtemplate, array $resource = []) : void {
unset(ContentData::$content[$point]);
$this->addSubtemplate($point, $subtemplate, $resource);
}
/**
* @param array $globals
*/
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
* @inheritDoc
*/
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){
$this->data->{$resKey} = $resVal;
$this->{$resKey} = $resVal;
}
if(empty($incFile) || !file_exists($this->subtemplatePath."/".$incFile)) throw new AppException("Empty inc file");
ob_start(); include $this->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($this->content[$point])) return "";
return implode("", $this->content[$point]);
if (empty($point) || empty(ContentData::$content[$point])) return "";
return implode("", ContentData::$content[$point]);
}
/**
* @return string
* @throws AppException
*/
/** @inheritDoc */
public function getResponse(): string {
if (empty($this->template) || !file_exists($this->template)) throw new AppError("Invalid template file");
ob_start(); include $this->template; $out = ob_get_contents(); ob_end_clean();
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();
return $out;
}
}

View File

@@ -5,17 +5,10 @@ namespace Rmphp\Content;
class ContentData {
private array $data;
public static string $basePath = "";
public static string $template;
public static string $subtemplatePath;
public static array $subtemplatePathAlias = [];
public static array $content = [];
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __isset($name) {
return isset($this->data[$name]);
}
public function __get($name) {
return $this->data[$name] ?? null;
}
}