dependency() public static method

Combination dependency-injection and service-locator method; returns a dependency object as passed, or an object from the registry, or a new factory instance.
public static dependency ( string $class, mixed $spec ) : object
$class string The dependency object should be an instance of this class. Technically, this is more a hint than a requirement, although it will be used as the class name if [[Solar::factory()]] gets called.
$spec mixed If an object, check to make sure it's an instance of $class. If a string, treat as a [[Solar_Registry::get()]] key. Otherwise, use this as a config param to [[Solar::factory()]] to create a $class object.
return object The dependency object.
Example #1
0
 /**
  * 
  * Open session handler.
  * 
  * @return bool
  * 
  */
 public function open()
 {
     if (!$this->_sql) {
         $this->_sql = Solar::dependency('Solar_Sql', $this->_config['sql']);
     }
     return true;
 }
Example #2
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     // inflection dependency
     $this->_inflect = Solar::dependency('Solar_Inflect', 'inflect');
     // model stack
     $this->_setStack($this->_config['classes']);
 }
Example #3
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     // only set up the handler if it doesn't exist yet.
     if (!self::$_handler) {
         self::$_handler = Solar::dependency('Solar_Session_Handler', $this->_config['handler']);
     }
     $this->_request = Solar_Registry::get('request');
 }
Example #4
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     $this->_response = Solar::dependency('Solar_Http_Response', $this->_config['response']);
     $this->_json = Solar::factory('Solar_Json');
     // Setup headers based on the wildfire standard
     $this->_response->setHeader('X-Wf-Protocol-1', 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
     $this->_response->setHeader('X-Wf-1-Plugin-1', 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3');
     $this->_response->setHeader('X-Wf-1-Structure-1', 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
 }
Example #5
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     $this->_response = Solar::dependency('Solar_Http_Response', $this->_config['response']);
     $this->_json = Solar::factory('Solar_Json');
     $this->_response->setHeader('X-FirePHP-Data-100000000001', '{');
     $this->_response->setHeader('X-FirePHP-Data-300000000001', '"FirePHP.Firebug.Console":[');
     $this->_response->setHeader('X-FirePHP-Data-399999999999', '["__SKIP__"]],');
     $this->_response->setHeader('X-FirePHP-Data-200000000001', '"FirePHP.Dump":{');
     $this->_response->setHeader('X-FirePHP-Data-299999999999', '"__SKIP__":"__SKIP__"},');
     $this->_response->setHeader('X-FirePHP-Data-999999999999', '"__SKIP__":"__SKIP__"}');
 }
Example #6
0
 /**
  * 
  * Fetches the roles for a user.
  * 
  * @param string $handle User handle to get roles for.
  * 
  * @return array An array of roles discovered in the table.
  * 
  */
 public function fetch($handle)
 {
     // get the dependency object of class Solar_Sql
     $sql = Solar::dependency('Solar_Sql', $this->_config['sql']);
     // get a selection tool using the dependency object
     $select = Solar::factory('Solar_Sql_Select', array('sql' => $sql));
     // build the select
     $select->from($this->_config['table'], $this->_config['role_col'])->where("{$this->_config['handle_col']} = ?", $handle)->multiWhere($this->_config['where']);
     // get the results (a column of rows)
     $result = $select->fetch('col');
     // done!
     return $result;
 }
Example #7
0
 /**
  * 
  * Verifies a username handle and password.
  * 
  * @return mixed An array of verified user information, or boolean false
  * if verification failed.
  * 
  * 
  */
 protected function _processLogin()
 {
     // get the dependency object of class Solar_Sql
     $obj = Solar::dependency('Solar_Sql', $this->_config['sql']);
     // get a selection tool using the dependency object
     $select = Solar::factory('Solar_Sql_Select', array('sql' => $obj));
     // list of optional columns as (property => field)
     $optional = array('email' => 'email_col', 'moniker' => 'moniker_col', 'uri' => 'uri_col', 'uid' => 'uid_col');
     // always get the user handle
     $cols = array($this->_config['handle_col']);
     // get optional columns
     foreach ($optional as $key => $val) {
         if ($this->_config[$val]) {
             $cols[] = $this->_config[$val];
         }
     }
     // salt and hash the password
     $hash = hash($this->_config['hash_algo'], $this->_config['salt'] . $this->_passwd);
     // make sure the handle col is dotted so it gets quoted properly
     $handle_col = $this->_config['handle_col'];
     if (strpos($handle_col, '.') === false) {
         $handle_col = "{$this->_config['table']}.{$handle_col}";
     }
     // make sure the passwd col is dotted so it gets quoted properly
     $passwd_col = $this->_config['passwd_col'];
     if (strpos($passwd_col, '.') === false) {
         $passwd_col = "{$this->_config['table']}.{$passwd_col}";
     }
     // build the select, fetch up to 2 rows (just in case there's actually
     // more than one, we don't want to select *all* of them).
     $select->from($this->_config['table'], $cols)->where("{$handle_col} = ?", $this->_handle)->where("{$passwd_col} = ?", $hash)->multiWhere($this->_config['where'])->limit(2);
     // get the results
     $rows = $select->fetchAll();
     // if we get back exactly 1 row, the user is authenticated;
     // otherwise, it's more or less than exactly 1 row.
     if (count($rows) == 1) {
         // set base info
         $info = array('handle' => $this->_handle);
         // set optional info from optional cols
         $row = current($rows);
         foreach ($optional as $key => $val) {
             if ($this->_config[$val]) {
                 $info[$key] = $row[$this->_config[$val]];
             }
         }
         // done
         return $info;
     } else {
         return false;
     }
 }
Example #8
0
 public function setup()
 {
     $this->_model = Solar::dependency('Solar_Sql_Model_Catalog', 'model_catalog');
     $sql = Solar::dependency('Solar_Sql', 'sql');
     $sql->begin();
     $this->_setupUsers();
     $this->_setupPrefs();
     $this->_setupAreas();
     $this->_setupNodes();
     $this->_setupMetas();
     $this->_setupTags();
     $this->_setupTaggings();
     $this->_setupComments();
     $sql->commit();
 }
Example #9
0
 /**
  * 
  * Sends the Solar_Mail_Message through an SMTP server connection; 
  * lazy-loads the SMTP dependency if needed.
  * 
  * @return bool True on success, false on failure.
  * 
  */
 protected function _send()
 {
     // lazy-load the SMTP dependency if it's not already present
     if (!$this->_smtp) {
         $this->_smtp = Solar::dependency('Solar_Smtp', $this->_config['smtp']);
     }
     // get the headers for the message
     $headers = $this->_mail->fetchHeaders();
     // who are we sending from?
     $from = null;
     foreach ($headers as $header) {
         if ($header[0] == 'Return-Path') {
             $from = trim($header[1], '<>');
             break;
         }
     }
     if (!$from) {
         throw $this->_exception('ERR_NO_RETURN_PATH');
     }
     // who are we sending to?
     $rcpt = $this->_mail->getRcpt();
     // get the content
     $content = $this->_mail->fetchContent();
     // change headers from array to string
     $headers = $this->_headersToString($headers);
     // prepare the message data
     $crlf = $this->_mail->getCrlf();
     $data = $headers . $crlf . $content;
     // make sure we're connected to the server
     if (!$this->_smtp->isConnected()) {
         $this->_smtp->connect();
         $this->_smtp->helo();
     }
     // reset previous connections
     $this->_smtp->rset();
     // tell who this is MAIL FROM
     $this->_smtp->mail($from);
     // tell who this is RCPT TO (each to, cc, and bcc)
     foreach ($rcpt as $addr) {
         $this->_smtp->rcpt($addr);
     }
     // send the message
     $this->_smtp->data($data, $crlf);
     // done!
     return true;
 }
Example #10
0
 /**
  * 
  * Fetches the roles for a user.
  * 
  * @param string $handle User handle to get roles for.
  * 
  * @return array An array of roles discovered in the table.
  * 
  */
 public function fetch($handle)
 {
     // get the dependency object of class Solar_Sql
     $sql = Solar::dependency('Solar_Sql', $this->_config['sql']);
     // get a selection tool using the dependency object
     $select = Solar::factory('Solar_Sql_Select', array('sql' => $sql));
     // make sure the handle col is dotted so it gets quoted properly
     $handle_col = $this->_config['handle_col'];
     if (strpos($handle_col, '.') === false) {
         $handle_col = "{$this->_config['table']}.{$handle_col}";
     }
     // build the select
     $select->from($this->_config['table'], $this->_config['role_col'])->where("{$handle_col} = ?", $handle)->multiWhere($this->_config['where']);
     // get the results (a column of rows)
     $result = $select->fetch('col');
     // done!
     return $result;
 }
Example #11
0
    /**
     * 
     * Generates the script block required by Facebook.
     * 
     * @return void
     * 
     */
    protected function _postConstruct()
    {
        parent::_postConstruct();
        // retain the facebook dependency
        $this->_facebook = Solar::dependency('Facebook', $this->_config['facebook']);
        // add the FB script to the foot helper
        $href = "http://connect.facebook.net/en_US/all.js";
        $this->_view->foot()->addScript($href);
        // initialize the application and set up login event subscription,
        // also done via the foot helper
        $appid = $this->_facebook->getAppId();
        $inline = <<<INLINE
FB.init({appId: '{$appid}', xfbml: true, cookie: true});
FB.Event.subscribe('auth.login', function(response) {
  window.location.reload();
});
INLINE;
        $this->_view->foot()->addScriptInline($inline);
    }
Example #12
0
 /**
  * 
  * Establish state of this object prior to _setup().
  * 
  * @return void
  * 
  */
 protected function _preSetup()
 {
     // inflection reference
     $this->_inflect = Solar_Registry::get('inflect');
     // our class name so that we don't call get_class() all the time
     $this->_class = get_class($this);
     // get the catalog injection
     $this->_catalog = Solar::dependency('Solar_Sql_Model_Catalog', $this->_config['catalog']);
     // connect to the database
     $this->_sql = Solar::dependency('Solar_Sql', $this->_config['sql']);
 }
Example #13
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     // custom boundary string
     if ($this->_config['boundary']) {
         $this->_boundary = $this->_config['boundary'];
     } else {
         $this->_boundary = '__' . hash('md5', uniqid());
     }
     // custom encoding
     if ($this->_config['encoding']) {
         $this->_encoding = $this->_config['encoding'];
     }
     // custom charset
     if ($this->_config['charset']) {
         $this->_charset = $this->_config['charset'];
     }
     // custom CRLF
     if ($this->_config['crlf']) {
         $this->_crlf = $this->_config['crlf'];
     }
     // custom headers
     if ($this->_config['headers']) {
         foreach ((array) $this->_config['headers'] as $label => $value) {
             $this->addHeader($label, $value);
         }
     }
     // do we have an injected transport?
     if ($this->_config['transport']) {
         $this->_transport = Solar::dependency('Solar_Mail_Transport', $this->_config['transport']);
     }
 }
