Example #1
0
 public static function get($class, $key = null)
 {
     $key = is_null($key) ? sha1($class) : $key;
     $classInstances = isAke(static::$instances, $class);
     $keyInstance = isAke($classInstances, $key, null);
     return $keyInstance;
 }
Example #2
0
 public function __call($method, $args)
 {
     if (true === $this->__has($method)) {
         return $this->__fire($method, $args);
     }
     $reverse = strrev($method);
     $last = $reverse[0];
     if ('s' == $last) {
         if (!count($args)) {
             return isAke($this->values, $method);
         } else {
             $this->values[$method] = !Arrays::is($this->values[$method]) ? array() : $this->values[$method];
             foreach ($args as $arg) {
                 array_push($this->values[$method], $arg);
             }
         }
         return $this;
     } else {
         $method .= 's';
         if (!count($args)) {
             $val = isAke($this->values, $method);
             return count($val) ? Arrays::first($val) : null;
         } else {
             $this->values[$method] = !Arrays::is($this->values[$method]) ? array() : $this->values[$method];
             foreach ($args as $arg) {
                 array_push($this->values[$method], $arg);
             }
         }
         return $this;
     }
 }
Example #3
0
function displayCodeLines()
{
    $back = '';
    // $traces = Thin\Input::globals('dbg_stack', []);
    $traces = debug_backtrace();
    array_pop($traces);
    if (!empty($traces)) {
        foreach ($traces as $trace) {
            $file = isAke($trace, 'file', false);
            $line = isAke($trace, 'line', false);
            if (false !== $file && false !== $line && $file != __FILE__) {
                $start = $line > 5 ? $line - 5 : $line;
                $code = Thin\File::readLines($file, $start, $line + 5);
                $lines = explode("\n", $code);
                $codeLines = [];
                $i = $start;
                foreach ($lines as $codeLine) {
                    if ($i == $line) {
                        array_push($codeLines, $i . '. <span style="background-color: gold; color: black;">' . $codeLine . '</span>');
                    } else {
                        array_push($codeLines, $i . '. ' . $codeLine);
                    }
                    $i++;
                }
                if (strlen($back)) {
                    $back .= "\n";
                }
                $back .= "File: {$file} [<em>line: <u>{$line}</u></em>]\n\nCode\n*******************************\n<div style=\"font-weight: normal; font-family: Consolas;\">" . implode("\n", $codeLines) . "</div>\n*******************************\n";
            }
        }
    }
    return $back;
}
Example #4
0
 public function run()
 {
     foreach ($this->logics as $k) {
         $step = isAke($this->steps, $k, null);
         if ($step) {
             $action = isAke($step, 'action', null);
             if ($action) {
                 $a = array_merge(isAke($step, 'action', []), [$this->instance]);
                 $check = call_user_func_array($action, $a);
                 if ($check) {
                     $this->successes[] = $k;
                     $success = isAke($step, 'success', null);
                     if ($success) {
                         call_user_func_array($success, [$this->instance]);
                     }
                 } else {
                     $this->errors[] = $k;
                     $error = isAke($step, 'error', null);
                     if ($error) {
                         call_user_func_array($error, [$this->instance]);
                     }
                 }
             } else {
                 $this->errors[] = $k;
                 $error = isAke($step, 'error', null);
                 if ($error) {
                     call_user_func_array($error, [$this->instance]);
                 }
             }
         }
     }
     return ['successes' => $this->successes, 'errors' => $this->errors];
 }
Example #5
0
 /**
  * Connect to MongoDB, select database
  *
  * @return bool
  */
 public function connect()
 {
     if ($this->_connection) {
         return;
     }
     /**
      * Add required variables
      * Clear the connection parameters for security
      */
     $options = $this->_config->assoc();
     unset($this->_config);
     $hostname = isAke($options, 'hostname', 'localhost');
     $port = isAke($options, 'port', '27017');
     $conn = 'mongodb://' . $hostname . ':' . $port;
     unset($options['hostname']);
     unset($options['port']);
     $this->_connection = new \Mongo($conn, $options);
     /* Try connect */
     try {
         $this->_connection->connect();
     } catch (\MongoConnectionException $e) {
         throw new \Exception('Unable to connect to MongoDB server at ' . $hostname);
     }
     $this->_db = new \MongoDB($this->_connection, $options['db']);
     return $this->_connected = TRUE;
 }
