Exemplo n.º 1
0
 /**
  * Resolve an array of commands through the application.
  *
  * @param  array|dynamic  $commands
  * @return void
  */
 public function resolveCommands($commands)
 {
     $commands = Arrays::isArray($commands) ? $commands : func_get_args();
     foreach ($commands as $command) {
         $this->resolve($command);
     }
 }
Exemplo n.º 2
0
Arquivo: Di.php Projeto: schpill/thin
 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);
         }
     }
 }
Exemplo n.º 3
0
 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;
     }
 }
Exemplo n.º 4
0
 /**
  * Constructor
  *
  * @param array $config
  */
 public function __construct(array $config = [])
 {
     // Populate Keyring
     Keyring::setAppKey($config['AK']);
     // Application Key
     Keyring::setAppSecret($config['AS']);
     // Application Secret
     Keyring::setConsumerKey($config['CK']);
     // Consumer Key
     // Backward compatibility
     if (Arrays::exists('RG', $config)) {
         Keyring::setAppUrlRegion($config['RG']);
         // Region
     } else {
         Keyring::setAppUrlRegion("FR");
     }
     if (Arrays::exists('protocol', $config)) {
         Keyring::setAppHost($config['protocol']);
         // protocol
     } else {
         Keyring::setAppHost(static::$zeliftApiProtocol);
     }
     if (Arrays::exists('host', $config)) {
         Keyring::setAppProtocol($config['host']);
         // host
     } else {
         Keyring::setAppProtocol(static::$zeliftApiHost);
     }
 }
Exemplo n.º 5
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');
         }
     }
 }
Exemplo n.º 6
0
 /**
  * Getter
  * @param mixed $key
  * @return mixed
  */
 public function __get($key)
 {
     if (Arrays::exists($key, $this->data)) {
         return $this->data[$key];
     } else {
         return false;
     }
 }
Exemplo n.º 7
0
 /**
  * Get an Instantiated ReflectionClass.
  *
  * @param string $class Optional name of a class
  * @return mixed null or a ReflectionClass instance
  * @throws Exception if class was not found
  */
 public function get($class = null)
 {
     $class = $this->getClass($class);
     if (Arrays::exists($class, $this->reflections)) {
         return $this->reflections[$class];
     }
     throw new Exception("Class not found: {$class}");
 }
Exemplo n.º 8
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;
 }
Exemplo n.º 9
0
 /**
  * Creates a new navigation container
  *
  * @param array $pages  [optional] pages to add
  * @throws Exception    if $pages is invalid
  */
 public function __construct($pages = null)
 {
     if (Arrays::is($pages)) {
         $this->addPages($pages);
     } elseif (null !== $pages) {
         throw new Exception('Invalid argument: $pages must be an array or null');
     }
 }
Exemplo n.º 10
0
 /**
  * where()
  * Sets where object
  * @access public
  * @param string $where
  * @param string $value
  * @return \Thin\Webquery
  */
 public function where($where = null, $value = null)
 {
     if (Arrays::is($this->where)) {
         $this->whereKey = count($this->where) + 1;
     }
     $this->where[$this->whereKey]['attr'] = $where;
     $this->where[$this->whereKey]['value'] = $value;
     return $this;
 }
Exemplo n.º 11
0
 /**
  * Construct new RangeQuery component
  *
  * @return \ElasticSearch\DSL\RangeQuery
  * @param array $options
  */
 public function __construct(array $options = [])
 {
     $this->fieldname = key($options);
     $values = Arrays::first($options);
     if (Arrays::is($values)) {
         foreach ($values as $key => $val) {
             $this->{$key} = $val;
         }
     }
 }
Exemplo n.º 12
0
 public function remove($id)
 {
     if (!Arrays::exists($id, $this->services)) {
         throw new InvalidArgumentException(sprintf('Service "%s" is not defined.', $id));
     }
     list($prefix, $sid) = $this->getPrefixAndSid($id);
     if ($prefix) {
         unset($this->prefixed[$prefix][$sid]);
     }
     unset($this->services[$id]);
 }
