public function __call($event, $args) { if (substr($event, 0, 3) == 'get' && strlen($event) > 3) { $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3))); $key = Inflector::lower($uncamelizeMethod); return $this->get($key); } elseif (substr($event, 0, 3) == 'set' && strlen($event) > 3) { $value = Arrays::first($args); $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3))); $key = Inflector::lower($uncamelizeMethod); return $this->set($key, $value); } if (true === $this->__has($event)) { array_push($args, $this); return $this->__fire($event, $args); } else { if (method_exists($this, $event)) { throw new Exception("The method {$event} is a native class' method. Please choose an other name."); } $value = Arrays::first($args); if ($value instanceof Closure) { $eventable = $this->__event($event, $value); } else { $set = function () use($value) { return $value; }; $eventable = $this->__event($event, $set); } return $this; } }
public function __call($event, $args) { if (substr($event, 0, 3) == 'get') { $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3))); $key = Inflector::lower($uncamelizeMethod); return self::get($key); } elseif (substr($event, 0, 3) == 'set') { $value = Arrays::first($args); $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3))); $key = Inflector::lower($uncamelizeMethod); return self::set($key, $value); } if (true === self::__has($event)) { return self::__fire($event, $args); } else { $value = Arrays::first($args); if ($value instanceof Closure) { $eventable = self::__event($event, $value); } else { $set = function () use($value) { return $value; }; $eventable = self::__event($event, $set); } } }
public function __call($func, $argv) { if (substr($func, 0, 3) == 'get' && strlen($func) > 3) { $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3))); $var = Inflector::lower($uncamelizeMethod); if (isset($this->{$var})) { $this->clean(); return $this->{$var}; } else { return null; } } elseif (substr($func, 0, 3) == 'set' && strlen($func) > 3) { $value = $argv[0]; $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3))); $var = Inflector::lower($uncamelizeMethod); $this->{$var} = $value; return $this->save(); } elseif (substr($func, 0, 6) == 'forget' && strlen($func) > 6) { $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 6))); $var = Inflector::lower($uncamelizeMethod); $this->erase($var); return $this->save(); } $id = sha1($func); if (isset($this->{$id})) { if ($this->{$id} instanceof \Closure) { return call_user_func_array($this->{$id}, $argv); } } if (!is_callable($func) || substr($func, 0, 3) !== 'set' || substr($func, 0, 3) !== 'get') { throw new \BadMethodCallException(__CLASS__ . ' => ' . $func); } }
public function run() { if (fnmatch('*cli*', php_sapi_name())) { $dir = Config::get('dir.schedules', APPLICATION_PATH . DS . 'schedules'); if (is_dir($dir)) { Timer::start(); Cli::show("Start of execution", 'COMMENT'); $files = glob($dir . DS . '*.php'); foreach ($files as $file) { require_once $file; $object = str_replace('.php', '', Arrays::last(explode(DS, $file))); $class = 'Thin\\' . ucfirst(Inflector::camelize($object . '_schedule')); $instance = lib('app')->make($class); $methods = get_class_methods($instance); Cli::show("Start schedule '{$object}'", 'COMMENT'); foreach ($methods as $method) { $when = $this->getWhen($instance, $method); $isDue = $this->isDue($object, $method, $when); if (true === $isDue) { Cli::show("Execution of {$object}->{$method}", 'INFO'); $instance->{$method}(); } else { Cli::show("No need to execute {$object}->{$method}", 'QUESTION'); } } } Cli::show("Time of execution [" . Timer::get() . " s.]", 'SUCCESS'); Cli::show("end of execution", 'COMMENT'); } } }
public function __destruct() { /* On efface le model de la base tampon et on vide la base */ $modelFile = APPLICATION_PATH . DS . 'models' . DS . 'Bigdata' . DS . 'models' . DS . Inflector::lower($this->to->db) . DS . ucfirst(Inflector::lower($this->to->table)) . '.php'; File::delete($modelFile); $this->to->drop(); }
public function __construct($db) { $this->orm = $db; $this->db = $db->db(); $this->table = $db->table(); $dir = Config::get('dir.blizz.store', session_save_path()); if (!is_dir($dir)) { File::mkdir($dir); } $dir .= DS . Inflector::urlize(Inflector::uncamelize($this->db)); if (!is_dir($dir)) { File::mkdir($dir); } $this->dir = $dir . DS . Inflector::urlize(Inflector::uncamelize($this->table)); if (!is_dir($this->dir)) { File::mkdir($this->dir); } $file = $this->dir . DS . 'data.db'; $new = false; if (!is_file($file)) { File::create($file); $new = true; File::put($this->dir . DS . 'age.blizz', ''); } $link = new SQLite3($file); Now::set("blizz.link.{$this->db}.{$this->table}", $link); if ($new) { $this->init(); } }
public static function __callStatic($fn, $args) { $method = Inflector::upper($fn); if (count($args) < 2) { throw new Exception("You must provide at least a path and a mvc pattern."); } $path = $args[0]; $mvc = $args[1]; $argsRoute = []; $optionsRoute = []; if (count($args) > 2) { $argsRoute = $args[2]; if (count($args) > 3) { array_shift($args); array_shift($args); array_shift($args); $optionsRoute = $args; } } list($module, $controller, $action) = explode('::', $mvc, 3); if (!isset($module) || !isset($controller) || !isset($action)) { throw new Exception("MVC '{$mvc}' is incorrect."); } return ['name' => Inflector::lower($method) . '::' . $module . '::' . $controller . '::' . $action, 'method' => $method, 'path' => $path, 'module' => $module, 'controller' => $controller, 'action' => $action, 'args' => $argsRoute, 'options' => $optionsRoute]; }
public static function callback($class, $method, Closure $cb) { $callbackClass = ucfirst(Inflector::lower($class)); if (!class_exists('Thin\\' . $callbackClass)) { eval("namespace Thin; class {$callbackClass} extends Alias {}"); } static::$cb[$callbackClass][$method] = $cb; }
public static function __callStatic($method, $args) { if (count($args)) { $type = Inflector::upper($method); $fnArgs = array_merge([$type], $args); return call_user_func_array('staticLog', $fnArgs); } throw new Exception('You must provide a message to log.'); }
public function register() { $args = func_get_args(); if (count($args) < 1 || count($args) > 1) { throw new Exception("You need to provide a helper to register."); } $helper = reset($args); $this->type = 'helpers'; $this->file = APPLICATION_PATH . DS . 'helpers' . DS . ucfirst(Inflector::lower($helper)) . DS . ucfirst(Inflector::lower($helper)) . '.php'; $this->class = 'ThinHelper\\' . ucfirst(Inflector::lower($helper)) . '\\' . ucfirst(Inflector::lower($helper)); return $this->factory(); }
/** * Creates a new request with values from PHP's super globals. * * @return Request A new request * * @api */ public static function createFromGlobals() { $request = new static($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER); if ((0 === strpos($request->server->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded') || 0 === strpos($request->server->get('HTTP_CONTENT_TYPE'), 'application/x-www-form-urlencoded')) && in_array(\Thin\Inflector::upper($request->server->get('REQUEST_METHOD', 'GET')), array('PUT', 'DELETE', 'PATCH'))) { parse_str($request->getContent(), $data); if (magic_quotes()) { $data = arrayStripslashes($data); } $request->request = new ParameterBag($data); } return $request; }
function run($lib, $args = null) { $lib = Inflector::lower(Inflector::uncamelize($lib)); $script = str_replace('_', DS, $lib) . '.php'; if (fnmatch('*_*', $lib)) { $class = 'Thin\\' . str_replace('_', '\\', $lib); $tab = explode('\\', $class); $first = $tab[1]; $class = str_replace('Thin\\' . $first, 'Thin\\' . ucfirst($first) . 'Libs', $class); if (count($tab) > 2) { for ($i = 2; $i < count($tab); $i++) { $seg = trim($tab[$i]); $class = str_replace('\\' . $seg, '\\' . ucfirst($seg), $class); } } } else { $class = 'Thin\\' . ucfirst($lib) . 'Libs'; } $file = __DIR__ . DS . 'libs' . DS . $script; if (is_file($file)) { require_once $file; if (empty($args)) { return new $class(); } else { if (!is_array($args)) { if (is_string($args)) { if (fnmatch('*,*', $args)) { $args = explode(',', str_replace(', ', ',', $args)); } else { $args = [$args]; } } else { $args = [$args]; } } $methods = get_class_methods($class); if (in_array('instance', $methods)) { return call_user_func_array([$class, 'instance'], $args); } else { return construct($class, $args); } } } if (class_exists('Thin\\' . $lib)) { $c = 'Thin\\' . $lib; return new $c(); } if (class_exists($lib)) { return new $lib(); } throw new Exception("The library {$class} does not exist."); }
public function prepare($text) { $slugs = explode(' ', Inflector::slug($text, ' ')); if (count($slugs)) { $collection = array(); foreach ($slugs as $slug) { if (strlen($slug) > 1) { if (!Arrays::in($slug, $collection)) { array_push($collection, $slug); } } } asort($collection); } return $collection; }
public static function __callStatic($method, $args) { $db = Inflector::uncamelize($method); if (fnmatch('*_*', $db)) { list($database, $table) = explode('_', $db, 2); } else { $database = SITE_NAME; $table = $db; } if (empty($args)) { return new Redis($database, $table); } elseif (count($args) == 1) { $id = Arrays::first($args); if (is_numeric($id)) { return with(new Redis($database, $table))->find($id); } } }
public static function __callStatic($method, $args) { $db = Inflector::uncamelize($method); if (fnmatch('*_*', $db)) { list($database, $table) = explode('_', $db, 2); } else { $database = SITE_NAME; $table = $db; } if (empty($args)) { return Db::instance($database, $table); } elseif (count($args) == 1) { $id = current($args); if (is_numeric($id)) { return Db::instance($database, $table)->find((int) $id); } } }
public static function flush($db = null, $table = null, $name = null) { $db = is_null($db) ? '*' : $db; $table = is_null($table) ? '*' : $table; $name = is_null($name) ? '*' : Inflector::urlize($name, '.'); if ($db != '*' && $table != '*') { $cache = Database::instance($db, $table)->cache(); } else { $cache = Database::instance('auth', 'user')->cache(); } $hashes = $cache->keys("dbjson::cachedQueries::{$db}::{$table}"); $ages = $cache->keys("dbjson::cachedQueries::{$db}::{$table}::{$name}::age"); if (count($hashes)) { foreach ($hashes as $hash) { $cache->del($hash); } } if (count($ages)) { foreach ($ages as $age) { $cache->del($age); } } }
public static function make($db, $what) { if (is_string($what)) { $file = APPLICATION_PATH . DS . 'models' . DS . 'Bigdata' . DS . 'views' . DS . $db->db . DS . ucfirst(I::lower($db->table)) . DS . I::camelize($what) . '.php'; if (files()->exists($file)) { require_once $file; $class = '\\Thin\\' . ucfirst(I::lower($db->db)) . ucfirst(I::lower($db->table)) . 'View'; return $class::make($db); } else { return $db; } } elseif (A::is($what)) { $nameview = 'view_' . $db->db . '_' . $db->table . '_' . sha1(serialize($what)); $ageDb = $db->getage(); $viewDb = Db::instance($db->db, $nameview); $ageView = $db->getage(); $exists = strlen($db->cache()->get('dbRedis.views.' . $nameview)) ? true : false; if ($ageView < $ageDb || !$exists) { $viewDb->getCollection()->remove(); foreach ($what as $wh) { $op = 'AND'; if (count($wh) == 4) { $op = $wh[3]; unset($wh[3]); } $db = $db->where($wh); } $res = $db->exec(); foreach ($res as $row) { $viewDb->saveView($row); } $db->cache()->set('dbRedis.views.' . $nameview, 1); } return $viewDb; } }
private function compare($comp, $op, $value) { $res = false; if (strlen($comp) && strlen($op) && !empty($value)) { if (is_numeric($comp)) { if (fnmatch('*,*', $comp) || fnmatch('*.*', $comp)) { $comp = floatval($comp); } else { $comp = intval($comp); } } if (is_numeric($value)) { if (fnmatch('*,*', $value) || fnmatch('*.*', $value)) { $value = floatval($value); } else { $value = intval($value); } } switch ($op) { case '=': $res = sha1($comp) == sha1($value); break; case '=i': $comp = Inflector::lower(Inflector::unaccent($comp)); $value = Inflector::lower(Inflector::unaccent($value)); $res = sha1($comp) == sha1($value); break; case '>=': $res = $comp >= $value; break; case '>': $res = $comp > $value; break; case '<': $res = $comp < $value; break; case '<=': $res = $comp <= $value; break; case '<>': case '!=': $res = sha1($comp) != sha1($value); break; case 'LIKE': $value = str_replace("'", '', $value); $value = str_replace('%', '*', $value); $res = fnmatch($value, $comp); break; case 'NOT LIKE': case 'NOTLIKE': $value = str_replace("'", '', $value); $value = str_replace('%', '*', $value); $check = fnmatch($value, $comp); $res = !$check; break; case 'BETWEEN': $res = $comp >= $value[0] && $comp <= $value[1]; break; case 'NOT BETWEEN': case 'NOTBETWEEN': $res = $comp < $value[0] || $comp > $value[1]; break; case 'LIKE START': case 'LIKESTART': $value = str_replace(["'", '%'], '', $value); $res = substr($comp, 0, strlen($value)) === $value; break; case 'LIKE END': case 'LIKEEND': $value = str_replace(["'", '%'], '', $value); if (!strlen($comp)) { $res = true; } $res = substr($comp, -strlen($value)) === $value; break; case 'IN': $value = str_replace('(', '', $value); $value = str_replace(')', '', $value); $tabValues = explode(',', $value); $res = in_array($comp, $tabValues); break; case 'NOT IN': case 'NOTIN': $value = str_replace('(', '', $value); $value = str_replace(')', '', $value); $tabValues = explode(',', $value); $res = !in_array($comp, $tabValues); break; } } return $res; }
private function parseQuery($query) { $groupBy = array(); $orderBy = array(); $orderDir = array(); $wheres = array(); $limit = 0; $offset = 0; $query = preg_replace('/\\s+/u', ' ', $query); $query = preg_replace('/[\\)`\\s]from[\\(`\\s]/ui', ' FROM ', $query); if (preg_match('/(limit([0-9\\s\\,]+)){1}$/ui', $query, $matches)) { $query = str_ireplace(Arrays::first($matches), '', $query); $tmp = explode(',', $matches[2]); if (isset($tmp[1])) { $offset = (int) trim(Arrays::first($tmp)); $limit = (int) trim($tmp[1]); } else { $offset = 0; $limit = (int) trim(Arrays::first($tmp)); } } if (preg_match('/(order\\sby([^\\(\\)]+)){1}$/ui', $query, $matches)) { $query = str_ireplace(Arrays::first($matches), '', $query); $tmp = explode(',', $matches[2]); foreach ($tmp as $item) { $item = trim($item); $direct = mb_strripos($item, ' desc') == mb_strlen($item) - 5 || mb_strripos($item, '`desc') == mb_strlen($item) - 5 ? 'desc' : 'asc'; $item = str_ireplace(array(' asc', ' desc', '`asc', '`desc', '`'), '', $item); $orderBy[] = $item; $orderDir[] = Inflector::upper($direct); } } if (preg_match('/(group\\sby([^\\(\\)]+)){1}$/ui', $query, $matches)) { $query = str_ireplace(Arrays::first($matches), '', $query); $tmp = explode(',', $matches[2]); foreach ($tmp as $item) { $item = trim($item); $groupBy[] = $item; } } $tmp = preg_replace_callback('/\\( (?> [^)(]+ | (?R) )+ \\)/xui', array($this, 'queryParamsCallback'), $query); $words = explode(' ', $query); $method = Inflector::lower(Arrays::first($words)); $parts = explode(' where ', Inflector::lower($query)); if (2 == count($parts)) { $whs = Arrays::last($parts); $whs = str_replace(array(' and ', ' or ', ' xor ', ' && ', ' || ', ' | '), array(' AND ', ' OR ', ' XOR ', ' AND ', ' OR ', ' XOR '), $whs); $wheres['AND'] = strstr($whs, ' AND ') ? explode(' AND ', $whs) : array(); $wheres['OR'] = strstr($whs, ' OR ') ? explode(' OR ', $whs) : array(); $wheres['XOR'] = strstr($whs, ' XOR ') ? explode(' XOR ', $whs) : array(); } return array('method' => $method, 'wheres' => $wheres, 'groupBy' => $groupBy, 'orderBy' => $orderBy, 'orderDir' => $orderDir, 'limit' => $limit, 'offset' => $offset); }
public function __call($fn, $args) { $method = substr($fn, 0, strlen('findLastBy')); $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('findLastBy')))); if (strlen($fn) > strlen('findLastBy')) { if ('findLastBy' == $method) { $obj = count($args) == 2 ? $args[1] : true; if (!is_bool($obj)) { $obj = true; } return $this->where([$object, '=', Arrays::first($args)])->last($obj); } } $method = substr($fn, 0, strlen('findFirstBy')); $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('findFirstBy')))); if (strlen($fn) > strlen('findFirstBy')) { if ('findFirstBy' == $method) { $obj = count($args) == 2 ? $args[1] : true; if (!is_bool($obj)) { $obj = true; } return $this->findFirstBy($object, Arrays::first($args), $obj); } } $method = substr($fn, 0, strlen('findOneBy')); $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('findOneBy')))); if (strlen($fn) > strlen('findOneBy')) { if ('findOneBy' == $method) { $obj = count($args) == 2 ? $args[1] : false; if (!is_bool($obj)) { $obj = false; } return $this->findOneBy($object, Arrays::first($args), $obj); } } $method = substr($fn, 0, strlen('orderBy')); $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('orderBy')))); if (strlen($fn) > strlen('orderBy')) { if ('orderBy' == $method) { $fields = $this->fieldsRow(); if (!Arrays::in($object, $fields) && 'id' != $object) { $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object; } $direction = !empty($args) ? Arrays::first($args) : 'ASC'; return $this->order($object, $direction); } elseif ('groupBy' == $method) { $fields = $this->fieldsRow(); if (!Arrays::in($object, $fields)) { $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object; } return $this->groupBy($object); } } $method = substr($fn, 0, strlen('where')); $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('where')))); if (strlen($fn) > strlen('where')) { if ('where' == $method) { return $this->where([$object, '=', Arrays::first($args)]); } } $method = substr($fn, 0, strlen('sortBy')); $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('sortBy')))); if (strlen($fn) > strlen('sortBy')) { if ('sortBy' == $method) { $fields = $this->fieldsRow(); if (!Arrays::in($object, $fields) && 'id' != $object) { $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object; } $direction = !empty($args) ? Arrays::first($args) : 'ASC'; return $this->order($object, $direction); } elseif ('findBy' == $method) { $obj = count($args) == 2 ? $args[1] : false; if (!is_bool($obj)) { $obj = false; } return $this->findBy($object, Arrays::first($args), false, $obj); } } $model = $this->model(); $scope = lcfirst(Inflector::camelize('scope_' . Inflector::uncamelize($fn))); if (method_exists($model, $scope)) { return call_user_func_array([$model, $scope], $args); } throw new Exception("Method '{$fn}' is unknown."); }
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); } }
/** * * @method __call * * @param [type] * @param [type] * * @return [type] */ public function __call($m, $a) { if (fnmatch('get*', $m)) { $key = Inflector::uncamelize(substr($m, 3)); $default = empty($a) ? null : current($a); return $this->get($key, $default); } elseif (fnmatch('set*', $m)) { $key = Inflector::uncamelize(substr($m, 3)); return $this->set($key, current($a)); } elseif (fnmatch('has*', $m)) { $key = Inflector::uncamelize(substr($m, 3)); return $this->has($key); } elseif (fnmatch('del*', $m)) { $key = Inflector::uncamelize(substr($m, 3)); return $this->delete($key); } else { $closure = $this->get($m); if (is_string($closure) && fnmatch('*::*', $closure)) { list($c, $f) = explode('::', $closure, 2); try { $i = lib('caller')->make($c); return call_user_func_array([$i, $f], $a); } catch (\Exception $e) { $default = empty($a) ? null : current($a); return empty($closure) ? $default : $closure; } } else { if (is_callable($closure)) { return call_user_func_array($closure, $a); } if (!empty($a) && empty($closure)) { if (count($a) == 1) { return $this->set($m, current($a)); } } $default = empty($a) ? null : current($a); return empty($closure) ? $default : $closure; } } }
public function __call($m, $a) { if (fnmatch('get*', $m)) { $k = Inflector::uncamelize(substr($m, 3)); $default = empty($a) ? null : current($a); return $this->get($k, $default); } elseif (fnmatch('set*', $m)) { $k = Inflector::uncamelize(substr($m, 3)); return $this->set($k, current($a)); } elseif (fnmatch('has*', $m)) { $k = Inflector::uncamelize(substr($m, 3)); return $this->has($k); } elseif (fnmatch('del*', $m)) { $k = Inflector::uncamelize(substr($m, 3)); return $this->del($k); } else { if (!empty($a)) { if (count($a) == 1) { return $this->set($m, current($a)); } } $closure = $this->get($m); if (fnmatch('*::*', $closure)) { list($c, $f) = explode('::', $closure, 2); $i = lib('caller')->make($c); return call_user_func_array([$i, $f], $a); } else { if (is_callable($closure)) { return call_user_func_array($closure, $a); } return $closure; } } }
private function map() { $q = "SHOW COLUMNS FROM {$this->table}"; $res = $this->query($q, false); if (empty($res)) { throw new Exception("The system cannot access to the table {$this->table} on {$this->db}."); } $conf = $pks = $keys = []; foreach ($res as $data) { $field = $data['Field']; $conf[$field] = []; $conf[$field]['type'] = typeSql($data['Type']); $conf[$field]['nullable'] = 'yes' == Inflector::lower($data['Null']) ? true : false; if ($data['Key'] == 'PRI') { array_push($pks, $field); } if (strlen($data['Key']) && $data['Key'] != 'PRI') { array_push($keys, $field); } } $this->pks = $pks; $this->keys = $keys; $this->fields = $conf; }
private function related($obj) { $settings = isAke(self::$config, "{$this->database}.{$this->table}"); $relations = isAke($settings, 'relations'); $params = $this->args; if (count($relations)) { foreach ($relations as $relation) { $field = $relation . '_id'; if (is_string($field)) { $value = $obj->{$field}; if (!is_callable($value)) { $fk = $tableFk = $relation; $fks = $fk . 's'; $cb = function ($object = true) use($value, $tableFk, $params) { list($database, $table, $host, $username, $password) = $params; $db = Database::instance($database, $tableFk, $host, $username, $password); if ($db) { return $db->find($value, $object); } return null; }; $obj->event($fk, $cb); $cb = function ($object = true) use($value, $tableFk, $params) { list($database, $table, $host, $username, $password) = $params; $db = Database::instance($database, $tableFk, $host, $username, $password); if ($db) { return $db->where($db->pk() . " = '" . addslashes($value) . "'")->exec($object); } return null; }; $obj->event($fks, $cb); $setter = lcfirst(Inflector::camelize("link_{$fk}")); $cb = function (Container $fkObject) use($obj, $field, $fk) { $obj->{$field} = $fkObject->id(); $newCb = function () use($fkObject) { return $fkObject; }; $obj->event($fk, $newCb); return $obj; }; $obj->event($setter, $cb); } } } } return $obj; }
public function check($id, $html) { require_once APPLICATION_PATH . DS . '..' . '/public/vendeur/lib/simple_html_dom.php'; $str = str_get_html($html); $segLangs = $str->find('lang'); foreach ($segLangs as $segLang) { $default = $segLang->innertext; $args = $segLang->args; if (!empty($args)) { $replace = "<lang args=\"{$args}\">{$default}</lang>"; } else { $args = '[]'; $replace = "<lang>{$default}</lang>"; } $by = '<?php __(\'' . $default . '\', \'' . $id . '.' . Inflector::urlize($default, '-') . '\', ' . $args . '); ?>'; $html = str_replace($replace, $by, $html); } return $html; }
private function upload($field) { $bucket = container()->bucket(); if (Arrays::exists($field, $_FILES)) { $fileupload = $_FILES[$field]['tmp_name']; $fileuploadName = $_FILES[$field]['name']; if (strlen($fileuploadName)) { $tab = explode(".", $fileuploadName); $data = fgc($fileupload); if (!strlen($data)) { return null; } $ext = Inflector::lower(Arrays::last($tab)); $res = $bucket->data($data, $ext); return $res; } } return null; }
public function __call($m, $a) { if (fnmatch('set*', $m)) { $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set')))); $field = Inflector::lower($uncamelizeMethod); if (!empty($a)) { $val = current($a); } else { $val = null; } $this->{$field} = $val; return $this; } elseif (fnmatch('get*', $m)) { $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set')))); $field = Inflector::lower($uncamelizeMethod); $default = count($a) == 1 ? current($a) : null; return isset($this->{$field}) ? $this->{$field} : $default; } else { return call_user_func_array([$this->_db], $a); } }
public static function __callStatic($fn, $args) { $method = Inflector::uncamelize($fn); $tab = explode('_', $method); $table = array_shift($tab); $function = implode('_', $tab); $function = lcfirst(Inflector::camelize($function)); $instance = self::instance(SITE_NAME, $table); return call_user_func_array([$instance, $function], $args); }
public static function generate($model, $overwrite = false) { $file = APPLICATION_PATH . DS . 'models' . DS . 'Crud' . DS . ucfirst(Inflector::camelize($model)) . '.php'; if (!File::exists($file) || $overwrite) { $db = model($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); } }