/** * @return null */ public function testClearConnectionParams() { $key1 = 'param1'; $key2 = 'param2'; $key3 = 'param3'; $param1 = array('a' => 'value'); $param2 = array('b' => 'value'); $param3 = array('c' => 'value'); DbRegistry::addConnectionParams($key1, $param1); DbRegistry::addConnectionParams($key2, $param2); DbRegistry::addConnectionParams($key3, $param3); $expected = array($key1 => new Dictionary($param1), $key2 => new Dictionary($param2), $key3 => new Dictionary($param3)); $this->assertEquals($expected, DbRegistry::getAllConnectionParams()); $this->assertNull(DbRegistry::clearConnectionParams()); $this->assertEquals(array(), DbRegistry::getAllConnectionParams()); }
/** * @param array $params config params * @return null */ public function execute(array $params = null) { if (empty($params)) { $err = 'database startup requires config params which are empty'; throw new InvalidArgumentException($err); } if (!isset($params['db']) || !is_array($params['db'])) { $err = 'database config is not set or not an array'; throw new InvalidArgumentException($err); } $db = $params['db']; if (!is_array($db) || $db === array_values($db)) { $err = 'database config must be an associative array of '; $err .= 'connector-key=>connector-config pairs'; throw new InvalidArgumentException($err); } if (!isset($db['connectors']) || !is_array($db['connectors'])) { $err = 'db connectors have been set but are not an array'; throw new InvalidArgumentException($err); } $connectors = $db['connectors']; if (isset($db['default-connector'])) { DbRegistry::setDefaultConnectorKey($db['default-connector']); } /* * used when many database connectors are defined in the * common section of a config but you want to confine your app to * a subset of those connectors */ if (isset($db['use'])) { $scope = $db['use']; if (is_string($scope) && isset($connectors[$scope])) { $connectors = array($scope => $connectors[$scope]); } else { if (is_array($scope)) { if ($scope === array_values($scope)) { $tmp = array(); foreach ($connectors as $key => $data) { if (in_array($key, $scope)) { $tmp[$key] = $data; } } $connectors = $tmp; } else { if (isset($scope) && is_array($scope)) { $tmp = array(); foreach ($connectors as $key => $data) { if (in_array($key, $scope)) { $tmp[$key] = $data; } } $connectors = $tmp; } } } } } /** * For every connector we need to determine the adapter which * is a vendor specific connection class, and if that connector * is using replication and finally if their is a custom connector * class to be used. With that we create a series of connectors and * add them to the db registry to be used */ $factory = $this->getFactory(); foreach ($connectors as $key => $config) { if (!is_string($key) || empty($key)) { $err = 'connector key must be a non empty string'; throw new InvalidArgumentException($err); } if (!isset($config['adapter'])) { $err = 'missing config setting -(adapter)'; throw new InvalidArgumentException($err); } /* save the connection parameters separately in the registry * so that connections setting can be reused without having to * go back to the config file */ DbRegistry::addConnectionParams($key, $config); $slave = null; $adapterClass = $config['adapter']; $connectorClass = null; if (isset($config['connector-class'])) { $connectorClass = $config['connector']; if (!is_string($connectorClass) || empty($connectorClass)) { $err = 'connector class must be a non empty string'; throw new InvalidArgumentException($err); } } if (isset($config['master-params'])) { $master = $factory->createConnection($adapterClass, $config['master-params']); if (isset($config['slave-params'])) { $slave = $factory->createConnection($adapterClass, $config['slave-params']); } } else { if (isset($config['conn-params'])) { $master = $factory->createConnection($adapterClass, $config['conn-params']); } else { $err = 'could not find any connection parameters, looking '; $err .= 'for: replication -(master-params|[slave-params]) or '; $err .= 'non-replication -(conn-params)'; throw new InvalidArgumentException($err); } } /* when connector class is null the factory will create a default * connector, for appfuel that is DbConnector */ $connector = $factory->createConnector($master, $slave, $connectorClass); /* * The framework relies on the fact that we build and add db * connectors to the registry. Datasources have no idea how to * create connectors. */ DbRegistry::addConnector($key, $connector); } }