Example #1
0
 public function search($query, $strict = false)
 {
     $ageChange = redis()->get(sha1(amodel($this->table)->dir));
     $keyCache = 'cache::index::array::' . $this->table . '::' . sha1(serialize($this->fields)) . '::' . sha1($query);
     $keyCacheData = $keyCache . '::data';
     $keyCacheAge = $keyCache . '::age';
     $age = redis()->get($keyCacheAge);
     if (!strlen($age) || $age < $ageChange) {
         $keys = $this->keys($query);
         $collection = [];
         $tuples = [];
         if (count($keys)) {
             foreach ($this->fields as $field) {
                 $rows = redis()->keys('indexes::' . $this->table . '::*');
                 if (count($rows)) {
                     foreach ($rows as $row) {
                         list($rowDummy, $typeDummy, $rowTable, $rowValue) = explode('::', $row, 3);
                         $subRows = redis()->hgetall($row);
                         if (count($subRows)) {
                             foreach ($subRows as $index => $val) {
                                 list($ind, $rowField) = explode('::', $index, 2);
                                 if ($rowField == $field) {
                                     $dbRow = amodel($this->table)->find($ind, false);
                                     if ($dbRow) {
                                         $compare = isAke($dbRow, $field, false);
                                         if (false !== $compare) {
                                             foreach ($keys as $compareKey) {
                                                 if (false === $strict) {
                                                     $check = strstr($rowValue, $compareKey) ? true : false;
                                                 } else {
                                                     $check = sha1($compareKey) == sha1($rowValue);
                                                 }
                                                 if (true === $check && !Arrays::in($ind, $tuples)) {
                                                     array_push($collection, $dbRow);
                                                     array_push($tuples, $ind);
                                                     break;
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         redis()->set($keyCacheData, serialize($collection));
         redis()->set($keyCacheAge, time());
     } else {
         $collection = unserialize(redis()->get($keyCacheData));
     }
     return new Collection($collection);
 }
Example #2
0
 public static function generate($model, $overwrite = false)
 {
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudArray')) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudArray', 0777);
     }
     $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudArray' . DS . ucfirst(Inflector::camelize($model)) . '.php';
     if (!File::exists($file) || $overwrite) {
         $db = amodel($model);
         $crud = new Crud($db);
         File::delete($file);
         $tplModel = fgc(__DIR__ . DS . 'Model.tpl');
         $tplField = fgc(__DIR__ . DS . 'Field.tpl');
         $fields = $crud->fields();
         $singular = ucfirst($model);
         $plural = $singular . 's';
         $default_order = $crud->pk();
         $tplModel = str_replace(array('##singular##', '##plural##', '##default_order##'), array($singular, $plural, $default_order), $tplModel);
         $fieldsSection = '';
         foreach ($fields as $field) {
             if ($field != $crud->pk()) {
                 $label = substr($field, -3) == '_id' ? ucfirst(str_replace('_id', '', $field)) : ucfirst(Inflector::camelize($field));
                 $fieldsSection .= str_replace(array('##field##', '##label##'), array($field, $label), $tplField);
             }
         }
         $tplModel = str_replace('##fields##', $fieldsSection, $tplModel);
         File::put($file, $tplModel);
     }
 }
Example #3
0
 public static function tables()
 {
     $dbt = amodel('ama_table');
     $dirs = glob(STORAGE_PATH . DS . 'dbarray' . DS . '*', GLOB_NOSORT);
     $rows = [];
     if (count($dirs)) {
         foreach ($dirs as $dir) {
             $tmp = glob($dir . DS . '*', GLOB_NOSORT);
             $rows = array_merge($rows, $tmp);
         }
     }
     $tables = [];
     if (count($rows)) {
         foreach ($rows as $row) {
             $tab = explode(DS, $row);
             $index = Arrays::last($tab);
             $ns = $tab[count($tab) - 2];
             if (!strstr($index, 'ama_')) {
                 $t = $dbt->where('name = ' . $index)->where('ns = ' . $ns)->first(true);
                 if (is_null($t)) {
                     $data = amodel($index, $ns)->fetch()->exec();
                     if (count($data)) {
                         $first = Arrays::first($data);
                         $fields = array_keys($first);
                         $tables[$index]['fields'] = $fields;
                     } else {
                         $fields = [];
                     }
                     self::structure($ns, $index, $fields);
                 }
             }
         }
     }
     return $tables;
 }