From e97946d454b2bcd95d4a3aa5a745c960118c0c5e Mon Sep 17 00:00:00 2001 From: tutida Date: Fri, 22 Jul 2016 23:00:29 +0900 Subject: [PATCH] initial commit --- LICENSE | 21 ++++ README.md | 83 +++++++++++++ composer.json | 23 ++++ phpunit.xml.dist | 43 +++++++ src/Controller/Component/PackComponent.php | 130 +++++++++++++++++++++ src/View/Helper/PackHelper.php | 76 ++++++++++++ 6 files changed, 376 insertions(+) create mode 100755 LICENSE create mode 100755 README.md create mode 100755 composer.json create mode 100755 phpunit.xml.dist create mode 100755 src/Controller/Component/PackComponent.php create mode 100755 src/View/Helper/PackHelper.php diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..c62e51b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100755 index 0000000..23d3fff --- /dev/null +++ b/README.md @@ -0,0 +1,83 @@ +# Pack plugin is for CakePHP3 + +You can easy to pass CakePHP3 variables to JS in View. + +## Requirements ## + +* PHP >=5.4 +* CakePHP >= ~3.2 + +## Installation + +In bootstrap.php. + +```php +loadComponent('Pack.Pack'); + } + ... + } +``` + +In layout ctp or template ctp. + +```php + Pack->render();?> +``` + +## Usage + +Just set variables in your controller. +``` +Hoge->get($id); + $array = [...]; + + $this->Pack->set('entity', $entity); + $this->Pack->set('array', $array); + + ## OR ## + $this->Pack->set(compact('entity', 'array')); + +``` + +Just get the variables in your JS in view. +```js + Pack.entity; + Pack.array; +``` + + +## Methods + +1. set($varName, $variable) … Set variable in Pack. +2. remove($varNamee) … Remove variable in Pack. +3. show() … Show all variable in Pack. +4. rename($namespace) … Change Pack's namespace in JS. + +example + +In controller +```php + $this->Pack->rename('Hoge'); + $this->Pack->set('array', $array); +``` + +In js +```js + Hoge.array; +``` + diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..2b1ce76 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "tutida/Pack", + "description": "CakePHP3 variables in JS", + "type": "cakephp-plugin", + "require": { + "php": ">=5.4.16", + "cakephp/cakephp": "~3.0" + }, + "require-dev": { + "phpunit/phpunit": "*" + }, + "autoload": { + "psr-4": { + "Pack\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Pack\\Test\\": "tests", + "Cake\\Test\\": "./vendor/cakephp/cakephp/tests" + } + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100755 index 0000000..85b420f --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,43 @@ + + + + + + + + + + + ./tests/TestCase + + + + + + + + + + + + + + + + ./vendor/ + ./vendor/ + + ./tests/ + ./tests/ + + + + diff --git a/src/Controller/Component/PackComponent.php b/src/Controller/Component/PackComponent.php new file mode 100755 index 0000000..2fec0c9 --- /dev/null +++ b/src/Controller/Component/PackComponent.php @@ -0,0 +1,130 @@ + '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 + */ + public function beforeRender(Event $event) + { + $namespace = $this->config('namespace'); + + $variables = []; + if (isset($this->variables[$namespace])) { + $variables = $this->variables[$namespace]; + } + + $event->subject->helpers += [ + 'Pack.Pack' => [ + 'namespace' => $namespace, + 'variables' => $variables, + ] + ]; + } + + + /** + * rename + */ + public function rename($namespace = null) + { + $this->config('namespace', $namespace); + } + + /** + * set + */ + public function set($varName, $data = null) + { + if (is_array($varName)) { + foreach ($varName as $key => $data) { + $this->variableSet($key, $data); + } + } else { + $this->variableSet($varName, $data); + } + } + + /** + * unset + */ + public function remove($varName) + { + $namespace = $this->config('namespace'); + + if (isset($this->variables[$namespace][$varName])) { + unset($this->variables[$namespace][$varName]); + + return true; + } + return false; + } + + /** + * show + */ + public function show() + { + return $this->variables; + } + + /** + * variableSet + */ + private function variableSet($varName, $data) + { + $namespace = $this->config('namespace'); + + $data = $this->json_safe_encode($data); + + $this->variables[$namespace][$varName] = [$data]; + } + + + /** + * json_safe_encode + */ + private function json_safe_encode($data) + { + return json_encode($data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); + } +} diff --git a/src/View/Helper/PackHelper.php b/src/View/Helper/PackHelper.php new file mode 100755 index 0000000..845e96c --- /dev/null +++ b/src/View/Helper/PackHelper.php @@ -0,0 +1,76 @@ + 'Pack', + 'variables' => [] + ]; + + /** + * blocks. + * + * @var array + */ + private $blocks = [ + 'javascriptstart' => '', + ]; + + /** + * render + */ + public function render() + { + $scripts = ''; + $config = $this->config(); + + if (empty($config['variables'])) { + return $scripts; + } + + $scripts .= $this->blocks['javascriptstart']; + + $scripts .= $this->renderWrap($config['namespace']); + + $scripts .= $this->renderVariables($config['namespace'], $config['variables']); + + $scripts .= $this->blocks['javascriptend']; + + return $scripts; + } + + /** + * renderWrap + */ + private function renderWrap($namespace) + { + return "window.{$namespace}={};"; + } + + /** + * renderVariables + */ + private function renderVariables($namespace, $variables) + { + $jsVars = ''; + + foreach ($variables as $key => $var) { + $jsVars .= "{$namespace}.{$key} = {$var[0]};"; + } + + return $jsVars; + } +}