Files
FreshRSS/lib/Minz/ModelArray.php
Frans de Jonge 4e174ed9dd [ci] Add Travis (#1619)
* [ci] Add Travis

* Exclude some libs

* Semi-auto whitespace fixes

* line length in SQLite

* Exclude tests from line length

* Feed.php line length

* Feed.php: get rid of unnecessary concat

* Feed.php: line length

* bootstrap.php: no newline at end of file

* Allow concatenating across multiple lines

* Add Travis badge

* do-install line length

* update-or-create-user line length

* cli/create-user line length

* tests/app/Models/SearchTest.php fix indentation

* tests/app/Models/UserQueryTest.php fix indentation

* tests/app/Models/CategoryTest.php fix indentation

* [fix] PHP 5.3 on precise

* cli/do-install no spaces

* cli/list-users line length

* cli/reconfigure line length

* empty catch statements

* api/index line length nonsense

* spaces before semicolon

* app/Models/EntryDAO bunch of indentation

* extra blank lines

* spaces before comma in function call

* testing tabwidth

* increase to 10

* comment out tabwidth line

* try older phpcs version 3.0.0RC4

* line length exception for app/install.php

* proper spaces

* stray spaces in i18n

* Minz/ModelPdo line length

* Minz whitespace

* greader line length

* greader elseif placement

* app/Models/Feed.php spacing in function argument

* ignore php 5.3

* app/Models/ConfigurationSetter.php stray whitespace

* EntryDAOSQLite line length

* I vote for higher max line length =P

* ignore SQL

* remove classname complaint

* line length/more legible SQL

* ignore line length nonsense

* greader line length

* feedController issues

* uppercase TRUE, FALSE, NULL

* revert

* importExportController lowercase null

* Share.php default value not necessary because ! is_array () a few lines down

* CategoryDAO constants should be UPPERCASE

* EntryDAO reduce line length

* contentious autofix

* Allow failures on all versions of PHP except 7.1 because reasons
2017-09-22 12:13:46 +02:00

81 lines
1.9 KiB
PHP

<?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 {
/**
* $filename est le nom du fichier
*/
protected $filename;
/**
* 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 ($filename) {
$this->filename = $filename;
}
protected function loadArray() {
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 {
$data = include($this->filename);
$this->releaseLock($handle);
if ($data === false) {
throw new Minz_PermissionDeniedException($this->filename);
} elseif (!is_array($data)) {
$data = array();
}
return $data;
}
}
/**
* Sauve le tableau $array dans le fichier $filename
**/
protected function writeArray($array) {
if (file_put_contents($this->filename, "<?php\n return " . var_export($array, true) . ';', LOCK_EX) === false) {
throw new Minz_PermissionDeniedException($this->filename);
}
if (function_exists('opcache_invalidate')) {
opcache_invalidate($this->filename); //Clear PHP 5.5+ cache for include
}
return true;
}
private function getLock() {
$handle = fopen($this->filename, 'r');
if ($handle === false) {
return false;
}
$count = 50;
while (!flock($handle, LOCK_SH) && $count > 0) {
$count--;
usleep(1000);
}
if ($count > 0) {
return $handle;
} else {
fclose($handle);
return false;
}
}
private function releaseLock($handle) {
flock($handle, LOCK_UN);
fclose($handle);
}
}