/** * 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); } }
/** * Get values of an enum class. * * @param string $class * Class name. * @throws InvalidEnumException If the class invalid or does not contain * constants. * @return string[] Enum values. */ public static function getValues($class = null) { if (!isset($class)) { $class = get_called_class(); } if (!isset(self::$values[$class])) { if (!self::classExists($class)) { throw new InvalidEnumException('Enum class not found: ' . $class); } $class = self::$classes[$class]; Assume::isSubclassOf($class, 'Jivoo\\Models\\Enum'); $ref = new \ReflectionClass($class); self::$values[$class] = array_flip($ref->getConstants()); if (count(self::$values[$class]) < 1) { throw new InvalidEnumException('Enum type "' . $class . '" must contain at least one constant'); } } return self::$values[$class]; }
/** * Construct active model. * * @param Schema $schema * Schema. * @throws InvalidActiveModelException If model is incorrectly defined. * @throws InvalidTableException If table not found. * @throws InvalidAssociationException If association models are invalid. * @throws InvalidMixinException If a mixin is invalid. */ public final function __construct(Schema $schema) { $this->name = Utilities::getClassName(get_class($this)); $this->schema = $schema; if (!isset($this->table)) { $this->table = $this->name; } $table = $this->table; if (!isset($this->schema->{$table})) { throw new InvalidTableException('Table "' . $table . '" not found in schema'); } $this->source = $this->schema->{$table}; $this->definition = $this->source->getDefinition(); if (!isset($this->definition)) { throw new InvalidTableException('Definition for table "' . $table . '" not found'); } $pk = $this->definition->getPrimaryKey(); if (count($pk) == 1) { $pk = $pk[0]; $this->primaryKey = $pk; $type = $this->definition->{$pk}; if ($type->isInteger() and $type->autoIncrement) { $this->aiPrimaryKey = $pk; } } else { throw new InvalidActiveModelException('ActiveModel does not support multi-field primary keys'); } $this->nonVirtualFields = $this->definition->getFields(); $this->fields = $this->nonVirtualFields; foreach ($this->virtual as $field) { $this->fields[] = $field; $this->virtualFields[] = $field; } $this->validator = new ValidatorBuilder($this, $this->validate); $this->definition->createValidationRules($this->validator); foreach ($this->nonVirtualFields as $field) { $type = $this->definition->{$field}; if (isset($type->default)) { $this->defaults[$field] = $type->default; } } if (isset($this->record)) { Utilities::assumeSubclassOf($this->record, 'Jivoo\\Data\\ActiveModel\\ActiveRecord'); } $this->attachEventListener($this); foreach ($this->mixins as $mixin => $options) { if (!is_string($mixin)) { $mixin = $options; $options = array(); } if (class_exists('Jivoo\\Data\\ActiveModel\\' . $mixin . 'Mixin')) { $mixin = 'Jivoo\\Data\\ActiveModel\\' . $mixin . 'Mixin'; } elseif (class_exists($mixin . 'Mixin')) { $mixin .= 'Mixin'; } elseif (!class_exists($mixin)) { throw new InvalidMixinException('Mixin class not found: ' . $mixin); } Assume::isSubclassOf($mixin, 'Jivoo\\Data\\ActiveModel\\ActiveModelMixin'); $mixin = new $mixin($this->app, $this, $options); $this->attachEventListener($mixin); $this->mixinObjects[] = $mixin; foreach ($mixin->getMethods() as $method) { $this->methods[$method] = array($mixin, $method); } } $this->init(); }