Beispiel #1
0
 public function model($data = [])
 {
     $view = false;
     $db = $this->db;
     $table = $this->table;
     $modelFile = APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models' . DS . Inflector::lower($db) . DS . ucfirst(Inflector::lower($table)) . '.php';
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql')) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql');
     }
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models')) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models');
     }
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models' . DS . Inflector::lower($db))) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Mysql' . DS . 'models' . DS . Inflector::lower($db));
     }
     if (!File::exists($modelFile)) {
         File::put($modelFile, str_replace('##class##', ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'SQLModel', File::read(__DIR__ . DS . 'dbModel.tpl')));
     }
     $class = '\\Thin\\' . ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'SQLModel';
     if (!class_exists($class)) {
         require_once $modelFile;
     }
     $model = $this;
     if (true === $view) {
         $model = self::instance($db, $table);
     }
     return new $class($model, $data);
 }
Beispiel #2
0
 public function background()
 {
     $file = realpath(APPLICATION_PATH . DS . '..' . DS . 'public' . DS . 'scripts' . DS . 'schedule.php');
     if (File::exists($file)) {
         $cmd = 'php ' . $file;
         lib('utils')->backgroundTask($cmd);
     }
 }
Beispiel #3
0
 public function __construct($namespace, $entity)
 {
     $this->db = STORAGE_PATH . DS . 'dbNode_' . $namespace . '::' . $entity . '.data';
     $this->lock = STORAGE_PATH . DS . 'dbNode_' . $namespace . '::' . $entity . '.lock';
     if (!File::exists($this->db)) {
         File::put($this->db, json_encode(array()));
     }
     $this->buffer = json_decode(fgc($this->db), true);
     $this->clean();
 }
Beispiel #4
0
 public function read($file)
 {
     $key = $this->redisKey($file);
     $cache = $this->model->cache()->get($key);
     if (!strlen($cache)) {
         if (File::exists($file)) {
             $cache = File::read($file);
             $this->model->cache()->set($key, $cache);
         }
     }
     return $cache;
 }
Beispiel #5
0
 public function getOptionsFromMarket($market_id)
 {
     if (!is_integer($market_id)) {
         throw new Exception("market_id must be an integer id.");
     }
     $file = APPLICATION_PATH . DS . 'models' . DS . 'options' . DS . $market_id . '.php';
     if (File::exists($file)) {
         $options = (include $file);
         return $options;
     }
     return [];
 }
Beispiel #6
0
 public function __construct(Database $model)
 {
     $this->model = $model->inCache(false);
     $fileConfig = APPLICATION_PATH . DS . 'models' . DS . 'CrudNosql' . DS . ucfirst(Inflector::camelize($model->db)) . DS . ucfirst(Inflector::camelize($model->table)) . '.php';
     if (File::exists($fileConfig)) {
         $this->config = (include $fileConfig);
     } else {
         $this->config = [];
     }
     if (!empty($this->config)) {
         $this->prepareFields();
     }
     if (get_magic_quotes_gpc()) {
         $_REQUEST = $this->stripslashes($_REQUEST);
     }
 }
Beispiel #7
0
 public function model($data = [])
 {
     $db = $this->db;
     $table = $this->table;
     $modelFile = APPLICATION_PATH . DS . 'models' . DS . 'Fast' . DS . 'models' . DS . Inflector::lower($db) . DS . ucfirst(Inflector::lower($table)) . '.php';
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'Fast' . DS . 'models' . DS . Inflector::lower($db))) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'Fast' . DS . 'models' . DS . Inflector::lower($db));
     }
     if (!File::exists($modelFile)) {
         File::put($modelFile, str_replace('##class##', ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'ModelFast', File::read(__DIR__ . DS . 'dbModel.tpl')));
     }
     $class = '\\Thin\\' . ucfirst(Inflector::lower($db)) . ucfirst(Inflector::lower($table)) . 'ModelFast';
     if (!class_exists($class)) {
         require_once $modelFile;
     }
     $model = $this;
     return new $class($model, $data);
 }
Beispiel #8
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();
     }
 }
Beispiel #9
0
 public function hage($hash, $key)
 {
     $file = $this->getHashFile($hash, $key);
     if (File::exists($file)) {
         return filemtime($file);
     }
     return 0;
 }
