Skip to content

Commit

Permalink
Merge pull request #2 from tutida/code_refactoring
Browse files Browse the repository at this point in the history
code refactoring
  • Loading branch information
tutida authored Sep 7, 2016
2 parents 1c63af4 + 80ca268 commit 77d9592
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 127 deletions.
9 changes: 0 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@
"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"
}
}
}
43 changes: 0 additions & 43 deletions phpunit.xml.dist

This file was deleted.

76 changes: 40 additions & 36 deletions src/Controller/Component/PackComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,18 @@
use Pack\Statics\PackVariables;
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'
];

/**
* beforeRender
*/
public function beforeRender(Event $event)
{
$namespace = $this->config('namespace');

$variables = PackVariables::getAll();

$event->subject->helpers += ['Pack.Pack' => ['namespace' => $namespace]];
}

/**
* rename
*/
public function rename($namespace = null)
{
$this->config('namespace', $namespace);
$event->subject->helpers += ['Pack.Pack'];
}

/**
Expand All @@ -59,17 +37,14 @@ public function set($varName, $data = null)
*/
public function remove($varName)
{
$namespace = $this->config('namespace');

$variable = PackVariables::get($varName);

if (!is_null($variable)) {
PackVariables::remove($varName);

return true;
if (is_null($variable)) {
return false;
}

return false;
PackVariables::remove($varName);
return true;
}

/**
Expand All @@ -85,19 +60,48 @@ public function show()
*/
private function variableSet($varName, $data)
{
$namespace = $this->config('namespace');
PackVariables::set($varName, $data);
}

/**
* renameNamespace
*/
public function renameNamespace($namespace)
{
if (empty($namespace)) {
return false;
}

$data = $this->json_safe_encode($data);
PackVariables::renameNamespace($namespace);
return true;
}

PackVariables::set($varName, $data);
/**
* getNamespace
*/
public function getNamespace()
{
return PackVariables::getNamespace();
}

/**
* setScriptAttr
*/
public function setScriptAttr($attr)
{
if (empty($attr)) {
return false;
}

PackVariables::setScriptAttr($attr);
return true;
}

/**
* json_safe_encode
* getScriptAttr
*/
private function json_safe_encode($data)
public function getScriptAttr()
{
return json_encode($data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);
return PackVariables::getScriptAttr();
}
}
46 changes: 39 additions & 7 deletions src/Statics/PackVariables.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
<?php

namespace Pack\Statics;

/**
* Pack Variables
*/
class PackVariables
{
private static $variables = [];
private static $variables = [];
private static $namespace = 'Pack';
private static $scriptAttr = '';

/**
* set
*/
public function set($varName, $data = null)
public static function set($varName, $data)
{
self::$variables[$varName] = [$data];
self::$variables[$varName] = $data;
}

/**
* remove
*/
public function remove($varName)
public static function remove($varName)
{
unset(self::$variables[$varName]);
}

/**
* get
*/
public function get($varName = null)
public static function get($varName)
{
if (!isset(self::$variables[$varName])) {
return null;
Expand All @@ -40,9 +41,40 @@ public function get($varName = null)
/**
* getAll
*/
public function getAll()
public static function getAll()
{
return self::$variables;
}

/**
* renameNamespace
*/
public static function renameNamespace($namespace)
{
self::$namespace = $namespace;
}

/**
* getNamespace
*/
public static function getNamespace()
{
return self::$namespace;
}

/**
* setScriptAttr
*/
public static function setScriptAttr($scriptAttr)
{
self::$scriptAttr = $scriptAttr;
}

/**
* getScriptAttr
*/
public static function getScriptAttr()
{
return self::$scriptAttr;
}
}
58 changes: 26 additions & 32 deletions src/View/Helper/PackHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,69 @@

use Pack\Statics\PackVariables;
use Cake\View\Helper;
use Cake\View\View;
use Cake\View\StringTemplateTrait;

/**
* Pack helper
*/
class PackHelper extends Helper
{
use StringTemplateTrait;

/**
* Default configuration.
*
* @var array
*/
protected $_defaultConfig = [
'namespace' => 'Pack',
];

/**
* blocks.
*
* @var array
*/
private $blocks = [
'javascriptstart' => '<script>',
'javascriptend' => '</script>',
'templates' => [
'script' => '<script {{attr}}>window.{{namespace}}={};{{variables}}</script>'
],
];

/**
* render
*/
public function render()
{
$scripts = '';
$config = $this->config();
$variables = PackVariables::getAll();

if (empty($variables)) {
return $scripts;
}

$scripts .= $this->blocks['javascriptstart'];

$scripts .= $this->renderWrap($config['namespace']);

$scripts .= $this->renderVariables($config['namespace'], $variables);
$scripts = '';
$namespace = PackVariables::getNamespace();
$attr = PackVariables::getScriptAttr();

$scripts .= $this->blocks['javascriptend'];
$variables = $this->encodeVariables($namespace, $variables);
$scripts = $this->formatTemplate('script', [
'namespace' => $namespace,
'variables' => $variables,
'attr' => $attr
]);

return $scripts;
}

/**
* renderWrap
*/
private function renderWrap($namespace)
{
return "window.{$namespace}={};";
}

/**
* renderVariables
* encodeVariables
*/
private function renderVariables($namespace, $variables)
private function encodeVariables($namespace, $variables)
{
$jsVars = '';

foreach ($variables as $key => $var) {
$jsVars .= "{$namespace}.{$key} = {$var[0]};";
$jsVars .= "{$namespace}.{$key} = {$this->json_safe_encode($var)};";
}

return $jsVars;
}

/**
* 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);
}
}

0 comments on commit 77d9592

Please sign in to comment.