ModelArray.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * The Minz_ModelArray class is the model to interact with text files containing a PHP array
  8. */
  9. class Minz_ModelArray {
  10. /**
  11. * $filename est le nom du fichier
  12. */
  13. protected string $filename;
  14. /**
  15. * Ouvre le fichier indiqué, charge le tableau dans $array et le $filename
  16. * @param string $filename le nom du fichier à ouvrir contenant un tableau
  17. * Remarque : $array sera obligatoirement un tableau
  18. */
  19. public function __construct(string $filename) {
  20. $this->filename = $filename;
  21. }
  22. /** @return array<string,mixed> */
  23. protected function loadArray(): array {
  24. if (!file_exists($this->filename)) {
  25. throw new Minz_FileNotExistException($this->filename, Minz_Exception::WARNING);
  26. } elseif (($handle = $this->getLock()) === false) {
  27. throw new Minz_PermissionDeniedException($this->filename);
  28. } else {
  29. $data = include($this->filename);
  30. $this->releaseLock($handle);
  31. if ($data === false) {
  32. throw new Minz_PermissionDeniedException($this->filename);
  33. } elseif (!is_array($data)) {
  34. $data = array();
  35. }
  36. return $data;
  37. }
  38. }
  39. /**
  40. * Sauve le tableau $array dans le fichier $filename
  41. * @param array<string,mixed> $array
  42. */
  43. protected function writeArray(array $array): bool {
  44. if (file_put_contents($this->filename, "<?php\n return " . var_export($array, true) . ';', LOCK_EX) === false) {
  45. throw new Minz_PermissionDeniedException($this->filename);
  46. }
  47. if (function_exists('opcache_invalidate')) {
  48. opcache_invalidate($this->filename); //Clear PHP cache for include
  49. }
  50. return true;
  51. }
  52. /** @return resource|false */
  53. private function getLock() {
  54. $handle = fopen($this->filename, 'r');
  55. if ($handle === false) {
  56. return false;
  57. }
  58. $count = 50;
  59. while (!flock($handle, LOCK_SH) && $count > 0) {
  60. $count--;
  61. usleep(1000);
  62. }
  63. if ($count > 0) {
  64. return $handle;
  65. } else {
  66. fclose($handle);
  67. return false;
  68. }
  69. }
  70. /** @param resource $handle */
  71. private function releaseLock($handle): void {
  72. flock($handle, LOCK_UN);
  73. fclose($handle);
  74. }
  75. }