Skip to content

Commit

Permalink
Merge pull request #1 from tutida/modify_pass_variables
Browse files Browse the repository at this point in the history
Changed the way for passing variables
  • Loading branch information
tutida authored Aug 2, 2016
2 parents a971e60 + 35a95d2 commit 1c63af4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 44 deletions.
49 changes: 11 additions & 38 deletions src/Controller/Component/PackComponent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Pack\Controller\Component;

use Pack\Statics\PackVariables;
use Cake\Event\Event;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
Expand All @@ -19,49 +20,18 @@ class PackComponent extends Component
'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];
}
$variables = PackVariables::getAll();

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


/**
* rename
*/
Expand All @@ -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;
}

Expand All @@ -104,7 +77,7 @@ public function remove($varName)
*/
public function show()
{
return $this->variables;
return PackVariables::getAll();
}

/**
Expand All @@ -116,7 +89,7 @@ private function variableSet($varName, $data)

$data = $this->json_safe_encode($data);

$this->variables[$namespace][$varName] = [$data];
PackVariables::set($varName, $data);
}


Expand Down
48 changes: 48 additions & 0 deletions src/Statics/PackVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Pack\Statics;

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

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

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

/**
* get
*/
public function get($varName = null)
{
if (!isset(self::$variables[$varName])) {
return null;
}

return self::$variables[$varName];
}

/**
* getAll
*/
public function getAll()
{
return self::$variables;
}

}
13 changes: 7 additions & 6 deletions src/View/Helper/PackHelper.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
namespace Pack\View\Helper;

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

/**
* Escape helper
* Pack helper
*/
class PackHelper extends Helper
{
Expand All @@ -16,7 +17,6 @@ class PackHelper extends Helper
*/
protected $_defaultConfig = [
'namespace' => 'Pack',
'variables' => []
];

/**
Expand All @@ -34,18 +34,19 @@ 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;
}

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

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

$scripts .= $this->renderVariables($config['namespace'], $config['variables']);
$scripts .= $this->renderVariables($config['namespace'], $variables);

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

Expand Down

0 comments on commit 1c63af4

Please sign in to comment.