Example #6
0
 public static function __callStatic($method, $args)
 {
     if (!isset(self::$resources[$method])) {
         throw new Exception("[{$name}] is not a known resource.");
     }
     return isAke(self::$resources, $method);
 }
Example #7
0
 public function table($table)
 {
     $instance = isAke($this->tables, $table);
     if (null === $onstance) {
         $instance = new Store($table);
     }
 }
Example #8
0
File: Cli.php Project: schpill/thin
 private function format($text = '', $parameters = array())
 {
     if (!Arrays::is($parameters) && 'NONE' == $parameters) {
         return $text;
     }
     if (!Arrays::is($parameters) && isset($this->styles[$parameters])) {
         $parameters = $this->styles[$parameters];
     }
     $codes = array();
     $fg = isAke($parameters, 'fg', null);
     $bg = isAke($parameters, 'bg', null);
     if (!empty($fg)) {
         $codes[] = $this->foreground[$fg];
     }
     if (!empty($bg)) {
         $codes[] = $this->background[$bg];
     }
     foreach ($this->options as $option => $value) {
         $paramOpt = isAke($parameters, $option, null);
         if (!empty($paramOpt)) {
             $codes[] = $value;
         }
     }
     return "[" . implode(';', $codes) . 'm' . $text . "";
 }
Example #9
0
 public function listen()
 {
     set_time_limit(false);
     $dbTask = Model::Latertask();
     $dbInstance = Model::Laterinstance();
     $tasks = $dbTask->where(['when', '<', time()])->cursor();
     if ($tasks->count() > 0) {
         foreach ($tasks as $task) {
             $check = $dbInstance->where(['task_id', '=', (int) $task['id']])->cursor()->count();
             $callback_id = isAke($task, 'callback_id', null);
             if ($check == 0) {
                 $instance = $dbInstance->create(['task_id' => (int) $task['id'], 'start' => time()])->save();
                 $res = lib('closure')->fireStore((int) $task['closure_id'], (array) $task['args']);
                 if ($callback_id) {
                     $cb = $dbTask->find((int) $callback_id);
                     $t = $cb->toArray();
                     $args = array_merge([$res], $t['args']);
                     $res = lib('closure')->fireStore((int) $t['closure_id'], (array) $args);
                     $cb->delete();
                 }
                 $dt = $dbTask->find((int) $task['id']);
                 if ($dt) {
                     $dt->delete();
                 }
                 $instance->delete();
                 Model::Laterhistory()->create(['task' => (array) $task, 'execution_time' => time()])->save();
             }
         }
     }
     return true;
 }
Example #10
0
 public static function instance($ns = 'core')
 {
     $i = isAke(self::$instances, $ns, false);
     if (!$i) {
         self::$instances[$ns] = $i = lib('now', [$ns]);
     }
     return $i;
 }
Example #11
0
 public function create($args)
 {
     $points = isAke($args, 'points', false);
     if (false === $points) {
         $args['points'] = 0;
     }
     return Model::Account()->create($args)->save();
 }
Example #12
0
 public function getValue($username, $field, $default = null)
 {
     $row = coll(Now::get('acl.users.' . $this->ns, []))->firstBy('username', $username);
     if ($row) {
         return isAke($row, $field, $default);
     }
     return $default;
 }
Example #13
0
 public function fire($event, $parameters = [])
 {
     $events = Now::get('core.events', []);
     $closure = isAke($events, $event, null);
     if ($closure) {
         return call_user_func_array($closure, $parameters);
     }
     return null;
 }
