get() публичный статический Метод

Usage: Gets the names of all available configurations $configurations = Connections::get(); Gets the configuration array for the connection named 'db' $config = Connections::get('db', array('config' => true)); Gets the instance of the connection object, configured with the settings defined for this object in Connections::add() $dbConnection = Connections::get('db'); Gets the connection object, but only if it has already been built. Otherwise returns null. $dbConnection = Connections::get('db', array('autoCreate' => false));
public static get ( string $name = null, array $options = [] ) : mixed
$name string The name of the connection to get, as defined in the first parameter of `add()`, when the connection was initially created.
$options array Options to use when returning the connection: - `'autoCreate'`: If `false`, the connection object is only returned if it has already been instantiated by a previous call. - `'config'`: If `true`, returns an array representing the connection's internal configuration, instead of the connection itself.
Результат mixed A configured instance of the connection, or an array of the configuration used.
Пример #1
0
 /**
  * Check request reCAPTCHA validity
  * This method return `true` or `false` after validation, and set error in
  * helper. If `true` error is set to null, otherwise `'incorrect-captcha-sol'`
  * 
  * Example:
  * {{{
  *		class YourController extends \lithium\action\Controller {
  *			public function index() {
  *				if ($this->request->data) {
  *					if (!Recaptcha::check($this->request)) {
  *						return;
  *					}
  *				}
  *			}
  *		}
  * }}}
  * @param object $request Pass request object to check method
  * @return boolean
  * @throws ConfigException
  * @throws RuntimeException 
  */
 public static function check(\lithium\net\http\Request $request)
 {
     $config = Libraries::get('li3_recaptcha', 'keys');
     if (!$config['private']) {
         throw new ConfigException('To use reCAPTCHA you must get API key from' . 'https://www.google.com/recaptcha/admin/create');
     }
     if (!$request->env('SERVER_ADDR')) {
         throw new RuntimeException('For security reasons, you must pass the remote ip to reCAPTCHA');
     }
     $data = array('privatekey' => $config['private'], 'remoteip' => $request->env('SERVER_ADDR'), 'challenge' => null, 'response' => null);
     if ($request->data) {
         $data['challenge'] = $request->data['recaptcha_challenge_field'];
         $data['response'] = $request->data['recaptcha_response_field'];
     }
     if (!$data['challenge'] || !$data['response']) {
         RecaptchaHelper::$error = 'incorrect-captcha-sol';
         return false;
     }
     $service = Connections::get('recaptcha');
     $serviceRespond = explode("\n", $service->post('/recaptcha/api/verify', $data));
     if ($serviceRespond[0] == 'true') {
         RecaptchaHelper::$error = null;
         return true;
     } else {
         RecaptchaHelper::$error = 'incorrect-captcha-sol';
         return false;
     }
 }
Пример #2
0
 /**
  * Skip the test if a Sqlite adapter configuration is unavailable.
  *
  * @return void
  * @todo Tie into the Environment class to ensure that the test database is being used.
  */
 public function skip()
 {
     $this->_dbConfig = Connections::get('sqlite3', array('config' => true));
     $hasDb = isset($this->_dbConfig['adapter']) && $this->_dbConfig['adapter'] == 'Sqlite3';
     $message = 'Test database is either unavailable, or not using a Sqlite adapter';
     $this->skipIf(!$hasDb, $message);
 }
Пример #3
0
 /**
  * Skip the test if no `test` CouchDb connection available.
  *
  * @return void
  */
 public function skip()
 {
     $isAvailable = Connections::get('test', array('config' => true)) && Connections::get('test')->isConnected(array('autoConnect' => true));
     $this->skipIf(!$isAvailable, "No test connection available");
     $couchConnection = strpos(get_class(Connections::get('test')), 'CouchDb');
     $this->skipIf(!$couchConnection, "Test connection is not CouchDb");
 }
Пример #4
0
	public function skip() {
		$isAvailable = (
			Connections::get('test', array('config' => true)) &&
			Connections::get('test')->isConnected(array('autoConnect' => true))
		);
		$this->skipIf(!$isAvailable, "No test connection available");
	}
