7 Commits
3.0.1 ... 4.0

Author SHA1 Message Date
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
4 changed files with 49 additions and 11 deletions

View File

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

View File

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

View File

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

View File

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