Example #1
0
 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');
         }
     }
 }
Example #2
0
 public function beginTransaction()
 {
     $last = Arrays::last(explode(DS, $this->dir));
     $target = str_replace(DS . $last, DS . 'copy.' . $last, $this->dir);
     File::cpdir($this->dir, $target);
     $this->dir = $target;
     return $this;
 }
Example #3
0
 public function all($dir)
 {
     $path = $this->path . DS . str_replace('.', DS, $dir);
     $files = glob($path . DS . '*.php');
     $collection = [];
     foreach ($files as $file) {
         $content = (include $file);
         if (!Arrays::isAssoc($content)) {
             $content = current($content);
         }
         $id = (int) str_replace('.php', '', Arrays::last(explode(DS, $file)));
         $collection[$id] = $content;
     }
     return $collection;
 }
Example #4
0
 public function all($dir)
 {
     $path = $this->collection . '.' . $dir;
     $files = $this->cache()->keys($path . '.*');
     $collection = [];
     foreach ($files as $file) {
         $content = $this->import($file);
         if (!Arrays::isAssoc($content)) {
             $content = current($content);
         }
         $id = (int) Arrays::last(explode('.', $file));
         $collection[$id] = $content;
     }
     return $collection;
 }
Example #5
0
 public function __construct()
 {
     $class = get_called_class();
     if (strstr($class, '\\')) {
         $class = Arrays::last(explode('\\', $class));
         $class = str_replace('Model_', '', $class);
     }
     if (fnmatch('*_*', $class)) {
         list($database, $table) = explode('_', $class, 2);
     } else {
         $database = SITE_NAME;
         $table = $class;
     }
     self::$db = Db::instance($database, $table);
     $this->model = self::$db->model(func_get_args());
 }
Example #6
0
 public static function run()
 {
     Request::$route = $route = Utils::get('appDispatch');
     container()->setRoute($route);
     $render = $route->getRender();
     $tplDir = $route->getTemplateDir();
     $module = $route->getModule();
     $controller = $route->getController();
     $action = $route->getAction();
     $alert = $route->getAlert();
     $page = container()->getPage();
     $isCms = !empty($page);
     if (!empty($render)) {
         $tplMotor = $route->getTemplateMotor();
         $tplDir = empty($tplDir) ? APPLICATION_PATH . DS . SITE_NAME . DS . 'app' . DS . 'views' : $tplDir;
         $tpl = $tplDir . DS . $render . '.phtml';
         if (File::exists($tpl)) {
             if ('Twig' == $tplMotor) {
                 if (!class_exists('Twig_Autoloader')) {
                     require_once 'Twig/Autoloader.php';
                 }
                 $tab = explode(DS, $tpl);
                 $file = Arrays::last($tab);
                 $path = repl(DS . $file, '', $tpl);
                 $loader = new \Twig_Loader_Filesystem($path);
                 $view = new \Twig_Environment($loader, array('cache' => CACHE_PATH, 'debug' => false, 'charset' => 'utf-8', 'strict_variables' => false));
                 container()->setView($view);
                 if ($action instanceof Closure) {
                     $action($view);
                 }
                 $params = null === container()->getViewParams() ? array() : container()->getViewParams();
                 echo $view->render($file, $params);
                 /* stats */
                 if (null === container()->getNoShowStats() && null === $route->getNoShowStats()) {
                     echo View::showStats();
                 }
             } else {
                 $view = new View($tpl);
                 container()->setView($view);
                 if ($action instanceof Closure) {
                     $action($view);
                 }
                 $view->render();
                 /* stats */
                 if (null === container()->getNoShowStats() && null === $route->getNoShowStats()) {
                     echo View::showStats();
                 }
             }
             return;
         }
     }
     $module = Inflector::lower($module);
     $controller = Inflector::lower($controller);
     $action = Inflector::lower($action);
     if (true === container()->getMultiSite()) {
         $moduleDir = APPLICATION_PATH . DS . SITE_NAME . DS . 'modules' . DS . $module;
     } else {
         $moduleDir = APPLICATION_PATH . DS . 'modules' . DS . $module;
     }
     if (!is_dir($moduleDir)) {
         throw new Exception("The module '{$module}' does not exist.");
     }
     $controllerDir = $moduleDir . DS . 'controllers';
     if (!is_dir($controllerDir)) {
         throw new Exception("The controller '{$controller}' does not exist.");
     }
     $controllerFile = $controllerDir . DS . $controller . 'Controller.php';
     if (!File::exists($controllerFile)) {
         throw new Exception("The controller '{$controllerFile}' does not exist.");
     }
     require_once $controllerFile;
     $controllerClass = 'Thin\\' . $controller . 'Controller';
     $controller = new $controllerClass();
     $controller->view = new View($route->getView());
     if (null !== $alert) {
         $controller->view->alert($alert);
     }
     container()->setController($controller);
     $actions = get_class_methods($controllerClass);
     if (true === $isCms) {
         if (!Arrays::inArray($action, $actions)) {
             $action = 'page';
         }
     }
     container()->setAction($action);
     if (strstr($action, '-')) {
         $words = explode('-', $action);
         $newAction = '';
         for ($i = 0; $i < count($words); $i++) {
             $word = trim($words[$i]);
             if ($i > 0) {
                 $word = ucfirst($word);
             }
             $newAction .= $word;
         }
         $action = $newAction;
     }
     $actionName = $action . 'Action';
     if (Arrays::in('init', $actions)) {
         $controller->init();
     }
     if (Arrays::in('preDispatch', $actions)) {
         $controller->preDispatch();
     }
     if (!Arrays::in($actionName, $actions)) {
         throw new Exception("The action '{$actionName}' does not exist.");
     }
     $controller->{$actionName}();
     $controller->view->render();
     if (Arrays::inArray('postDispatch', $actions)) {
         $controller->postDispatch();
     }
     /* stats */
     if (null !== Utils::get("showStats")) {
         echo View::showStats();
     }
 }