Beispiel #10
0
 public function read($file, $default = null, $mode = 'rb', $try = 1)
 {
     $file = $this->dir . DS . $file . '.php';
     if (File::exists($file)) {
         $fp = fopen($file, 'rb');
         if (!flock($fp, LOCK_EX)) {
             if ($try < 100) {
                 usleep(50000);
                 return $this->read($file, $default, $mode, $try++);
             } else {
                 throw new Exception("The file '{$file}' can not be locked.");
             }
         }
         $data = unserialize(File::read($file));
         flock($fp, LOCK_UN);
         fclose($fp);
         return $data;
     }
     return $default;
 }
Beispiel #11
0
 private static function go($module, $controller, $action)
 {
     $cdir = APPLICATION_PATH . DS . 'modules' . DS . SITE_NAME . DS . Inflector::lower($module) . DS . 'controllers';
     if (!is_dir($cdir)) {
         static::is404();
     } else {
         $dirApps = realpath(APPLICATION_PATH);
         $tplDir = realpath($dirApps . DS . 'modules' . DS . SITE_NAME . DS . Inflector::lower($module) . DS . 'views');
         $controllerDir = realpath($dirApps . DS . 'modules' . DS . SITE_NAME . DS . Inflector::lower($module) . DS . 'controllers');
         $tpl = $tplDir . DS . Inflector::lower($controller) . DS . Inflector::lower($action) . '.phtml';
         $controllerFile = $controllerDir . DS . Inflector::lower($controller) . 'Controller.php';
         if (!file::exists($controllerFile)) {
             return static::is404();
         } else {
             if (File::exists($tpl)) {
                 $view = new View($tpl);
             }
             require_once $controllerFile;
             $controllerClass = 'Thin\\' . Inflector::lower($controller) . 'Controller';
             $instance = new $controllerClass();
             if (File::exists($tpl)) {
                 $instance->view = $view;
             }
             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';
             $actions = get_class_methods($controllerClass);
             if (!Arrays::in($actionName, $actions)) {
                 $continue = false;
                 foreach ($actions as $act) {
                     if (Inflector::lower($act) == Inflector::lower($actionName)) {
                         $actionName = $act;
                         $continue = true;
                         break;
                     }
                 }
                 if (false === $continue) {
                     return static::is404();
                 }
             }
             if (Arrays::in('init', $actions)) {
                 $instance->init();
             }
             if (Arrays::in('preDispatch', $actions)) {
                 $instance->preDispatch();
             }
             $instance->{$actionName}();
             if (File::exists($tpl)) {
                 $instance->view->render();
             }
             /* stats */
             if (File::exists($tpl) && null === container()->getNoShowStats()) {
                 echo View::showStats();
             }
             if (Arrays::in('postDispatch', $actions)) {
                 $instance->postDispatch();
             }
             if (Arrays::in('exit', $actions)) {
                 $instance->exit();
             }
         }
     }
     exit;
 }
Beispiel #12
0
 public static function evaluate($template, array $parameters)
 {
     extract($parameters, EXTR_SKIP);
     if (File::exists($template)) {
         if (empty($view)) {
             throw new Exception("A view is needed to evaluate.");
         }
         ob_start();
         require $template;
         return ob_get_clean();
     } elseif (is_string($template)) {
         if (empty($view)) {
             throw new Exception("A view is needed to evaluate.");
         }
         ob_start();
         eval('; ?>' . $template . '<?php ;');
         return ob_get_clean();
     } else {
         throw new Exception("A view is needed to evaluate.");
     }
 }
Beispiel #13
0
 private function search($condition = null, $results = array(), $populate = true)
 {
     $searchFile = $this->dir . DS . sha1(serialize(func_get_args()));
     $changeFile = $this->dir . DS . 'change';
     if (File::exists($changeFile) && File::exists($searchFile)) {
         $ageSearch = filemtime($searchFile);
         $ageChange = filemtime($changeFile);
         if ($ageSearch > $ageChange) {
             $collection = json_decode(file_get_contents($searchFile), true);
             if (true === $populate) {
                 $this->results = $collection;
             }
             return $collection;
         } else {
             File::delete($searchFile);
         }
     }
     $datas = !count($results) ? $this->all() : $results;
     if (empty($condition)) {
         return $datas;
     }
     $collection = array();
     $condition = repl('LIKE START', 'LIKESTART', $condition);
     $condition = repl('LIKE END', 'LIKEEND', $condition);
     $condition = repl('NOT LIKE', 'NOTLIKE', $condition);
     $condition = repl('NOT IN', 'NOTIN', $condition);
     list($field, $op, $value) = explode(' ', $condition, 3);
     if ($value instanceof Container) {
         $value = $value->id();
         $field = $field . '_id';
     }
     if (count($datas)) {
         foreach ($datas as $tab) {
             if (!empty($tab)) {
                 $val = isAke($tab, $field, null);
                 if (strlen($val)) {
                     $val = repl('|', ' ', $val);
                     $check = $this->compare($val, $op, $value);
                 } else {
                     $check = 'null' == $value ? true : false;
                 }
                 if (true === $check) {
                     array_push($collection, $tab);
                 }
             }
         }
     }
     if (true === $populate) {
         $this->results = $collection;
     }
     File::put($searchFile, json_encode($collection));
     return $collection;
 }