Example #14
0
 /**
  * 
  * Set up the dependency to the Facebook object.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     $this->_facebook = Solar::dependency('Facebook', $this->_config['facebook']);
 }
Example #15
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     // verbosity
     if ($this->_config['verbose'] !== null) {
         $this->setVerbose($this->_config['verbose']);
     }
     // keep a Solar_Debug_Var object around for later
     $this->_var = Solar::factory('Solar_Debug_Var');
     // set the include directory
     $this->_dir = Solar::$system . "/include";
     // logging
     $this->_log = Solar::dependency('Solar_Log', $this->_config['log']);
 }
Example #16
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 public function _postConstruct()
 {
     parent::_postConstruct();
     // get the current request environment
     $this->_request = Solar::dependency('Solar_Request', $this->_config['request']);
     // make sure we have a default action
     $action = $this->_request->server('REQUEST_URI');
     $this->_default_attribs['action'] = $action;
     // reset the form propertes
     $this->reset();
 }
Example #17
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     if ($this->_config['cache']) {
         $this->_cache = Solar::dependency('Solar_Cache', $this->_config['cache']);
     }
 }
Example #18
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     // only set up the handler if it doesn't exist yet.
     if (!self::$_handler) {
         self::$_handler = Solar::dependency('Solar_Session_Handler', $this->_config['handler']);
     }
     // only set up the request if it doesn't exist yet.
     if (!self::$_request) {
         self::$_request = Solar_Registry::get('request');
     }
     // determine the storage segment; use trim() and strict-equals to
     // allow for string zero segment names.
     $this->_class = trim($this->_config['class']);
     if ($this->_class === '') {
         $this->_class = 'Solar';
     }
     // set the class
     $this->setClass($this->_class);
     // lazy-start any existing session
     $this->lazyStart();
 }
Example #19
0
 /**
  * 
  * Injects a cache dependency for `$_cache`.
  * 
  * @param mixed $spec A [[Solar::dependency()]] specification.
  * 
  * @return void
  * 
  * @see $_cache
  * 
  */
 public function setCache($spec)
 {
     $this->_cache = Solar::dependency('Solar_Cache', $spec);
 }
