Model_array.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * La classe Model_array représente le modèle interragissant avec les fichiers de type texte gérant des tableaux php
  8. */
  9. class Model_array extends Model_txt {
  10. /**
  11. * $array Le tableau php contenu dans le fichier $nameFile
  12. */
  13. protected $array = array ();
  14. /**
  15. * Ouvre le fichier indiqué, charge le tableau dans $array et le $nameFile
  16. * @param $nameFile le nom du fichier à ouvrir contenant un tableau
  17. * Remarque : $array sera obligatoirement un tableau
  18. */
  19. public function __construct ($nameFile) {
  20. parent::__construct ($nameFile);
  21. if (!$this->getLock ('read')) {
  22. throw new PermissionDeniedException ($this->filename);
  23. } else {
  24. $this->array = include ($this->filename);
  25. $this->releaseLock ();
  26. if (!is_array ($this->array)) {
  27. $this->array = array ();
  28. }
  29. $this->array = $this->decodeArray ($this->array);
  30. }
  31. }
  32. /**
  33. * Écrit un tableau dans le fichier $nameFile
  34. * @param $array le tableau php à enregistrer
  35. **/
  36. public function writeFile ($array) {
  37. if (!$this->getLock ('write')) {
  38. throw new PermissionDeniedException ($this->namefile);
  39. } else {
  40. $this->erase ();
  41. $this->writeLine ('<?php');
  42. $this->writeLine ('return ', false);
  43. $this->writeArray ($array);
  44. $this->writeLine (';');
  45. $this->releaseLock ();
  46. }
  47. }
  48. private function writeArray ($array, $profondeur = 0) {
  49. $tab = '';
  50. for ($i = 0; $i < $profondeur; $i++) {
  51. $tab .= "\t";
  52. }
  53. $this->writeLine ('array (');
  54. foreach ($array as $key => $value) {
  55. if (is_int ($key)) {
  56. $this->writeLine ($tab . "\t" . $key . ' => ', false);
  57. } else {
  58. $this->writeLine ($tab . "\t" . '\'' . $key . '\'' . ' => ', false);
  59. }
  60. if (is_array ($value)) {
  61. $this->writeArray ($value, $profondeur + 1);
  62. $this->writeLine (',');
  63. } else {
  64. if (is_numeric ($value)) {
  65. $this->writeLine ($value . ',');
  66. } else {
  67. $this->writeLine ('\'' . addslashes ($value) . '\',');
  68. }
  69. }
  70. }
  71. $this->writeLine ($tab . ')', false);
  72. }
  73. private function decodeArray ($array) {
  74. $new_array = array ();
  75. foreach ($array as $key => $value) {
  76. if (is_array ($value)) {
  77. $new_array[$key] = $this->decodeArray ($value);
  78. } else {
  79. $new_array[$key] = stripslashes ($value);
  80. }
  81. }
  82. return $new_array;
  83. }
  84. private function getLock ($type) {
  85. if ($type == 'write') {
  86. $lock = LOCK_EX;
  87. } else {
  88. $lock = LOCK_SH;
  89. }
  90. $count = 1;
  91. while (!flock ($this->file, $lock) && $count <= 50) {
  92. $count++;
  93. }
  94. if ($count >= 50) {
  95. return false;
  96. } else {
  97. return true;
  98. }
  99. }
  100. private function releaseLock () {
  101. flock ($this->file, LOCK_UN);
  102. }
  103. }