ModelArray.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * @var string
  13. */
  14. protected $filename;
  15. /**
  16. * Ouvre le fichier indiqué, charge le tableau dans $array et le $filename
  17. * @param string $filename le nom du fichier à ouvrir contenant un tableau
  18. * Remarque : $array sera obligatoirement un tableau
  19. */
  20. public function __construct ($filename) {
  21. $this->filename = $filename;
  22. }
  23. /** @return array<string,mixed> */
  24. protected function loadArray(): array {
  25. if (!file_exists($this->filename)) {
  26. throw new Minz_FileNotExistException($this->filename, Minz_Exception::WARNING);
  27. } elseif (($handle = $this->getLock()) === false) {
  28. throw new Minz_PermissionDeniedException($this->filename);
  29. } else {
  30. $data = include($this->filename);
  31. $this->releaseLock($handle);
  32. if ($data === false) {
  33. throw new Minz_PermissionDeniedException($this->filename);
  34. } elseif (!is_array($data)) {
  35. $data = array();
  36. }
  37. return $data;
  38. }
  39. }
  40. /**
  41. * Sauve le tableau $array dans le fichier $filename
  42. * @param array<string,mixed> $array
  43. */
  44. protected function writeArray(array $array): bool {
  45. if (file_put_contents($this->filename, "<?php\n return " . var_export($array, true) . ';', LOCK_EX) === false) {
  46. throw new Minz_PermissionDeniedException($this->filename);
  47. }
  48. if (function_exists('opcache_invalidate')) {
  49. opcache_invalidate($this->filename); //Clear PHP cache for include
  50. }
  51. return true;
  52. }
  53. /** @return resource|false */
  54. private function getLock() {
  55. $handle = fopen($this->filename, 'r');
  56. if ($handle === false) {
  57. return false;
  58. }
  59. $count = 50;
  60. while (!flock($handle, LOCK_SH) && $count > 0) {
  61. $count--;
  62. usleep(1000);
  63. }
  64. if ($count > 0) {
  65. return $handle;
  66. } else {
  67. fclose($handle);
  68. return false;
  69. }
  70. }
  71. /** @param resource $handle */
  72. private function releaseLock($handle): void {
  73. flock($handle, LOCK_UN);
  74. fclose($handle);
  75. }
  76. }