作者:noiki
项目:inov
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;
}
}
作者:schpil
项目:thi
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);
}
}
}
作者:schpil
项目:thi
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);
}
}
作者:schpil
项目:standalon
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');
}
}
}
作者:schpil
项目:standalon
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();
}
作者:schpil
项目:standalon
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();
}
}
作者:schpil
项目:standalon
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];
}
作者:schpil
项目:thi
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;
}
作者:schpil
项目:standalon
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.');
}
作者:schpil
项目:thi
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();
}
作者:noiki
项目:inov
/**
* 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;
}
作者:schpil
项目:blo
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.");
}
作者:schpil
项目:thi
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;
}
作者:schpil
项目:standalon
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);
}
}
}
作者:schpil
项目:standalon
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);
}
}
}
作者:schpil
项目:standalon
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);
}
}
}
作者:schpil
项目:standalon
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;
}
作者:schpil
项目:thi
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);
}
作者:schpil
项目:standalon
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.");
}
作者:schpil
项目:standalon
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);
}
}