Example #7
0
 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;
 }
Example #8
0
 public static function getPaypalPaymentInfos($paypalUrl)
 {
     $url = Arrays::first($paypalUrl);
     $execute = Arrays::last($paypalUrl);
     $tab = parse_url($url);
     parse_str($tab['query'], $infos);
     extract($infos);
     /* turl + oken + execute */
     return array('url' => $url, 'token' => $token, 'execute' => $execute);
 }
Example #9
0
 private function getTime()
 {
     $time = microtime();
     $time = explode(' ', $time, 2);
     return Arrays::last($time) + Arrays::first($time);
 }
Example #10
0
 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;
 }
Example #11
0
 public function last(array $results)
 {
     if (count($results)) {
         $row = Arrays::last($results);
         if (is_object($row)) {
             return $row;
         }
     }
     return null;
 }
Example #12
0
 public function last($results = array())
 {
     $res = count($results) ? $results : $this->results;
     $row = null;
     if (count($res)) {
         $row = $this->row(Arrays::last($res));
     }
     $this->reset();
     return $row;
 }
Example #13
0
 private function extractId($file)
 {
     return str_replace('.row', '', Arrays::last(explode(DS, $file)));
 }
Example #14
0
 private function localFile($key)
 {
     $tab = explode('::', $key);
     $id = Arrays::last($tab);
     return $this->model->dir . DS . $id . '.row';
 }