Example #20
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     $this->_request = Solar::dependency('Solar_Request', $this->_config['request']);
 }
Example #21
0
 /**
  *
  * Verifies a username handle and password.
  *
  * @return mixed An array of verified user information, or boolean false
  * if verification failed.
  *
  *
  */
 protected function _processLogin()
 {
     // get the dependency object of class Solar_Sql
     $obj = Solar::dependency('Solar_Sql', $this->_config['sql']);
     // get a selection tool using the dependency object
     $select = Solar::factory('Solar_Sql_Select', array('sql' => $obj));
     // list of optional columns as (property => field)
     $optional = array('email' => 'email_col', 'moniker' => 'moniker_col', 'uri' => 'uri_col', 'uid' => 'uid_col');
     // always get the user handle
     $cols = array($this->_config['handle_col']);
     // get optional columns
     foreach ($optional as $key => $val) {
         if ($this->_config[$val]) {
             $cols[] = $this->_config[$val];
         }
     }
     // build the select, fetch up to 2 rows (just in case there's actually
     // more than one, we don't want to select *all* of them).
     $select->from($this->_config['table'], $cols)->where("{$this->_config['handle_col']} = ?", $this->_handle)->multiWhere($this->_config['where'])->limit(2);
     // get the results
     $rows = $select->fetchAll();
     // if we get back exactly 1 row, check the hash;
     // otherwise, it's more or less than exactly 1 row.
     if (count($rows) == 1) {
         $row = current($rows);
         $valid = $this->_checkHash($this->_passwd, $row[$this->_config['passwd_col']]);
         if (!$valid) {
             return false;
         }
         // set base info
         $info = array('handle' => $this->_handle);
         // set optional info from optional cols
         foreach ($optional as $key => $val) {
             if ($this->_config[$val]) {
                 $info[$key] = $row[$this->_config[$val]];
             }
         }
         // done
         return $info;
     } else {
         return false;
     }
 }
