Adjust usages of Icinga\Util\File to suit the new interface introduced earlier

This commit is contained in:
Johannes Meyer 2014-06-26 15:56:57 +02:00
parent 3b191d36c4
commit 7f99be73fd
7 changed files with 49 additions and 59 deletions

View File

@ -329,12 +329,9 @@ class Module
if (file_exists($this->metadataFile)) { if (file_exists($this->metadataFile)) {
$file = File::open($this->metadataFile, 'r');
$lines = $file->readlines();
$file->close();
$key = null; $key = null;
$file = new File($this->metadataFile, 'r');
foreach ($lines as $line) { foreach ($file as $line) {
$line = rtrim($line); $line = rtrim($line);
if ($key === 'description') { if ($key === 'description') {

View File

@ -93,6 +93,8 @@ class FileWriter extends LogWriter
*/ */
protected function write($text) protected function write($text)
{ {
File::open($this->path, 'a')->write($text . PHP_EOL)->close(); $file = new File($this->path, 'a');
$file->fwrite($text . PHP_EOL);
$file->fflush();
} }
} }

View File

@ -47,7 +47,7 @@ class LocalPipe implements Transport
private $path; private $path;
/** /**
* The mode to use for fopen() * The mode to use to access the pipe
* *
* @var string * @var string
*/ */
@ -69,7 +69,9 @@ class LocalPipe implements Transport
Logger::debug('Attempting to send external icinga command %s to local command file ', $message, $this->path); Logger::debug('Attempting to send external icinga command %s to local command file ', $message, $this->path);
try { try {
File::open($this->path, $this->openMode)->write('[' . time() . '] ' . $message . PHP_EOL)->close(); $file = new File($this->path, $this->openMode);
$file->fwrite('[' . time() . '] ' . $message . PHP_EOL);
$file->fflush();
} catch (Exception $e) { } catch (Exception $e) {
throw new ConfigurationError( throw new ConfigurationError(
sprintf( sprintf(
@ -86,7 +88,7 @@ class LocalPipe implements Transport
/** /**
* Overwrite the open mode (useful for testing) * Overwrite the open mode (useful for testing)
* *
* @param string $mode A open mode supported by fopen() * @param string $mode The mode to use to access the pipe
*/ */
public function setOpenMode($mode) public function setOpenMode($mode)
{ {

View File

@ -29,7 +29,7 @@
namespace Icinga\Protocol\Statusdat; namespace Icinga\Protocol\Statusdat;
use Icinga\Exception\ConfigurationError; use Icinga\Util\File;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Protocol\Statusdat\Exception\ParsingException as ParsingException; use Icinga\Protocol\Statusdat\Exception\ParsingException as ParsingException;
@ -46,11 +46,11 @@ class Parser
private $deferred = array(); private $deferred = array();
/** /**
* The resource pointing to the currently read file * The currently read file
* *
* @var resource * @var File
*/ */
private $filehandle; private $file;
/** /**
* String representation of the currently parsed object type * String representation of the currently parsed object type
@ -83,18 +83,12 @@ class Parser
/** /**
* Create a new parser using the given file * Create a new parser using the given file
* *
* @param resource $filehandle The file handle to usefor parsing * @param File $file The file to parse
* @param array $baseState The state using for the base * @param array $baseState The state to use for the base
*
* @throws ConfigurationError When the file can't be used
*/ */
public function __construct($filehandle = null, $baseState = null) public function __construct(File $file, $baseState = null)
{ {
if (!is_resource($filehandle)) { $this->file = $file;
throw new ConfigurationError("Statusdat parser can't find $filehandle");
}
$this->filehandle = $filehandle;
$this->icingaState = $baseState; $this->icingaState = $baseState;
} }
@ -104,12 +98,9 @@ class Parser
public function parseObjectsFile() public function parseObjectsFile()
{ {
$DEFINE = strlen("define "); $DEFINE = strlen("define ");
$filehandle = $this->filehandle;
$this->icingaState = array(); $this->icingaState = array();
while (!feof($filehandle)) { foreach ($this->file as $line) {
$line = trim($line);
$line = trim(fgets($filehandle));
$this->lineCtr++; $this->lineCtr++;
if ($line === "" || $line[0] === "#") { if ($line === "" || $line[0] === "#") {
continue; continue;
@ -124,22 +115,24 @@ class Parser
} }
/** /**
* Parse the given file handle as an status.dat file and read runtime information * Parse the given file as an status.dat file and read runtime information
*
* @param File $file The file to parse or null to parse the one passed to the constructor
*/ */
public function parseRuntimeState($filehandle = null) public function parseRuntimeState(File $file = null)
{ {
if ($filehandle != null) { if ($file != null) {
$this->filehandle = $filehandle; $this->file = $file;
} else { } else {
$filehandle = $this->filehandle; $file = $this->file;
} }
if (!$this->icingaState) { if (!$this->icingaState) {
throw new ProgrammingError("Tried to read runtime state without existing objects data"); throw new ProgrammingError("Tried to read runtime state without existing objects data");
} }
$this->overwrites = array(); $this->overwrites = array();
while (!feof($filehandle)) { foreach ($file as $line) {
$line = trim(fgets($filehandle)); $line = trim($line);
$this->lineCtr++; $this->lineCtr++;
if ($line === "" || $line[0] === "#") { if ($line === "" || $line[0] === "#") {
continue; continue;
@ -156,10 +149,9 @@ class Parser
*/ */
private function readCurrentObject() private function readCurrentObject()
{ {
$filehandle = $this->filehandle;
$monitoringObject = new PrintableObject(); $monitoringObject = new PrintableObject();
while (!feof($filehandle)) { foreach ($this->file as $line) {
$line = explode("\t", trim(fgets($filehandle)), 2); $line = explode("\t", trim($line), 2);
$this->lineCtr++; $this->lineCtr++;
if (!$line) { if (!$line) {
continue; continue;
@ -185,7 +177,6 @@ class Parser
*/ */
private function readCurrentState() private function readCurrentState()
{ {
$filehandle = $this->filehandle;
$statusdatObject = new RuntimeStateContainer(); $statusdatObject = new RuntimeStateContainer();
$objectType = $this->getObjectTypeForState(); $objectType = $this->getObjectTypeForState();
@ -273,12 +264,12 @@ class Parser
protected function skipObject($returnString = false) protected function skipObject($returnString = false)
{ {
if (!$returnString) { if (!$returnString) {
while (trim(fgets($this->filehandle)) !== "}") { while (trim($this->file->fgets()) !== "}") {
} }
return null; return null;
} else { } else {
$str = ""; $str = "";
while (($val = trim(fgets($this->filehandle))) !== "}") { while (($val = trim($this->file->fgets())) !== "}") {
$str .= $val . "\n"; $str .= $val . "\n";
} }
return $str; return $str;

View File

@ -29,6 +29,7 @@
namespace Icinga\Protocol\Statusdat; namespace Icinga\Protocol\Statusdat;
use Icinga\Util\File;
use Icinga\Logger\Logger; use Icinga\Logger\Logger;
use Icinga\Data\DatasourceInterface; use Icinga\Data\DatasourceInterface;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
@ -274,7 +275,7 @@ class Reader implements IReader, DatasourceInterface
); );
} }
if (!$this->parser) { if (!$this->parser) {
$this->parser = new Parser(fopen($this->config->object_file, "r")); $this->parser = new Parser(new File($this->config->object_file, 'r'));
} }
$this->parser->parseObjectsFile(); $this->parser->parseObjectsFile();
$this->lastState = $this->parser->getRuntimeState(); $this->lastState = $this->parser->getRuntimeState();
@ -293,9 +294,9 @@ class Reader implements IReader, DatasourceInterface
); );
} }
if (!$this->parser) { if (!$this->parser) {
$this->parser = new Parser(fopen($this->config->status_file, "r"), $this->lastState); $this->parser = new Parser(new File($this->config->status_file, 'r'), $this->lastState);
} }
$this->parser->parseRuntimeState(fopen($this->config->status_file, "r")); $this->parser->parseRuntimeState(new File($this->config->status_file, 'r'));
$this->lastState = $this->parser->getRuntimeState(); $this->lastState = $this->parser->getRuntimeState();
if (!$this->noCache) { if (!$this->noCache) {
$this->statusCache->save(array("true" => true), "state" . md5($this->config->object_file)); $this->statusCache->save(array("true" => true), "state" . md5($this->config->object_file));

View File

@ -109,7 +109,7 @@ class IniStore extends PreferencesStore
); );
} }
File::open($this->preferencesFile, 'a')->chmod(0664)->close(); File::create($this->preferencesFile, 0664);
} }
if (!is_writable($this->preferencesFile)) { if (!is_writable($this->preferencesFile)) {

View File

@ -29,7 +29,8 @@
namespace Icinga\Module\Translation\Util; namespace Icinga\Module\Translation\Util;
use \Exception; use Exception;
use Icinga\Util\File;
use Icinga\Application\Modules\Manager; use Icinga\Application\Modules\Manager;
use Icinga\Application\ApplicationBootstrap; use Icinga\Application\ApplicationBootstrap;
@ -350,36 +351,32 @@ class GettextTranslationHelper
*/ */
private function createFileCatalog() private function createFileCatalog()
{ {
$catalogHandle = fopen($this->catalogPath, 'w'); $catalog = new File($this->catalogPath, 'w');
if (!$catalogHandle) {
throw new Exception('Unable to create ' . $this->catalogPath);
}
try { try {
if ($this->moduleDir) { if ($this->moduleDir) {
$this->getSourceFileNames($this->moduleDir, $catalogHandle); $this->getSourceFileNames($this->moduleDir, $catalog);
} else { } else {
$this->getSourceFileNames($this->appDir, $catalogHandle); $this->getSourceFileNames($this->appDir, $catalog);
$this->getSourceFileNames(realpath($this->appDir . '/../library/Icinga'), $catalogHandle); $this->getSourceFileNames(realpath($this->appDir . '/../library/Icinga'), $catalog);
} }
} catch (Exception $error) { } catch (Exception $error) {
fclose($catalogHandle);
throw $error; throw $error;
} }
fclose($catalogHandle); $catalog->fflush();
} }
/** /**
* Recursively scan the given directory for translatable source files * Recursively scan the given directory for translatable source files
* *
* @param string $directory The directory where to search for sources * @param string $directory The directory where to search for sources
* @param resource $fileHandle The file where to write the results * @param File $file The file where to write the results
* @param array $blacklist A list of directories to omit * @param array $blacklist A list of directories to omit
* *
* @throws Exception In case the given directory is not readable * @throws Exception In case the given directory is not readable
*/ */
private function getSourceFileNames($directory, &$fileHandle) private function getSourceFileNames($directory, File $file)
{ {
$directoryHandle = opendir($directory); $directoryHandle = opendir($directory);
if (!$directoryHandle) { if (!$directoryHandle) {
@ -390,7 +387,7 @@ class GettextTranslationHelper
while (($filename = readdir($directoryHandle)) !== false) { while (($filename = readdir($directoryHandle)) !== false) {
$filepath = $directory . DIRECTORY_SEPARATOR . $filename; $filepath = $directory . DIRECTORY_SEPARATOR . $filename;
if (preg_match('@^[^\.].+\.(' . implode('|', $this->sourceExtensions) . ')$@', $filename)) { if (preg_match('@^[^\.].+\.(' . implode('|', $this->sourceExtensions) . ')$@', $filename)) {
fwrite($fileHandle, $filepath . PHP_EOL); $file->fwrite($filepath . PHP_EOL);
} elseif (is_dir($filepath) && !preg_match('@^(\.|\.\.)$@', $filename)) { } elseif (is_dir($filepath) && !preg_match('@^(\.|\.\.)$@', $filename)) {
$subdirs[] = $filepath; $subdirs[] = $filepath;
} }
@ -398,7 +395,7 @@ class GettextTranslationHelper
closedir($directoryHandle); closedir($directoryHandle);
foreach ($subdirs as $subdir) { foreach ($subdirs as $subdir) {
$this->getSourceFileNames($subdir, $fileHandle); $this->getSourceFileNames($subdir, $file);
} }
} }