-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e97946d
Showing
6 changed files
with
376 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
<?php | ||
Plugin::load('Search'); | ||
``` | ||
|
||
In controller. | ||
|
||
```php | ||
<?php | ||
class AppController extends Controller | ||
{ | ||
|
||
public function initialize() | ||
{ | ||
$this->loadComponent('Pack.Pack'); | ||
} | ||
... | ||
} | ||
``` | ||
|
||
In layout ctp or template ctp. | ||
|
||
```php | ||
<?= $this->Pack->render();?> | ||
``` | ||
|
||
## Usage | ||
|
||
Just set variables in your controller. | ||
``` | ||
<?php | ||
$entity = $this->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; | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
colors="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="./tests/bootstrap.php" | ||
> | ||
<php> | ||
<ini name="memory_limit" value="-1"/> | ||
<ini name="apc.enable_cli" value="1"/> | ||
</php> | ||
|
||
<!-- Add any additional test suites you want to run here --> | ||
<testsuites> | ||
<testsuite name="Pack Test Suite"> | ||
<directory>./tests/TestCase</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<!-- Setup a listener for fixtures --> | ||
<listeners> | ||
<listener | ||
class="\Cake\TestSuite\Fixture\FixtureInjector" | ||
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php"> | ||
<arguments> | ||
<object class="\Cake\TestSuite\Fixture\FixtureManager" /> | ||
</arguments> | ||
</listener> | ||
</listeners> | ||
|
||
<!-- Prevent coverage reports from looking in tests and vendors --> | ||
<filter> | ||
<blacklist> | ||
<directory suffix=".php">./vendor/</directory> | ||
<directory suffix=".ctp">./vendor/</directory> | ||
|
||
<directory suffix=".php">./tests/</directory> | ||
<directory suffix=".ctp">./tests/</directory> | ||
</blacklist> | ||
</filter> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
namespace Pack\Controller\Component; | ||
|
||
use Cake\Event\Event; | ||
use Cake\Controller\Component; | ||
use Cake\Controller\ComponentRegistry; | ||
|
||
/** | ||
* Pack component | ||
*/ | ||
class PackComponent extends Component | ||
{ | ||
/** | ||
* Default configuration. | ||
* | ||
* @var array | ||
*/ | ||
protected $_defaultConfig = [ | ||
'namespace' => '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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
namespace Pack\View\Helper; | ||
|
||
use Cake\View\Helper; | ||
use Cake\View\View; | ||
|
||
/** | ||
* Escape helper | ||
*/ | ||
class PackHelper extends Helper | ||
{ | ||
/** | ||
* Default configuration. | ||
* | ||
* @var array | ||
*/ | ||
protected $_defaultConfig = [ | ||
'namespace' => 'Pack', | ||
'variables' => [] | ||
]; | ||
|
||
/** | ||
* blocks. | ||
* | ||
* @var array | ||
*/ | ||
private $blocks = [ | ||
'javascriptstart' => '<script>', | ||
'javascriptend' => '</script>', | ||
]; | ||
|
||
/** | ||
* 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; | ||
} | ||
} |