Exemplo n.º 13
0
 public function __construct(array $array)
 {
     $this->key = sha1(serialize($array));
     if (Arrays::isAssoc($array)) {
         $tab = new SplFixedArray(1);
         $tab[0] = $array;
         $this->key .= 'AAA';
     } else {
         $tab = SplFixedArray::fromArray($array);
     }
     lib('resource')->set($this->key, $tab);
 }
Exemplo n.º 14
0
 public function read($name, $default = null)
 {
     $file = $this->getFile($name);
     $content = $this->import($file);
     if ($content) {
         if (Arrays::isAssoc($content)) {
             return $content;
         }
         return current($content);
     }
     return $default;
 }
Exemplo n.º 15
0
 /**
  * Get or set a config
  *
  * @param string $key
  * @param mixed $value
  * @throws \Exception
  * @return array|void
  */
 public function config($key, $value = null)
 {
     if (Arrays::is($key)) {
         $this->config = $key + $this->config;
     } else {
         if ($value !== null) {
             $this->config[$key] = $value;
         }
         if (!isset($this->config[$key])) {
             throw new Exception("Configuration key `type` is not set");
         }
         return $this->config[$key];
     }
 }
Exemplo n.º 16
0
 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;
 }
Exemplo n.º 17
0
 private static function eagerly($mongo, &$parents, $include)
 {
     $first = reset($parents);
     $mongo->attributes = $first->attributes;
     $relationship = $mongo->{$include}();
     $mongo->attributes = $_ids = array();
     foreach ($parents as &$parent) {
         $_ids[] = $parent->_id;
         $parent->ignore[$include] = Arrays::in($mongo->relating, array('has_many', 'has_and_belongs_to_many')) ? array() : null;
     }
     if (Arrays::in($relating = $mongo->relating, array('has_one', 'has_many', 'belongs_to'))) {
         static::$relating($relationship, $parents, $mongo->relating_key, $include, $_ids);
     } else {
         static::manyToMany($relationship, $parents, $mongo->relating_key, $mongo->relating_table, $include, $_ids);
     }
 }
Exemplo n.º 18
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());
 }
Exemplo n.º 19
0
 private function getOrm($seg)
 {
     if (!count($seg)) {
         throw new \Exception('Query is invalid.');
     }
     $seg = Arrays::first($seg);
     $table = isAke($seg, 'table', false);
     if (!$table) {
         throw new \Exception('Query is invalid.');
     }
     if (fnmatch('*.*', $table)) {
         list($database, $table) = explode('.', $table);
     } else {
         $database = SITE_NAME;
     }
     return jdb($database, $table);
 }
Exemplo n.º 20
0
 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 = Arrays::first($args);
         if (is_numeric($id)) {
             return Db::instance($database, $table)->find($id);
         }
     }
 }
Exemplo n.º 21
0
 /**
  * Build the DSL as array
  *
  * @throws \ElasticSearch\Exception
  * @return array
  */
 public function build()
 {
     $built = [];
     if ($this->from != null) {
         $built['from'] = $this->from;
     }
     if ($this->size != null) {
         $built['size'] = $this->size;
     }
     if ($this->sort && Arrays::is($this->sort)) {
         $built['sort'] = $this->sort;
     }
     if (!$this->query) {
         throw new \ElasticSearch\Exception("Query must be specified");
     } else {
         $built['query'] = $this->query->build();
     }
     return $built;
 }
Exemplo n.º 22
0
 public function query($query, $meta = [])
 {
     $q = json_encode($query);
     $q = urlencode($q);
     $url = $this->uri . '/db/' . $this->db . '/collection/' . $this->col . '/apiKey/' . $this->apiKey . '/query/' . $q;
     if (!empty($meta)) {
         $mv = [];
         foreach ($meta as $k => $v) {
             if (Arrays::is($v)) {
                 $v = json_encode($v);
             }
             $mv[] = $k . '=' . $v;
         }
         $mv = implode('&', $mv);
         $url .= '&' . $mv;
     }
     $ret = $this->request->get($url);
     return json_decode($ret, true);
 }
