/** Gets a part, either from the cache or newly included and instantiated.
* The actual part class inclusion and instatiation itself is tried like this:
* - <pntType><partName> from application folder
* - <pntType><partName> from classes folder
* - <partName> from application folder
* - <partName> from classes folder
* Where <pntType> may be replaced by overriding ::getSpecificPartPrefix()
* @param array $args with 0 => $partName (string) and the rest (mixed) to be passed as extra parameters to the part constructor.
* @param boolean $cache wheather to try cache first and cache the part by $partName
* @return PntPagePart the part, from the cache if available, or newly included and instantiated
*/
function ($args, $cache=true) {
$partName = $args[0];
// try cache first
if ($cache) {
$key = $cache===true ? $partName : $cache;
if (isSet($this->parts[$key])) return $this->parts[$key];
}
$className = $this->($partName).$partName;
$included = $this->($className, $this->()); //does checkAlphaNumeric
if (!$included) {
$className = $partName;
$included = $this->($className, $this->());
}
if ($included) {
//2DO: use reflection, see http://www.phphulp.nl/php/forum/topic/class-instantieren-met-variabel-aantal-parameters/88097/
switch (count($args)) {
case 1: $part = new $className($this, $this->requestData); break;
case 2: $part = new $className($this, $this->requestData, $args[1]); break;
case 3: $part = new $className($this, $this->requestData, $args[1], $args[2]); break;
case 4: $part = new $className($this, $this->requestData, $args[1], $args[2], $args[3]); break;
case 5: $part = new $className($this, $this->requestData, $args[1], $args[2], $args[3], $args[4]); break;
case 6: $part = new $className($this, $this->requestData, $args[1], $args[2], $args[3], $args[4], $args[5]); break;
default: throw new PntError('too many part arguments: '. count($args));
}
if ($cache)
$this->parts[$key] = $part;
return $part;
} else {
return null;
}
}
|