From 4f71f488c1f5bf49fff3bb13c0ff6b70e4670c0a Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 7 Sep 2016 18:02:30 +0200 Subject: [PATCH] objects fix --- lib/php/db.php | 52 ++++++++++++++++++++++++--------------------- lib/php/db/page.php | 3 +-- lib/php/out.php | 20 +++++++++++++++-- lib/php/page.php | 12 ++++++++++- 4 files changed, 58 insertions(+), 29 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index cb2de02a..c875dc23 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -27,10 +27,13 @@ class Db extends nb { public $password = null; # Database infos - public $title; + public $id; public $name; + public $title; public $type; public $tables = []; + public $types = []; + public $conf = []; public $row_parse; # Function to call in rows() # Web @@ -40,6 +43,9 @@ class Db extends nb { public $formats = [ 'table','div','csv','xml','json','yaml','sh' ]; public $limits = ['10','20','50','100','500','1000']; + # Classes + public $out; + function __construct($opt = '') { # Args defaults @@ -65,10 +71,10 @@ class Db extends nb { } # Args into this - foreach ($opt as $k=>$v) $this->$k = $v; - #parent::__construct($opt); + #foreach ($opt as $k=>$v) $this->$k = $v; + parent::__construct($opt); #if(!is_scalar($opt['type'])) debug($opt['type']); - if (self::$encoding) out::$charset=self::$encoding; + if (empty($this->out)) $this->out = new Out(['charset'=>self::$encoding]); # id if (!empty($this->conf)) { @@ -77,8 +83,7 @@ class Db extends nb { } # Formats - #$this->formats += array_keys(out::$types); - foreach(out::$types as $t=>$v) if (!in_array($t,$this->formats)) $this->formats[] = $t; + foreach($this->out->types() as $t=>$v) if (!in_array($t,$this->formats)) $this->formats[] = $t; # Pdo $this->connect_init(); @@ -416,7 +421,7 @@ class Db extends nb { } public function out($rows,$head=[]) { - out::rows($this->p('format',out::php_cli() ? 'csv' : 'table'),$rows,$head); + $this->out->rows($this->p('format',$this->out->php_cli() ? 'csv' : 'table'),$rows,$head); return true; } @@ -590,7 +595,7 @@ class Db extends nb { # Construct assoc array $db = array_merge($conf[$id],array( - 'dbs' => array_keys($conf), + #'dbs' => array_keys($conf), 'conf' => $conf, 'id' => $id, )); @@ -911,27 +916,26 @@ class Db extends nb { 'o' => 'format', ]); - if ($action=self::p('action')) { - foreach ([ + if ($action=self::p('action')) { + foreach ([ - 'ls' => 'db.ls', - 'tables' => 'db.tables', + 'ls' => 'db.ls', + 'tables' => 'db.tables', - 'fields' => 'table.fields', - 'rows' => 'table.rows', - 'insert' => 'table.insert', - 'replace' => 'table.replace', - 'update' => 'table.update', - 'delete' => 'table.delete', + 'fields' => 'table.fields', + 'rows' => 'table.rows', + 'insert' => 'table.insert', + 'replace' => 'table.replace', + 'update' => 'table.update', + 'delete' => 'table.delete', - ] as $src => $dest) { - if ($action === $src) { - $action = $dest; + ] as $src => $dest) { + if ($action === $src) { + $action = $dest; + } + self::pset('action',$action); } - self::pset('action',$action); } - } - #if($format = out::client_type()) self::pset('format',$format); # Param - Extract dbname from table if (false and preg_match('/^(\w+)\.(.*?)$/',self::p('table'),$m)) { diff --git a/lib/php/db/page.php b/lib/php/db/page.php index 5ecfa15e..9421f5f0 100644 --- a/lib/php/db/page.php +++ b/lib/php/db/page.php @@ -1,10 +1,9 @@ 'Test', - 'content_type' => (Page::php_cli() and !Page::p('web') ? 'text/plain' : 'text/html'), + 'content_type' => ((Page::php_cli() and !Page::p('web') )? 'text/plain' : 'text/html'), 'call' => array( 'begin', ['out', "Hello World !!!\n"], diff --git a/lib/php/out.php b/lib/php/out.php index 2c9d8fa6..37c63ce8 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -122,10 +122,26 @@ Class Out extends Nb { self::$type = self::client_type(); } - public function __construct($type,&$data,$head=array()) { - if (!empty($type) and !empty($data)) return $this->rows($type,$data,$head); + public function __construct($opt=[],&$data=[],$head=[]) { + // Statics + foreach ([ + 'charset', + 'type', + ] as $k) { + if (isset($opt[$k])) self::${$k} = $opt[$k]; + unset($opt[$k]); + } + + if (isset($opt['type'])) self::$type = $opt['type']; + unset($opt['type']); + + if (!empty(self::$type) and !empty($data)) return self::rows(self::$type,$data,$head); + return parent::__construct($opt); } + public static function charset($set=null) { if (!empty($set)) self::$charset = $set; return self::$charset; } + public static function types($set=null) { if (!empty($set)) self::$types = $set; return self::$types; } + public static function scalar($v) { if ($v === null) return ''; if (is_scalar($v)) return $v; diff --git a/lib/php/page.php b/lib/php/page.php index f2038b66..fe470cf4 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -53,9 +53,19 @@ class Page extends nb { // Content-type if (isset($opt['content_type'])) $this->content_type($opt['content_type']); - elseif($this->php_cli()) $this->content_type('text/html'); + #elseif($this->php_cli()) $this->content_type('text/html'); unset($opt['content_type']); + // Statics + foreach ([ + 'charset', + 'lang', + 'html5', + ] as $k) { + if (isset($opt[$k])) self::${$k} = $opt[$k]; + unset($opt[$k]); + } + // Opt parent::__construct($opt); -- 2.47.3