Example #15
0
 public function __call($func, $argv)
 {
     $key = sha1('orm' . $this->_token);
     $orm = isAke($this->values, $key, false);
     if (substr($func, 0, 4) == 'link' && false !== $orm) {
         $value = Arrays::first($argv);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 4)));
         $var = Inflector::lower($uncamelizeMethod);
         if (!empty($var)) {
             $var = setter($var . '_id');
             $this->{$var}($value->id());
             return $this;
         }
     } elseif (substr($func, 0, 3) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (isset($this->{$var})) {
             if (isset($this->thin_type)) {
                 $type = $this->thin_type;
                 Data::getModel($type);
                 $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : array();
                 if (Arrays::exists('relationships', $settings)) {
                     if (Arrays::exists($var, $settings['relationships'])) {
                         return Data::getById($var, $this->{$var});
                     }
                 }
             }
             if (Arrays::is($this->{$var}) && count($argv) == 1) {
                 $o = new self();
                 $getter = getter(Arrays::first($argv));
                 $o->populate($this->{$var});
                 return $o->{$getter}();
             }
             if ($this->{$var} instanceof \Closure) {
                 if (is_callable($this->{$var}) && count($argv)) {
                     return call_user_func_array($this->{$var}, $argv);
                 }
             }
             return count($argv) && is_null($this->{$var}) ? Arrays::first($argv) : $this->{$var};
         } else {
             if (isset($this->db_instance)) {
                 return $this->db_instance->getValue($this, $var);
             }
             if (isset($this->thin_type)) {
                 $type = $this->thin_type;
                 Data::getModel($type);
                 $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : array();
                 $relationships = Arrays::exists('relationships', $settings) ? $settings['relationships'] : array();
                 if (Arrays::exists($var, $relationships) && 's' == $var[strlen($var) - 1]) {
                     if (Arrays::exists($var, $relationships)) {
                         $res = dm(substr($var, 0, -1))->where("{$type} = " . $this->id)->get();
                         $collection = array();
                         if (count($res)) {
                             foreach ($res as $obj) {
                                 array_push($collection, $obj);
                             }
                         }
                         return $collection;
                     }
                 } elseif (Arrays::exists('defaultValues', $settings)) {
                     if (Arrays::is($settings['defaultValues'])) {
                         if (Arrays::exists($this->{$var}, $settings['defaultValues'])) {
                             return $settings['defaultValues'][$this->{$var}];
                         }
                     }
                 }
             }
             if (count($argv) == 1) {
                 return Arrays::first($argv);
             }
             return null;
         }
     } elseif (substr($func, 0, 3) == 'has') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (isset($this->{$var})) {
             return !empty($this->{$var});
         } elseif (isset($this->db_instance)) {
             return $this->db_instance->hasValue($this, $var);
         }
     } elseif (substr($func, 0, 3) == 'set') {
         $value = Arrays::first($argv);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (!empty($var)) {
             if (isset($this->thin_type)) {
                 Data::getModel($this->thin_type);
                 $fields = Arrays::exists($this->thin_type, Data::$_fields) ? Data::$_fields[$this->thin_type] : array();
                 if (!Arrays::exists($var, $fields)) {
                     throw new Exception($var . ' is not defined in the model => ' . $this->thin_type);
                 } else {
                     $settingsField = $fields[$var];
                     if (Arrays::exists('checkValue', $settingsField)) {
                         $functionCheck = $settingsField['checkValue'];
                         $value = $functionCheck($value);
                     }
                     if (is_object($value)) {
                         if (isset($value->thin_type)) {
                             if ($value->thin_type == $var) {
                                 $value = $value->id;
                             }
                         }
                     }
                 }
             }
             $this->{$var} = $value;
             if (!Arrays::in($var, $this->_fields)) {
                 $this->_fields[] = $var;
             }
             if (isset($this->is_thin_object)) {
                 $name = $this->is_thin_object;
                 $objects = Utils::get('thinObjects');
                 $this->values = null;
                 $objects[$name] = $this;
                 Utils::set('thinObjects', $objects);
             }
             if (isset($this->is_app)) {
                 if (true === $this->is_app) {
                     Utils::set('ThinAppContainer', $this);
                 }
             }
         } elseif (isset($this->db_instance)) {
             return $this->db_instance->setValue($this, $var, $value);
         }
         return $this;
     } elseif (substr($func, 0, 3) == 'add') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod) . 's';
         $value = Arrays::first($argv);
         if (!isset($this->{$var})) {
             $this->{$var} = array();
         }
         if (!Arrays::is($this->{$var})) {
             $this->{$var} = array();
         }
         array_push($this->{$var}, $value);
         return $this;
     } elseif (substr($func, 0, 6) == 'remove') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 6)));
         $var = Inflector::lower($uncamelizeMethod) . 's';
         $value = Arrays::first($argv);
         if (isset($this->{$var})) {
             if (Arrays::is($this->{$var})) {
                 if (count($this->{$var})) {
                     $remove = false;
                     foreach ($this->{$var} as $key => $tmpValue) {
                         $comp = md5(serialize($value)) == md5(serialize($tmpValue));
                         if (true === $comp) {
                             $remove = true;
                             break;
                         }
                     }
                     if (true === $remove) {
                         unset($this->{$var}[$key]);
                     }
                 }
             }
         }
         return $this;
     }
     if (Arrays::in($func, $this->_fields)) {
         if ($this->{$func} instanceof \Closure) {
             return call_user_func_array($this->{$func}, $argv);
         }
     }
     if (Arrays::exists($func, $this->_closures)) {
         if ($this->_closures[$func] instanceof \Closure) {
             return call_user_func_array($this->_closures[$func], $argv);
         }
     }
     if (isset($this->_token)) {
         $id = sha1($func . $this->_token);
         if (Arrays::is($this->values)) {
             if (Arrays::exists($id, $this->values)) {
                 return call_user_func_array($this->values[$id], $argv);
             }
         }
     }
     if (true === hasEvent($func)) {
         array_push($argv, $this);
         return fire($func, $argv);
     }
     if (!is_callable($func) || substr($func, 0, 6) !== 'array_' || substr($func, 0, 3) !== 'set' || substr($func, 0, 3) !== 'get' || substr($func, 0, 3) !== 'has' || substr($func, 0, 3) !== 'add' || substr($func, 0, 6) !== 'remove') {
         $callable = strrev(repl('_', '', $func));
         if (!is_callable($callable)) {
             if (method_exists($this, $callable)) {
                 return call_user_func_array(array($this, $callable), $argv);
             }
         } else {
             return call_user_func_array($callable, $argv);
         }
         if (isset($this->thin_litedb)) {
             $closure = isAke($this->thin_litedb->closures, $func);
             if (!empty($closure) && $closure instanceof \Closure) {
                 return $closure($this);
             }
         }
         if (isset($this->db_instance)) {
             return $this->db_instance->{$func}($this, $var, $value);
             call_user_func_array(array($this->db_instance, $func), array_merge(array($this), $argv));
         }
         if (false !== $orm) {
             $db = call_user_func_array($orm, array());
             $fields = array_keys($db->map['fields']);
             if (Arrays::in($func, $fields)) {
                 if (!count($argv)) {
                     return $this->{$func};
                 } else {
                     $setter = setter($func);
                     $this->{$setter}(Arrays::first($argv));
                     return $this;
                 }
             }
             $tab = str_split($func);
             $many = false;
             if (Arrays::last($tab) == 's') {
                 array_pop($tab);
                 $table = implode('', $tab);
                 $many = true;
             } else {
                 $table = $func;
             }
             $object = count($argv) == 1 ? Arrays::first($argv) : true;
             $model = model($table);
             return true === $many ? $model->where($db->table . '_id = ' . $this->id())->exec($object) : $model->where($db->table . '_id = ' . $this->id())->first($object);
         }
         return null;
     }
     return call_user_func_array($func, array_merge(array($this->getArrayCopy()), $argv));
 }
