Skip to content

Commit

Permalink
Moving loaded helpers into a protected array.
Browse files Browse the repository at this point in the history
Adding magic methods to access the object array.
  • Loading branch information
markstory committed Aug 11, 2010
1 parent c78e869 commit fcbfb55
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
30 changes: 30 additions & 0 deletions cake/libs/object_collection.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
12 changes: 6 additions & 6 deletions cake/libs/view/helper_collection.php
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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];
}

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

Expand Down

0 comments on commit fcbfb55

Please sign in to comment.