Example #14
0
 public function fire($hook, $parameters = [])
 {
     $hooks = Now::get('core.hooks', []);
     $closure = isAke($hooks, $hook, null);
     if ($closure) {
         return call_user_func_array($closure, $parameters);
     }
     return null;
 }
Example #15
0
 public static function forget($class, $key = null)
 {
     $key = is_null($key) ? sha1($class) : $key;
     $classInstances = isAke(static::$instances, $class);
     $keyInstance = isAke($classInstances, $key, null);
     if (!is_null($keyInstance)) {
         unset(static::$instances[$class][$key]);
     }
 }
Example #16
0
 public static function instance($ns = 'core')
 {
     $i = isAke(static::$instances, $ns, null);
     if (is_null($i)) {
         $i = new self($ns);
         static::$instances[$ns] = $i;
     }
     return $i;
 }
Example #17
0
 public function get($key, $default = null)
 {
     $resource = isAke(self::$resources, $key, curl_init());
     $data = @unserialize(curl_getinfo($resource, CURLINFO_PRIVATE));
     if (false !== $data) {
         return $data;
     }
     return $default;
 }
Example #18
0
 public function getUri()
 {
     $before = str_replace('/index.php', '', isAke($_SERVER, 'SCRIPT_NAME', ''));
     $uri = substr($_SERVER['REQUEST_URI'], strlen($before));
     if (strstr($uri, '?')) {
         $uri = substr($uri, 0, strpos($uri, '?'));
     }
     $uri = '/' . trim($uri, '/');
     return $uri;
 }
Example #19
0
 public function del($key)
 {
     if (is_string($key)) {
         $val = isAke($this->data, $key, '__null__');
         if ('__null__' != $val) {
             unset($this->data[$key]);
         }
     }
     return context('collection');
 }
Example #20
0
 public static function instance($namespace, $entity)
 {
     $key = sha1($namespace . $entity);
     $instance = isAke(static::$instances, $key, null);
     if (is_null($instance)) {
         $instance = new self($namespace, $entity);
         static::$instances[$key] = $instance;
     }
     return $instance;
 }
Example #21
0
 public function can($policy)
 {
     $closure = isAke($this->policies, $policy, null);
     if ($closure) {
         if (is_callable($closure)) {
             return (bool) call_user_func_array($closure, [$this->user]);
         }
     }
     return false;
 }
Example #22
0
 public function get($key)
 {
     $pattern = strstr($key, '#') ? $key : $key . '#';
     $files = $this->search($pattern);
     if (count($files)) {
         $key = Arrays::first($files);
         $data = isAke($this->buffer, $key, null);
         return $data;
     }
     return null;
 }
Example #23
0
 public function broadcast($event, $args = [], $cb = null)
 {
     $closure = isAke(self::$datas[$this->ns], $event, false);
     if (is_callable($closure)) {
         $result = call_user_func_array($closure, $args);
         if (is_callable($cb)) {
             return call_user_func_array($closure, [$result]);
         }
         return $result;
     }
     return false;
 }
Example #24
0
 public function check($key, $args = [])
 {
     $row = isAke($this->steps, $key, []);
     $check = isAke($row, 'check', false);
     if ($check) {
         if (is_callable($check)) {
             $args[] = $this;
             return call_user_func_array($check, $args);
         }
     }
     throw new Exception('Step ' . $key . ' does not exist in this workflow.');
 }
Example #25
0
 public function can()
 {
     $this->method = strtolower(\Thin\Request::method());
     if (in_array($this->method, ['post', 'put', 'delete'])) {
         $_POST = json_decode(file_get_contents('php://input'), true);
         $this->token = isAke($_POST, 'token', false);
         if ($this->token) {
             $this->checkToken();
         } else {
             Api::forbidden();
         }
     }
 }
Example #26
0
 public static function __callStatic($method, $args)
 {
     $auth = ['GET', 'POST', 'COOKIE', 'SESSION', 'SERVER', 'REQUEST', 'GLOBALS'];
     $method = Inflector::upper($method);
     if (Arrays::in($method, $auth) && count($args) > 0) {
         $default = isset($args[1]) ? $args[1] : null;
         return isAke(self::tab($method), Arrays::first($args), $default);
     } elseif (Arrays::in($method, $auth) && count($args) == 0) {
         return self::tab($method);
     } else {
         throw new Exception("Wrong parameters.");
     }
 }