Example #16
0
 private function extension($file)
 {
     return strtolower(Arrays::last(explode('.', $file)));
 }
Example #17
0
 /**
  * [fromSql description]
  *
  * @method fromSql
  *
  * @param  [type]  $sql [description]
  *
  * @return [type]       [description]
  */
 public static function fromSql($sql)
 {
     $select = Utils::cut('SELECT ', ' FROM', $sql);
     $from = Utils::cut(' FROM ', ' ', $sql);
     $wheres = '';
     if (fnmatch('* WHERE *', $sql)) {
         $wheres = Arrays::last(explode(' WHERE ', $sql));
     }
     $joins = [];
     if (fnmatch('* JOIN *', $sql)) {
         $segs = explode(' JOIN ', $sql);
         array_shift($segs);
         foreach ($segs as $seg) {
             $fk = Arrays::first(explode(' ', $seg));
             if (!in_array($joins, $joins)) {
                 $joins[] = $fk;
             }
         }
     }
     if (fnmatch('*.*', $from)) {
         list($db, $table) = explode('.', Inflector::lower($from), 2);
     } else {
         $db = SITE_NAME;
         $table = Inflector::lower($from);
     }
     $instance = self::instance($db, $table);
     if (!empty($select) && $select != '*') {
         $selects = explode(',', str_replace(' ', '', Inflector::lower($select)));
         foreach ($selects as $field) {
             $instance->select($field);
         }
     }
     if (fnmatch('* ORDER BY *', $wheres)) {
         list($wheres, $orders) = explode(' ORDER BY ', $wheres, 2);
     }
     $whs = [$wheres];
     $or = false;
     if (fnmatch('* && *', $wheres)) {
         $whs = explode(' && ', $wheres);
     }
     if (fnmatch('* || *', $wheres)) {
         $whs = explode(' || ', $wheres);
         $or = true;
     }
     foreach ($whs as $wh) {
         list($f, $o, $v) = explode(' ', $wh, 3);
         if ($v[0] == "'") {
             $v = substr($v, 1);
         }
         if ($v[strlen($v) - 1] == "'") {
             $v = substr($v, 0, -1);
         }
         if (is_numeric($v)) {
             if ($v == intval($v)) {
                 $v = (int) $v;
             }
         }
         if (!$or) {
             $instance = $instance->where([$f, $o, $v]);
         } else {
             $instance = $instance->where([$f, $o, $v], 'OR');
         }
     }
     if (isset($orders)) {
         if (fnmatch('*,*', $orders)) {
             $orders = explode(',', str_replace(', ', ',', $orders));
         } else {
             $orders = [$orders];
         }
         foreach ($orders as $order) {
             if (fnmatch('* *', $order)) {
                 list($f, $d) = explode(' ', $order, 2);
             } else {
                 $f = $order;
                 $d = 'ASC';
             }
             $instance = $instance->order($f, $d);
         }
     }
     return $instance;
 }
