aboutsummaryrefslogtreecommitdiff
path: root/lib/Minz/ModelArray.php
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2013-12-15 03:30:24 +0100
committerGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2013-12-15 03:30:24 +0100
commit878e96202e8a22e4857b98e29b0a1fce68eccbc9 (patch)
treef9233c3b48a0cd6e0ac2536ddcc1897201595ad4 /lib/Minz/ModelArray.php
parent4af233e1f736eb2256e5e1696418635165467855 (diff)
Grosse refactorisation pour permettre le chargement automatique des classes
C'est parti de changements pour https://github.com/marienfressinaud/FreshRSS/issues/255 et finalement j'ai continué la refactorisation... Ajout de préfixes FreshRSS_ et Minz_ sur le modèle de SimplePie_. Toutes les classes sont maintenant en chargement automatique (devrait améliorer les performances en évitant de charger plein de classes inutilisées, et faciliter la maintenance). Suppression de set_include_path(). Si souhaité, certaines classes de Minz pourraient être déplacées dans un sous-répertoire, par exemple les exceptions. Tests et relecture nécessaires.
Diffstat (limited to 'lib/Minz/ModelArray.php')
-rw-r--r--lib/Minz/ModelArray.php122
1 files changed, 122 insertions, 0 deletions
diff --git a/lib/Minz/ModelArray.php b/lib/Minz/ModelArray.php
new file mode 100644
index 000000000..4ba022143
--- /dev/null
+++ b/lib/Minz/ModelArray.php
@@ -0,0 +1,122 @@
+<?php
+/**
+ * MINZ - Copyright 2011 Marien Fressinaud
+ * Sous licence AGPL3 <http://www.gnu.org/licenses/>
+*/
+
+/**
+ * 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 {
+ /**
+ * $array Le tableau php contenu dans le fichier $nameFile
+ */
+ protected $array = array ();
+
+ /**
+ * Ouvre le fichier indiqué, charge le tableau dans $array et le $nameFile
+ * @param $nameFile 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);
+ } else {
+ $this->array = include ($this->filename);
+ $this->releaseLock ();
+
+ if (!is_array ($this->array)) {
+ $this->array = array ();
+ }
+
+ $this->array = $this->decodeArray ($this->array);
+ }
+ }
+
+ /**
+ * Écrit un tableau dans le fichier $nameFile
+ * @param $array le tableau php à enregistrer
+ **/
+ 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) . '\',');
+ }
+ }
+ }
+
+ $this->writeLine ($tab . ')', false);
+ }
+
+ private function decodeArray ($array) {
+ $new_array = array ();
+
+ foreach ($array as $key => $value) {
+ if (is_array ($value)) {
+ $new_array[$key] = $this->decodeArray ($value);
+ } else {
+ $new_array[$key] = stripslashes ($value);
+ }
+ }
+
+ return $new_array;
+ }
+
+ private function getLock ($type) {
+ if ($type == 'write') {
+ $lock = LOCK_EX;
+ } else {
+ $lock = LOCK_SH;
+ }
+
+ $count = 1;
+ while (!flock ($this->file, $lock) && $count <= 50) {
+ $count++;
+ }
+
+ if ($count >= 50) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ private function releaseLock () {
+ flock ($this->file, LOCK_UN);
+ }
+}