Skip to content
Extraits de code Groupes Projets
Valider 084372dc rédigé par Fil's avatar Fil
Parcourir les fichiers

reecriture de tous les iterateurs sur le mode de la class Iterator de PHP

http://php.net/manual/fr/class.iterator.php

il devient possible de faire :

{{{
<?php
$s = new IterSQL(

array (
    'select' => 
    array (
      0 => 'articles.date',
      1 => 'articles.id_article',
      2 => 'articles.id_rubrique',
      3 => 'articles.titre',
      5 => 'articles.descriptif',
      6 => 'articles.chapo',
      7 => 'articles.lang',
    ),
    'from' => 
    array (
      'articles' => 'spip_articles',
    ),
    'type' => 
    array (
    ),
    'where' => 
    array (
      0 => 
      array (
        0 => '=',
        1 => 'articles.statut',
        2 => '\'publie\'',
      ),
      1 => '1=1',
    ),
    'join' => 
    array (
    ),
    'groupby' => 
    array (
    ),
    'orderby' => 
    array (
      0 => 'articles.date DESC',
    ),
    'limit' => '0,1',
    'having' => 
    array (
    ),
    'table' => 'articles',
    'id' => '_articles_recents',
    'connect' => '',
  )

);

foreach ($s as $k=>$v) {
	var_dump($v);
}

?>
}}}

mais aussi :