Example #22
0
 public function testDependency_config()
 {
     // set a random config-key and value so we know we're getting back
     // the "right" factoried object.
     $key = __FUNCTION__;
     $val = rand();
     $actual = Solar::dependency('Mock_Solar_Example', array($key => $val));
     $this->assertInstance($actual, 'Mock_Solar_Example');
     $this->assertEquals($actual->getConfig($key), $val);
 }
Example #23
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     // connect to the database with dependency injection
     $this->_sql = Solar::dependency('Solar_Sql', $this->_config['sql']);
     // set up defaults
     $this->setPaging($this->_config['paging']);
 }
Example #24
0
 /**
  * 
  * Setup for the auth, role, and access objects.
  * 
  * @return void
  * 
  */
 protected function _setup()
 {
     // set up an authentication object.
     $this->auth = Solar::dependency('Solar_Auth', $this->_config['auth']);
     // set up the roles object.
     $this->role = Solar::dependency('Solar_Role', $this->_config['role']);
     // set up the access object.
     $this->access = Solar::dependency('Solar_Access', $this->_config['access']);
 }
Example #25
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     // get the current request environment
     $this->_request = Solar_Registry::get('request');
     // set per config
     $this->allow = (bool) $this->_config['allow'];
     // cache dependency injection
     $this->_cache = Solar::dependency('Solar_Cache', $this->_config['cache']);
 }
Example #26
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     // only set up the handler if it doesn't exist yet.
     if (!self::$_handler) {
         self::$_handler = Solar::dependency('Solar_Session_Handler', $this->_config['handler']);
     }
     // only set up the request if it doesn't exist yet.
     if (!self::$_request) {
         self::$_request = Solar_Registry::get('request');
     }
     // determine the storage segment; use trim() and strict-equals to
     // allow for string zero segment names.
     $this->_class = trim($this->_config['class']);
     if ($this->_class === '') {
         $this->_class = 'Solar';
     }
     // set the class
     $this->setClass($this->_class);
     // lazy start: find the cookie name and look for the session cookie
     $name = session_name();
     if (self::$_request->cookie($name)) {
         // a previous session exists, start it
         $this->start();
     }
 }
