From 7f99be73fda804e483f69ee2b460795ad50c229a Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 26 Jun 2014 15:56:57 +0200 Subject: [PATCH] Adjust usages of Icinga\Util\File to suit the new interface introduced earlier --- library/Icinga/Application/Modules/Module.php | 7 +-- library/Icinga/Logger/Writer/FileWriter.php | 4 +- .../Commandpipe/Transport/LocalPipe.php | 8 ++- library/Icinga/Protocol/Statusdat/Parser.php | 55 ++++++++----------- library/Icinga/Protocol/Statusdat/Reader.php | 7 ++- .../User/Preferences/Store/IniStore.php | 2 +- .../Util/GettextTranslationHelper.php | 25 ++++----- 7 files changed, 49 insertions(+), 59 deletions(-) diff --git a/library/Icinga/Application/Modules/Module.php b/library/Icinga/Application/Modules/Module.php index d7670a8e2..f70ab0700 100644 --- a/library/Icinga/Application/Modules/Module.php +++ b/library/Icinga/Application/Modules/Module.php @@ -329,12 +329,9 @@ class Module if (file_exists($this->metadataFile)) { - $file = File::open($this->metadataFile, 'r'); - $lines = $file->readlines(); - $file->close(); $key = null; - - foreach ($lines as $line) { + $file = new File($this->metadataFile, 'r'); + foreach ($file as $line) { $line = rtrim($line); if ($key === 'description') { diff --git a/library/Icinga/Logger/Writer/FileWriter.php b/library/Icinga/Logger/Writer/FileWriter.php index cc4772269..a9d8b537f 100644 --- a/library/Icinga/Logger/Writer/FileWriter.php +++ b/library/Icinga/Logger/Writer/FileWriter.php @@ -93,6 +93,8 @@ class FileWriter extends LogWriter */ 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(); } } diff --git a/library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php b/library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php index f8120a733..65a3dab45 100644 --- a/library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php +++ b/library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php @@ -47,7 +47,7 @@ class LocalPipe implements Transport private $path; /** - * The mode to use for fopen() + * The mode to use to access the pipe * * @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); 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) { throw new ConfigurationError( sprintf( @@ -86,7 +88,7 @@ class LocalPipe implements Transport /** * 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) { diff --git a/library/Icinga/Protocol/Statusdat/Parser.php b/library/Icinga/Protocol/Statusdat/Parser.php index 3e920def9..f53e8dd41 100644 --- a/library/Icinga/Protocol/Statusdat/Parser.php +++ b/library/Icinga/Protocol/Statusdat/Parser.php @@ -29,7 +29,7 @@ namespace Icinga\Protocol\Statusdat; -use Icinga\Exception\ConfigurationError; +use Icinga\Util\File; use Icinga\Exception\ProgrammingError; use Icinga\Protocol\Statusdat\Exception\ParsingException as ParsingException; @@ -46,11 +46,11 @@ class Parser 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 @@ -83,18 +83,12 @@ class Parser /** * Create a new parser using the given file * - * @param resource $filehandle The file handle to usefor parsing - * @param array $baseState The state using for the base - * - * @throws ConfigurationError When the file can't be used + * @param File $file The file to parse + * @param array $baseState The state to use for the base */ - public function __construct($filehandle = null, $baseState = null) + public function __construct(File $file, $baseState = null) { - if (!is_resource($filehandle)) { - throw new ConfigurationError("Statusdat parser can't find $filehandle"); - } - - $this->filehandle = $filehandle; + $this->file = $file; $this->icingaState = $baseState; } @@ -104,12 +98,9 @@ class Parser public function parseObjectsFile() { $DEFINE = strlen("define "); - $filehandle = $this->filehandle; $this->icingaState = array(); - while (!feof($filehandle)) { - - $line = trim(fgets($filehandle)); - + foreach ($this->file as $line) { + $line = trim($line); $this->lineCtr++; if ($line === "" || $line[0] === "#") { 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) { - $this->filehandle = $filehandle; + if ($file != null) { + $this->file = $file; } else { - $filehandle = $this->filehandle; + $file = $this->file; } if (!$this->icingaState) { throw new ProgrammingError("Tried to read runtime state without existing objects data"); } $this->overwrites = array(); - while (!feof($filehandle)) { - $line = trim(fgets($filehandle)); + foreach ($file as $line) { + $line = trim($line); $this->lineCtr++; if ($line === "" || $line[0] === "#") { continue; @@ -156,10 +149,9 @@ class Parser */ private function readCurrentObject() { - $filehandle = $this->filehandle; $monitoringObject = new PrintableObject(); - while (!feof($filehandle)) { - $line = explode("\t", trim(fgets($filehandle)), 2); + foreach ($this->file as $line) { + $line = explode("\t", trim($line), 2); $this->lineCtr++; if (!$line) { continue; @@ -185,7 +177,6 @@ class Parser */ private function readCurrentState() { - $filehandle = $this->filehandle; $statusdatObject = new RuntimeStateContainer(); $objectType = $this->getObjectTypeForState(); @@ -273,12 +264,12 @@ class Parser protected function skipObject($returnString = false) { if (!$returnString) { - while (trim(fgets($this->filehandle)) !== "}") { + while (trim($this->file->fgets()) !== "}") { } return null; } else { $str = ""; - while (($val = trim(fgets($this->filehandle))) !== "}") { + while (($val = trim($this->file->fgets())) !== "}") { $str .= $val . "\n"; } return $str; diff --git a/library/Icinga/Protocol/Statusdat/Reader.php b/library/Icinga/Protocol/Statusdat/Reader.php index f27b3309f..4e55a69c0 100644 --- a/library/Icinga/Protocol/Statusdat/Reader.php +++ b/library/Icinga/Protocol/Statusdat/Reader.php @@ -29,6 +29,7 @@ namespace Icinga\Protocol\Statusdat; +use Icinga\Util\File; use Icinga\Logger\Logger; use Icinga\Data\DatasourceInterface; use Icinga\Exception\ConfigurationError; @@ -274,7 +275,7 @@ class Reader implements IReader, DatasourceInterface ); } 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->lastState = $this->parser->getRuntimeState(); @@ -293,9 +294,9 @@ class Reader implements IReader, DatasourceInterface ); } 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(); if (!$this->noCache) { $this->statusCache->save(array("true" => true), "state" . md5($this->config->object_file)); diff --git a/library/Icinga/User/Preferences/Store/IniStore.php b/library/Icinga/User/Preferences/Store/IniStore.php index cf20f3b8c..7b7d58503 100644 --- a/library/Icinga/User/Preferences/Store/IniStore.php +++ b/library/Icinga/User/Preferences/Store/IniStore.php @@ -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)) { diff --git a/modules/translation/library/Translation/Util/GettextTranslationHelper.php b/modules/translation/library/Translation/Util/GettextTranslationHelper.php index 3c9ca5b15..aa1aaa5db 100644 --- a/modules/translation/library/Translation/Util/GettextTranslationHelper.php +++ b/modules/translation/library/Translation/Util/GettextTranslationHelper.php @@ -29,7 +29,8 @@ namespace Icinga\Module\Translation\Util; -use \Exception; +use Exception; +use Icinga\Util\File; use Icinga\Application\Modules\Manager; use Icinga\Application\ApplicationBootstrap; @@ -350,36 +351,32 @@ class GettextTranslationHelper */ private function createFileCatalog() { - $catalogHandle = fopen($this->catalogPath, 'w'); - if (!$catalogHandle) { - throw new Exception('Unable to create ' . $this->catalogPath); - } + $catalog = new File($this->catalogPath, 'w'); try { if ($this->moduleDir) { - $this->getSourceFileNames($this->moduleDir, $catalogHandle); + $this->getSourceFileNames($this->moduleDir, $catalog); } else { - $this->getSourceFileNames($this->appDir, $catalogHandle); - $this->getSourceFileNames(realpath($this->appDir . '/../library/Icinga'), $catalogHandle); + $this->getSourceFileNames($this->appDir, $catalog); + $this->getSourceFileNames(realpath($this->appDir . '/../library/Icinga'), $catalog); } } catch (Exception $error) { - fclose($catalogHandle); throw $error; } - fclose($catalogHandle); + $catalog->fflush(); } /** * Recursively scan the given directory for translatable source files * * @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 * * @throws Exception In case the given directory is not readable */ - private function getSourceFileNames($directory, &$fileHandle) + private function getSourceFileNames($directory, File $file) { $directoryHandle = opendir($directory); if (!$directoryHandle) { @@ -390,7 +387,7 @@ class GettextTranslationHelper while (($filename = readdir($directoryHandle)) !== false) { $filepath = $directory . DIRECTORY_SEPARATOR . $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)) { $subdirs[] = $filepath; } @@ -398,7 +395,7 @@ class GettextTranslationHelper closedir($directoryHandle); foreach ($subdirs as $subdir) { - $this->getSourceFileNames($subdir, $fileHandle); + $this->getSourceFileNames($subdir, $file); } }