{{{
<?php

$s = new IterDATA(
	array (
	'source' => 'http://rezo.net/backend/',
	'sourcemode' => 'rss'
  )
);

foreach ($s as $k=>$v) {
	var_dump($v);
}

?>
}}}
parent 403a3446
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -362,7 +362,7 @@ define('CODE_CORPS_BOUCLE', '%s ...@@ -362,7 +362,7 @@ define('CODE_CORPS_BOUCLE', '%s
%s, %s,
array(%s) array(%s)
); );
if ($iter->ok) { if ($iter->valid()) {
%s%s$SP++; %s%s$SP++;
// RESULTATS // RESULTATS
%s %s
...@@ -472,7 +472,7 @@ function calculer_boucle_nonrec($id_boucle, &$boucles, $trace) { ...@@ -472,7 +472,7 @@ function calculer_boucle_nonrec($id_boucle, &$boucles, $trace) {
$boucle->numrows = true; $boucle->numrows = true;
$corps = "\n\t\$t0 = str_repeat($corps, \$Numrows['$id_boucle']['total']);"; $corps = "\n\t\$t0 = str_repeat($corps, \$Numrows['$id_boucle']['total']);";
} }
} else $corps = "while (\$Pile[\$SP] = \$iter->next()) {\n$corps\n }"; } else $corps = "while (\$Pile[\$SP]=\$iter->fetch()) {\n$corps\n }";
$count = ''; $count = '';
if (!$boucle->select) { if (!$boucle->select) {
...@@ -490,7 +490,7 @@ function calculer_boucle_nonrec($id_boucle, &$boucles, $trace) { ...@@ -490,7 +490,7 @@ function calculer_boucle_nonrec($id_boucle, &$boucles, $trace) {
if ($boucle->numrows OR $boucle->mode_partie) { if ($boucle->numrows OR $boucle->mode_partie) {
if ($count == 'count(*)') if ($count == 'count(*)')
$count = "array_shift(\$iter->next())"; $count = "array_shift(\$iter->next())";
else $count = "\$iter->count()"; else $count = "\$iter->total()";
$nums .= "\$Numrows['$id_boucle']['total'] = @intval($count);" $nums .= "\$Numrows['$id_boucle']['total'] = @intval($count);"
. $boucle->mode_partie . $boucle->mode_partie
. "\n\t"; . "\n\t";
...@@ -523,7 +523,7 @@ function calculer_boucle_nonrec($id_boucle, &$boucles, $trace) { ...@@ -523,7 +523,7 @@ function calculer_boucle_nonrec($id_boucle, &$boucles, $trace) {
)'; )';
break; break;
case 'IterPOUR': case 'IterDATA':
case 'IterENUM': case 'IterENUM':
$command = 'array("where" => $where, "source"=>$source, "sourcemode"=>$sourcemode, "limit" => $limit)'; $command = 'array("where" => $where, "source"=>$source, "sourcemode"=>$sourcemode, "limit" => $limit)';
break; break;
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
// annonce au compilo les "champs" disponibles // annonce au compilo les "champs" disponibles
// //
function public_creer_boucle_DATA_dist($b) { function public_creer_boucle_DATA_dist($b) {
$b->iterateur = 'IterPOUR'; # designe la classe d'iterateur $b->iterateur = 'IterDATA'; # designe la classe d'iterateur
$b->show = array( $b->show = array(
'field' => array( 'field' => array(
'cle' => 'STRING', 'cle' => 'STRING',
......
...@@ -18,6 +18,7 @@ function public_creer_boucle_ENUM_dist($b) { ...@@ -18,6 +18,7 @@ function public_creer_boucle_ENUM_dist($b) {
$b->iterateur = 'IterENUM'; # designe la classe d'iterateur $b->iterateur = 'IterENUM'; # designe la classe d'iterateur
$b->show = array( $b->show = array(
'field' => array( 'field' => array(
'cle' => 'STRING',
'valeur' => 'STRING', 'valeur' => 'STRING',
) )
); );
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
// annonce au compilo les "champs" disponibles // annonce au compilo les "champs" disponibles
// //
function public_creer_boucle_POUR_dist($b) { function public_creer_boucle_POUR_dist($b) {
$b->iterateur = 'IterPOUR'; # designe la classe d'iterateur $b->iterateur = 'IterDATA'; # designe la classe d'iterateur
$b->show = array( $b->show = array(
'field' => array( 'field' => array(
'tableau' => 'ARRAY', 'tableau' => 'ARRAY',
......
...@@ -16,91 +16,108 @@ ...@@ -16,91 +16,108 @@
// Iterateur SQL // Iterateur SQL
// //
class Iter { class Iter implements Iterator {
var $ok = false; # http://php.net/manual/fr/class.iterator.php
var $type; public function __construct() {} // initialise
var $command; public function rewind() {} // revient au depart
var $info; public function valid() {} // avons-nous un element
public function current() {} // quel est sa valeur
private $result = false; public function key() {} // quelle est sa cle
public function next() {} // avancer d'un cran
/*
* array command: les commandes d'initialisation # Iter SPIP
* array info: les infos sur le squelette var $type; # type de l'iterateur
*/ var $command; # parametres de l'iterateur
public function Iter($command, $info=array()) { var $info; # infos de compilateur
$this->type = '??';
$this->command = $command; // avancer en position n
$this->info = $info; public function seek($n=0, $continue=null) {
$this->rewind();
while($n-->0 AND $this->valid()) $this->next();
return true;
}
public function fetch() {
if ($this->valid()) {
$r = array('cle' => $this->key(), 'valeur' => $this->current());
$this->next();
} else
$r = false;
return $r;
} }
public function seek($n=0, $continue=null) {} public function free() {} // liberer la ressource
public function next() {} public function total() {} // #TOTAL_BOUCLE
public function free() {}
public function count() {}
} }
class IterSQL extends Iter { class IterSQL extends Iter {
var $ok = false;
var $type;
var $command;
var $info;
private $result = false; private $sqlresult = false; # ressource sql
private $row = null; # row sql courante
private function select() { private function select() {
$v = &$this->command; $v = &$this->command;
$this->result = calculer_select($v['select'], $v['from'], $v['type'], $v['where'], $v['join'], $v['groupby'], $v['orderby'], $v['limit'], $v['having'], $v['table'], $v['id'], $v['connect'], $this->info); $this->sqlresult = calculer_select($v['select'], $v['from'], $v['type'], $v['where'], $v['join'], $v['groupby'], $v['orderby'], $v['limit'], $v['having'], $v['table'], $v['id'], $v['connect'], $this->info);
$this->ok = !!$this->result; $this->ok = !!$this->sqlresult;
if ($this->ok)
$this->row = sql_fetch($this->sqlresult, $this->command['connect']);
} }
/* /*
* array command: les commandes d'initialisation * array command: les commandes d'initialisation
* array info: les infos sur le squelette * array info: les infos sur le squelette
*/ */
public function IterSQL($command, $info=array()) { public function __construct($command, $info=array()) {
$this->type='SQL'; $this->type='SQL';
$this->command = $command; $this->command = $command;
$this->info = $info; $this->info = $info;
$this->select(); $this->select();
} }
public function rewind() {
return $this->seek(0);
}
public function valid() {
return is_array($this->row);
}
public function current() {
return $this->row;
}
public function seek($n=0, $continue=null) { public function seek($n=0, $continue=null) {
# SQLite ne sait pas seek(), il faut relancer la query # SQLite ne sait pas seek(), il faut relancer la query
if (!$a = sql_seek($this->result, $this->command['connect'], $n, $continue)) { if (!$a = sql_seek($this->sqlresult, $this->command['connect'], $n, $continue)) {
$this->free(); $this->free();
$this->select(); $this->select();
return true; # ?? return true;
} }
return $a; return $a;
} }
public function next(){ public function next(){
return sql_fetch($this->result, $this->command['connect']); $this->row = sql_fetch($this->sqlresult, $this->command['connect']);
}
public function fetch(){
if ($this->valid()) {
$r = $this->current();
$this->next();
} else
$r = false;
return $r;
} }
public function free(){ public function free(){
return sql_free($this->result, $this->command['connect']); return sql_free($this->sqlresult, $this->command['connect']);
} }
public function count() { public function total() {
return sql_count($this->result, $this->command['connect']); return sql_count($this->sqlresult, $this->command['connect']);
} }
} }
class IterENUM extends Iter { class IterENUM extends Iter {
var $ok = true; private $n = 0;
var $type; private $pos = 0;
var $command; private $start = 0;
var $info; private $offset = 0;
private $total = 1000000;
var $n = 0; private $max = 1000000;
var $max = 1000000; private $filtre = array();
var $filtre = array(); public function __construct($command=array(), $info=array()) {
private $result = false;
/*
* array command: les commandes d'initialisation
* array info: les infos sur le squelette
*/
public function IterENUM($command, $info=array()) {
$this->type='ENUM'; $this->type='ENUM';
$this->command = $command; $this->command = $command;
$this->info = $info; $this->info = $info;
...@@ -128,60 +145,82 @@ class IterENUM extends Iter { ...@@ -128,60 +145,82 @@ class IterENUM extends Iter {
// critere {2,7} // critere {2,7}
if ($this->command['limit']) { if ($this->command['limit']) {
$limit = explode(',',$this->command['limit']); $limit = explode(',',$this->command['limit']);
$this->n = $limit[0]; $this->offset = $limit[0];
$this->max = $limit[0]+$limit[1]-1; $this->total = $limit[1];
} }
// Appliquer les filtres sur (valeur) // Appliquer les filtres sur (valeur)
if ($this->filtre) { if ($this->filtre) {
$this->filtre = create_function('$valeur', $b = 'return ('.join(') AND (', $this->filtre).');'); $this->filtre = create_function('$cle,$valeur', $b = 'return ('.join(') AND (', $this->filtre).');');
} }
} }
public function seek($n=0, $continue=null) { public function rewind() {
$this->n = $n; $this->n = $this->start-1;
return true; $this->pos = -1;
for ($i=0; $i<=$this->offset; $i++) {
$this->next(); # pour filtre
$this->pos=0;
}
}
public function valid(){
return
$this->n <= $this->max
AND $this->pos < $this->total;
}
public function current() {
return $this->n;
}
public function key() {
return $this->pos;
} }
public function next() { public function next() {
$this->pos++;
$this->n++;
if ($f = $this->filtre) { if ($f = $this->filtre) {
while ( while (
$this->n < $this->max $this->n <= $this->max
AND !$f($a = $this->n++)){}; AND !$f($this->pos,$this->n)) {
} else $this->n++;
$a = $this->n++; }
}
if ($this->n <= 1+$this->max)
return array('valeur' => $a);
} }
public function free(){
public function seek($n=0, $continue=null) {
$this->n = $this->start-1;
$this->pos = -1;
for ($i=0; $i<=$n; $i++) {
$this->next(); # pour filtre
}
return true;
} }
public function count() { public function total() {
return $this->max; return $this->total;
} }
} }
class IterPOUR extends Iter { class IterDATA extends Iter {
var $ok = false; private $tableau = array();
var $type; private $filtre = array();
var $command; private $cle = null;
var $info; private $valeur = null;
var $tableau = array();
var $filtre = array();
private $result = false;
/* public function __construct($command, $info=array()) {
* array command: les commandes d'initialisation $this->type='DATA';
* array info: les infos sur le squelette
*/
public function IterPOUR($command, $info=array()) {
$this->type='POUR';
$this->command = $command; $this->command = $command;
$this->info = $info; $this->info = $info;
$this->select($command);
}
public function rewind() {
reset($this->tableau);
list($this->cle, $this->valeur) = each($this->tableau);
}
private function select($command) {
// les commandes connues pour l'iterateur POUR // les commandes connues pour l'iterateur POUR
// sont : tableau=#ARRAY ; cle=...; valeur=... // sont : tableau=#ARRAY ; cle=...; valeur=...
// source URL // source URL
...@@ -305,23 +344,29 @@ class IterPOUR extends Iter { ...@@ -305,23 +344,29 @@ class IterPOUR extends Iter {
$limit[0],$limit[1],true); $limit[0],$limit[1],true);
} }
$this->rewind();
reset($this->tableau);
#var_dump($this->tableau); #var_dump($this->tableau);
} }
public function seek($n=0, $continue=null) { public function seek($n=0, $continue=null) {
reset($this->tableau); $this->rewind();
while($n-->0 AND list($cle, $valeur) = each($this->tableau)){}; while($n-->0
AND list($this->cle, $this->valeur) = each($this->tableau)){};
return true; return true;
} }
public function next(){ public function valid(){
if (list($cle, $valeur) = each($this->tableau)) { return !is_null($this->cle);
return array('cle' => $cle, 'valeur' => $valeur);
}
} }
public function free(){ public function current() {
return $this->valeur;
}
public function key() {
return $this->cle;
}
public function next(){
if ($this->valid())
list($this->cle, $this->valeur) = each($this->tableau);
} }
public function count() { public function total() {
return count($this->tableau); return count($this->tableau);
} }
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter