Moving loaded helpers into a protected array.

Adding magic methods to access the object array.
This commit is contained in:
mark_story 2010-07-04 00:29:28 -04:00
parent c78e869be6
commit fcbfb556c5
2 changed files with 36 additions and 6 deletions

View file

@ -35,6 +35,13 @@ abstract class ObjectCollection {
*/ */
protected $_disabled = array(); 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 * Loads a new object onto the collection. Can throw a variety of exceptions
* *
@ -96,6 +103,29 @@ abstract class ObjectCollection {
return true; 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 * Enables callbacks on a behavior or array of behaviors
* *

View file

@ -48,8 +48,8 @@ class HelperCollection extends ObjectCollection {
public function load($helper, $settings = array(), $enable = true) { public function load($helper, $settings = array(), $enable = true) {
list($plugin, $name) = pluginSplit($helper, true); list($plugin, $name) = pluginSplit($helper, true);
if (isset($this->{$name})) { if (isset($this->_loaded[$name])) {
return $this->{$name}; return $this->_loaded[$name];
} }
$helperClass = $name . 'Helper'; $helperClass = $name . 'Helper';
if (!class_exists($helperClass)) { if (!class_exists($helperClass)) {
@ -60,11 +60,11 @@ class HelperCollection extends ObjectCollection {
throw new MissingHelperClassException($helperClass); 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'); $vars = array('base', 'webroot', 'here', 'params', 'action', 'data', 'theme', 'plugin');
foreach ($vars as $var) { foreach ($vars as $var) {
$this->{$name}->{$var} = $this->_View->{$var}; $this->_loaded[$name]->{$var} = $this->_View->{$var};
} }
if (!in_array($name, $this->_attached)) { if (!in_array($name, $this->_attached)) {
@ -73,7 +73,7 @@ class HelperCollection extends ObjectCollection {
if ($enable === false) { if ($enable === false) {
$this->_disabled[] = $name; $this->_disabled[] = $name;
} }
return $this->{$name}; return $this->_loaded[$name];
} }
/** /**
@ -84,7 +84,7 @@ class HelperCollection extends ObjectCollection {
*/ */
public function unload($name) { public function unload($name) {
list($plugin, $name) = pluginSplit($name); list($plugin, $name) = pluginSplit($name);
unset($this->{$name}); unset($this->_loaded[$name]);
$this->_attached = array_values(array_diff($this->_attached, (array)$name)); $this->_attached = array_values(array_diff($this->_attached, (array)$name));
} }