public static instance ( string $type, string $name, array $options = [] ) : object | ||
$type | string | The type of class as defined by `Libraries::$_paths`. |
$name | string | The un-namespaced name of the class to instantiate. |
$options | array | An array of constructor parameters to pass to the class. |
return | object | If the class is found, returns an instance of it, otherwise throws an exception. |
public static function instance($type, $name, array $options = array())
{
$params = compact('type', 'name', 'options');
$_paths =& static::$_paths;
$implementation = function ($self, $params) use(&$_paths) {
$name = $params['name'];
$type = $params['type'];
if (!$name && !$type) {
$message = "Invalid class lookup: `\$name` and `\$type` are empty.";
throw new ClassNotFoundException($message);
}
if (!is_string($type) && $type !== null && !isset($_paths[$type])) {
throw new ClassNotFoundException("Invalid class type `{$type}`.");
}
if (!($class = $self::locate($type, $name))) {
throw new ClassNotFoundException("Class `{$name}` of type `{$type}` not found.");
}
if (is_object($class)) {
return $class;
}
if (!(is_string($class) && class_exists($class))) {
throw new ClassNotFoundException("Class `{$name}` of type `{$type}` not defined.");
}
return new $class($params['options']);
};
if (!isset(static::$_methodFilters[__FUNCTION__])) {
return $implementation(get_called_class(), $params);
}
$class = get_called_class();
$method = __FUNCTION__;
$data = array_merge(static::$_methodFilters[__FUNCTION__], array($implementation));
return Filters::run($class, $params, compact('data', 'class', 'method'));
}
/** * Perform initialization. * * @return void */ protected function _init() { Object::_init(); $type = isset($this->_config['type']) ? $this->_config['type'] : null; if ($type === 'text') { $h = function ($data) { return $data; }; } else { $encoding = 'UTF-8'; if ($this->_message) { $encoding =& $this->_message->charset; } $h = function ($data) use(&$encoding) { return htmlspecialchars((string) $data, ENT_QUOTES, $encoding); }; } $this->outputFilters += compact('h') + $this->_config['outputFilters']; foreach (array('loader', 'renderer') as $key) { if (is_object($this->_config[$key])) { $this->{'_' . $key} = $this->_config[$key]; continue; } $class = $this->_config[$key]; $config = array('view' => $this) + $this->_config; $path = 'adapter.template.mail'; $instance = Libraries::instance($path, $class, $config); $this->{'_' . $key} = $instance; } }