Example #27
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     // PHPDoc parser
     $this->_phpdoc = Solar::dependency('Solar_Docs_Phpdoc', $this->_config['phpdoc']);
     // Logger
     $this->_log = Solar::dependency('Solar_Log', $this->_config['log']);
 }
Example #28
0
 /**
  * 
  * Post-construction tasks to complete object construction.
  * 
  * @return void
  * 
  */
 protected function _postConstruct()
 {
     parent::_postConstruct();
     // request environment
     $this->_request = Solar::dependency('Solar_Request', $this->_config['request']);
     // filter object
     $this->_filter = Solar::dependency('Solar_Filter', $this->_config['filter']);
     // csrf object
     $this->_csrf = Solar::factory('Solar_Csrf');
     // set the default action attribute
     $action = $this->_request->server('REQUEST_URI');
     $this->_default_attribs['action'] = $action;
     // reset everything
     $this->reset();
 }
Example #29
0
 /**
  * 
  * Follow-on setup for the constructor to build the $_slaves array.
  * 
  * @return void
  * 
  * @see $_slaves
  * 
  */
 protected function _setup()
 {
     // build up the $_slaves array info, using some of the values from
     // the master as defaults
     $base = array('host' => null, 'port' => $this->_config['port'], 'sock' => null, 'user' => $this->_config['user'], 'pass' => $this->_config['pass'], 'name' => $this->_config['name']);
     foreach ($this->_config['slaves'] as $key => $val) {
         $this->_slaves[$key] = array_merge($base, $val);
     }
     // is this a GET-after-POST/PUT request?
     $request = Solar::dependency('Solar_Request', $this->_config['request']);
     $this->_is_gap = $request->isGap();
     // done, on to the main setup
     parent::_setup();
 }
Example #30
0
 /**
  * 
  * Fetches access privileges for a user handle and roles.
  * 
  * Uses a SELECT similar to the following:
  * 
  * {{code: sql
  *     SELECT $cols
  *     FROM $table
  *     WHERE (type = 'handle' AND name IN ($handle_list))
  *     OR (type = 'role' AND name IN ($role_list))
  *     OR (type = 'owner')
  *     ORDER BY $order
  * }}
  * 
  * @param string $handle User handle.
  * 
  * @param array $roles User roles.
  * 
  * @return array
  * 
  */
 public function fetch($handle, $roles)
 {
     /**
      * prepare handle and role lists
      */
     // the handle list
     if ($handle) {
         // user is authenticated
         $handle_list = array($handle, '*', '+');
     } else {
         // user is anonymous
         $handle_list = array('*');
     }
     // the role list
     $role_list = (array) $roles;
     $role_list[] = '*';
     /**
      * prepare the sql object
      */
     // get the dependency object of class Solar_Sql
     $sql = Solar::dependency('Solar_Sql', $this->_config['sql']);
     // get a selection tool using the dependency object
     $select = Solar::factory('Solar_Sql_Select', array('sql' => $sql));
     // select these columns from the table
     $cols = array($this->_config['flag_col'] . ' AS allow', $this->_config['type_col'] . ' AS type', $this->_config['name_col'] . ' AS name', $this->_config['class_col'] . ' AS class', $this->_config['action_col'] . ' AS action');
     $select->from($this->_config['table'], $cols);
     /**
      * add major conditions
      */
     // pretty vars
     $type_col = $this->_config['type_col'];
     $name_col = $this->_config['name_col'];
     // `WHERE (type = 'handle' AND name IN (...))`
     $select->where("({$type_col} = ?", 'handle');
     $select->where("{$name_col} IN (?))", $handle_list);
     // `OR (type = 'role' AND name IN (...))`
     $select->orWhere("({$type_col} = ?", 'role');
     $select->where("{$name_col} IN (?))", $role_list);
     // `OR (type = 'owner')`
     $select->orWhere("({$type_col} = ?)", 'owner');
     // order the columns
     $select->order($this->_config['order_col']);
     /**
      * fetch, process, and return
      */
     // fetch the access list
     $access = $select->fetchAll();
     // set 'allow' flag to boolean on each access item
     $allow = array('allow', 't', 'T', 'y', 'Y', '1');
     foreach ($access as $key => $val) {
         $access[$key]['allow'] = (bool) in_array($val['allow'], $allow);
     }
     // return access list
     return $access;
 }