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)) {
$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') {

View File

@ -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();
}
}

View File

@ -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)
{

View File

@ -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;

View File

@ -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));

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)) {

View File

@ -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);
}
}