From 35a95d2356cb08f24c66983b297d53797f4c4b0e Mon Sep 17 00:00:00 2001 From: tutida Date: Tue, 2 Aug 2016 22:54:39 +0900 Subject: [PATCH] Changed the way for passing variables --- src/Controller/Component/PackComponent.php | 49 +++++----------------- src/Statics/PackVariables.php | 48 +++++++++++++++++++++ src/View/Helper/PackHelper.php | 13 +++--- 3 files changed, 66 insertions(+), 44 deletions(-) create mode 100755 src/Statics/PackVariables.php diff --git a/src/Controller/Component/PackComponent.php b/src/Controller/Component/PackComponent.php index 2fec0c9..d966bd7 100755 --- a/src/Controller/Component/PackComponent.php +++ b/src/Controller/Component/PackComponent.php @@ -1,6 +1,7 @@ 'Pack' ]; - /** - * set variables - * - * @var array - */ - private $variables = []; - - /** - * controller object - * - * @var Cake\Controller\Controller - */ - private $controller; - - /** - * Initialize properties. - */ - public function initialize(array $config) - { - $this->controller = $this->_registry->getController(); - } - /** * beforeRender */ @@ -48,20 +27,11 @@ public function beforeRender(Event $event) { $namespace = $this->config('namespace'); - $variables = []; - if (isset($this->variables[$namespace])) { - $variables = $this->variables[$namespace]; - } + $variables = PackVariables::getAll(); - $event->subject->helpers += [ - 'Pack.Pack' => [ - 'namespace' => $namespace, - 'variables' => $variables, - ] - ]; + $event->subject->helpers += ['Pack.Pack' => ['namespace' => $namespace]]; } - /** * rename */ @@ -85,17 +55,20 @@ public function set($varName, $data = null) } /** - * unset + * remove */ public function remove($varName) { $namespace = $this->config('namespace'); - if (isset($this->variables[$namespace][$varName])) { - unset($this->variables[$namespace][$varName]); + $variable = PackVariables::get($varName); + + if (!is_null($variable)) { + PackVariables::remove($varName); return true; } + return false; } @@ -104,7 +77,7 @@ public function remove($varName) */ public function show() { - return $this->variables; + return PackVariables::getAll(); } /** @@ -116,7 +89,7 @@ private function variableSet($varName, $data) $data = $this->json_safe_encode($data); - $this->variables[$namespace][$varName] = [$data]; + PackVariables::set($varName, $data); } diff --git a/src/Statics/PackVariables.php b/src/Statics/PackVariables.php new file mode 100755 index 0000000..79b4c4c --- /dev/null +++ b/src/Statics/PackVariables.php @@ -0,0 +1,48 @@ + 'Pack', - 'variables' => [] ]; /** @@ -34,10 +34,11 @@ class PackHelper extends Helper */ public function render() { - $scripts = ''; - $config = $this->config(); + $scripts = ''; + $config = $this->config(); + $variables = PackVariables::getAll(); - if (empty($config['variables'])) { + if (empty($variables)) { return $scripts; } @@ -45,7 +46,7 @@ public function render() $scripts .= $this->renderWrap($config['namespace']); - $scripts .= $this->renderVariables($config['namespace'], $config['variables']); + $scripts .= $this->renderVariables($config['namespace'], $variables); $scripts .= $this->blocks['javascriptend'];