Пример #5
0
 public function testBasicGet()
 {
     $topsy = Connections::get('test-topsy');
     $headers = array('Content-Type' => 'application/json');
     $results = json_decode($topsy->connection->get('authorinfo.json?url=http://twitter.com/mehlah', array(), compact('headers')));
     $this->assertEqual('Mehdi Lahmam B.', $results->response->name);
 }
 public function testStreamConnection()
 {
     $config = array('socket' => 'Stream', 'host' => 'localhost', 'login' => 'root', 'password' => '', 'port' => '80');
     Connections::add('stream-test', 'Http', $config);
     $result = Connections::get('stream-test');
     $this->assertTrue($result instanceof \lithium\data\source\Http);
 }
Пример #7
0
 public static function __init(array $options = array())
 {
     parent::__init($options);
     $self = static::_instance();
     $self->_finders['count'] = function ($self, $params, $chain) use(&$query, &$classes) {
         $db = Connections::get($self::meta('connection'));
         $records = $db->read('SELECT count(*) as count FROM posts', array('return' => 'array'));
         return $records[0]['count'];
     };
     Post::applyFilter('save', function ($self, $params, $chain) {
         $post = $params['record'];
         if (!$post->id) {
             $post->created = date('Y-m-d H:i:s');
         } else {
             $post->modified = date('Y-m-d H:i:s');
         }
         $params['record'] = $post;
         return $chain->next($self, $params, $chain);
     });
     Validator::add('isUniqueTitle', function ($value, $format, $options) {
         $conditions = array('title' => $value);
         // If editing the post, skip the current psot
         if (isset($options['values']['id'])) {
             $conditions[] = 'id != ' . $options['values']['id'];
         }
         // Lookup for posts with same title
         return !Post::find('first', array('conditions' => $conditions));
     });
 }
Пример #8
0
 public function tearDown()
 {
     $connection = Connections::get('default');
     $mongo = $connection->connection;
     foreach ($mongo->listCollections() as $collection) {
         $collection->drop();
     }
 }
Пример #9
0
 /**
  * Skip the test if no test database connection available.
  */
 public function skip()
 {
     $dbConfig = Connections::get($this->_connection, ['config' => true]);
     $isAvailable = $dbConfig && Connections::get($this->_connection)->isConnected(['autoConnect' => true]);
     $this->skipIf(!$isAvailable, "No {$this->_connection} connection available.");
     $db = Connections::get($this->_connection);
     $this->skipIf(!$db instanceof Database, "The {$this->_connection} connection is not a relational database.");
 }
Пример #10
0
 /**
  * Skip the test if a MySQL adapter configuration is unavailable.
  *
  * @return void
  * @todo Tie into the Environment class to ensure that the test database is being used.
  */
 public function skip()
 {
     $this->skipIf(!MySql::enabled(), 'MySQL Extension is not loaded');
     $this->_dbConfig = Connections::get('test', array('config' => true));
     $hasDb = isset($this->_dbConfig['adapter']) && $this->_dbConfig['adapter'] == 'MySql';
     $message = 'Test database is either unavailable, or not using a MySQL adapter';
     $this->skipIf(!$hasDb, $message);
 }
Пример #11
0
 /**
  * Setup method run before every test method.
  */
 public function setUp()
 {
     if (empty($this->_backup)) {
         foreach (Connections::get() as $conn) {
             $this->_backup[$conn] = Connections::get($conn, array('config' => true));
         }
     }
     Connections::reset();
 }
Пример #12
0
 public function connect($connection, $options = array())
 {
     $options += array('autoConnect' => true);
     $this->_dbConfig = Connections::get($connection, array('config' => true));
     $db = $this->_db = Connections::get($connection);
     $this->skipIf(!$db, "The `{$connection}` connection is not correctly configured.");
     $this->skipIf(!$db::enabled(), 'Extension is not loaded.');
     $this->skipIf(!$db->isConnected($options), "No `{$connection}` connection available.");
 }
