/** * Get a JTK tool/snippet. * @param string $name Tool name. * @return JtkSnippet Snippet object for tool. */ public function getTool($name) { // if (isset($this->toolInstances[$name])) // return $this->toolInstances[$name]; if (isset($this->tools[$name])) { $class = $this->tools[$name]; Utilities::assumeSubclassOf($class, 'Jivoo\\Jtk\\JtkSnippet'); return new $class($this->app); } return null; }
/** * 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(tr('Enum class not found: %1', $class)); } $class = self::$classes[$class]; Utilities::assumeSubclassOf($class, 'Jivoo\\Models\\Enum'); $ref = new \ReflectionClass($class); self::$values[$class] = array_flip($ref->getConstants()); if (count(self::$values[$class]) < 1) { throw new InvalidEnumException(tr('Enum type "%1" must contain at least one constant', $class)); } } return self::$values[$class]; }
/** * {@inheritdoc} */ public function __invoke($parameters = array()) { $object = null; if (isset($parameters['object'])) { $object = $parameters['object']; } else { if (isset($parameters[0])) { $object = $parameters[0]; } } if (isset($object)) { Utilities::assumeSubclassOf($object, $this->objectType); } else { throw new InvalidArgumentException(tr('JTK object is null')); } $this->viewData['object'] = $object; $response = parent::__invoke($parameters); if ($response instanceof ViewResponse) { return $response->body; } return $response; }
/** * Construct active model. * @param App $app Associated application. * @param DatabaseLoader $databases Databases module. * @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(App $app, Loader $databases) { parent::__construct($app); $databaseName = $this->database; $database = $databases->{$databaseName}; $this->name = Utilities::getClassName(get_class($this)); if (!isset($database)) { throw new InvalidActiveModelException(tr('Database "%1" not found', $this->database)); } $this->database = $database; if (!isset($this->table)) { $this->table = $this->name; } $table = $this->table; if (!isset($this->database->{$table})) { throw new InvalidTableException(tr('Table "%1" not found in database', $table)); } $this->source = $this->database->{$table}; $this->schema = $this->source->getSchema(); if (!isset($this->schema)) { throw new InvalidTableException('Schema for table "' . $table . '" not found'); } $pk = $this->schema->getPrimaryKey(); if (count($pk) == 1) { $pk = $pk[0]; $this->primaryKey = $pk; $type = $this->schema->{$pk}; if ($type->isInteger() and $type->autoIncrement) { $this->aiPrimaryKey = $pk; } } else { throw new InvalidActiveModelException(tr('ActiveModel does not support multi-field primary keys')); } $this->nonVirtualFields = $this->schema->getFields(); $this->fields = $this->nonVirtualFields; foreach ($this->virtual as $field) { $this->fields[] = $field; $this->virtualFields[] = $field; } $this->validator = new ValidatorBuilder($this, $this->validate); $this->schema->createValidationRules($this->validator); foreach ($this->nonVirtualFields as $field) { $type = $this->schema->{$field}; if (isset($type->default)) { $this->defaults[$field] = $type->default; } } if (isset($this->record)) { Utilities::assumeSubclassOf($this->record, 'Jivoo\\ActiveModels\\ActiveRecord'); } $this->attachEventListener($this); foreach ($this->mixins as $mixin => $options) { if (!is_string($mixin)) { $mixin = $options; $options = array(); } if (class_exists('Jivoo\\ActiveModels\\' . $mixin . 'Mixin')) { $mixin = 'Jivoo\\ActiveModels\\' . $mixin . 'Mixin'; } else { if (class_exists($mixin . 'Mixin')) { $mixin .= 'Mixin'; } else { if (!class_exists($mixin)) { throw new InvalidMixinException('Mixin class not found: ' . $mixin); } } } Assume::isSubclassOf($mixin, 'Jivoo\\ActiveModels\\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->database->{$table} = $this; $this->init(); }
/** * 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\\Migrations\\Migration'); // The migration schema keeps track of the state of the database if (!isset($this->migrationSchemas[$dbName])) { $this->migrationSchemas[$dbName] = new MigrationSchema($db); } $migrationSchema = $this->migrationSchemas[$dbName]; $migration = new $migrationName($db, $migrationSchema); try { $migration->up(); $db->SchemaRevision->insert(array('revision' => $migrationName)); } catch (\Exception $e) { $migration->revert(); throw new MigrationException(tr('Migration failed: ' . $migrationName), null, $e); } }
/** * Load ACL module. * @param string|Acl $name Name or object. * @param array $options Options for new object. */ private function loadAcl($name, $options = array()) { if ($name instanceof Acl) { return $this->addAcl($name); } $class = 'Jivoo\\AccessControl\\Acl\\' . $name . 'Acl'; Utilities::assumeSubclassOf($class, 'Jivoo\\AccessControl\\LoadableAcl'); $this->addAcl(new $class($this->app, $options), $name); }
/** * Insert one or more objects. * @param int $offset The offset to insert the objects(s) at. * @param JtkObject|JtkObject[] $item Object or array of objects with * optional keys. * @param string $id Optional id for object. */ public function insert($offset, $item, $id = null) { if (is_array($item)) { $item = array_reverse($item, true); foreach ($item as $id => $it) { if (!is_string($id)) { $id = null; } $this->insert($offset, $it, $id); } } else { Utilities::assumeSubclassOf($item, $this->type); if (isset($id)) { $itemArray = array($id => $item); } else { $itemArray = array($item); } $head = array_splice($this->items, 0, $offset); $this->items = array_merge($head, $itemArray, $this->items); } }
/** * Get a module provided by an extension. Load it if has not yet been * loaded. * @param string $name Name of module. * @return ExtensionModule Module. * @throws InvalidExtensionException If extension module not found in the * load list, i.e. if the extension that provides the module has not been * imported. */ public function getModule($name) { if (!isset($this->extensionModules[$name])) { if (!isset($this->loadList[$name])) { throw new InvalidExtensionException(tr('Extension not in load list: "%1"', $name)); } $this->triggerEvent('beforeLoadExtension', new LoadExtensionEvent($this, $name)); Utilities::assumeSubclassOf($name, 'Jivoo\\Extensions\\ExtensionModule'); $info = $this->loadList[$name]; $this->extensionModules[$name] = new $name($this->app, $info, $this->config['config'][$info->canonicalName]); $this->triggerEvent('afterLoadExtension', new LoadExtensionEvent($this, $name, $this->extensionModules[$name])); } return $this->extensionModules[$name]; }