Example #18
0
File: Ini.php Project: schpill/thin
 /**
  * Assign the key's value to the property list. Handles the
  * nest separator for sub-properties.
  *
  * @param  array  $config
  * @param  string $key
  * @param  string $value
  * @throws Exception
  * @return array
  */
 protected function _processKey($config, $key, $value)
 {
     if (strpos($key, $this->_nestSeparator) !== false) {
         $parts = explode($this->_nestSeparator, $key, 2);
         if (strlen(Arrays::first($parts)) && strlen(Arrays::last($parts))) {
             if (!isset($config[Arrays::first($parts)])) {
                 if (Arrays::first($parts) === '0' && !empty($config)) {
                     // convert the current values in $config into an array
                     $config = array(Arrays::first($parts) => $config);
                 } else {
                     $config[Arrays::first($parts)] = array();
                 }
             } elseif (!Arrays::is($config[Arrays::first($parts)])) {
                 throw new Exception("Cannot create sub-key for '{Arrays::first({$parts})}' as key already exists");
             }
             $config[Arrays::first($parts)] = $this->_processKey($config[Arrays::first($parts)], Arrays::last($parts), $value);
         } else {
             throw new Exception("Invalid key '{$key}'");
         }
     } else {
         $config[$key] = $value;
     }
     return $config;
 }
Example #19
0
 function htmlToPng($html, $name = null, $orientation = null, $count = 0)
 {
     $name = is_null($name) ? 'doc.pdf' : Inflector::urlize($name) . '.pdf';
     $orientation = is_null($orientation) ? 'portrait' : $orientation;
     $file = TMP_PUBLIC_PATH . DS . sha1(serialize(func_get_args())) . '.html';
     files()->put($file, $html);
     $pdf = str_replace('.html', '.pdf', $file);
     // $keep = lib('keep')->instance();
     $url = URLSITE . 'tmp/' . Arrays::last(explode('/', $file));
     $this->urlToPng($url);
 }
Example #20
0
 public function __get($key)
 {
     $array = isset($this->values) ? $this->values : [];
     if (count($array)) {
         foreach ($array as $k => $v) {
             if (!Arrays::in($k, $this->_fields)) {
                 $this->_fields[] = $k;
             }
             $this->{$k} = $v;
         }
     }
     if (isset($this->{$key})) {
         if (isset($this->thin_type)) {
             $type = $this->thin_type;
             Data::getModel($this->thin_type);
             $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : [];
             if (Arrays::exists('relationships', $settings)) {
                 if (Arrays::exists($key, $settings['relationships']) && 's' != $key[strlen($key) - 1]) {
                     return Data::getById($key, $this->{$key});
                 }
                 if (Arrays::exists($key, $settings['relationships']) && 's' == $key[strlen($key) - 1]) {
                     return Data::query(substr($key, 0, -1), "{$type} = " . $this->id);
                 }
             }
         }
         return $this->{$key};
     }
     if (isset($this->_token) && isset($this->values)) {
         $keyJson = sha1('touch' . $this->_token);
         $dbjson = isAke($this->values, $keyJson, false);
         if (false !== $dbjson) {
             $keyT = sha1('db' . $this->_token);
             $cb = isAke($this->values, $keyT, false);
             if (false !== $cb) {
                 $db = call_user_func_array($cb, []);
                 $fields = $db->fields();
                 if (Arrays::in($key, $fields)) {
                     return null;
                 }
                 $tab = str_split($key);
                 $many = false;
                 if (Arrays::last($tab) == 's') {
                     array_pop($tab);
                     $table = implode('', $tab);
                     $many = true;
                 } else {
                     $table = $func;
                 }
                 $object = true;
                 $model = jdb($db->db, $table);
                 return true === $many ? $model->where($db->table . '_id = ' . $this->id)->exec($object) : $model->where($db->table . '_id = ' . $this->id)->first($object);
             }
         }
     }
     return null;
 }
Example #21
0
 /**
  * Last item
  *
  * @return Model
  */
 public function last()
 {
     return count($this->_items) > 0 ? Arrays::last($this->_items) : null;
 }
Example #22
0
File: Eav.php Project: noikiy/inovi
 public function last($object = false)
 {
     $res = $this->results;
     $this->reset();
     if (true === $object) {
         return count($res) ? $this->makeObject(Arrays::last($res)) : null;
     } else {
         return count($res) ? Arrays::last($res) : array();
     }
 }
