public function __construct(Horde_Injector $injector = null) { if ($injector === null) { $this->_injector = new Horde_Injector(new Horde_Injector_TopLevel()); $this->_injector->bindFactory('Horde_Log_Logger', 'Horde_Kolab_Filter_Factory', 'getLogger'); $this->_injector->bindFactory('Horde_Kolab_Server_Composite', 'Horde_Kolab_Filter_Factory', 'getUserDb'); $this->_injector->bindImplementation('Horde_Kolab_Filter_Temporary', 'Horde_Kolab_Filter_Temporary_File'); $this->_injector->setInstance('Horde_Kolab_Filter', $this); } else { $this->_injector = $injector; } }
/** * Test creating the server object. * * @return NULL */ public function testCreation() { try { $injector = new Horde_Injector(new Horde_Injector_TopLevel()); $config = new stdClass(); $config->driver = 'dummy'; $injector->setInstance('Horde_Kolab_Server_Config', $config); $injector->bindFactory('Horde_Kolab_Server_Structure', 'Horde_Kolab_Server_Factory', 'getStructure'); $injector->bindFactory('Horde_Kolab_Server', 'Horde_Kolab_Server_Factory', 'getServer'); Horde_Kolab_Server_Factory::getServer($injector); $this->assertFail('No error!'); } catch (Horde_Kolab_Server_Exception $e) { $this->assertEquals('Server type definition "Horde_Kolab_Server_Dummy" missing.', $e->getMessage()); } }
public function testMock() { $injector = new Horde_Injector(new Horde_Injector_TopLevel()); $injector->bindFactory('Horde_Group', 'Horde_Core_Factory_Group', 'create'); $GLOBALS['conf']['group']['driver'] = 'mock'; $this->assertInstanceOf('Horde_Group_Mock', $injector->getInstance('Horde_Group')); }
/** * Handle the current request. * * @return NULL */ public function dispatch() { try { $this->get('Horde_Controller_ResponseWriter')->writeResponse($this->get('Horde_Controller_Runner')->execute($this->_injector, $this->get('Horde_Controller_Request'), $this->get('Horde_Kolab_FreeBusy_Controller_RequestConfiguration'))); } catch (Exception $e) { $this->_injector->bindFactory('Horde_Controller_ResponseWriter', 'Horde_Kolab_FreeBusy_Factory_Base', 'createResponseWriter'); $response = $this->_injector->createInstance('Horde_Controller_Response'); $response->setHeaders(array('Status' => '404 Not Found', 'HTTP/1.0' => '404 Not Found')); $response->setBody($e->getMessage()); $this->get('Horde_Controller_ResponseWriter')->writeResponse($response); } }
/** * Create a new Horde_Registry instance. * * @param integer $session_flags Any session flags. * @param array $args See appInit(). * * @throws Horde_Exception */ public function __construct($session_flags = 0, array $args = array()) { /* Set a valid timezone. */ date_default_timezone_set(ini_get('date.timezone') ?: getenv('TZ') ?: 'UTC'); /* Save arguments. */ $this->_args = $args; /* Define factories. By default, uses the 'create' method in the given * classname (string). If other function needed, define as the second * element in an array. */ $factories = array('Horde_ActiveSyncBackend' => 'Horde_Core_Factory_ActiveSyncBackend', 'Horde_ActiveSyncServer' => 'Horde_Core_Factory_ActiveSyncServer', 'Horde_ActiveSyncState' => 'Horde_Core_Factory_ActiveSyncState', 'Horde_Alarm' => 'Horde_Core_Factory_Alarm', 'Horde_Browser' => 'Horde_Core_Factory_Browser', 'Horde_Cache' => 'Horde_Core_Factory_Cache', 'Horde_Controller_Request' => 'Horde_Core_Factory_Request', 'Horde_Controller_RequestConfiguration' => array('Horde_Core_Controller_RequestMapper', 'getRequestConfiguration'), 'Horde_Core_Auth_Signup' => 'Horde_Core_Factory_AuthSignup', 'Horde_Core_CssCache' => 'Horde_Core_Factory_CssCache', 'Horde_Core_JavascriptCache' => 'Horde_Core_Factory_JavascriptCache', 'Horde_Core_Perms' => 'Horde_Core_Factory_PermsCore', 'Horde_Dav_Server' => 'Horde_Core_Factory_DavServer', 'Horde_Dav_Storage' => 'Horde_Core_Factory_DavStorage', 'Horde_Db_Adapter' => 'Horde_Core_Factory_DbBase', 'Horde_Editor' => 'Horde_Core_Factory_Editor', 'Horde_ElasticSearch_Client' => 'Horde_Core_Factory_ElasticSearch', 'Horde_Group' => 'Horde_Core_Factory_Group', 'Horde_HashTable' => 'Horde_Core_Factory_HashTable', 'Horde_History' => 'Horde_Core_Factory_History', 'Horde_Kolab_Server_Composite' => 'Horde_Core_Factory_KolabServer', 'Horde_Kolab_Session' => 'Horde_Core_Factory_KolabSession', 'Horde_Kolab_Storage' => 'Horde_Core_Factory_KolabStorage', 'Horde_Lock' => 'Horde_Core_Factory_Lock', 'Horde_Log_Logger' => 'Horde_Core_Factory_Logger', 'Horde_Mail' => 'Horde_Core_Factory_MailBase', 'Horde_Memcache' => 'Horde_Core_Factory_Memcache', 'Horde_Nosql_Adapter' => 'Horde_Core_Factory_NosqlBase', 'Horde_Notification' => 'Horde_Core_Factory_Notification', 'Horde_Perms' => 'Horde_Core_Factory_Perms', 'Horde_Queue_Storage' => 'Horde_Core_Factory_QueueStorage', 'Horde_Routes_Mapper' => 'Horde_Core_Factory_Mapper', 'Horde_Routes_Matcher' => 'Horde_Core_Factory_Matcher', 'Horde_Secret' => 'Horde_Core_Factory_Secret', 'Horde_Secret_Cbc' => 'Horde_Core_Factory_Secret_Cbc', 'Horde_Service_Facebook' => 'Horde_Core_Factory_Facebook', 'Horde_Service_Twitter' => 'Horde_Core_Factory_Twitter', 'Horde_Service_UrlShortener' => 'Horde_Core_Factory_UrlShortener', 'Horde_SessionHandler' => 'Horde_Core_Factory_SessionHandler', 'Horde_Template' => 'Horde_Core_Factory_Template', 'Horde_Timezone' => 'Horde_Core_Factory_Timezone', 'Horde_Token' => 'Horde_Core_Factory_Token', 'Horde_Variables' => 'Horde_Core_Factory_Variables', 'Horde_View' => 'Horde_Core_Factory_View', 'Horde_View_Base' => 'Horde_Core_Factory_View', 'Horde_Weather' => 'Horde_Core_Factory_Weather', 'Net_DNS2_Resolver' => 'Horde_Core_Factory_Dns', 'Text_LanguageDetect' => 'Horde_Core_Factory_LanguageDetect'); /* Define implementations. */ $implementations = array('Horde_Controller_ResponseWriter' => 'Horde_Controller_ResponseWriter_Web'); /* Setup injector. */ $GLOBALS['injector'] = $injector = new Horde_Injector(new Horde_Injector_TopLevel()); foreach ($factories as $key => $val) { if (is_string($val)) { $val = array($val, 'create'); } $injector->bindFactory($key, $val[0], $val[1]); } foreach ($implementations as $key => $val) { $injector->bindImplementation($key, $val); } $GLOBALS['registry'] = $this; $injector->setInstance(__CLASS__, $this); /* Setup autoloader instance. */ $injector->setInstance('Horde_Autoloader', $GLOBALS['__autoloader']); /* Import and global Horde's configuration values. */ $this->importConfig('horde'); $conf = $GLOBALS['conf']; /* Set the umask according to config settings. */ if (isset($conf['umask'])) { umask($conf['umask']); } /* Set the error reporting level in accordance with the config * settings. */ error_reporting($conf['debug_level']); /* Set the maximum execution time in accordance with the config * settings, but only if not running from the CLI */ if (!isset($GLOBALS['cli'])) { set_time_limit($conf['max_exec_time']); } /* The basic framework is up and loaded, so set the init flag. */ $this->hordeInit = true; /* Initial Horde-wide settings. */ /* Initialize browser object. */ $GLOBALS['browser'] = $injector->getInstance('Horde_Browser'); /* Get modified time of registry files. */ $regfiles = array(HORDE_BASE . '/config/registry.php', HORDE_BASE . '/config/registry.d'); if (file_exists(HORDE_BASE . '/config/registry.local.php')) { $regfiles[] = HORDE_BASE . '/config/registry.local.php'; } if (!empty($conf['vhosts'])) { $vhost = HORDE_BASE . '/config/registry-' . $conf['server']['name'] . '.php'; if (file_exists($vhost)) { $regfiles[] = $this->vhost = $vhost; } } $this->_regmtime = max(array_map('filemtime', $regfiles)); /* Start a session. */ if ($session_flags & self::SESSION_NONE) { /* Never start a session if the session flags include SESSION_NONE. */ $GLOBALS['session'] = $session = new Horde_Session_Null(); $session->setup(true, $args['session_cache_limiter']); } elseif (PHP_SAPI === 'cli' || empty($_SERVER['SERVER_NAME']) && (PHP_SAPI === 'cgi' || PHP_SAPI === 'cgi-fcgi')) { $GLOBALS['session'] = $session = new Horde_Session(); $session->setup(false, $args['session_cache_limiter']); } else { $GLOBALS['session'] = $session = new Horde_Session(); $session->setup(true, $args['session_cache_limiter']); if ($session_flags & self::SESSION_READONLY) { /* Close the session immediately so no changes can be made but values are still available. */ $session->close(); } } $injector->setInstance('Horde_Session', $session); /* Always need to load applications information. */ $this->_loadApplications(); /* Stop system if Horde is inactive. */ if ($this->applications['horde']['status'] == 'inactive') { throw new Horde_Exception(Horde_Core_Translation::t("This system is currently deactivated.")); } /* Initialize language configuration object. */ $this->nlsconfig = new Horde_Registry_Nlsconfig(); /* Initialize the localization routines and variables. */ $this->setLanguageEnvironment(null, 'horde'); /* Initialize global page output object. */ $GLOBALS['page_output'] = $injector->getInstance('Horde_PageOutput'); /* Initialize notification object. Always attach status listener by * default. */ $nclass = null; switch ($this->getView()) { case self::VIEW_DYNAMIC: $nclass = 'Horde_Core_Notification_Listener_DynamicStatus'; break; case self::VIEW_SMARTMOBILE: $nclass = 'Horde_Core_Notification_Listener_SmartmobileStatus'; break; } $GLOBALS['notification'] = $injector->getInstance('Horde_Notification'); if (empty($args['nonotificationinit'])) { $GLOBALS['notification']->attachAllAppHandlers(); } $GLOBALS['notification']->attach('status', null, $nclass); Horde_Shutdown::add($this); }
* PHP version 5 * * @category Horde * @package Injector * @author Gunnar Wrobel <*****@*****.**> * @license http://www.horde.org/licenses/bsd BSD * @link http://pear.horde.org/index.php?package=Injector */ require 'Horde/Autoloader.php'; class Greet { public function __construct($somebody) { $this->somebody = $somebody; } public function greet() { print 'Hello ' . $this->somebody; } } class Factory { public static function getGreeter(Horde_Injector $injector) { return new Greet($injector->getInstance('Person')); } } $a = new Horde_Injector(new Horde_Injector_TopLevel()); $a->setInstance('Person', 'Bob'); $a->bindFactory('Greet', 'Factory', 'getGreeter'); $a->getInstance('Greet')->greet();