/** * Get the name of the id field. * * @return string Name of id field. */ protected function getId() { $class = Utilities::getClassName($this); if (preg_match('/^(.+)MetaSchema$/', $class, $matches) !== 1) { throw new InvalidMixinException('Invalid meta class name format.'); } return lcfirst($matches[1]) . 'Id'; }
/** * Touch a state document (make sure that it exists). * * @param string $key * State document key. * @return bool True if document exists. */ public function touch($key) { if (!isset($this->files[$key])) { if (!Utilities::dirExists($this->dir, true)) { return false; } $this->files[$key] = new PhpStore($this->dir . '/' . $key . '.php'); } return $this->files[$key]->touch(); }
/** * Construct file log handler. * @param string $filePath Log file path. * @param string $level Minimum log level, see {@see \Psr\Log\LogLevel}. * @param bool $useLocking Whether to lock the file before appending to ensure * atomicity of each write. */ public function __construct($filePath, $level = LogLevel::DEBUG, $useLocking = false) { if (!file_exists($filePath)) { $dir = dirname($filePath); if (!Utilities::dirExists($dir)) { trigger_error('Could not create log directory: ' . $dir, E_USER_WARNING); $this->stream = false; return; } if (!touch($filePath)) { trigger_error('Could not create log file: ' . $filePath, E_USER_WARNING); $this->stream = false; return; } } $this->file = realpath($filePath); parent::__construct(null, $level, $useLocking); }
protected function getDb() { $def = new DatabaseDefinitionBuilder(); $tableDef = new DefinitionBuilder(); $tableDef->a = DataType::string(); $tableDef->b = DataType::string(); $tableDef->c = DataType::string(); $def->addDefinition('Foo', $tableDef); $typeAdapter = $this->getMockBuilder('Jivoo\\Data\\Database\\TypeAdapter')->getMock(); $typeAdapter->method('encode')->willReturnCallback(function ($type, $value) { return '"' . $value . '"'; }); $db = $this->getMockBuilder('Jivoo\\Data\\Database\\Common\\SqlDatabase')->getMock(); $db->method('getTypeAdapter')->willReturn($typeAdapter); $db->method('getDefinition')->willReturn($def); $db->method('sqlLimitOffset')->willReturnCallback(function ($limit, $offset) { if (isset($offset)) { return 'LIMIT ' . $limit . ' OFFSET ' . $offset; } return 'LIMIT ' . $limit; }); $db->method('tableName')->willReturnCallback(function ($table) { return \Jivoo\Utilities::camelCaseToUnderscores($table); }); $db->method('quoteModel')->willReturnCallback(function ($model) { return '{' . $model . '}'; }); $db->method('quoteField')->willReturnCallback(function ($field) { return $field; }); $db->method('quoteLiteral')->willReturnCallback(function ($type, $value) { return '"' . $value . '"'; }); $db->method('quoteString')->willReturnCallback(function ($value) { return '"' . $value . '"'; }); return $db; }
/** * {@inheritdoc} */ public function tableName($name) { return $this->tablePrefix . Utilities::camelCaseToUnderscores($name); }
/** * Run a migration on a database. * Will attempt to revert if migration fails. * * @param string $dbName * Name of database. * @param string $migrationName * Name of migration. * @throws MigrationException If migration fails. */ public function run($dbName, $migrationName) { $db = $this->getDatabase($dbName); $this->logger->info('Initializing migration ' . $migrationName); Utilities::assumeSubclassOf($migrationName, 'Jivoo\\Data\\Migration\\Migration'); // The migration definition keeps track of the state of the database if (!isset($this->migrationDefinitions[$dbName])) { $this->migrationDefinitions[$dbName] = new MigrationDefinition($db); } $migrationSchema = $this->migrationDefinitions[$dbName]; $migration = new $migrationName($db, $migrationSchema); try { $migration->up(); $db->SchemaRevision->insert(array('revision' => $migrationName)); } catch (\Exception $e) { $migration->revert(); throw new MigrationException('Migration failed: ' . $migrationName, null, $e); } }
/** * {@inheritdoc} */ public function getTables() { $prefix = $this->db->tableName(''); $prefixLength = strlen($prefix); $result = $this->db->query('SHOW TABLES'); $tables = array(); while ($row = $result->fetchRow()) { $name = $row[0]; if (substr($name, 0, $prefixLength) == $prefix) { $name = substr($name, $prefixLength); $tables[] = Utilities::underscoresToCamelCase($name); } } return $tables; }
/** * Get MIME type of a file. * @param string $fileName File name or path. * @return string A MIME type. */ public function getMimeType($fileName) { return $this->mimeTypes->getMimeType(\Jivoo\Utilities::getFileExtension($fileName)); }
/** * {@inheritdoc} */ public function getTables() { $prefix = $this->db->tableName(''); $prefixLength = strlen($prefix); $result = $this->db->query("SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public'"); $tables = array(); while ($row = $result->fetchRow()) { $name = $row[0]; if (substr($name, 0, $prefixLength) == $prefix) { $name = substr($name, $prefixLength); $tables[] = Utilities::underscoresToCamelCase($name); } } return $tables; }
/** * @param callable[] $middleware * @param callable $last * @return callable */ private function getNext(array $middleware, callable $last) { return function (ServerRequestInterface $request, ResponseInterface $response) use($middleware, $last) { if (!$request instanceof ActionRequest) { $request = new ActionRequest($request); } $this->request = $request; if (count($middleware)) { $next = array_shift($middleware); $response = $next($request, $response, $this->getNext($middleware, $last)); if (!$response instanceof ResponseInterface) { throw new InvalidResponseException('Invalid response returned from: ' . \Jivoo\Utilities::callableToString($next)); } return $response; } $response = $last($request, $response); if (!$response instanceof ResponseInterface) { throw new InvalidResponseException('Invalid response returned from: ' . \Jivoo\Utilities::callableToString($last)); } return $response; }; }
/** * 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(); }