Example #23
0
 public function __call($fn, $args)
 {
     $fields = $this->fields();
     $method = substr($fn, 0, 2);
     $object = lcfirst(substr($fn, 2));
     if ('is' === $method && strlen($fn) > 2) {
         $field = Inflector::uncamelize($object);
         if (!Arrays::in($field, $fields)) {
             $field = $field . '_id';
             $model = Arrays::first($args);
             if ($model instanceof Container) {
                 $idFk = $model->id();
             } else {
                 $idFk = $model;
             }
             return $this->where("{$field} = {$idFk}");
         } else {
             return $this->where($field . ' = ' . Arrays::first($args));
         }
     }
     $method = substr($fn, 0, 4);
     $object = lcfirst(substr($fn, 4));
     if ('orIs' === $method && strlen($fn) > 4) {
         $field = Inflector::uncamelize($object);
         if (!Arrays::in($field, $fields)) {
             $field = $field . '_id';
             $model = Arrays::first($args);
             if ($model instanceof Container) {
                 $idFk = $model->id();
             } else {
                 $idFk = $model;
             }
             return $this->where("{$field} = {$idFk}", 'OR');
         } else {
             return $this->where($field . ' = ' . Arrays::first($args), 'OR');
         }
     } elseif ('like' === $method && strlen($fn) > 4) {
         $field = Inflector::uncamelize($object);
         $op = count($args) == 2 ? Arrays::last($args) : 'AND';
         return $this->like($field, Arrays::first($args), $op);
     }
     $method = substr($fn, 0, 5);
     $object = lcfirst(substr($fn, 5));
     if (strlen($fn) > 5) {
         if ('where' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}");
             } else {
                 return $this->where($field . ' ' . Arrays::first($args));
             }
         } elseif ('xorIs' === $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}", 'XOR');
             } else {
                 return $this->where($field . ' = ' . Arrays::first($args), 'XOR');
             }
         } elseif ('andIs' === $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}");
             } else {
                 return $this->where($field . ' = ' . Arrays::first($args));
             }
         }
     }
     $method = substr($fn, 0, 6);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 6)));
     if (strlen($fn) > 6) {
         if ('findBy' == $method) {
             return $this->findBy($object, Arrays::first($args));
         }
     }
     $method = substr($fn, 0, 7);
     $object = lcfirst(substr($fn, 7));
     if (strlen($fn) > 7) {
         if ('orWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}", 'OR');
             } else {
                 return $this->where($field . ' ' . Arrays::first($args), 'OR');
             }
         } elseif ('orderBy' == $method) {
             $object = Inflector::uncamelize(lcfirst(substr($fn, 7)));
             if ($object == 'id') {
                 $object = $this->pk();
             }
             if (!Arrays::in($object, $fields)) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             $direction = count($args) ? Arrays::first($args) : 'ASC';
             return $this->order($object, $direction);
         } elseif ('groupBy' == $method) {
             $object = Inflector::uncamelize(lcfirst(substr($fn, 7)));
             if ($object == 'id') {
                 $object = $this->pk();
             }
             if (!Arrays::in($object, $fields)) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             return $this->groupBy($object);
         }
     }
     $method = substr($fn, 0, 9);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 9)));
     if (strlen($fn) > 9) {
         if ('findOneBy' == $method) {
             return $this->findOneBy($object, Arrays::first($args));
         }
     }
     $method = substr($fn, 0, 13);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 13)));
     if (strlen($fn) > 13) {
         if ('findObjectsBy' == $method) {
             return $this->findBy($object, Arrays::first($args), true);
         }
     }
     $method = substr($fn, 0, 15);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 15)));
     if (strlen($fn) > 15) {
         if ('findOneObjectBy' == $method) {
             return $this->findOneBy($object, Arrays::first($args), true);
         }
     }
     $method = substr($fn, 0, 8);
     $object = lcfirst(substr($fn, 8));
     if (strlen($fn) > 8) {
         if ('xorWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}", 'XOR');
             } else {
                 return $this->where($field . ' ' . Arrays::first($args), 'XOR');
             }
         } elseif ('andWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}");
             } else {
                 return $this->where($field . ' ' . Arrays::first($args));
             }
         }
     } else {
         $field = $fn;
         $fieldFk = $fn . '_id';
         $op = count($args) == 2 ? Inflector::upper(Arrays::last($args)) : 'AND';
         if (Arrays::in($field, $fields)) {
             return $this->where($field . ' = ' . Arrays::first($args), $op);
         } else {
             if (Arrays::in($fieldFk, $fields)) {
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$fieldFk} = {$idFk}", $op);
             }
         }
     }
     throw new Exception("Method '{$fn}' is unknown.");
 }
