public function model($data = []) { $view = false; $db = $this->db; $table = $this->table; $modelFile = APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models' . DS . Inflector::lower($db) . DS . ucfirst(Inflector::lower($table)) . '.php'; if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql')) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql'); } if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models')) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models'); } if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models' . DS . Inflector::lower($db))) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models' . DS . Inflector::lower($db)); } if (!File::exists($modelFile)) { File::put($modelFile, str_replace('##class##', ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'SQLModel', File::read(__DIR__ . DS . 'dbModel.tpl'))); } $class = '\\Thin\\' . ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'SQLModel'; if (!class_exists($class)) { require_once $modelFile; } $model = $this; if (true === $view) { $model = self::instance($db, $table); } return new $class($model, $data); }
public function get($key, $default = null) { $key = $this->key($key); $this->clean(); $file = $this->getFile($key); if (File::exists($file)) { return unserialize(File::read($file)); } return $default; }
public function read($file) { $key = $this->redisKey($file); $cache = $this->model->cache()->get($key); if (!strlen($cache)) { if (File::exists($file)) { $cache = File::read($file); $this->model->cache()->set($key, $cache); } } return $cache; }
public function bag($k, $data = []) { $key = sha1($this->getIdentifier() . $k); $file = '/home/php/storage/bag/' . $key; $data = array_merge($data, ['id' => $key]); if (file_exists($file)) { $data = array_merge($data, unserialize(File::read($file))); } return dyn(lib('model', [$data]))->extend('save', function ($app) use($file, $k) { $row = $app->getNative()->toArray(); File::delete($file); File::put($file, serialize($row)); return bag($k); })->extend('delete', function ($app) use($file) { return File::delete($file); }); }
public function model($data = []) { $db = $this->db; $table = $this->table; $modelFile = APPLICATION_PATH . DS . 'models' . DS . 'Fast' . DS . 'models' . DS . Inflector::lower($db) . DS . ucfirst(Inflector::lower($table)) . '.php'; if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Fast' . DS . 'models' . DS . Inflector::lower($db))) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Fast' . DS . 'models' . DS . Inflector::lower($db)); } if (!File::exists($modelFile)) { File::put($modelFile, str_replace('##class##', ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'ModelFast', File::read(__DIR__ . DS . 'dbModel.tpl'))); } $class = '\\Thin\\' . ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'ModelFast'; if (!class_exists($class)) { require_once $modelFile; } $model = $this; return new $class($model, $data); }
public function read($id, $default = null) { $key = $this->key . '::' . $id; try { $result = $this->client->getObject(array('Bucket' => $this->bucket, 'Key' => $key)); } catch (NoSuchKeyException $e) { if (!strstr($id, 'file::')) { $data = File::read($this->findFile($id)); } else { $data = File::read(str_replace('thinseparator', '', str_replace('file::', '', $id))); } if (strlen($data)) { $this->write($id, $data); return $data; } else { return $default; } } return $result['Body']; }
public function read($id, $default = null) { $key = $this->key . '::' . $id; $row = $this->collection->findOne(function ($query) use($key) { $query->where('key', $key); }); if ($row) { return isAke($row, 'value', $default); } else { if (!strstr($id, 'file::')) { $data = File::read($this->findFile($id)); } else { $data = File::read(str_replace('thinseparator', '', str_replace('file::', '', $id))); } if (strlen($data)) { $this->write($id, $data); return $data; } else { return $default; } } return $default; }
public function hgetall($h) { $dirH = self::$dir . DS . $h; if (!is_dir($dirH)) { umask(00); mkdir($dirH, 0777); } $keys = glob($dirH . DS . '*', GLOB_NOSORT); $collection = []; if (count($keys)) { foreach ($keys as $cache) { $k = str_replace('.cache', '', Arrays::last(explode(DS, $cache))); $collection[$k] = unserialize(File::read($cache)); } } return $collection; }
public function upload($field) { $bucket = new Bucket(SITE_NAME, 'http://zelift.com/bucket'); if (Arrays::exists($field, $_FILES)) { $fileupload = $_FILES[$field]['tmp_name']; $fileuploadName = $_FILES[$field]['name']; if (strlen($fileuploadName)) { $tab = explode(".", $fileuploadName); $data = File::read($fileupload); if (!strlen($data)) { return null; } $ext = strtolower(end($tab)); $res = $bucket->data($data, $ext); return $res; } } return false; }
public function getFieldValueById($field, $id, $d = null) { return isAke(unserialize(File::read($this->db->dir . DS . $id)), $field, $d); }
public function importCsvFile($csv, $delimiter = '|||') { return $this->importCsvData(File::read($csv), $delimiter); }
public function hvals($hash) { $glob = glob($this->dir . DS . 'hash.' . $hash . DS . '*.store', GLOB_NOSORT); foreach ($glob as $row) { if (fnmatch('*expire.*', $row)) { continue; } (yield unserialize(File::read($row))); } }
public function clean() { $glob = glob($this->dir . DS . 'expire.', GLOB_NOSORT); foreach ($glob as $row) { $row = str_replace([$this->dir . DS, '.raw'], '', $row); list($d, $key) = explode('expire.', $row, 2); $expire = $this->getFile($row); $expiration = (int) unserialize(File::read($expire)); if (time() > $expiration) { $file = $this->getFile($key); File::delete($expire); File::delete($file); } } }
protected function makeCompile($file) { $route = container()->getRoute(); $module = !isset($route->module) ? 'default' : $route->module; $content = File::read($file); if (strstr($content, '@@layout')) { $layout = Utils::cut("@@layout('", "')", $content); $layoutFile = APPLICATION_PATH . DS . 'modules' . DS . SITE_NAME . DS . $module . DS . 'views' . DS . 'layouts' . DS . $layout . '.phtml'; $layoutFile2 = APPLICATION_PATH . DS . 'modules' . DS . $module . DS . 'views' . DS . 'layouts' . DS . $layout . '.phtml'; if (File::exists($layoutFile)) { $contentL = File::read($layoutFile); $content = str_replace('@@content', str_replace("@@layout('{$layout}')", '', $content), $contentL); } else { if (File::exists($layoutFile2)) { $contentL = File::read($layoutFile2); $content = str_replace('@@content', str_replace("@@layout('{$layout}')", '', $content), $contentL); } } } $content = repl('<php>', '<?php ', $content); $content = repl('<php>=', '<?php echo ', $content); $content = repl('</php>', '?>', $content); $content = repl('{{=', '<?php echo ', $content); $content = repl('{{', '<?php ', $content); $content = repl('}}', '?>', $content); $content = repl('<?=', '<?php echo ', $content); $content = repl('<? ', '<?php ', $content); $content = repl('<?[', '<?php [', $content); $content = repl('[if]', 'if ', $content); $content = repl('[elseif]', 'elseif ', $content); $content = repl('[else if]', 'else if ', $content); $content = repl('[else]', 'else:', $content); $content = repl('[/if]', 'endif;', $content); $content = repl('[for]', 'for ', $content); $content = repl('[foreach]', 'foreach ', $content); $content = repl('[while]', 'while ', $content); $content = repl('[switch]', 'switch ', $content); $content = repl('[/endfor]', 'endfor;', $content); $content = repl('[/endforeach]', 'endforeach;', $content); $content = repl('[/endwhile]', 'endwhile;', $content); $content = repl('[/endswitch]', 'endswitch;', $content); $content = repl('$this->partial(', 'context("view")->partial(', $content); $content = repl('$this->tpl(', 'context("view")->partial(', $content); $content = repl('includes(', 'context("view")->partial(', $content); if (count($this->_grammar)) { foreach ($this->_grammar as $grammar => $replace) { $content = repl($grammar, $replace, $content); } } $base_uri = Config::get('application.base_uri', ''); $content = repl(array('src="/', 'href="/', 'action="/'), array('src="/' . $base_uri, 'href="/' . $base_uri, 'action="/' . $base_uri), $content); return $content; }
public function delete($k) { $tab = $this->unserialize(File::read($this->file)); unset($tab[$k]); File::delete($this->file); File::put($this->file, $this->serialize($tab)); return $this; }
private function makeId() { $file = $this->dir . DS . 'lastid.blazz'; if (is_file($file)) { $last = File::read($file); $new = $last + 1; File::delete($file); File::put($file, $new); return $new; } File::put($file, 1); return 1; }
private function makeId() { $file = $this->getFile('coreids'); if (File::exists($file)) { $last = (int) File::read($file); $new = $last + 1; } else { $new = 1; } File::delete($file); File::put($file, (int) $new); return $new; }
public function read($file) { return File::read($this->hashFile($file)); }
public static function dispatch() { header("Access-Control-Allow-Origin: *"); static::$method = Request::method(); $uri = substr(str_replace('/mobi/', '/', $_SERVER['REQUEST_URI']), 1); $tab = explode('/', $uri); if (!strlen($uri) || $uri == '/') { $namespace = 'static'; $controller = 'home'; $action = 'index'; } else { if (count($tab) < 3) { self::isForbidden(); } $namespace = current($tab); $controller = $tab[1]; $action = $tab[2]; $tab = array_slice($tab, 3); $count = count($tab); if (0 < $count && $count % 2 == 0) { for ($i = 0; $i < $count; $i += 2) { $_REQUEST[$tab[$i]] = $tab[$i + 1]; } } } $file = APPLICATION_PATH . DS . 'mobi' . DS . $namespace . DS . 'controllers' . DS . $controller . '.php'; // dd($file); if (!File::exists($file)) { self::is404(); } require_once $file; $class = 'Thin\\' . ucfirst($controller) . 'Mobi'; $i = new $class(); $methods = get_class_methods($i); $call = strtolower(static::$method) . ucfirst($action); if (!Arrays::in($call, $methods)) { self::is404(); } if (Arrays::in('init', $methods)) { $i->init($call); } $i->{$call}(); if ($i->view === true) { $tpl = APPLICATION_PATH . DS . 'mobi' . DS . $namespace . DS . 'views' . DS . $controller . DS . $action . '.phtml'; if (File::exists($tpl)) { $content = File::read($tpl); $content = str_replace('$this->', '$i->', $content); $fileTpl = CACHE_PATH . DS . sha1($content) . '.display'; File::put($fileTpl, $content); ob_start(); include $fileTpl; $html = ob_get_contents(); ob_end_clean(); File::delete($fileTpl); self::render($html); } else { self::render('OK'); } } if (Arrays::in('after', $methods)) { $i->after(); } }
public static function generate($database, $model, $fields = [], $overwrite = false) { if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw')) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw'); } $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database)) . DS . ucfirst(Inflector::camelize($model)) . '.php'; if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database)))) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database))); } if (!File::exists($file) || $overwrite) { $db = Db::instance($database, $model); $crud = Crud::instance($db); File::delete($file); $tplModel = File::read(__DIR__ . DS . 'Model.tpl'); $tplField = File::read(__DIR__ . DS . 'Field.tpl'); $fields = empty($fields) ? $crud->fields() : $fields; $singular = ucfirst($model); $plural = $singular . 's'; $default_order = $crud->pk(); $tplModel = str_replace(['##singular##', '##plural##', '##default_order##', '##foreigns##', '##uniques##', '##soft_delete##', '##before_create##', '##after_create##', '##before_update##', '##after_update##', '##before_read##', '##after_read##', '##before_delete##', '##after_delete##', '##before_list##', '##after_list##'], [$singular, $plural, $default_order, '[]', '[]', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false'], $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(['##field##', '##form_type##', '##helper##', '##required##', '##form_plus##', '##length##', '##is_listable##', '##is_exportable##', '##is_searchable##', '##is_sortable##', '##is_readable##', '##is_creatable##', '##is_updatable##', '##is_deletable##', '##content_view##', '##content_list##', '##content_search##', '##content_create##', '##label##'], [$field, 'text', 'false', 'true', 'false', 'false', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'false', 'false', 'false', 'false', $label], $tplField); } } $tplModel = str_replace('##fields##', $fieldsSection, $tplModel); File::put($file, $tplModel); } }
public function hget($hash, $key, $default = null) { $file = $this->getHashFile($hash, $key); if (File::exists($file)) { return unserialize(File::read($file)); } return $default; }
public function __destruct() { $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudJson' . DS . ucfirst(Inflector::camelize($this->model->db)) . DS . ucfirst(Inflector::camelize($this->model->table)) . '.php'; if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudJson' . DS . ucfirst(Inflector::camelize($this->model->db)))) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudJson' . DS . ucfirst(Inflector::camelize($this->model->db))); } if (!File::exists($file) || $this->replace) { File::delete($file); $tplModel = File::read(__DIR__ . DS . 'Model.tpl'); $tplField = File::read(__DIR__ . DS . 'Field.tpl'); $uniques = isAke($this->indices, 'unique', []); $foreigns = isAke($this->indices, 'foreign', []); $softDelete = true === $this->softDelete ? 'true' : 'false'; $uString = '['; if (count($uniques)) { foreach ($uniques as $unique) { $uString .= "'" . $unique . "'" . ','; } $uString = substr($uString, 0, -1); } $uString .= ']'; $fString = '['; if (count($foreigns)) { foreach ($foreigns as $foreign) { $fString .= "'" . $foreign . "'" . ','; } $fString = substr($fString, 0, -1); } $fString .= ']'; $before_create = $this->getHook('before_create'); $after_create = $this->getHook('after_create'); $before_update = $this->getHook('before_update'); $after_update = $this->getHook('after_update'); $before_read = $this->getHook('before_read'); $after_read = $this->getHook('after_read'); $before_delete = $this->getHook('before_delete'); $after_delete = $this->getHook('after_delete'); $before_list = $this->getHook('before_list'); $after_list = $this->getHook('after_list'); $tplModel = str_replace(['##singular##', '##plural##', '##default_order##', '##soft_delete##', '##foreigns##', '##uniques##', '##before_create##', '##after_create##', '##before_update##', '##after_update##', '##before_read##', '##after_read##', '##before_delete##', '##after_delete##', '##before_list##', '##after_list##'], [$this->singular, $this->plural, $this->order, $softDelete, $fString, $uString, $before_create, $after_create, $before_update, $after_update, $before_read, $after_read, $before_delete, $after_delete, $before_list, $after_list], $tplModel); $fieldsSection = ''; foreach ($this->fields as $field => $infos) { if ($field != 'id') { $label = $this->getSetting($field, 'label', ucfirst($field)); $form_type = $this->getSetting($field, 'form_type', 'text'); $helper = $this->getSetting($field, 'helper', 'false'); $required = $this->getSetting($field, 'required', 'true'); $form_plus = $this->getSetting($field, 'form_plus', 'false'); $length = $this->getSetting($field, 'length', 'false'); $is_listable = $this->getSetting($field, 'is_listable', 'true'); $is_exportable = $this->getSetting($field, 'is_exportable', 'true'); $is_searchable = $this->getSetting($field, 'is_searchable', 'true'); $is_sortable = $this->getSetting($field, 'is_sortable', 'true'); $is_readable = $this->getSetting($field, 'is_readable', 'true'); $is_creatable = $this->getSetting($field, 'is_creatable', 'true'); $is_updatable = $this->getSetting($field, 'is_updatable', 'true'); $is_deletable = $this->getSetting($field, 'is_deletable', 'true'); $content_view = $this->getSetting($field, 'content_view', 'false'); $content_list = $this->getSetting($field, 'content_list', 'false'); $content_search = $this->getSetting($field, 'content_search', 'false'); $content_create = $this->getSetting($field, 'content_create', 'false'); $fieldsSection .= str_replace(['##field##', '##form_type##', '##helper##', '##required##', '##form_plus##', '##length##', '##is_listable##', '##is_exportable##', '##is_searchable##', '##is_sortable##', '##is_readable##', '##is_creatable##', '##is_updatable##', '##is_deletable##', '##content_view##', '##content_list##', '##content_search##', '##content_create##', '##label##'], [$field, $form_type, $helper, $required, $form_plus, $length, $is_listable, $is_exportable, $is_searchable, $is_sortable, $is_readable, $is_creatable, $is_updatable, $is_deletable, $content_view, $content_list, $content_search, $content_create, $label], $tplField); } } $tplModel = str_replace('##fields##', $fieldsSection, $tplModel); File::put($file, $tplModel); } }
public function readCache($key) { $age = filemtime($this->dir); $file = $this->cacheDir . DS . $key; if (file_exists($file) && filemtime($file) >= $age) { return unserialize(File::read($file)); } File::delete($file); return null; }
public function getFieldValueById($field, $id, $d = null) { $file = $this->dir . DS . $field . DS . $id . '.blazz'; if (file_exists($file)) { return unserialize(File::read($file)); } return $d; }
public static function dispatch() { lib('lang')->locale('web'); $controller = isAke(static::$route, 'controller', false); $action = isAke(static::$route, 'action', false); $file = APPLICATION_PATH . DS . 'front' . DS . 'controllers' . DS . $controller . '.php'; $tpl = APPLICATION_PATH . DS . 'front' . DS . 'views' . DS . $controller . DS . $action . '.phtml'; if (!File::exists($file)) { static::is404(); } require_once $file; $class = 'Thin\\' . ucfirst($controller) . 'Controller'; $i = new $class(); if (File::exists($tpl)) { $i->view = new Container(); if (static::$pjax) { $i->view->partial = function ($partial) { return true; }; } else { $i->view->partial = function ($partial) use($i) { $tpl = APPLICATION_PATH . DS . 'front' . DS . 'views' . DS . 'partials' . DS . $partial . '.phtml'; if (File::exists($tpl)) { $code = View::lng(File::read($tpl)); $code = str_replace('$this', '$i->view', $code); eval('; ?>' . $code . '<?php ;'); } }; } } $methods = get_class_methods($i); $call = strtolower(static::$method) . ucfirst($action); if (!Arrays::in($call, $methods)) { static::is404(); } if (Arrays::in('init', $methods)) { $i->init(); } $i->{$call}(); if (Arrays::in('after', $methods)) { $i->after(); } if (File::exists($tpl)) { $code = View::lng(File::read($tpl)); $code = str_replace('$this', '$i->view', $code); header("HTTP/1.0 200 OK"); eval('; ?>' . $code . '<?php ;'); exit; } }
public function repo() { $view = false; if (fnmatch('view_*_*', $this->table)) { $tab = explode('_', $this->table); $db = $tab[1]; $table = $tab[2]; $view = true; } else { $db = $this->db; $table = $this->table; } $repoFile = APPLICATION_PATH . DS . 'models' . DS . 'Bigdata' . DS . 'repositories' . DS . Inflector::lower($db) . DS . ucfirst(Inflector::lower($table)) . '.php'; if (!File::exists($repoFile)) { File::put($repoFile, str_replace('##class##', ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'Repository', File::read(__DIR__ . DS . 'dbRepo.tpl'))); } $class = '\\Thin\\' . ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'Repository'; if (!class_exists($class)) { require_once $repoFile; } $model = $this; if (true === $view) { $model = self::instance($db, $table); } return new $class($model); }
public function read($file, $default = null, $mode = 'rb', $try = 1) { $file = $this->dir . DS . $file . '.php'; if (File::exists($file)) { $fp = fopen($file, 'rb'); if (!flock($fp, LOCK_EX)) { if ($try < 100) { usleep(50000); return $this->read($file, $default, $mode, $try++); } else { throw new Exception("The file '{$file}' can not be locked."); } } $data = unserialize(File::read($file)); flock($fp, LOCK_UN); fclose($fp); return $data; } return $default; }
/** * Returns the full contents of the file for this frame, * if it's known. * @return string|null */ public function getFileContents() { if ($this->fileContentsCache === null && ($filePath = $this->getFile())) { // Return null if the file doesn't actually exist - this may // happen in cases where the filename is provided as, for // example, 'Unknown' if (!File::exists($filePath)) { return null; } $this->fileContentsCache = File::read($filePath); } return $this->fileContentsCache; }