Skip to content
Permalink
Browse files

Moving loaded helpers into a protected array.

Adding magic methods to access the object array.
  • Loading branch information...
markstory committed Jul 4, 2010
1 parent c78e869 commit fcbfb556c5fb83ae5bc98938f133189411b0ce78
Showing with 36 additions and 6 deletions.
  1. +30 −0 cake/libs/object_collection.php
  2. +6 −6 cake/libs/view/helper_collection.php
@@ -35,6 +35,13 @@ abstract class ObjectCollection {
*/
protected $_disabled = array();
/**
* A hash of loaded helpers, indexed by the classname
*
* @var array
*/
protected $_loaded = array();
/**
* Loads a new object onto the collection. Can throw a variety of exceptions
*
@@ -96,6 +103,29 @@ public function trigger($callback, $params = array(), $options = array()) {
return true;
}
/**
* Provide public read access to the loaded objects
*
* @param string $name Name of property to read
* @return mixed
*/
public function __get($name) {
if (isset($this->_loaded[$name])) {
return $this->_loaded[$name];
}
return null;
}
/**
* Provide isset access to _loaded
*
* @param sting $name Name of object being checked.
* @return boolean
*/
public function __isset($name) {
return isset($this->_loaded[$name]);
}
/**
* Enables callbacks on a behavior or array of behaviors
*
@@ -48,8 +48,8 @@ public function __construct(View $view) {
public function load($helper, $settings = array(), $enable = true) {
list($plugin, $name) = pluginSplit($helper, true);
if (isset($this->{$name})) {
return $this->{$name};
if (isset($this->_loaded[$name])) {
return $this->_loaded[$name];
}
$helperClass = $name . 'Helper';
if (!class_exists($helperClass)) {
@@ -60,11 +60,11 @@ public function load($helper, $settings = array(), $enable = true) {
throw new MissingHelperClassException($helperClass);
}
}
$this->{$name} = new $helperClass($this->_View, $settings);
$this->_loaded[$name] = new $helperClass($this->_View, $settings);
$vars = array('base', 'webroot', 'here', 'params', 'action', 'data', 'theme', 'plugin');
foreach ($vars as $var) {
$this->{$name}->{$var} = $this->_View->{$var};
$this->_loaded[$name]->{$var} = $this->_View->{$var};
}
if (!in_array($name, $this->_attached)) {
@@ -73,7 +73,7 @@ public function load($helper, $settings = array(), $enable = true) {
if ($enable === false) {
$this->_disabled[] = $name;
}
return $this->{$name};
return $this->_loaded[$name];
}
/**
@@ -84,7 +84,7 @@ public function load($helper, $settings = array(), $enable = true) {
*/
public function unload($name) {
list($plugin, $name) = pluginSplit($name);
unset($this->{$name});
unset($this->_loaded[$name]);
$this->_attached = array_values(array_diff($this->_attached, (array)$name));
}

0 comments on commit fcbfb55

Please sign in to comment.
You can’t perform that action at this time.