Example #24
0
 private function siren2($siren)
 {
     $infos = [];
     if (is_numeric($siren) && strlen($siren)) {
         $cache = redis()->get('siren::' . $siren);
         if (!strlen($cache)) {
             $data = dwn("http://www.verif.com/imprimer/{$siren}/1/1/");
             redis()->set('siren::' . $siren, $data);
         } else {
             $data = $cache;
         }
         $formeJuridique = $registre = $capital = $dirigeant = $immatriculation = $departement = $codePostal = $ville = $adresse = $ape = $creation = $activite = $tel = $fax = $effectif = $siret = null;
         $cmdTel = "curl 'http://www.pagespro.com/recherche.php' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' -H 'Connection: keep-alive' -H 'Cookie: EIRAM=1; xtvrn=\$486926\$; xtan=-; xtant=1' -H 'Host: www.pagespro.com' -H 'Referer: http://www.pagespro.com/recherche.php' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0' -H 'Content-Type: application/x-www-form-urlencoded' --data 'p_ACTION=&p_ORDRE=AfficheRes&a_PAGE=1&a_TAG=&a_OccRecherche=&typeRecherche=express&satellite=siret&a_activ=&a_rai_soc=&a_naf=&a_siren={$siren}&a_tel=&a_geo=&typeTri=&ordreTri=&a_session='";
         $cacheTel = redis()->get('siren::tel::' . $siren);
         if (!strlen($cacheTel)) {
             exec($cmdTel, $dataTel);
             $dataTel = implode("", $dataTel);
             redis()->set('siren::tel::' . $siren, $dataTel);
         } else {
             $dataTel = $cacheTel;
         }
         if (strstr($dataTel, '<span itemprop="tel">')) {
             $tel = strReplaceFirst('0', '+33', str_replace(["\t", '&nbsp;', ' '], '', Utils::cut('<span itemprop="tel">', '</span>', $dataTel)));
         }
         $dataFax = str_replace(["\t"], '', $dataTel);
         if (strstr($dataFax, "<span>fax")) {
             $segFax = Utils::cut('<span>fax', '</d', $dataFax);
             $fax = strReplaceFirst('0', '+33', str_replace(["\t", '&nbsp;', ' '], '', Utils::cut('<span itemprop="tel">', '</span>', $segFax)));
         }
         if (strstr($dataTel, '<b>Effectif')) {
             $segEffectif = Utils::cut('<b>Effectif', '/div>', $dataTel);
             $effectif = str_replace(["\t", '&nbsp;'], '', Utils::cut('</b>', '<', $segEffectif));
         }
         if (strstr($dataTel, '<b>Siret')) {
             $segSiret = Utils::cut('<b>Siret', '/div>', $dataTel);
             $siret = str_replace(["\t", '&nbsp;'], '', Utils::cut('</b>', '<', $segSiret));
         }
         $seg = Utils::cut('<h4>Informations g&eacute;n&eacute;rales</h4>', '</table>', $data);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">Raison sociale</td>', '</tr>', $seg);
         $raisonSociale = Utils::cut('<td>', '</td>', $sousSeg);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">APE</td>', '</tr>', $seg);
         $ape = Utils::cut('<td>', '</td>', $sousSeg);
         list($ape, $activite) = explode(' / ', Utils::cut('>', '</', $ape), 2);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">Forme juridique', '</tr>', $seg);
         list($formeJuridique, $creation) = explode(', cr&eacute;&eacute;e le ', Utils::cut('<td>', '</td>', $sousSeg), 2);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">Adresse', '</tr>', $seg);
         $adr = Utils::cut('<td>', '</td>', $sousSeg);
         $adr = explode("\n", $adr);
         array_pop($adr);
         $adresse = Arrays::first($adr);
         $cpville = Arrays::last($adr);
         $adresse = $this->clean(str_replace(["\t", "<br />"], '', $adresse));
         list($codePostal, $ville) = explode('&nbsp;', $cpville, 2);
         $codePostal = $this->clean(str_replace(["\t", " "], '', $codePostal));
         $departement = substr($codePostal, 0, 2);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">Capital Social', '</tr>', $seg);
         $capital = Utils::cut('<td>', '&', $sousSeg);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">Registre du commerce', '</tr>', $seg);
         $registre = Utils::cut('<td>', '<', $sousSeg);
         $registre = str_replace(' ', '', $registre);
         if (null === $siret && strstr($seg, 'tdhead">SIRET</td>')) {
             $sousSeg = Utils::cut('<td class="fiche_tdhead">SIRET</td>', '</tr>', $seg);
             $siret = str_replace(' ', '', Utils::cut('<td>', '</td>', $sousSeg));
         }
         if (strstr($data, '<H4>Dirigeants</H4>')) {
             $segDirigeant = Utils::cut('<H4>Dirigeants</H4>', '</div>', $data);
             $d = Utils::cut('<table', '</table>', $segDirigeant);
             $d = Utils::cut('<tr', '</tr>', $d);
             $rows = explode("\n", $d);
             array_shift($rows);
             array_pop($rows);
             $fonction = Utils::cut('>', '<', Arrays::first($rows));
             $segPersonne = Arrays::last($rows);
             if (fnmatch('*(*', $segPersonne)) {
                 $personne = Utils::cut('>', '(', $segPersonne);
                 $personne = str_replace(['&nbsp;'], '', $personne);
             } else {
                 $personne = Utils::cut('>', '</', $segPersonne);
             }
             $dirigeant = "{$personne} - {$fonction}";
         }
         $infos = ['siren' => $this->clean($siren), 'siret' => $this->clean($siret), 'raison_sociale' => $this->clean($raisonSociale), 'ape' => $this->clean($ape), 'activite' => $this->clean($activite), 'forme_juridique' => $this->clean($formeJuridique), 'telephone' => $this->clean($tel), 'fax' => $this->clean($fax), 'adresse' => $this->clean($adresse), 'code_postal' => $this->clean($codePostal), 'ville' => $this->clean($ville), 'departement' => $this->clean($departement), 'effectif' => $this->clean($effectif), 'date_immatriculation' => $this->clean($creation), 'registre' => $this->clean($registre), 'capital' => $this->clean($capital), 'dirigeant' => $this->clean($dirigeant)];
     }
     return $infos;
 }