Example #27
0
 public function listen($class)
 {
     $closures = isAke(self::$datas, $class, []);
     if (empty($closures)) {
         $i = 0;
         foreach ($closures as $closure) {
             if (is_callable($closure)) {
                 $args = isset(self::$args[$class][$i]) ? self::$args[$class][$i] : [];
                 call_user_func_array($closure, $args);
             }
             $i++;
         }
     }
 }
Example #28
0
 public function create(array $fields, $delete = true)
 {
     $sql = '';
     if ($delete) {
         $delsql = 'DROP TABLE IF EXISTS ' . $this->table;
         $result = mysqli_query($this->db, $delsql);
         if (is_bool($result) && strlen($this->db->error)) {
             throw new Exception($this->db->error);
         }
     }
     $sql .= 'CREATE TABLE IF NOT EXISTS ' . $this->table . ' (
           `id` int(11) NOT NULL AUTO_INCREMENT,##fields##,
           `created_at` INT(11) unsigned DEFAULT NULL,
           `updated_at` INT(11) unsigned DEFAULT NULL,
           PRIMARY KEY (`id`)
         ) ENGINE=InnoDB DEFAULT CHARSET=utf8;';
     $sqlFields = [];
     foreach ($fields as $name => $infos) {
         if (is_int($name)) {
             $name = $infos;
             $infos = [];
         }
         $type = isAke($infos, 'type', 'varchar');
         if (fnmatch('*_id', $name)) {
             $type = 'int';
         }
         if ($type == 'varchar') {
             $sqlField = '`' . $name . '` varchar(255) DEFAULT NULL';
         } elseif ($type == 'int') {
             $sqlField = '`' . $name . '` int(11) unsigned DEFAULT \'0\'';
         } elseif ($type == 'double') {
             $sqlField = '`' . $name . '` double(11) unsigned DEFAULT \'0\'';
         } elseif ($type == 'float') {
             $sqlField = '`' . $name . '` float(11) unsigned DEFAULT \'0\'';
         } elseif ($type == 'text') {
             $sqlField = '`' . $name . '` text DEFAULT NULL';
         } elseif ($type == 'longtext') {
             $sqlField = '`' . $name . '` longtext DEFAULT NULL';
         } elseif ($type == 'date') {
             $sqlField = '`' . $name . '` date DEFAULT NULL';
         }
         $sqlFields[] = $sqlField;
     }
     $sql = str_replace('##fields##', implode(",", $sqlFields), $sql);
     $result = mysqli_query($this->db, $sql);
     if (is_bool($result) && strlen($this->db->error)) {
         throw new Exception($this->db->error);
     }
     return Db::instance($this->database, $this->table);
 }
Example #29
0
 public function get($resource, $limit = 50, $lifetime = 30)
 {
     $instance = isAke(self::$instances, $resource, false);
     if (!$instance) {
         $instance = new self();
         $instance->resource = $resource;
         $instance->limit = $resource;
         $instance->lifetime = $lifetime;
         self::$instances[$resource] = $instance;
         Own::set($resource, 0);
         Own::set($resource . '.time', time() + $lifetime);
     }
     return $instance;
 }
Example #30
0
 public function getAffilZl($reseller_id)
 {
     $row = Model::Restodata()->where(['reseller_id', '=', (int) $reseller_id])->first(true);
     if (!$row) {
         return $this->getAffiliation($reseller_id, false);
     }
     if (isAke($row['options'], 'affiliation_resto', null) == 1) {
         return 'resto';
     } elseif (isAke($row['options'], 'affiliation_snack', null) == 1) {
         return 'snack';
     } elseif (isAke($row['options'], 'affiliation_vin', null) == 1) {
         return 'vin';
     }
 }