Example #1
0
 /**
  * Add a tool snippet.
  * @param string $class JtkObject class name.
  * @param string $snippetClass Optional name of snippet class. Default is
  * $class . 'Snippet'.
  */
 public function addTool($class, $snippetClass = null)
 {
     $name = Utilities::getClassName($class);
     if (!isset($snippetClass)) {
         $snippetClass = $class . 'Snippet';
     }
     $this->tools[$name] = $snippetClass;
 }
Example #2
0
 /**
  * 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(tr('Invalid meta class name format.'));
     }
     return lcfirst($matches[1]) . 'Id';
 }
Example #3
0
 /**
  * Constructor
  * @param string $name Name of schema
  */
 public function __construct($name = null)
 {
     $className = get_class($this);
     if ($className != __CLASS__) {
         if (!isset($name)) {
             $name = preg_replace('/Schema$/', '', Utilities::getClassName($className));
         }
         if (defined($className . '::REVISION')) {
             $this->_revision = constant($className . '::REVISION');
         }
         $this->createSchema();
         $this->_readOnly = true;
     }
     if (isset($name)) {
         $this->_name = $name;
     }
 }
Example #4
0
 /**
  * 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();
 }
Example #5
0
 /**
  * Add an ACL module.
  * @param Acl $acl Module.
  * @param string $name Name that can be used to later access the module using
  * {@see __get}, default is the class name (without namespace).
  */
 public function addAcl(Acl $acl, $name = null)
 {
     $this->aclMethods[] = $acl;
     if (!isset($name)) {
         $name = Utilities::getClassName($acl);
     }
     $this->acModules[$name] = $acl;
 }
Example #6
0
 /**
  * Get multiple helpers.
  * @param string[] $names Names of helpers.
  * @return Helper[] Helper objects.
  */
 public function getHelpers($names)
 {
     $helpers = array();
     foreach ($names as $name) {
         $helper = $this->getHelper($name);
         if (strpos($name, '\\') !== false) {
             $name = Utilities::getClassName($name);
         }
         $helpers[$name] = $helper;
     }
     return $helpers;
 }