Example #25
0
 public static function populateDatabase($database = null)
 {
     $database = is_null($database) ? SITE_NAME : $database;
     $tables = glob(Config::get('directory.store', STORAGE_PATH) . DS . 'dbjson' . DS . $database . '_' . APPLICATION_ENV . DS . '*');
     foreach ($tables as $tableDir) {
         $rows = glob($tableDir . DS . '*.row');
         if (count($rows)) {
             $table = Arrays::last(explode(DS, $tableDir));
             $db = jdb($database, $table);
             $mongo = static::instance($db);
             foreach ($rows as $row) {
                 $mongo->read($mongo->extractId($row));
             }
         }
     }
 }
Example #26
0
 private function insert($query)
 {
     $seg = isAke($query, 'INSERT', []);
     $orm = $this->getOrm([Arrays::last($seg)]);
     dd($orm);
 }
Example #27
0
 /**
  * Step 3 of authentication. After the /oauth/authorize step is complete, the application can call /oauth/access_token to acquire an access token.
  * This method corresponds to Obtaining an Access Token in the OAuth Core 1.0 specification.
  *
  * @param  string $oauthToken The token returned after authorizing.
  * @return array
  */
 public function oAuthAccessToken($oauthToken = null)
 {
     // build parameters
     $parameters = array();
     if (empty($oauthToken)) {
         $oauthToken = $this->getOAuthToken();
     }
     $parameters['oauth_token'] = (string) $oauthToken;
     // make the call
     $response = $this->doOAuthCall('1/oauth/access_token', $parameters, 'POST', false);
     // validate
     $json = @json_decode($response, true);
     if (isset($json['error'])) {
         throw new Exception($json['error']);
     }
     // process response
     $response = (array) explode('&', $response);
     $return = array();
     // loop chunks
     foreach ($response as $chunk) {
         // split again
         $chunks = explode('=', $chunk, 2);
         // store return
         if (count($chunks) == 2) {
             $return[Arrays::first($chunks)] = Arrays::last($chunks);
         }
     }
     // return
     return $return;
 }
Example #28
0
 function kv()
 {
     $args = func_get_args();
     $s = session('kv');
     if (count($args) == 1) {
         $get = getter(Arrays::first($args));
         return $s->{$get}();
     } elseif (count($args) == 2) {
         $set = setter(Arrays::first($args));
         return $s->{$set}(Arrays::last($args));
     }
     return null;
 }
Example #29
0
 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);
 }
Example #30
0
 /**
  * Last item
  *
  * @return Model
  */
 public function last()
 {
     return \Thin\Arrays::last($this->_items);
 }