From b241919770d1f39db3586db773b9f2f81acb2564 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Mon, 20 May 2024 22:14:41 -0300 Subject: [PATCH] Some improvements in controller --- system/engine/action.php | 14 +++++----- system/engine/actionsystem.php | 4 +-- system/engine/controller.php | 28 +++++++++++++++---- system/engine/interfaces/action.php | 2 ++ system/engine/interfaces/common/registers.php | 2 ++ system/engine/interfaces/controller.php | 5 ++++ system/engine/render.php | 6 ++-- system/templateEngines/Twig/autoload.php | 14 ++++++++++ 8 files changed, 57 insertions(+), 18 deletions(-) diff --git a/system/engine/action.php b/system/engine/action.php index d3fec81..a615249 100644 --- a/system/engine/action.php +++ b/system/engine/action.php @@ -49,7 +49,7 @@ class Action implements ActionInterface { $parts = explode('/', str_replace('../', '', (string)$route)); $parts = array_map(function ($part) { - return preg_replace('/[^a-zA-Z0-9_]/', '', $part); + return preg_replace(self::REGEX_ROUTE_SANITIZE, '', $part); }, $parts); foreach ($parts as $part) { @@ -66,11 +66,11 @@ class Action implements ActionInterface { if (is_file(Config::DIR_SYSTEM() . '' . str_replace('../', '', $path) . '.php')) { $this->file = Config::DIR_SYSTEM() . '' . str_replace('../', '', $path) . '.php'; - $this->class = 'System' . preg_replace('/[^a-zA-Z0-9]/', '', $path); + $this->class = 'System' . preg_replace(self::REGEX_CLASS_SANITIZE, '', $path); $this->classAlt = [ 'legacy' => $this->class, - 'direct' => preg_replace('/[^a-zA-Z0-9]/', '', $part) + 'direct' => preg_replace(self::REGEX_CLASS_SANITIZE, '', $part) ]; array_shift($parts); @@ -101,7 +101,7 @@ class Action implements ActionInterface { $parts = explode('/', str_replace('../', '', (string)$route)); $parts = array_map(function($part){ - return preg_replace('/[^a-zA-Z0-9_]/', '', $part); + return preg_replace(self::REGEX_ROUTE_SANITIZE, '', $part); }, $parts); $this->route = $route; @@ -174,7 +174,7 @@ class Action implements ActionInterface { $possibleFile = null; $modulesPrepared = array_map(function ($item) { //return \Phacil\Framework\Registry::case_insensitive_pattern($item); - return preg_replace('/[^a-zA-Z0-9_]/', '', $item); + return preg_replace(self::REGEX_ROUTE_SANITIZE, '', $item); }, $modules); array_splice(($modulesPrepared), $position, 0, 'controller'); $modulesWithoutLast = $modulesPrepared; @@ -184,7 +184,7 @@ class Action implements ActionInterface { $possibleFile = Config::DIR_APPLICATION() . implode("/", $modulesPrepared) . ".php"; if(is_file($possibleFile)) { return [ - 'class' => preg_replace('/[^a-zA-Z0-9]/', '', 'Controller'.implode("", $modules)), + 'class' => preg_replace(self::REGEX_CLASS_SANITIZE, '', 'Controller'.implode("", $modules)), 'file' => $possibleFile, 'method' => 'index' ]; @@ -196,7 +196,7 @@ class Action implements ActionInterface { $modulesTxtWithoutLast = $modules; array_pop($modulesTxtWithoutLast); return [ - 'class' => preg_replace('/[^a-zA-Z0-9]/', '', 'Controller'.implode("", $modulesTxtWithoutLast)), + 'class' => preg_replace(self::REGEX_CLASS_SANITIZE, '', 'Controller'.implode("", $modulesTxtWithoutLast)), 'file' => $possibleFile, 'method' => end($modules) ]; diff --git a/system/engine/actionsystem.php b/system/engine/actionsystem.php index 0a34b44..238c408 100644 --- a/system/engine/actionsystem.php +++ b/system/engine/actionsystem.php @@ -48,11 +48,11 @@ final class ActionSystem implements ActionInterface if (is_file(Config::DIR_SYSTEM() . '' . str_replace('../', '', $path) . '.php')) { $this->file = Config::DIR_SYSTEM() . '' . str_replace('../', '', $path) . '.php'; - $this->class = 'System' . preg_replace('/[^a-zA-Z0-9]/', '', $path); + $this->class = 'System' . preg_replace(self::REGEX_CLASS_SANITIZE, '', $path); $this->classAlt = [ 'legacy' => $this->class, - 'direct' => preg_replace('/[^a-zA-Z0-9]/', '', $part) + 'direct' => preg_replace(self::REGEX_CLASS_SANITIZE, '', $part) ]; array_shift($parts); diff --git a/system/engine/controller.php b/system/engine/controller.php index 1a7a4c0..1eef0a5 100644 --- a/system/engine/controller.php +++ b/system/engine/controller.php @@ -281,8 +281,8 @@ abstract class Controller implements \Phacil\Framework\Interfaces\Controller { */ protected function render() { - foreach ($this->children as $child) { - $this->data[basename($child)] = $this->getChild($child); + foreach ($this->children as $key => $child) { + $this->data[(is_string($key) ? $key : basename($child))] = $this->getChild($child); } /** @@ -406,20 +406,36 @@ abstract class Controller implements \Phacil\Framework\Interfaces\Controller { } /** - * @param bool $commonChildren (optional) Whether to include the common children + * @param bool $commonChildren (optional) Whether to include the common children (common/header and common/footer) * @return \Phacil\Framework\Response * @throws Exception * @since 1.1.0 */ protected function out ($commonChildren = true) { if($commonChildren === true){ - $this->children = array_merge(array( - 'common/footer', - 'common/header'), $this->children + $this->children = array_merge( + self::DEFAULT_CHILDREN, + $this->children ); } return $this->response->setOutput($this->render()); } + + /** + * Add child to render + * + * @param string $route Route for render child + * @param string $name (Optional) Name for use as variable in template. If empty, the last word of route path will be used as the name. + * @return $this + */ + protected function addChild($route, $name = null) { + if(!empty($name) && is_string($name)){ + $this->children[$name] = $route; + return $this; + } + $this->children[] = $route; + return $this; + } } diff --git a/system/engine/interfaces/action.php b/system/engine/interfaces/action.php index fc4b9ba..9977e82 100644 --- a/system/engine/interfaces/action.php +++ b/system/engine/interfaces/action.php @@ -19,6 +19,8 @@ interface Action { const APP = "APP"; const SYSTEM = "SYSTEM"; const MAX_ROUTE_LENGTH = 255; + const REGEX_CLASS_SANITIZE = '/[^a-zA-Z0-9]/'; + const REGEX_ROUTE_SANITIZE = '/[^a-zA-Z0-9_]/'; /** * @param string $route HTTP route for the respective controller diff --git a/system/engine/interfaces/common/registers.php b/system/engine/interfaces/common/registers.php index b6a33ba..277a316 100644 --- a/system/engine/interfaces/common/registers.php +++ b/system/engine/interfaces/common/registers.php @@ -31,8 +31,10 @@ namespace Phacil\Framework\Interfaces\Common; interface Registers { /** + * Get an instance of this class * * @return $this + * @since 2.0.0 */ static public function getInstance(); diff --git a/system/engine/interfaces/controller.php b/system/engine/interfaces/controller.php index e914a0f..85b5d4d 100644 --- a/system/engine/interfaces/controller.php +++ b/system/engine/interfaces/controller.php @@ -19,6 +19,11 @@ interface Controller extends \Phacil\Framework\Interfaces\Common\Registers { const TEMPLATE_AREA_THEME = 2; const TEMPLATE_AREA_BOTH = 3; + const DEFAULT_CHILDREN = array( + "footer" => 'common/footer', + "header" => 'common/header' + ); + /** * * {@inheritdoc} diff --git a/system/engine/render.php b/system/engine/render.php index 0fc2de4..5c9be74 100644 --- a/system/engine/render.php +++ b/system/engine/render.php @@ -101,8 +101,8 @@ * @param array $extras * @return $this */ - public function setTemplate($templateType, $templatePath, $template, $data, $extras) { - $this->data = $data; + public function setTemplate($templateType, $templatePath, $template, &$data, &$extras) { + $this->data = &$data; $this->template = $template; @@ -110,7 +110,7 @@ $this->templateType = $templateType; - $this->extras = $extras; + $this->extras = &$extras; return $this; } diff --git a/system/templateEngines/Twig/autoload.php b/system/templateEngines/Twig/autoload.php index 7c664aa..2ea5068 100644 --- a/system/templateEngines/Twig/autoload.php +++ b/system/templateEngines/Twig/autoload.php @@ -8,6 +8,10 @@ namespace Phacil\Framework\templateEngines; +/** + * @since 2.0.0 + * @package Phacil\Framework\templateEngines + */ class Twig { static private $TwigFolderLoad; @@ -22,6 +26,7 @@ class Twig { static private $registered; + /** @return void */ public static function registryTwigVersion() { if(self::$registered) return; @@ -55,11 +60,20 @@ class Twig { self::$registered = true; } + /** + * @param string $var + * @param string $value + * @return void + */ protected static function define($var, $value) { if(property_exists(__CLASS__, $var)) self::$$var = $value; } + /** + * @param string $var + * @return mixed + */ public static function getVar($var) { return property_exists(__CLASS__, $var) ? self::$$var : null; }