4 Commits
3.0.2 ... 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
4 changed files with 60 additions and 11 deletions

View File

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

View File

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

View File

@@ -5,6 +5,7 @@ namespace Rmphp\Content;
use Rmphp\Foundation\Exceptions\AppError;
use Rmphp\Foundation\TemplateInterface;
#[\AllowDynamicProperties]
class Content implements TemplateInterface {
/**
@@ -36,12 +37,57 @@ class Content implements TemplateInterface {
return ContentData::$subtemplatePath;
}
/**
* @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 AppError("Empty point");
if (empty(ContentData::$subtemplatePath))throw new AppError("SubtemplatePath is not defined");
ContentData::$content[$point][] = $string;
}
@@ -57,13 +103,13 @@ class Content implements TemplateInterface {
* @inheritDoc
*/
public function addSubtemplate(string $point, string $subtemplate, array $resource = []) : void {
if (empty(ContentData::$subtemplatePath))throw new AppError("SubtemplatePath is not defined");
if (empty($point)) throw new AppError("Empty point");
if (empty($subtemplate) || !file_exists(ContentData::$subtemplatePath.$subtemplate)) throw new AppError("Subtemplate ".ContentData::$subtemplatePath.$subtemplate. " is not found");
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.$subtemplate; ContentData::$content[$point][] = ob_get_contents(); ob_end_clean();
ob_start(); include $inc; ContentData::$content[$point][] = ob_get_contents(); ob_end_clean();
}
/**
@@ -74,15 +120,17 @@ class Content implements TemplateInterface {
$this->addSubtemplate($point, $subtemplate, $resource);
}
/**
* @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 AppError("Inc file ".$incFile." is not found");
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;
}

View File

@@ -8,6 +8,7 @@ class ContentData {
public static string $basePath = "";
public static string $template;
public static string $subtemplatePath;
public static array $subtemplatePathAlias = [];
public static array $content = [];
}