Exemplo n.º 1
0
 /**
  * Construct router.
  *
  * @param \Jivoo\Store\Document $config Optional router configuration
  * document.
  */
 public function __construct(\Jivoo\Store\Document $config = null)
 {
     parent::__construct();
     if (isset($config)) {
         $this->rewrite = $config->get('rewrite', $this->rewrite);
     }
 }
Exemplo n.º 2
0
 /**
  * Make a database connection.
  *
  * @param string|Document|array $name
  *            Name of database connection.
  * @param DatabaseDefinition $definition
  *            Database definition (collecton of table definitions).
  * @throws ConfigurationException If the $options-array does not
  *         contain the necessary information for a connection to be made.
  * @throws InvalidSchemaException If one of the schema names listed
  *         in the $schemas-parameter is unknown.
  * @throws ConnectionException If the connection fails.
  * @return LoadableDatabase A database object.
  */
 public function connect($name, DatabaseDefinition $definition = null)
 {
     if (is_string($name)) {
         if (!isset($this->config[$name])) {
             throw new ConfigurationException('Database "' . $name . '" not configured');
         }
         $config = $this->config->getSubset($name);
     } elseif (is_array($name)) {
         $config = new Document($name);
         unset($name);
     } else {
         Assume::isInstanceOf($name, 'Jivoo\\Store\\Document');
         $config = $name;
         unset($name);
     }
     $driver = $config->get('driver', null);
     if (!isset($driver)) {
         throw new ConfigurationException('Database driver not set');
     }
     try {
         $driverInfo = $this->checkDriver($driver);
     } catch (InvalidDriverException $e) {
         throw new ConnectionException('Invalid database driver: ' . $e->getMessage(), 0, $e);
     }
     foreach ($driverInfo['requiredOptions'] as $option) {
         if (!isset($config[$option])) {
             throw new ConfigurationException('Database option missing: "' . $option . '"');
         }
     }
     try {
         $class = 'Jivoo\\Data\\Database\\Drivers\\' . $driver . '\\' . $driver . 'Database';
         Assume::isSubclassOf($class, 'Jivoo\\Data\\Database\\LoadableDatabase');
         if (!isset($definition)) {
             $definition = new DatabaseDefinitionBuilder([]);
         }
         $object = new $class($definition, $config);
         $object->setLogger($this->logger);
         if (isset($name)) {
             $this->connections[$name] = new DatabaseSchema($object);
         }
         return $object;
     } catch (ConnectionException $exception) {
         throw new ConnectionException('Database connection failed (' . $driver . '): ' . $exception->getMessage(), 0, $exception);
     }
 }