Пример #13
0
 public function skip()
 {
     $connection = $this->_connection;
     $this->_dbConfig = Connections::get($this->_connection, array('config' => true));
     $this->skipIf(!$this->with(array('Sqlite3')));
     $this->_db = new MockSqlite3($this->_dbConfig);
     $isConnected = $this->_db->isConnected(array('autoConnect' => true));
     $this->skipIf(!$isConnected, "No {$connection} connection available.");
 }
Пример #14
0
 public function testIssuesRead()
 {
     $gh = Connections::get('test-gh');
     $query = new Query(array('model' => $this->_models['issues']));
     $results = $gh->read($query);
     $expected = 'octocat';
     $result = $results->first();
     $this->assertEqual($expected, $result->user->login);
 }
Пример #15
0
 public function skip()
 {
     $connection = 'lithium_mysql_test';
     $this->_dbConfig = Connections::get($connection, array('config' => true));
     $isAvailable = $this->_dbConfig && Connections::get($connection)->isConnected(array('autoConnect' => true));
     $this->skipIf(!$isAvailable, "No {$connection} connection available.");
     $this->db = Connections::get($connection);
     $this->skipIf(!$this->db instanceof Database, "The {$connection} connection is not a relational database.");
 }
 public function testConnectWithWrongPassword()
 {
     $config = $this->_dbConfig;
     $config['login'] = '******';
     $config['password'] = '******';
     $connection = 'wrong_passord';
     Connections::add($connection, $config);
     $this->expectException('/Host connected, but could not access database/');
     Connections::get($connection)->connect();
 }
Пример #17
0
 /**
  * Skip the test if a MySQL adapter configuration is unavailable.
  *
  * @return void
  * @todo Tie into the Environment class to ensure that the test database is being used.
  */
 public function skip()
 {
     $message = 'MySQLi extension is not available for testing the adapter.';
     $hasClass = class_exists('\\mysqli');
     $this->skipIf(!$hasClass, $message);
     $this->_dbConfig = Connections::get('MySQLi-tests', array('config' => true));
     $hasDb = isset($this->_dbConfig['adapter']) && $this->_dbConfig['adapter'] == 'MySQLi';
     $message = 'Test database is either unavailable, or not using a MySQLi adapter';
     $this->skipIf(!$hasDb, $message);
 }
Пример #18
0
 /**
  * Skip the test if no test database connection available.
  */
 public function skip()
 {
     $connection = 'lithium_couch_test';
     $config = Connections::get($connection, array('config' => true));
     $isAvailable = $config && Connections::get($connection)->isConnected(array('autoConnect' => true));
     $this->skipIf(!$isAvailable, "No {$connection} connection available.");
     $this->_key = Companies::key();
     $this->_database = $config['database'];
     $this->_connection = Connections::get($connection);
 }
Пример #19
0
 /**
  * Skip the test if a Sqlite adapter configuration is unavailable.
  *
  * @return void
  * @todo Tie into the Environment class to ensure that the test database is being used.
  */
 public function skip()
 {
     $this->skipIf(!Sqlite3::enabled(), 'Sqlite3 adapter is not enabled.');
     $this->_dbConfig = Connections::get('test', array('config' => true));
     $hasDb = isset($this->_dbConfig['adapter']) && $this->_dbConfig['adapter'] == 'Sqlite3';
     $message = 'Test database is either unavailable, or not using a Sqlite3 adapter';
     $this->skipIf(!$hasDb, $message);
     $isMemory = $this->_dbConfig['database'] == ':memory:';
     $message = "Test database is not an in-memory database.";
     $this->skipIf(!$isMemory, $message);
 }
Пример #20
0
 public function testConnectWithWrongPassword()
 {
     $this->skipIf(!$this->with('PostgreSql'));
     $config = $this->_dbConfig;
     $config['login'] = '******';
     $config['password'] = '******';
     $config['object'] = null;
     $connection = 'wrong_passord';
     Connections::add($connection, $config);
     $this->expectException();
     Connections::get($connection)->connect();
 }
