]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 18 Sep 2016 04:03:59 +0000 (05:03 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 18 Sep 2016 04:03:59 +0000 (05:03 +0100)
etc/profile.d/php.sh
lib/php/db/table.php
lib/php/nb.php
lib/php/out.php

index fd7faa28fe7f139a947fe8c3f9d363655737e436..cdab7ed8e297cde3410fcc46b99ba8304a02557e 100644 (file)
@@ -1,7 +1,11 @@
 php425() {
 #array("");
-  local in=cat
-  [ -n "$*" ] && in="echo $*"
+  #local in=cat
+  #[ -n "$*" ] && in="echo $*"
   #$in | perl -pe ' s/\barray\(((?:[^()]++|\((?R)\))*)\)/[$1]/g '
-  $in | perl -e '$_=join("",<>); s/\barray\(((?:[^()]++|\((?R)\))*)\)/[$1]/sg; print $_; '
+  #$in | perl -e '$_=join("",<>); s/\barray\(((?:[^()]++|\((?R)\))*)\)/[$1]/sg; print $_; '
+  perl -e '$_=join("",<>); s/\barray\(((?:[^()]++|\((?R)\))*)\)/[$1]/sg; print $_; ' $@
 }
+
+function php_check_file() { php5 --define error_reporting=22519 --define display_errors=1 --define log_errors=1 --define html_errors=0 $@; }
+
index 2c3ce1380270c1974109ca991def063bba07091a..b6534041d48adfde0a31271b5eb46ba4fdf6b426 100644 (file)
@@ -772,15 +772,16 @@ Class Table extends nb {
     ).')$/',$format))
     {
 
-      if (!($out_conf = out::type($format))) $this->bye("Unknow format `$format`");
-      self::$params += array_values(out::$types);
+      if (!($out_conf = out::types($format))) $this->bye("Unknow format `$format`");
+      out::type($format);
+      self::$params += array_values(out::types());
 
       if (empty($out_conf['enclose'])) $out_conf['enclose'] = ['',''];
       debug('Using out module!',3);
 
     }
 
-    if ($opt['is_html']) out::$type = 'html';
+    if ($opt['is_html']) out::type('html');
     $count = 0;
     $extras = self::p('extras') === '0' ? 0 : 1;
     #bye($extras);
