Skip to content

Commit

Permalink
Issue #2 Move fp handling into getter/setter
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Feb 16, 2017
1 parent e622a8f commit 19048ed
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@ class Target extends BaseTarget
*/
public $url;

/**
* @var resource an open and writeable resource. This can also be one of
* PHP's pre-defined resources like `STDIN` or `STDERR`, which are available
* in CLI context.
*/
public $fp;

/**
* @var string|null a string that should replace all newline characters
* in a log message. Default ist `null` for no replacement.
*/
public $replaceNewline;

protected $fp;

/**
* @inheritdoc
*/
Expand All @@ -42,23 +37,40 @@ public function __destruct()
* @inheritdoc
*/
public function init() {
if (empty($this->fp)) {
if (empty($this->url)) {
throw new InvalidConfigException("Either 'url' or 'fp' mus be set.");
}
if (empty($this->fp) && empty($this->url)) {
throw new InvalidConfigException("Either 'url' or 'fp' mus be set.");
}
}

/**
* @param resource $value an open and writeable resource. This can also be
* one of PHP's pre-defined resources like `STDIN` or `STDERR`, which are
* available in CLI context.
*/
public function setFp($value)
{
if (!is_resource($value)) {
throw new InvalidConfigException("Invalid resource.");
}
$metadata = stream_get_meta_data($value);
if ($metadata['mode']!=='w') {
throw new InvalidConfigException("Resource is not writeable.");
}
$this->fp = $value;
}

/**
* @return resource the stream resource to write messages to
*/
public function getFp()
{
if ($this->fp===null) {
$this->fp = @fopen($this->url,'w');
if ($this->fp===false) {
throw new InvalidConfigException("Unable to open '{$this->url}' for writing.");
}
} else {
if (!is_resource($this->fp)) {
throw new InvalidConfigException("Invalid resource.");
}
$metadata = stream_get_meta_data($this->fp);
if ($metadata['mode']!=='w') {
throw new InvalidConfigException("Resource is not writeable.");
}
}
return $this->fp;
}

/**
Expand All @@ -68,7 +80,7 @@ public function init() {
public function export()
{
$text = implode("\n", array_map([$this, 'formatMessage'], $this->messages)) . "\n";
fwrite($this->fp, $text);
fwrite($this->getFp(), $text);
}

/**
Expand Down

0 comments on commit 19048ed

Please sign in to comment.