Beispiel #14
0
 public function limit($limit, $offset = 0)
 {
     $hash = sha1($this->db->getHash() . 'limit' . serialize(func_get_args()));
     $cursor = $this->db->motor()->getPath() . DS . 'cursors' . DS . $hash;
     if (is_dir($cursor)) {
         $ageCursor = filemtime($cursor . DS . '.');
         $ageDb = $this->db->getAge();
         if ($ageDb < $ageCursor) {
             $this->cursor = $cursor;
             return $this;
         } else {
             File::rmdir($cursor);
         }
     }
     File::mkdir($this->db->motor()->getPath() . DS . 'cursors');
     File::mkdir($this->db->motor()->getPath() . DS . 'cursors' . DS . $hash);
     $index = 0;
     for ($i = $offset; $i < $limit; $i++) {
         $file = $this->cursor . DS . $i . '.php';
         if (File::exists($file)) {
             $newFile = $cursor . DS . $index . '.php';
             $data = (include $file);
             File::put($newFile, "<?php\nreturn " . var_export($data, 1) . ';');
             $index++;
         }
     }
     $this->cursor = $cursor;
     return $this;
 }
Beispiel #15
0
 private function cached($key, $value = null)
 {
     if (false === $this->cache) {
         return null;
     }
     $settings = isAke(self::$configs, $this->entity);
     $event = isAke($settings, 'cache');
     if (!empty($event)) {
         return $this->{$event}($key, $value);
     }
     $file = STORAGE_PATH . DS . 'cache' . DS . $key . '.eav';
     if (empty($value)) {
         if (File::exists($file)) {
             $age = filemtime($file);
             $maxAge = time() - $this->ttl;
             if ($maxAge < $age) {
                 return json_decode(File::get($file), true);
             } else {
                 File::delete($file);
                 return null;
             }
         }
     } else {
         if (File::exists($file)) {
             File::delete($file);
         }
         File::put($file, json_encode($value));
         return true;
     }
 }
Beispiel #16
0
 public function cached($key, $data = null)
 {
     $file = CACHE_PATH . DS . $key . '_sql';
     if (!empty($data)) {
         File::put($file, serialize($data));
         return $data;
     }
     if (File::exists($file)) {
         $age = time() - filemtime($file);
         if ($age > $this->_tts) {
             File::delete($file);
         } else {
             return unserialize(fgc($file));
         }
     }
 }
Beispiel #17
0
 /**
  * Determine if a file exists.
  *
  * @param  string  $path
  * @return bool
  */
 public function exists($path)
 {
     return File::exists($path);
 }
Beispiel #18
0
 public static function dispatch()
 {
     lib('lang')->locale('web');
     $controller = isAke(static::$route, 'controller', false);
     $action = isAke(static::$route, 'action', false);
     $file = APPLICATION_PATH . DS . 'front' . DS . 'controllers' . DS . $controller . '.php';
     $tpl = APPLICATION_PATH . DS . 'front' . DS . 'views' . DS . $controller . DS . $action . '.phtml';
     if (!File::exists($file)) {
         static::is404();
     }
     require_once $file;
     $class = 'Thin\\' . ucfirst($controller) . 'Controller';
     $i = new $class();
     if (File::exists($tpl)) {
         $i->view = new Container();
         if (static::$pjax) {
             $i->view->partial = function ($partial) {
                 return true;
             };
         } else {
             $i->view->partial = function ($partial) use($i) {
                 $tpl = APPLICATION_PATH . DS . 'front' . DS . 'views' . DS . 'partials' . DS . $partial . '.phtml';
                 if (File::exists($tpl)) {
                     $code = View::lng(File::read($tpl));
                     $code = str_replace('$this', '$i->view', $code);
                     eval('; ?>' . $code . '<?php ;');
                 }
             };
         }
     }
     $methods = get_class_methods($i);
     $call = strtolower(static::$method) . ucfirst($action);
     if (!Arrays::in($call, $methods)) {
         static::is404();
     }
     if (Arrays::in('init', $methods)) {
         $i->init();
     }
     $i->{$call}();
     if (Arrays::in('after', $methods)) {
         $i->after();
     }
     if (File::exists($tpl)) {
         $code = View::lng(File::read($tpl));
         $code = str_replace('$this', '$i->view', $code);
         header("HTTP/1.0 200 OK");
         eval('; ?>' . $code . '<?php ;');
         exit;
     }
 }