Пример #21
0
 public static function __init(array $options = array())
 {
     parent::__init($options);
     $self = static::_object();
     #		$self = static::_instance(__CLASS__);
     #var_dump($self);
     $self->_finders['count'] = function ($self, $params, $chain) use(&$query, &$classes) {
         $db = Connections::get($self::meta('connection'));
         $records = $db->read('SELECT count(*) as count FROM posts', array('return' => 'array'));
         return $records[0]['count'];
     };
 }
Пример #22
0
 public function setUp()
 {
     if (empty($this->_backup)) {
         foreach (Connections::get() as $conn) {
             $this->_backup[$conn] = Connections::get($conn, array('config' => true));
         }
     }
     Connections::reset();
     Connections::add('mongo', array('type' => 'MongoDb', 'autoConnect' => false));
     Connections::add('couch', array('type' => 'http', 'adapter' => 'CouchDb'));
     MockDocumentPost::config(array('connection' => 'mongo'));
     MockDocumentMultipleKey::config(array('connection' => 'couch'));
 }
Пример #23
0
 public function connections()
 {
     $data = Connections::get();
     $connections = new Collection(compact('data'));
     if (true || $this->request->is('json')) {
         $connections->each(function ($name) {
             $config = Connections::get($name, array('config' => true));
             unset($config['object']);
             return array_merge(compact('name'), Set::flatten($config));
         });
     }
     return compact('connections');
 }
Пример #24
0
 /**
  * Skip the test if a MySQL adapter configuration is unavailable.
  * @todo Tie into the Environment class to ensure that the test database is being used.
  */
 public function skip()
 {
     $this->skipIf(!MySql::enabled(), 'MySQL Extension is not loaded');
     $this->_dbConfig = Connections::get('test', array('config' => true));
     $hasDb = isset($this->_dbConfig['adapter']) && $this->_dbConfig['adapter'] == 'MySql';
     $message = 'Test database is either unavailable, or not using a MySQL adapter';
     $this->skipIf(!$hasDb, $message);
     $this->db = new MySql($this->_dbConfig);
     $lithium = LITHIUM_LIBRARY_PATH . '/lithium';
     $sqlFile = $lithium . '/tests/mocks/data/source/database/adapter/mysql_companies.sql';
     $sql = file_get_contents($sqlFile);
     $this->db->read($sql, array('return' => 'resource'));
 }
Пример #25
0
 public function skip()
 {
     $this->_dbConfig = Connections::get('test', array('config' => true));
     $isAvailable = $this->_dbConfig && Connections::get('test')->isConnected(array('autoConnect' => true));
     $this->skipIf(!$isAvailable, "No test connection available.");
     $isDatabase = Connections::get('test') instanceof Database;
     $this->skipIf(!$isDatabase, "The 'test' connection is not a relational database.");
     $this->db = Connections::get('test');
     $mockBase = LITHIUM_LIBRARY_PATH . '/lithium/tests/mocks/data/source/database/adapter/';
     $files = array('galleries' => '_galleries.sql', 'images' => '_images.sql');
     $files = array_diff_key($files, array_flip($this->db->sources()));
     foreach ($files as $file) {
         $sqlFile = $mockBase . strtolower($this->_dbConfig['adapter']) . $file;
         $this->skipIf(!file_exists($sqlFile), "SQL file {$sqlFile} does not exist.");
         $sql = file_get_contents($sqlFile);
         $this->db->read($sql, array('return' => 'resource'));
     }
 }
 public function index()
 {
     $fh = fopen("../resources/primary.csv", "r");
     $posts = $groups = 0;
     $connection = Connections::get('default')->connection;
     foreach (array('posts', 'groups', 'grants', 'grant_groups') as $collection) {
         $connection->{$collection}->remove(array());
     }
     while ($data = fgetcsv($fh)) {
         list($tmp, $postId, $desc, $y2011, $y2010, $y2009) = $data;
         if (!empty($tmp)) {
             /**
              * Keep track of group name
              */
             if (strpos($tmp, "Totalt") === false) {
                 $group = Group::create();
                 list($number, $name) = explode(" - ", $tmp);
                 $name = str_replace('"', "", trim($name));
                 $number = trim($number);
                 $group->save(array('name' => $name, 'number' => $number));
                 $groupId = $group->_id;
                 $groups++;
             } elseif ($group) {
                 $group->y2011 = (int) ($y2011 / 1000);
                 $group->y2010 = (int) ($y2010 / 1000);
                 $group->y2009 = (int) ($y2009 / 1000);
                 $group->save();
                 continue;
             }
         }
         /**
          * Place in previously mentioned group
          */
         if (isset($groupId)) {
             $post = Post::create();
             $post->save(array('post' => (double) $postId, 'desc' => trim($desc), 'y2011' => (int) ($y2011 / 1000), 'y2010' => (int) ($y2010 / 1000), 'y2009' => (int) ($y2009 / 1000), 'groupId' => $groupId));
             $posts++;
         }
     }
     // Grants
     return compact('groups', 'posts') + $this->grants();
 }
