3 Commits
2.x ... 3.0

Author SHA1 Message Date
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
3 changed files with 28 additions and 57 deletions

View File

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

View File

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

View File

@@ -3,7 +3,6 @@
namespace Rmphp\Content; namespace Rmphp\Content;
use Rmphp\Foundation\Exceptions\AppError; use Rmphp\Foundation\Exceptions\AppError;
use Rmphp\Foundation\Exceptions\AppException;
use Rmphp\Foundation\TemplateInterface; use Rmphp\Foundation\TemplateInterface;
class Content implements TemplateInterface { class Content implements TemplateInterface {
@@ -16,51 +15,38 @@ 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 {
foreach ($resource as $resKey => $resVal){ foreach ($resource as $resKey => $resVal){
$this->{$resKey} = $resVal; $this->{$resKey} = $resVal;
} }
ContentData::$basePath = dirname(__DIR__, 4); ContentData::$basePath = dirname(__DIR__, 4);
ContentData::$template = ContentData::$basePath.'/'.$template; ContentData::$template = ContentData::$basePath.$template;
return $this; return $this;
} }
/** /** @inheritDoc */
* @param string $subtemplatePath public function setSubtemplatePath(string $subtemplatePath = "") : TemplateInterface {
* @return TemplateInterface ContentData::$subtemplatePath = ContentData::$basePath.$subtemplatePath;
*/
public function setSubtemplePath(string $subtemplatePath) : TemplateInterface {
ContentData::$subtemplatePath = ContentData::$basePath.'/'.$subtemplatePath;
return $this; return $this;
} }
/** /** @inheritDoc */
* @return string public function getSubtemplatePath(): string {
*/
public function getSubtemplePath(): string {
return ContentData::$subtemplatePath; return ContentData::$subtemplatePath;
} }
/** /**
* @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(ContentData::$subtemplatePath))throw new AppException("SubtemplatePath is not defined"); if (empty(ContentData::$subtemplatePath))throw new AppError("SubtemplatePath is not defined");
ContentData::$content[$point][] = $string; ContentData::$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(ContentData::$content[$point]); unset(ContentData::$content[$point]);
@@ -68,60 +54,45 @@ class Content implements TemplateInterface {
} }
/** /**
* @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(ContentData::$subtemplatePath))throw new AppException("SubtemplatePath is not defined"); if (empty(ContentData::$subtemplatePath))throw new AppError("SubtemplatePath is not defined");
if (empty($point)) throw new AppException("Empty point"); if (empty($point)) throw new AppError("Empty point");
if (empty($subTempl) || !file_exists(ContentData::$subtemplatePath."/".$subTempl)) throw new AppException(ContentData::$subtemplatePath."/".$subTempl. " is not found"); if (empty($subtemplate) || !file_exists(ContentData::$subtemplatePath.$subtemplate)) throw new AppError(ContentData::$subtemplatePath.$subtemplate. " is not found");
foreach ($resource as $resKey => $resVal){ foreach ($resource as $resKey => $resVal){
$this->{$resKey} = $resVal; $this->{$resKey} = $resVal;
} }
ob_start(); include ContentData::$subtemplatePath."/".$subTempl; ContentData::$content[$point][] = ob_get_contents(); ob_end_clean(); ob_start(); include ContentData::$subtemplatePath.$subtemplate; 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(ContentData::$content[$point]); unset(ContentData::$content[$point]);
$this->addSubtemple($point, $subTempl, $resource); $this->addSubtemplate($point, $subtemplate, $resource);
} }
/** /**
* @param string $incFile * @inheritDoc
* @param array $resource
* @return string
* @throws AppException
*/ */
public function inc(string $incFile, array $resource = []) : string { public function inc(string $incFile, array $resource = []) : string {
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 AppException("Empty inc file"); if(empty($incFile) || !file_exists(ContentData::$subtemplatePath.$incFile)) throw new AppError("Empty inc file");
ob_start(); include ContentData::$subtemplatePath."/".$incFile; $out = ob_get_contents(); ob_end_clean(); ob_start(); include ContentData::$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(ContentData::$content[$point])) return ""; if (empty($point) || empty(ContentData::$content[$point])) return "";
return implode("", ContentData::$content[$point]); return implode("", ContentData::$content[$point]);
} }
/** /** @inheritDoc */
* @return string
* @throws AppException
*/
public function getResponse(): string { public function getResponse(): string {
if (empty(ContentData::$template) || !file_exists(ContentData::$template)) throw new AppError("Invalid template file"); 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(); ob_start(); include ContentData::$template; $out = ob_get_contents(); ob_end_clean();