Beispiel #19
0
 public function keys($pattern = '*')
 {
     $coll = [];
     $glob = glob($this->dir . DS . $pattern, GLOB_NOSORT);
     foreach ($glob as $row) {
         if (fnmatch('*expire.*', $row)) {
             continue;
         }
         $row = str_replace([$this->dir . DS, '.raw'], '', $row);
         $expire = $this->getFile('expire.' . $row);
         if (File::exists($expire)) {
             $expiration = (int) unserialize(File::read($expire));
             if (time() > $expiration) {
                 File::delete($this->getFile($row));
                 File::delete($expire);
             } else {
                 $coll[] = $row;
             }
         } else {
             $coll[] = $row;
         }
     }
     return SplFixedArray::fromArray($coll);
 }
Beispiel #20
0
 private function makeId()
 {
     $storage = Config::get($this->db . '.storage', 'local');
     if ($storage == 'local') {
         $instance = new Local($this);
     }
     if (true === $this->cacheEnabled) {
         $id = $this->cache()->incr(sha1($this->dir) . 'indexes');
         if ($storage == 'local') {
             while (File::exists($instance->hashFile($this->dir . DS . $id . '.row', false))) {
                 $id = $this->cache()->incr(sha1($this->dir) . 'indexes');
             }
         } else {
             while (File::exists($this->dir . DS . $id . '.row')) {
                 $id = $this->cache()->incr(sha1($this->dir) . 'indexes');
             }
         }
     } else {
         $id = 1;
         if ($storage == 'local') {
             while (File::exists($instance->hashFile($this->dir . DS . $id . '.row', false))) {
                 $id++;
             }
         } else {
             while (File::exists($this->dir . DS . $id . '.row')) {
                 $id++;
             }
         }
     }
     array_push($this->keys, $id);
     return $id;
 }
Beispiel #21
0
 public static function generate($model, $overwrite = false)
 {
     $file = APPLICATION_PATH . DS . 'models' . DS . 'Crud' . DS . ucfirst(Inflector::camelize($model)) . '.php';
     if (!File::exists($file) || $overwrite) {
         $db = model($model);
         $crud = new Crud($db);
         File::delete($file);
         $tplModel = fgc(__DIR__ . DS . 'Model.tpl');
         $tplField = fgc(__DIR__ . DS . 'Field.tpl');
         $fields = $crud->fields();
         $singular = ucfirst($model);
         $plural = $singular . 's';
         $default_order = $crud->pk();
         $tplModel = str_replace(array('##singular##', '##plural##', '##default_order##'), array($singular, $plural, $default_order), $tplModel);
         $fieldsSection = '';
         foreach ($fields as $field) {
             if ($field != $crud->pk()) {
                 $label = substr($field, -3) == '_id' ? ucfirst(str_replace('_id', '', $field)) : ucfirst(Inflector::camelize($field));
                 $fieldsSection .= str_replace(array('##field##', '##label##'), array($field, $label), $tplField);
             }
         }
         $tplModel = str_replace('##fields##', $fieldsSection, $tplModel);
         File::put($file, $tplModel);
     }
 }
Beispiel #22
0
 public function find($id, $object = true)
 {
     $file = $this->dir . DS . $id . '.row';
     $row = File::exists($file) ? fgc($file) : '';
     if (strlen($row)) {
         $tab = json_decode($row, true);
         return $object ? $this->makeObject($tab) : $tab;
     }
     return $object ? null : array();
 }
Beispiel #23
0
 public function valid()
 {
     $file = $this->cursor . DS . $this->position . '.php';
     return File::exists($file);
 }