@@ -1040,7 +1041,7 @@ Class Table extends nb {
   -----------------------------------------------------------------*/
   public function rows_begin_yaml() { return "---\n"; }
   #public function rows_rec_yaml(&$row) { return $this->yaml_encode($row); }
-  public function rows_rec_yaml(&$row) { $o=["function"=>"out_yaml"];out::row($o,$row); }
+  public function rows_rec_yaml(&$row) { $o=['row'=>'out_yaml'];out::row($o,$row); }
   public function rows_end_yaml() { return ''; }
 
   /*-----------------------------------------------------------------
index ab2f8813993e7fd567ca1396f9289c31802a0c5e..2451ddac16b4c3008a0306c76a42f2f2dc3f01bb 100644 (file)
@@ -71,7 +71,7 @@ class NB {
    * Return a param
    */
   public static function p($name=null,$default=null) {
-    if ($name === null) return isset($_REQUEST) ? $_REQUEST : array();
+    if ($name === null) return isset($_REQUEST) ? $_REQUEST : [];
     return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
   }
 
index 8f6d59dcc6f1d8f82e0ddb47845a0cf1c67fdbd1..33e651e2dc45cf8aab3f0ead9f38ee115889e3e1 100644 (file)
@@ -4,24 +4,27 @@ require_once(dirname(__FILE__).'/nb.php');
 Class Out extends Nb {
 
   public static $charset = 'utf-8';
-  public static $types;
-  public static $type;
+  protected static $types;
+  protected static $type;
   public static function init() {
 
+    $self = get_class();
     self::$types = array(
 
       'human' => [
-        'sep' => (self::p('sep') ? self::p('sep') : " | "),
-        'border' => '_',
+        'sep' => self::p('sep'," | "),
+        'corner' => self::p('corner',"+"),
+        'eol' => self::p('eol',"\n"),
+        'line' => self::p('line',"-"),
         'head' => 'out_human_head',
-        'function' => 'out_human',
+        'row' => 'out_human',
         'end' => 'out_human_end',
       ],
 
       'sql' => array(),
 
       'sh' => array(
-        'function' => function(&$r) {
+        'row' => function(&$r) {
           $line = [];
           foreach ($r as $k=>$v) {
             if (!preg_match('/^[a-zA-Z\w_]+$/',$k)) continue;
@@ -32,16 +35,16 @@ Class Out extends Nb {
         }
       ),
 
-      #'php' => array( 'function' => function(&$r) {var_dump($r);},),
+      #'php' => array( 'row' => function(&$r) {var_dump($r);},),
       'php' => array(
         #'enclose' => array('$VAR=array(',');'),
         'enclose' => array('$CONF = array('.NB_EOL,NB_EOL.');'),
-        'eol' => NB_EOL,
+        'eol' => self::p('eol',NB_EOL),
         'rec' => ',',
-        #'function' => function(&$r) { echo preg_replace('/[\r\n]/','',var_dump($r,true));},
-        #'function' => function(&$r) {print_r($r);}
-        #'function' => function(&$r) {var_dump($r);}
-        'function' => function(&$r) {
+        #'row' => function(&$r) { echo preg_replace('/[\r\n]/','',var_dump($r,true));},
+        #'row' => function(&$r) {print_r($r);}
+        #'row' => function(&$r) {var_dump($r);}
+        'row' => function(&$r) {
           return var_export($r);
           echo preg_replace(array(
             '/(=>\s*)\n\s+/m',
@@ -51,12 +54,12 @@ Class Out extends Nb {
             "),".NB_EOL."array"
           ),var_export($r,true));
         }
-        #'function' => function(&$r) { echo preg_replace('/\n/','',var_export($r,true));}
-        #'function' => function(&$r) {var_export($r);}
+        #'row' => function(&$r) { echo preg_replace('/\n/','',var_export($r,true));}
+        #'row' => function(&$r) {var_export($r);}
       ),
-      #'var_dump' => array( 'function' => function(&$r) {var_dump($r);},),
-      #'print_r' => array( 'function' => function(&$r) {print_r($r);},),
-      #'var_export' => array( 'function' => function(&$r) {var_export($r);},),
+      #'var_dump' => array( 'row' => function(&$r) {var_dump($r);},),
+      #'print_r' => array( 'row' => function(&$r) {print_r($r);},),
+      #'var_export' => array( 'row' => function(&$r) {var_export($r);},),
 
       'div' => array(
         'is_html' => true,
@@ -64,7 +67,7 @@ Class Out extends Nb {
         'tag_enclose' => 'div class="row"',
         'tag' => 'div',
         'tag_key' => 'label',
-        'function' => 'out_tag',
+        'row' => 'out_tag',
         'head' => 'out_tag_head',
       ),
 
@@ -73,7 +76,7 @@ Class Out extends Nb {
         'enclose' => array("<center class=\"rows\">".NB_EOL,"</center>".NB_EOL),
         'tag_enclose' => 'div class="row"',
         'tag' => 'div',
-        'function' => 'out_tag',
+        'row' => 'out_tag',
         'head' => 'out_tag_head',
       ),
 
@@ -83,43 +86,65 @@ Class Out extends Nb {
         'tag_enclose' => 'tr class="row"',
         'tag_head' => 'th',
         'tag' => 'td',
-        'function' => 'out_tag',
+        'row' => 'out_tag',
         'head' => 'out_tag_head',
       ),
 
       'xml' => array(
         'enclose' => array('<?xml version="1.0" encoding="utf-8"?'.'>'.NB_EOL."<rows>".NB_EOL,"</rows>".NB_EOL),
-        'function' => 'out_xml',
+        'eol' => self::p('eol',NB_EOL),
+        'row' => function (&$row,$o) {
+          echo ($o['eol'] ? '  ' : '')."<row>".$o['eol'];
+          foreach ($row as $k => $v) {
+            echo ($o['eol'] ? '    ' : '')."<$k><![CDATA[".out::scalar($v)."]]></$k>".$o['eol'];
+          }
+          echo ($o['eol'] ? '  ' : '')."</row>".$o['eol'];
+        },
       ),
 
-      'text' => array(
-        'eol' => "\n",
-        #'function' => 'print',
+      'txt' => array(
+        'col' => self::p('col',' : '),
+        'sep' => self::p('sep',"\n"),
+        'eol' => self::p('eol',">\n"),
       ),
 
       'csv' => array(
+        'sep' => self::p('sep',"\t"),
+        'eol' => self::p('eol',"\n"),
         'enclose' => array("","\n"),
-        'eol' => "\n",
-        'sep' => (self::p('sep') ? self::p('sep') : "\t"),
-        'head' => 'out_csv_head',
-        'function' => 'out_csv',
-      ),
+        'head' => function(&$o,&$row) {
+          if (!is_array($row)) echo 0;
+          echo "\r";
+          if (out::is_hash($row)) {
+            $ar = array_keys($row);
+            return out_csv($ar,$o);
+          } else {
+            return out_csv($row,$o);
+          }
+        },
 
-      'yaml' => array(
-        'enclose' => array("---\n",""),
-        'function' => 'out_yaml',
-        #'function' => 'yaml_emit',
+        'row' => 'out_csv',
       ),
 
+      'yaml' => [
+        'enclose' => ["---\n",""],
+        'row' => function (&$row,&$o) {
+          $yaml = '- '.out::yaml_encode($row);
+          $yaml = preg_replace("/^(?!-)/m","  ",$yaml);
+          echo $yaml;
+        }
+      ],
+
       'json' => array(
         'enclose' => array('['.NB_EOL,NB_EOL.']'),
-        'function' => 'json_encode',
-        'eol' => NB_EOL,
+        'row' => function($row) { return json_encode($row); },
+        'eol' => self::p('eol',NB_EOL),
         'rec' => ',',
       ),
 
     );
-    self::$type = self::client_type();
+
+    self::type(self::client_type());
   }
 
   public function __construct($opt=[],&$data=[],$head=[]) {
@@ -128,19 +153,22 @@ Class Out extends Nb {
       'charset',
       'type',
     ] as $k) {
-      if (isset($opt[$k])) self::${$k} = $opt[$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);
+    if (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 types($type=null) {
+    if (!empty($type)) return(empty(self::$types[$type]) ? null : self::$types[$type] );
+    return self::$types;
+  }
+
+  public static function type($set=null) { if (!empty($set)) self::$type = $set; return self::$type; }
 
   public static function scalar($v) {
     if ($v === null) return '';
@@ -166,8 +194,35 @@ Class Out extends Nb {
     return is_scalar($v) ? $v : json_encode($v);
   }
 
+  public static function type_call($k,&$o,&$row) {
+
+    $type = self::type();
+
+    if (!isset($o[$k])) {
+
+      # Try internal method
+      $call = self::type() ? self::type()."_$k" : '';
+      $call = str_replace('_row','',$call);
+      #if ($call and in_array($call,get_class_methods(get_class()))) return call_user_func_array([get_class(),$call],$params);
+      #if ($call and in_array($call,get_class_methods(get_class()))) debug($call);
+      if ($call and in_array($call,get_class_methods(get_class()))) return self::$call($o,$row);
+
+    } elseif(is_callable($o[$k]) and is_callable($o[$k])) {
+
+      $call = $o[$k];
+      echo $call($o,$row);
+      return '';
+
+    }
+
+    return '';
+
+  }
+
   public static function head(&$o,$head,$data=array()) {
+    $o['header'] = (self::p('header')==='0') ? false : true;
 
+    # Format $head
     if ($head !== false) {
 
       if (is_scalar($head) and !is_bool($head)) {
@@ -181,8 +236,8 @@ Class Out extends Nb {
     }
 
     # For sprintf
-    unset($o['head_max_len']);
     if (empty($data)) {
+      unset($o['head_max_len']);
 
     } else if (self::is_hash($data[0])) {
 
@@ -194,63 +249,41 @@ Class Out extends Nb {
 
     }
 
-    #bye($o);
+#bye($o);
 
     if (isset($o['enclose'])) echo $o['enclose'][0];
-    if (!isset($o['head'])) return;
-
-#var_dump($head); return;
-# NB 01.04.16     if (empty($head) and $head!== false) {
-# NB 01.04.16       if (self::is_hash($data[0])) {
-# NB 01.04.16         $head = array_keys($data[0]);
-# NB 01.04.16       } else {
-# NB 01.04.16         return;
-# NB 01.04.16       }
-# NB 01.04.16     }
-
-    if (self::p('header') === '0' ) return;
-    if ($head === false) return;
+    if (0
+      or !$o['header']
+      or $head === false
+      #or !isset($o['head'])
+    ) return;
 
-    echo $o['head']($head,$o) . empty($o['eol']) ? '' : $o['eol'];
+    self::type_call('head',$o,$head) . empty($o['eol']) ? '' : $o['eol'];
     return $head;
 
   }
 
   public static function row(&$o,&$row) {
 
+    #self::type_call('row',$o,$row);
     # Defined function
-    if (isset($o['function'])) {
-      if (is_scalar($o['function']) and preg_match('/^out_/',$o['function'])) {
-        $o['function']($row,$o);
+    if (isset($o['row'])) {
+
+      if (is_scalar($o['row']) and preg_match('/^out_/',$o['row'])) {
+        $o['row']($row,$o);
       } else {
-        echo $o['function']($row,$o);
+        echo $o['row']($row,$o);
       }
       return true;
     }
 
-    # Assume html/xml tag like
+    # Assume html/xml tag style
     if (isset($o['tag'])) {
       return out_tag($row,$o);
     }
 
-    # yaml
-    if (!self::is_hash($row)) {
-    #bye($row);
-    }
-
-# NB 10.04.16     if (!self::is_hash($row)) unset($o['head_max_len']);
-# NB 10.04.16     if (is_array($row) and count($row)>1) $o['head_max_len'] = mb_strlen(count($row));
-    #if (!self::is_hash($row)) return out_yaml($row,$o);
-
-    # text
-    foreach ($row as $k => $v) {
-      echo sprintf("%"
-        .(!empty($o['head_max_len']) ? '-'.($o['head_max_len']+1) : '')
-        ."s: %s\n",$k,preg_replace('/\n(\w)/m'
-          ,"\n".str_repeat(' ',(empty($o['head_max_len']) ? mb_strlen($k) : $o['head_max_len']+1)+2).'\1'
-        ,self::format($v))
-      );
-    }
+    # txt
+    return self::txt($row,$o);
 
     return true;
   }
@@ -276,19 +309,19 @@ Class Out extends Nb {
 
   public static function rows($type,&$data,$head=[],$conf=[]) {
 
-    # Ex: for action=tables when header=0
-
     if (empty($data)) return;
-    if (is_scalar($data)) $data = array(array($data));
+    if (is_scalar($data)) $data = [[$data]];
     if (!isset(self::$types[$type])) self::bye("Unknow type: `$type`");
-    $conf = self::$types[$type] + $conf;
+    if ($type) self::type($type);
+    #$conf['type'] = $type;
 
-    #echo $conf['function']($data); return true;
-    if (empty($conf['enclose'])) $conf['enclose'] = array('','');
+    #echo $conf['row']($data); return true;
+    $conf = self::$types[$type] + $conf;
+    if (empty($conf['enclose'])) $conf['enclose'] = ['',''];
     if (empty($conf['eol'])) $conf['eol'] = '';
 
-    if (self::is_hash($data)) $data = array($data);
-    if (is_scalar($data)) $data = array($data);
+    if (self::is_hash($data)) $data = [$data];
+    if (is_scalar($data)) $data = [$data];
 
     $tot = count($data);
     $count = 0;
@@ -322,12 +355,6 @@ Class Out extends Nb {
     if ($count) self::end($conf);
   }
 
-  public static function type($name=null,$key='') {
-    if (empty(self::$types[$name])) return null;
-    $type = self::$types[$name];
-    if ($key) return( (array_key_exists($key,$type)) ? $type[$key] : null );
-    return $type;
-  }
 
   public static function client_type() {
     foreach (array_keys(self::$types) as $t) {
@@ -337,7 +364,7 @@ Class Out extends Nb {
   }
 
   public static function format($v,$type=null,$conf=[]) {
-    if ($type === null) $type = self::$type;
+    if ($type === null) $type = self::type();
     $is_scalar = is_scalar($v);
     $v = self::scalar($v);
     if ($v==='') $is_scalar = true;
@@ -349,8 +376,7 @@ Class Out extends Nb {
   }
 
   public static function is_html($type=null) {
-    if ($type === null) $type = self::$type;
-    #return self::type($type,'is_html')
+    if ($type === null) $type = self::type();
     return (strpos($type,'html') !==false or (
       !empty(self::$types[$type]['is_html'])
       #and !empty(self::$types[$type]['is_xml'])
@@ -366,7 +392,7 @@ Class Out extends Nb {
 
       #$replace_flags = ENT_COMPAT | ENT_DISALLOWED;
       $replace_flags = ENT_COMPAT;
-      if ($type === null) $type = self::$type;
+      if ($type === null) $type = self::type();
 
       if (!$type) {
         return $v;
@@ -383,7 +409,7 @@ Class Out extends Nb {
       } elseif ($type=='html' or !empty(self::$types[$type]['is_html'])) {
         $replace_flags = $replace_flags | ENT_HTML5;
 
-      } else { # text
+      } else { # txt
 # NB 06.04.16         $v = str_replace("\n",'|',$v);
 # NB 06.04.16         $v = preg_replace('/\n/','',$v);
 
@@ -395,36 +421,32 @@ Class Out extends Nb {
     return $v;
   }
 
+  public static function txt(&$row,&$o) {
+    $header = (out::p('header')==='0') ? false : true;
+    foreach ($row as $k => $v) {
+
+      if (!$header) {
+        echo self::format($v).$o['sep'];
+        continue;
+      }
+
+      printf(
+        "%".(!empty($o['head_max_len']) ? '-'.($o['head_max_len']) : '').'s'
+        .$o['col']."%s".$o['sep']
+        ,$k
+        ,preg_replace('/\n(\w)/m'
+          ,$o['sep'].str_repeat(' ',(empty($o['head_max_len']) ? mb_strlen($k,self::$charset) : $o['head_max_len']+1)+2).'\1'
+        ,self::format($v))
+      );
+    }
+  }
+
 } Out::init() ; # < Class
 /*--------------------------------------------------------------------------*/
 // Functions
 /*--------------------------------------------------------------------------*/
 
-//
-// Yaml
-//
-function out_yaml(&$row,$o) {
-  $yaml = '- '.out::yaml_encode($row);
-  $yaml = preg_replace("/^(?!-)/m","  ",$yaml);
-  echo $yaml;
-}
-
-//
-// Csv
-//
-function out_csv_head(&$row,$o) {
-  if (!is_array($row)) echo 0;
-  echo "\r";
-  if (out::is_hash($row)) {
-    $ar = array_keys($row);
-    return out_csv($ar,$o);
-  } else {
-    return out_csv($row,$o);
-  }
-}
-
 function out_csv(&$row,$o) {
-
   $values = array();
 
   foreach (array_values($row) as $k=>$v) {
@@ -433,13 +455,12 @@ function out_csv(&$row,$o) {
 
   if (!empty($o['return'])) return join($o['sep'],$values);
   echo join($o['sep'],$values);
-  #echo str_replace("\n",'\\n',join($o['sep'],$values));
 }
 
 //
 // Tag
 //
-function out_tag_head(&$data,$o) {
+function out_tag_head($o,&$data) {
   #if (isset($o['tag_key'])) return ''; # No header, field will be in rows - NB 24.03.16
   if (isset($o['tag_head'])) $o['tag'] = $o['tag_head'];
   $row = out::ar_first($data);
@@ -484,22 +505,12 @@ function out_tag(&$row,&$o) {
   if (isset($o['tag_enclose'])) echo (NB_EOL ? '  ' : '').'</'.preg_replace('/ .*$/','',$o['tag_enclose']).'>'.NB_EOL;
 }
 
-//
-// Xml
-//
-function out_xml(&$row,$o) {
-  echo (NB_EOL ? '  ' : '')."<row>".NB_EOL;
-  foreach ($row as $k => $v) {
-    echo (NB_EOL ? '    ' : '')."<$k><![CDATA[".out::scalar($v)."]]></$k>".NB_EOL;
-  }
-  echo (NB_EOL ? '  ' : '')."</row>".NB_EOL;
-}
-
 //
 // Human
 //
-function out_human_head(&$head,&$o) {
+function out_human_head(&$o,&$head) {
   #$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line));
+
   if (out::p('header')==='0') return '';
   $o['_human'] = [
     'head' => 1,
@@ -509,15 +520,23 @@ function out_human_head(&$head,&$o) {
 }
 
 function out_human(&$row,&$o) {
-  if (empty($GLOBALS['_human_len'])) {
-    $GLOBALS['_human_len'] = [];
+
+  # switch options
+  if (!isset($o['mark.human'])) {
     if (empty($o['_human'])) $o['_human'] = [];
-    $o['tmpfile'] = tmpfile();
+    $o['mark.human'] = 1;
 
-    # switch for later use of out_csv()
     $o['sep.human'] = $o['sep'];
     $o['sep'] = "\t";
 
+    $o['eol.human'] = $o['eol'];
+    $o['eol'] = "";
+  }
+
+  if (empty($GLOBALS['_human_len'])) {
+    $GLOBALS['_human_len'] = [];
+    $o['tmpfile'] = tmpfile();
+
     # no echo with out_csv()
     $o['return'] = true;
   }
@@ -525,11 +544,6 @@ function out_human(&$row,&$o) {
   $line = out_csv($row,$o);
   $i = 0;
   foreach (explode($o['sep'],$line) as $v) {
-# NB 16.09.16     if (preg_match('/^utf-?8$/i',out::$charset)) {
-# NB 16.09.16       $len = strlen( utf8_decode( $v ) );
-# NB 16.09.16     } else {
-# NB 16.09.16       $len = mb_strlen($v,out::$charset);
-# NB 16.09.16     }
     $len = mb_strlen($v,out::$charset);
     if (0
       or empty($GLOBALS['_human_len'][$i])
@@ -539,7 +553,7 @@ function out_human(&$row,&$o) {
   }
 
   #debug($line);
-  return fwrite($o['tmpfile'],$line."\n");
+  fwrite($o['tmpfile'],$line."\n");
 }
 
 function out_human_end(&$o) {
@@ -552,9 +566,9 @@ function out_human_end(&$o) {
   if (out::p('sep_line') !== '0') {
 
     foreach ($len as $i) {
-      $sep_line .= '+'.str_repeat('-',$i+strlen($o['sep.human'])-1);
+      $sep_line .= $o['corner'].str_repeat($o['line'],$i+strlen($o['sep.human'])-1);
     }
-    $sep_line .= '+'."\n";
+    $sep_line .= $o['corner'].$o['eol.human'];
   }
 
   $header = empty($o['_human']['head']) ? 0 : 1;
@@ -575,11 +589,11 @@ function out_human_end(&$o) {
     }
 
     #debug($count);
-    if ($header or $count>0)echo ''
+    if ($header or $count>0) echo ''
       . ltrim($o['sep.human'])
       . join($o['sep.human'],$values)
       . rtrim($o['sep.human'])
-    ."\n";
+    .$o['eol.human'];
 
     if ($header and !$count) echo $sep_line;
     $count++;