/**
* Retrieve an API object.
*
* @param string $app The application to load.
* @param string $type Either 'application' or 'api'.
*
* @return Horde_Registry_Api|Horde_Registry_Application The API object.
* @throws Horde_Exception
*/
public function getApiInstance($app, $type)
{
if (isset($this->_cache['ob'][$app][$type])) {
return $this->_cache['ob'][$app][$type];
}
$path = $this->get('fileroot', $app) . '/lib';
/* Set up autoload paths for the current application. This needs to
* be done here because it is possible to try to load app-specific
* libraries from other applications. */
if (!isset($this->_cache['ob'][$app])) {
$autoloader = $GLOBALS['injector']->getInstance('Horde_Autoloader');
$app_mappers = array('Controller' => 'controllers', 'Helper' => 'helpers', 'SettingsExporter' => 'settings');
$applicationMapper = new Horde_Autoloader_ClassPathMapper_Application($this->get('fileroot', $app) . '/app');
foreach ($app_mappers as $key => $val) {
$applicationMapper->addMapping($key, $val);
}
$autoloader->addClassPathMapper($applicationMapper);
/* Skip horde, since this was already setup in core.php. */
if ($app != 'horde') {
$autoloader->addClassPathMapper(new Horde_Autoloader_ClassPathMapper_PrefixString($app, $path));
}
}
$cname = Horde_String::ucfirst($type);
/* Can't autoload here, since the application may not have been
* initialized yet. */
$classname = Horde_String::ucfirst($app) . '_' . $cname;
$path = $path . '/' . $cname . '.php';
if (file_exists($path)) {
include_once $path;
} else {
$classname = __CLASS__ . '_' . $cname;
}
if (!class_exists($classname, false)) {
throw new Horde_Exception("{$app} does not have an API");
}
$this->_cache['ob'][$app][$type] = $type == 'application' ? new $classname($app) : new $classname();
return $this->_cache['ob'][$app][$type];
}