Beispiel #24
0
 public function read($name)
 {
     $file = $this->getFile($name);
     if (File::exists($file)) {
         $content = (include $file);
         if (Arrays::isAssoc($content)) {
             return $content;
         }
         return current($content);
     }
     return null;
 }
Beispiel #25
0
 private function parse($pageConfig, $htmlTemplate, $htmlPage)
 {
     eval($pageConfig);
     $code = str_replace('@@content', $htmlPage, $htmlTemplate);
     $code = str_replace(array('{{', '}}'), array('<?php', '?>'), $code);
     if (strstr($code, '@@partial')) {
         $partials = $this->getPartials($code);
         if (count($partials)) {
             foreach ($partials as $partial) {
                 $partialFile = $this->dir . DS . 'partials' . DS . $partial . '.html';
                 if (File::exists($partialFile)) {
                     $code = str_replace("@@partial({$partial})", fgc($partialFile), $code);
                 }
             }
         }
     }
     $vars = $this->getVars($code);
     $url = container()->getUrlsite();
     $assets = '/content/' . SITE_NAME . '/themes/' . $this->theme . '/assets';
     if (count($vars)) {
         foreach ($vars as $var) {
             if (!isset(${$var})) {
                 $value = CmsConfig::get($var, false);
                 if (false === $value) {
                     $value = isAke($_REQUEST, $var, false);
                     if (false === $value) {
                         $value = '';
                     }
                 }
             } else {
                 $value = ${$var};
             }
             $code = str_replace("%({$var})%", $value, $code);
         }
     }
     return $code;
 }
Beispiel #26
0
 function ThinLog($message, $logFile = null, $type = 'info')
 {
     if (null === $logFile) {
         $logFile = LOGS_PATH . DS . date('Y-m-d') . '.log';
     } else {
         if (false === File::exists($logFile)) {
             File::create($logFile);
         }
     }
     File::append($logFile, date('Y-m-d H:i:s') . "\t" . Inflector::upper($type) . "\t{$message}\n");
 }
Beispiel #27
0
 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);
     }
 }
Beispiel #28
0
 public static function dispatch()
 {
     header("Access-Control-Allow-Origin: *");
     static::$method = Request::method();
     $uri = substr(str_replace('/mobi/', '/', $_SERVER['REQUEST_URI']), 1);
     $tab = explode('/', $uri);
     if (!strlen($uri) || $uri == '/') {
         $namespace = 'static';
         $controller = 'home';
         $action = 'index';
     } else {
         if (count($tab) < 3) {
             self::isForbidden();
         }
         $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 . 'mobi' . DS . $namespace . DS . 'controllers' . DS . $controller . '.php';
     // dd($file);
     if (!File::exists($file)) {
         self::is404();
     }
     require_once $file;
     $class = 'Thin\\' . ucfirst($controller) . 'Mobi';
     $i = new $class();
     $methods = get_class_methods($i);
     $call = strtolower(static::$method) . ucfirst($action);
     if (!Arrays::in($call, $methods)) {
         self::is404();
     }
     if (Arrays::in('init', $methods)) {
         $i->init($call);
     }
     $i->{$call}();
     if ($i->view === true) {
         $tpl = APPLICATION_PATH . DS . 'mobi' . DS . $namespace . DS . 'views' . DS . $controller . DS . $action . '.phtml';
         if (File::exists($tpl)) {
             $content = File::read($tpl);
             $content = str_replace('$this->', '$i->', $content);
             $fileTpl = CACHE_PATH . DS . sha1($content) . '.display';
             File::put($fileTpl, $content);
             ob_start();
             include $fileTpl;
             $html = ob_get_contents();
             ob_end_clean();
             File::delete($fileTpl);
             self::render($html);
         } else {
             self::render('OK');
         }
     }
     if (Arrays::in('after', $methods)) {
         $i->after();
     }
 }
Beispiel #29
0
 /**
  * Destructor
  *
  * Delete all files created by syrup component run
  */
 function __destruct()
 {
     $preserveRunFolder = $this->preserveRunFolder;
     $fs = new Filesystem();
     foreach ($this->files as $file) {
         if ($file['preserve']) {
             $preserveRunFolder = true;
         }
         if (File::exists($file['file']) && File::is($file['file']) && true !== $file['preserve']) {
             $fs->remove($file['file']);
         }
     }
     if (false === $preserveRunFolder) {
         $fs->remove($this->tmpRunFolder);
     }
 }
Beispiel #30
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();
     }
 }