Ejemplo n.º 1
0
 /**
  * This will call the initial build a new instance. It should be called only once from within the script. All subsequent calls to controllers should be done through $this->add($path).
  *
  * @param object | string $db The database object in the first call and the controller path in the calls within the class (through Add)<em>(e.g books/466565 or html/home)</em>
  * @param string | object $parent The parent controller</em>
  * @return bool
  */
 public function __construct(\bbn\db\connection $db)
 {
     $this->control();
     if (count($_POST) > 0) {
         $this->post = array_map(function ($a) {
             return \bbn\str\text::correct_types($a);
         }, $_POST);
     }
     if (count($_GET) > 0) {
         $this->get = array_map(function ($a) {
             return \bbn\str\text::correct_types($a);
         }, $_GET);
     }
     if (count($_FILES) > 0) {
         foreach ($_FILES as $n => $f) {
             if (is_array($f['name'])) {
                 $this->files[$n] = [];
                 foreach ($f['name'] as $i => $v) {
                     array_push($this->files[$n], ['name' => $v, 'tmp_name' => $f['tmp_name'][$i], 'type' => $f['type'][$i], 'error' => $f['error'][$i], 'size' => $f['size'][$i]]);
                 }
             } else {
                 $this->files[$n] = $f;
             }
         }
     }
     if (isset($_SERVER['REQUEST_URI']) && (BBN_CUR_PATH === '' || strpos($_SERVER['REQUEST_URI'], BBN_CUR_PATH) !== false)) {
         $url = explode("?", urldecode($_SERVER['REQUEST_URI']))[0];
         $tmp = explode('/', substr($url, strlen(BBN_CUR_PATH)));
         $num_params = count($tmp);
         foreach ($tmp as $t) {
             if (!empty($t)) {
                 array_push($this->params, $t);
             }
         }
     }
     if (is_object($db) && ($class = get_class($db)) && ($class === 'PDO' || strpos($class, 'bbn\\db\\') !== false)) {
         $this->db = $db;
     } else {
         $this->db = false;
     }
     /** @property \stdClass $inc An object which properties will store another object accessible throughout controllers and models */
     $this->inc = new \stdClass();
     $this->routes = $parent;
     $this->cli = php_sapi_name() === 'cli';
     $this->mustache = false;
     // When using CLI a first parameter can be used as route,
     // a second JSON encoded can be used as $this->post
     if ($this->cli) {
         global $argv;
         if (isset($argv[1])) {
             $tmp = explode('/', $argv[1]);
             $num_params = count($tmp);
             foreach ($tmp as $t) {
                 if (!empty($t)) {
                     array_push($this->params, $t);
                 }
             }
             if (isset($argv[2]) && json_decode($argv[2])) {
                 $this->post = array_map(function ($a) {
                     return \bbn\str\text::correct_types($a);
                 }, json_decode($argv[2], 1));
             }
         }
     }
     // If an available mode starts the URL params, it will be picked up
     if (count($this->params) > 0 && isset($this->outputs[$this->params[0]])) {
         $this->original_mode = $this->params[0];
         array_shift($this->params);
     } else {
         if (isset($this->post['appui']) && isset($this->outputs[$this->post['appui']])) {
             $this->original_mode = $this->post['appui'];
             unset($this->post['appui']);
         } else {
             if (count($this->post) > 0) {
                 if (isset($this->post['appui'])) {
                     unset($this->post['appui']);
                 }
                 $this->original_mode = 'json';
             } else {
                 $this->original_mode = 'dom';
             }
         }
     }
     if ($this->cli) {
         $this->original_mode = 'cli';
     }
     $this->url = implode('/', $this->params);
     $this->mode = $this->original_mode;
     $path = $this->url;
     $this->inc =& $parent->inc;
     $this->routes =& $parent->routes;
     $this->mustache =& $parent->mustache;
     $this->cli =& $parent->cli;
     $this->db =& $parent->db;
     $this->post =& $parent->post;
     $this->get =& $parent->get;
     $this->files =& $parent->files;
     $this->params =& $parent->params;
     $this->url =& $parent->url;
     $this->original_controller =& $parent->original_controller;
     $this->original_mode =& $parent->original_mode;
     $this->known_controllers =& $parent->known_controllers;
     $this->loaded_views =& $parent->loaded_views;
     $this->ucontrollers =& $parent->ucontrollers;
     $this->arguments = [];
     if ($this->has_data($data)) {
         $this->data = $data;
     }
     $path = $db;
     while (strpos($path, '/') === 0) {
         $path = substr($path, 1);
     }
     while (substr($path, -1) === '/') {
         $path = substr($path, 0, -1);
     }
     $params = explode('/', $path);
     if ($this->cli) {
         $this->mode = 'cron';
     } else {
         if (isset($params[0]) && isset($this->outputs[$params[0]])) {
             $this->mode = array_shift($params);
             $path = implode('/', $params);
         } else {
             if ($this->original_mode === 'dom') {
                 $this->mode = 'html';
             } else {
                 $this->mode = $this->original_mode;
             }
         }
     }
     if (isset($path)) {
         $this->route($path);
     }
 }
Ejemplo n.º 2
0
 /**
  * @return void 
  */
 public function fetchObject($class_name = "stdClass", $ctor_args = array())
 {
     $this->execute();
     return \bbn\str\text::correct_types(parent::fetchObject($class_name, $ctor_args));
 }