|
|
@@ -1,5 +1,5 @@
|
|
|
<?php
|
|
|
-/**
|
|
|
+/**
|
|
|
* MINZ - Copyright 2011 Marien Fressinaud
|
|
|
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
|
|
|
*/
|
|
|
@@ -7,85 +7,58 @@
|
|
|
/**
|
|
|
* La classe Model_array représente le modèle interragissant avec les fichiers de type texte gérant des tableaux php
|
|
|
*/
|
|
|
-class Minz_ModelArray extends Minz_ModelTxt {
|
|
|
+class Minz_ModelArray {
|
|
|
/**
|
|
|
- * $array Le tableau php contenu dans le fichier $nameFile
|
|
|
+ * $array Le tableau php contenu dans le fichier $filename
|
|
|
*/
|
|
|
protected $array = array ();
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * $filename est le nom du fichier
|
|
|
+ */
|
|
|
+ protected $filename;
|
|
|
+
|
|
|
/**
|
|
|
- * Ouvre le fichier indiqué, charge le tableau dans $array et le $nameFile
|
|
|
- * @param $nameFile le nom du fichier à ouvrir contenant un tableau
|
|
|
+ * Ouvre le fichier indiqué, charge le tableau dans $array et le $filename
|
|
|
+ * @param $filename le nom du fichier à ouvrir contenant un tableau
|
|
|
* Remarque : $array sera obligatoirement un tableau
|
|
|
*/
|
|
|
- public function __construct ($nameFile) {
|
|
|
- parent::__construct ($nameFile);
|
|
|
-
|
|
|
- if (!$this->getLock ('read')) {
|
|
|
- throw new Minz_PermissionDeniedException ($this->filename);
|
|
|
+ public function __construct ($filename) {
|
|
|
+ $this->filename = $filename;
|
|
|
+
|
|
|
+ if (!file_exists($this->filename)) {
|
|
|
+ throw new Minz_FileNotExistException($this->filename, Minz_Exception::WARNING);
|
|
|
+ }
|
|
|
+ elseif (($handle = $this->getLock()) === false) {
|
|
|
+ throw new Minz_PermissionDeniedException($this->filename);
|
|
|
} else {
|
|
|
- $this->array = include ($this->filename);
|
|
|
- $this->releaseLock ();
|
|
|
-
|
|
|
- if (!is_array ($this->array)) {
|
|
|
+ $this->array = include($this->filename);
|
|
|
+ $this->releaseLock($handle);
|
|
|
+
|
|
|
+ if ($this->array === false) {
|
|
|
+ throw new Minz_PermissionDeniedException($this->filename);
|
|
|
+ } elseif (is_array($this->array)) {
|
|
|
+ $this->array = $this->decodeArray($this->array);
|
|
|
+ } else {
|
|
|
$this->array = array ();
|
|
|
}
|
|
|
-
|
|
|
- $this->array = $this->decodeArray ($this->array);
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
- * Écrit un tableau dans le fichier $nameFile
|
|
|
- * @param $array le tableau php à enregistrer
|
|
|
+ * Sauve le tableau $array dans le fichier $filename
|
|
|
**/
|
|
|
- public function writeFile ($array) {
|
|
|
- if (!$this->getLock ('write')) {
|
|
|
- throw new Minz_PermissionDeniedException ($this->namefile);
|
|
|
- } else {
|
|
|
- $this->erase ();
|
|
|
-
|
|
|
- $this->writeLine ('<?php');
|
|
|
- $this->writeLine ('return ', false);
|
|
|
- $this->writeArray ($array);
|
|
|
- $this->writeLine (';');
|
|
|
-
|
|
|
- $this->releaseLock ();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private function writeArray ($array, $profondeur = 0) {
|
|
|
- $tab = '';
|
|
|
- for ($i = 0; $i < $profondeur; $i++) {
|
|
|
- $tab .= "\t";
|
|
|
- }
|
|
|
- $this->writeLine ('array (');
|
|
|
-
|
|
|
- foreach ($array as $key => $value) {
|
|
|
- if (is_int ($key)) {
|
|
|
- $this->writeLine ($tab . "\t" . $key . ' => ', false);
|
|
|
- } else {
|
|
|
- $this->writeLine ($tab . "\t" . '\'' . $key . '\'' . ' => ', false);
|
|
|
- }
|
|
|
-
|
|
|
- if (is_array ($value)) {
|
|
|
- $this->writeArray ($value, $profondeur + 1);
|
|
|
- $this->writeLine (',');
|
|
|
- } else {
|
|
|
- if (is_numeric ($value)) {
|
|
|
- $this->writeLine ($value . ',');
|
|
|
- } else {
|
|
|
- $this->writeLine ('\'' . addslashes ($value) . '\',');
|
|
|
- }
|
|
|
- }
|
|
|
+ protected function writeFile() {
|
|
|
+ if (!file_put_contents($this->filename, "<?php\n return " . var_export($this->array, true) . ';', LOCK_EX)) {
|
|
|
+ throw new Minz_PermissionDeniedException($this->filename);
|
|
|
}
|
|
|
-
|
|
|
- $this->writeLine ($tab . ')', false);
|
|
|
+ return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ //TODO: check if still useful, and if yes, use a native function such as array_map
|
|
|
private function decodeArray ($array) {
|
|
|
$new_array = array ();
|
|
|
-
|
|
|
+
|
|
|
foreach ($array as $key => $value) {
|
|
|
if (is_array ($value)) {
|
|
|
$new_array[$key] = $this->decodeArray ($value);
|
|
|
@@ -93,30 +66,32 @@ class Minz_ModelArray extends Minz_ModelTxt {
|
|
|
$new_array[$key] = stripslashes ($value);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return $new_array;
|
|
|
}
|
|
|
-
|
|
|
- private function getLock ($type) {
|
|
|
- if ($type == 'write') {
|
|
|
- $lock = LOCK_EX;
|
|
|
- } else {
|
|
|
- $lock = LOCK_SH;
|
|
|
+
|
|
|
+ private function getLock() {
|
|
|
+ $handle = fopen($this->filename, 'r');
|
|
|
+ if ($handle === false) {
|
|
|
+ return false;
|
|
|
}
|
|
|
-
|
|
|
- $count = 1;
|
|
|
- while (!flock ($this->file, $lock) && $count <= 50) {
|
|
|
- $count++;
|
|
|
+
|
|
|
+ $count = 50;
|
|
|
+ while (!flock($handle, LOCK_SH) && $count > 0) {
|
|
|
+ $count--;
|
|
|
+ usleep(1000);
|
|
|
}
|
|
|
-
|
|
|
- if ($count >= 50) {
|
|
|
- return false;
|
|
|
+
|
|
|
+ if ($count > 0) {
|
|
|
+ return $handle;
|
|
|
} else {
|
|
|
- return true;
|
|
|
+ fclose($handle);
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- private function releaseLock () {
|
|
|
- flock ($this->file, LOCK_UN);
|
|
|
+
|
|
|
+ private function releaseLock($handle) {
|
|
|
+ flock($handle, LOCK_UN);
|
|
|
+ fclose($handle);
|
|
|
}
|
|
|
}
|