Пример #27
0
 /**
  * Handles the actual `MongoDb` connection and server connection adding for the adapter
  * constructor.
  *
  * @return void
  */
 protected function _init()
 {
     $this->_server = $this->_server ?: Connections::get($this->_config['connection']);
     $this->_connection = $this->_server->selectDb($this->_config['database']);
     $this->_connection->command(array('create' => $this->_config['collection'], 'capped' => $this->_config['capped'], 'size' => $this->_config['size'], 'max' => $this->_config['max']));
     $this->_collection = $this->_connection->selectCollection($this->_config['collection']);
     $options = $options = array('unique' => true, 'dropDups' => true, 'background' => $this->_config['capped']);
     if ($this->_config['useTtlCollection']) {
         if (isset($this->_config['expiry'])) {
             $options['expireAfterSeconds'] = strtotime($this->_config['expiry']);
             $index = array('key' => 1);
         } else {
             $this['useTtlCollection'] = false;
             $index = array('key' => 1, 'expires');
         }
     } else {
         $index = array('key' => 1, 'expires');
     }
     $this->_collection->ensureIndex($index, $options);
 }
 /**
  * Skip the test if a MySQL or PostgreSQL adapter configuration is unavailable and
  * preload test data.
  */
 public function skip()
 {
     $enabled = MySql::enabled() || PostgreSql::enabled();
     $this->skipIf(!$enabled, 'MySQL or PostgreSQL Extension is not loaded');
     $dbConfig = Connections::get('test', array('config' => true));
     $validAdapter = in_array($dbConfig['adapter'], array('MySql', 'PostgreSql'));
     $hasDb = isset($dbConfig['adapter']) && $validAdapter;
     $message = 'Test database is either unavailable, or not using a MySQL/PostgreSQL adapter';
     $this->skipIf(!$hasDb, $message);
     switch ($dbConfig['adapter']) {
         case "MySql":
             $this->db = new MySql($dbConfig);
             $this->mockPrefix = 'mysql';
             break;
         case "PostgreSql":
             $this->db = new PostgreSql($dbConfig);
             $this->mockPrefix = 'postgresql';
             break;
     }
 }
Пример #29
0
 /**
  * AutoIndex for MongoDB
  *
  * @todo not yet finished
  * @see li3_geo\extensions\data\behavior\Locatable
  * @param object $class
  * @param array $keys
  * @param array $options
  */
 public static function index($class, array $keys, array $options = array())
 {
     $defaults = array('include' => array(), 'background' => true);
     $options += $defaults;
     $meta = $class::meta();
     $database = Connections::get($meta['connection']);
     list($updated, $created) = $keys;
     $updated = is_string($updated) ? array($updated => 1) : $updated;
     $created = is_string($created) ? array($created => 1) : $created;
     if (!$database || !$updated || !$created) {
         return false;
     }
     if (is_a($database, 'lithium\\data\\source\\MongoDb')) {
         $index = array('name' => 'li3_dateable') + $options['include'] + $updated + $created;
         $collection = $meta['source'];
         unset($options['include']);
         if ($database->connection === null) {
             $database->connect();
         }
         $database->connection->{$collection}->ensureIndex($index, $options);
     }
 }
 public function testGetNullAdapter()
 {
     Connections::reset();
     $this->assertTrue(Connections::get(false) instanceof Mock);
 }