From dfb3986075aa0922d973a901f6f5ac178140c63b Mon Sep 17 00:00:00 2001 From: User Date: Mon, 29 May 2023 02:28:08 +0300 Subject: [PATCH] Init --- .gitignore | 3 + README.md | 4 ++ composer.json | 20 ++++++ src/Content.php | 149 ++++++++++++++++++++++++++++++++++++++++++++ src/ContentData.php | 21 +++++++ 5 files changed, 197 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Content.php create mode 100644 src/ContentData.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31dc5a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +/vendor +composer.lock \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..584fd5a --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +### Rmphp/Content + +Template component for **Rmphp** + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6e0a1ab --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "rmphp/content", + "license": "proprietary", + "authors": [ + { + "name": "Yuri Zuev", + "email": "y_zuev@mail.ru" + } + ], + "require": { + "php": "^8.1", + "rmphp/foundation": "1.0.x-dev" + }, + "autoload": { + "psr-4": { + "Rmphp\\Content\\": "src/" + } + } + +} diff --git a/src/Content.php b/src/Content.php new file mode 100644 index 0000000..2a2bb55 --- /dev/null +++ b/src/Content.php @@ -0,0 +1,149 @@ +setTemplate($template); + } + + /** + * @param string $template + * @param array $resource + * @return TemplateInterface + */ + 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->basePath = dirname(__DIR__, 4); + $this->template = $this->basePath.'/'.$template; + return $this; + } + + /** + * @param string $subtemplatePath + * @return TemplateInterface + */ + public function setSubtemplePath(string $subtemplatePath) : TemplateInterface { + $this->subtemplatePath = $this->basePath.'/'.$subtemplatePath; + return $this; + } + + /** + * @return string + */ + public function getSubtemplePath(): string { + return $this->subtemplatePath; + } + + + /** + * @param string $point + * @param string $string + * @throws AppException + */ + 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; + } + + /** + * @param string $point + * @param string $string + * @throws AppException + */ + public function setValue(string $point, string $string) : void { + unset($this->content[$point]); + $this->addValue($point, $string); + } + + /** + * @param string $point + * @param string $subTempl + * @param array $resource + * @throws AppException + */ + 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(); + foreach ($resource as $resKey => $resVal){ + $this->data->{$resKey} = $resVal; + } + ob_start(); include $this->subtemplatePath."/".$subTempl; $this->content[$point][] = ob_get_contents(); ob_end_clean(); + } + + /** + * @param string $point + * @param string $subTempl + * @param array $resource + * @throws AppException + */ + public function setSubtemple(string $point, string $subTempl, array $resource = []) : void { + unset($this->content[$point]); + $this->addSubtemple($point, $subTempl, $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 + */ + public function inc(string $incFile, array $resource = []) : string { + if(empty($this->data)) $this->data = new ContentData(); + foreach ($resource as $resKey => $resVal){ + $this->data->{$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(); + return $out; + } + + /** + * @param string $point + * @return string + */ + public function getPoint(string $point) : string { + if (empty($point) || empty($this->content[$point])) return ""; + return implode("", $this->content[$point]); + } + + /** + * @return string + */ + public function getResponse(): string { + if (empty($this->template) || !file_exists($this->template)) return ''; + ob_start(); include $this->template; $out = ob_get_contents(); ob_end_clean(); + return $out; + } +} \ No newline at end of file diff --git a/src/ContentData.php b/src/ContentData.php new file mode 100644 index 0000000..e066224 --- /dev/null +++ b/src/ContentData.php @@ -0,0 +1,21 @@ +data[$name] = $value; + } + + public function __isset($name) { + return isset($this->data[$name]); + } + + public function __get($name) { + return $this->data[$name] ?? null; + } +} \ No newline at end of file