/** * Morm Autoloader */ function morm_autoloader($class) { $morm_classes = array(MORM_PATH => array('Morm' => true, 'MormConf' => true, 'MormUtils' => true, 'Mormons' => true, 'MovableMorm' => true, 'SqlBuilder' => true, 'SqlTools' => true, 'TableDesc' => true, 'FieldDesc' => true, 'MormAttachement' => true, 'MormGenerator' => true), EXCEPTION_PATH => array('MormValidateException' => true, 'MormNoForeignObjectToLoadException' => true), FIELD_PATH => array('MormFieldSqlFunction' => true)); /** * is the required class a Morm class ? */ foreach ($morm_classes as $path => $classes) { if (isset($classes[$class])) { require_once $path . $class . '.php'; return true; } } /** * ok, maybe it's a model */ $class_path = MODELS_PATH . $class . '.php'; if (file_exists($class_path)) { require_once $class_path; return true; } /** * well let's try to generate it */ $generated_class_path = GENERATED_MODELS_PATH . $class . '.php'; MormConf::generateMorm($class); if (file_exists($generated_class_path)) { require_once $generated_class_path; return true; } /** * I'm sorry, I did the best I could, but I could not load your class */ return false; }
/** * getIniConf * * cache and return the parsed morm_conf.ini file * * @return array parsed ini file */ public static function loadIniConf() { if (!isset(self::$_morm_conf)) { $file = MORM_CONF_PATH . self::INI_CONF_FILE; self::$_morm_conf = file_exists($file) ? parse_ini_file($file, TRUE) : array(); } return self::$_morm_conf; }
/** * FactoryFromMormons * * almost the same as Factory but does strange things useful for Mormons * * @param string $super_class top class of the STI * @param Mormons $mormons mormons object to associate with the model * @params array $to_load array used to load the mmorm object * @access public * @return Morm */ public static function FactoryFromMormons($super_class, &$mormons, $to_load) { $model = new $super_class(); if ($sti_field = $model->getStiField()) { $sti_field_mormonized = 'morm' . MormConf::MORM_SEPARATOR . $model->_table . MormConf::MORM_SEPARATOR . $sti_field; if (isset($to_load[$sti_field_mormonized]) && !empty($to_load[$sti_field_mormonized])) { $sti_class = MormConf::generateMormClass($to_load[$sti_field_mormonized]); if (!$sti_class) { throw new MormSqlException('The class ' . $to_load[$sti_field_mormonized] . ' doesn\'t exists.'); } $sti_model = new $sti_class(); if ($sti_model->is_a($super_class)) { $model = $sti_model; } else { throw new Exception('The class ' . $sti_class . ' is not a ' . $super_class . ' and could not be used as a sti model'); } } else { throw new Exception('Could not guess the class to instantiate from this array, the sti field wasn\'t there'); } } $model->associateWithMormons($mormons); $model->loadFromMormons($to_load); return $model; }
/** * getIniConf * * cache and return the parsed morm_conf.ini file * * @return array parsed ini file */ public static function getIniConf() { if (!isset(self::$_morm_conf)) { self::$_morm_conf = parse_ini_file(MORM_CONF_PATH . self::INI_CONF_FILE); } return self::$_morm_conf; }
/** * set_join * * TODO take $type in account to define if join is RIGHT, LEFT, INNER, OUTER etc. * * @param mixed $table * @param mixed $on * @param string $type * @return void */ public function set_join($table_or_alias, $on = null, $type = 'LEFT') { $table = $this->getJoinableTable($table_or_alias); if (!$this->is_used_table($table)) { $table = MormConf::isInConf($table_or_alias) ? $this->add_table($table_or_alias) : $this->add_table($table); $this->join_tables[] = $table; } if (!is_null($on)) { $tables = array_keys($on); $this->joins[] = array(array($tables[0] => $tables[1]), $on); } else { $key = $this->base_models[$this->base_table]->getForeignKeyFrom($table_or_alias); try { $ft_key = $this->base_models[$this->base_table]->getForeignTableKey($key); } catch (Exception $e) { if ($this->base_models[$this->base_table]->isForeignUsingTable($table)) { $ft_key = $this->base_models[$this->base_table]->getForeignMormonsUsingKey($table); } else { $ft_key = $this->base_models[$this->base_table]->getForeignMormonsKey($table); } } $this->joins[] = array(array($this->base_table => $table), array($this->base_table => $key, $table => $ft_key)); } //@todo put executed tu false only when join has changed $this->_executed = false; // switch($type) // { // case 'LEFT': // break; // case 'RIGHT': // break; // default: // throw new Exception("The join type ".$type." does not exist or is not yet supported by Mormons"); // break; // } }