Exemplo n.º 23
0
 /**
  * Search
  *
  * @return array
  * @param array|string $query
  * @throws \ElasticSearch\Exception
  */
 public function search($query)
 {
     if (Arrays::is($query)) {
         if (Arrays::exists("query", $query)) {
             $dsl = new Stringify($query);
             $q = (string) $dsl;
             $url = $this->buildUrl(array($this->type, "_search?q=" . $q));
             $result = json_decode(redis()->get($url), true);
             return $result;
         }
         throw new \ElasticSearch\Exception("Redis protocol doesn t support the full DSL, only query");
     } elseif (is_string($query)) {
         /**
          * String based search means http query string search
          */
         $url = $this->buildUrl(array($this->type, '_search?q=' . $query));
         $result = json_decode(redis()->get($url), true);
         return $result;
     }
 }
Exemplo n.º 24
0
 public function init()
 {
     $this->view->config = context('config')->load('crudjson');
     $guestActions = array('login', 'logout', 'lost');
     $guestActions += arrayGet($this->view->config, 'guest.actions', array());
     $this->view->isAuth = false;
     $this->view->items = isAke($this->view->config, 'tables');
     $user = auth()->user();
     if (!$user) {
         if (!Arrays::in($this->action(), $guestActions)) {
             $this->forward('login');
         }
     } else {
         $this->view->isAuth = true;
         $this->view->user = $user;
         $this->isAdmin = auth()->is('admin');
         if ($this->action() == 'login' || $this->action() == 'lost') {
             $this->forward('home', 'static');
         }
     }
 }
Exemplo n.º 25
0
 public static function dispatch()
 {
     static::$method = Request::method();
     $uri = substr(str_replace('/ws/', '/', $_SERVER['REQUEST_URI']), 1);
     $tab = explode('/', $uri);
     if (count($tab) < 3) {
         Api::forbidden();
     }
     $namespace = current($tab);
     $controller = $tab[1];
     $action = $tab[2];
     $tab = array_slice($tab, 3);
     $count = count($tab);
     if (0 < $count && $count % 2 == 0) {
         for ($i = 0; $i < $count; $i += 2) {
             $_REQUEST[$tab[$i]] = $tab[$i + 1];
         }
     }
     $file = APPLICATION_PATH . DS . 'webservices' . DS . $namespace . DS . $controller . '.php';
     if (!File::exists($file)) {
         Api::NotFound();
     }
     require_once $file;
     $class = 'Thin\\' . ucfirst($controller) . 'Ws';
     $i = new $class();
     $methods = get_class_methods($i);
     $call = strtolower(static::$method) . ucfirst($action);
     if (!Arrays::in($call, $methods)) {
         Api::NotFound();
     }
     if (Arrays::in('init', $methods)) {
         $i->init($call);
     }
     $i->{$call}();
     if (Arrays::in('after', $methods)) {
         $i->after();
     }
 }
Exemplo n.º 26
0
 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;
     }
 }
Exemplo n.º 27
0
 public function __call($method, $parameters)
 {
     return $this->write($method, Arrays::first($parameters));
 }
Exemplo n.º 28
0
 /**
  * Eagerly load a 1:1 belonging relationship.
  *
  * @param  object  $relationship
  * @param  array   $parents
  * @param  string  $relating_key
  * @param  string  $include
  * @return void
  */
 private static function belongs_to($relationship, &$parents, $relating_key, $include)
 {
     $keys = array();
     foreach ($parents as &$parent) {
         $keys[] = $parent->{$relating_key};
     }
     $children = $relationship->where_in(Model::pk(get_class($relationship)), array_unique($keys))->get();
     foreach ($parents as &$parent) {
         if (Arrays::exists($parent->{$relating_key}, $children)) {
             $parent->ignore[$include] = $children[$parent->{$relating_key}];
         }
     }
 }
Exemplo n.º 29
0
 private function queryParamsCallback($matches)
 {
     return preg_replace('/./Uui', '*', Arrays::first($matches));
 }
Exemplo n.º 30
0
 public function select($what)
 {
     /* polymorphism */
     if (is_string($what)) {
         if (fnmatch('*,*', $what)) {
             $what = str_replace(' ', '', $what);
             $what = explode(',', $what);
         }
     }
     if (Arrays::is($what)) {
         foreach ($what as $seg) {
             if (!Arrays::in($seg, $this->selects)) {
                 $this->selects[] = $seg;
             }
         }
     } else {
         if (!Arrays::in($what, $this->selects)) {
             $this->selects[] = $what;
         }
     }
     return $this;
 }