icingaweb2/library/Icinga/Protocol/File/FileReader.php

190 lines
4.1 KiB
PHP
Raw Normal View History

2014-03-19 16:57:11 +01:00
<?php
2014-04-02 12:18:47 +02:00
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
2014-03-19 16:57:11 +01:00
namespace Icinga\Protocol\File;
use Icinga\Data\Selectable;
use Countable;
use Icinga\Util\Enumerate;
use Zend_Config;
2014-03-19 16:57:11 +01:00
2014-04-02 13:23:18 +02:00
/**
* Read file line by line
*/
class FileReader implements Selectable, Countable
2014-03-19 16:57:11 +01:00
{
2014-04-02 13:23:18 +02:00
/**
* A PCRE string with the fields to extract from the file's lines as named subpatterns
2014-04-02 13:23:18 +02:00
*
* @var string
*/
protected $fields;
2014-03-19 16:57:11 +01:00
2014-04-02 13:23:18 +02:00
/**
* Name of the target file
2014-04-02 13:23:18 +02:00
*
* @var string
2014-04-02 13:23:18 +02:00
*/
protected $filename;
2014-03-19 16:57:11 +01:00
2014-04-02 13:23:18 +02:00
/**
* Create a new reader
2014-04-01 16:29:09 +02:00
*
* @param Zend_Config $config
2014-04-01 16:29:09 +02:00
*
* @throws FileReaderException If a required $config directive (filename or fields) is missing
2014-04-01 16:29:09 +02:00
*/
public function __construct(Zend_Config $config)
2014-03-19 16:57:11 +01:00
{
foreach (array('filename', 'fields') as $key) {
if (isset($config->{$key})) {
$this->{$key} = $config->{$key};
} else {
throw new FileReaderException('The directive `%s\' is required', $key);
}
2014-03-19 16:57:11 +01:00
}
}
2014-04-01 16:29:09 +02:00
/**
* Instantiate a FileIterator object with the target file
*
* @return FileIterator
2014-04-01 16:29:09 +02:00
*/
public function iterate()
2014-03-19 16:57:11 +01:00
{
return new Enumerate(
new FileIterator($this->filename, $this->fields)
);
2014-03-19 16:57:11 +01:00
}
2014-04-01 16:29:09 +02:00
/**
* Instantiate a FileQuery object
2014-04-01 16:29:09 +02:00
*
* @return FileQuery
2014-04-01 16:29:09 +02:00
*/
public function select()
2014-03-19 16:57:11 +01:00
{
return new FileQuery($this);
2014-03-19 16:57:11 +01:00
}
2014-04-01 16:29:09 +02:00
/**
* Return the number of available valid lines.
2014-04-01 16:29:09 +02:00
*
* @return int
2014-04-01 16:29:09 +02:00
*/
public function count()
2014-03-19 16:57:11 +01:00
{
return iterator_count($this->iterate());
2014-03-19 16:57:11 +01:00
}
2014-04-01 16:29:09 +02:00
/**
* Fetch result as an array of objects
2014-04-01 16:29:09 +02:00
*
* @param FileQuery $query
2014-04-01 16:29:09 +02:00
*
* @return array
2014-04-01 16:29:09 +02:00
*/
public function fetchAll(FileQuery $query)
2014-03-19 16:57:11 +01:00
{
$all = array();
foreach ($this->fetchPairs($query) as $index => $value) {
$all[$index] = (object) $value;
2014-03-19 16:57:11 +01:00
}
return $all;
2014-03-19 16:57:11 +01:00
}
2014-04-01 16:29:09 +02:00
/**
* Fetch result as a key/value pair array
2014-04-01 16:29:09 +02:00
*
* @param FileQuery $query
2014-04-01 16:29:09 +02:00
*
* @return array
2014-04-01 16:29:09 +02:00
*/
public function fetchPairs(FileQuery $query)
2014-03-19 16:57:11 +01:00
{
$skip = $query->getOffset();
$read = $query->getLimit();
if ($skip === null) {
$skip = 0;
2014-03-19 16:57:11 +01:00
}
$lines = array();
if ($query->sortDesc()) {
$count = $this->count($query);
if ($count <= $skip) {
return $lines;
} else if ($count < ($skip + $read)) {
$read = $count - $skip;
$skip = 0;
} else {
$skip = $count - ($skip + $read);
2014-03-19 16:57:11 +01:00
}
}
foreach ($this->iterate() as $index => $line) {
if ($index >= $skip) {
if ($index >= $skip + $read) {
break;
}
$lines[] = $line;
2014-03-19 16:57:11 +01:00
}
}
if ($query->sortDesc()) {
$lines = array_reverse($lines);
}
2014-03-19 16:57:11 +01:00
return $lines;
}
2014-04-17 17:59:27 +02:00
/**
* Fetch first result row
*
* @param FileQuery $query
*
* @return object
2014-04-17 17:59:27 +02:00
*/
public function fetchRow(FileQuery $query)
2014-04-17 17:59:27 +02:00
{
$all = $this->fetchAll($query);
if (isset($all[0])) {
return $all[0];
2014-04-17 17:59:27 +02:00
}
return null;
2014-04-17 17:59:27 +02:00
}
2014-04-01 16:29:09 +02:00
/**
* Fetch first result column
*
* @param FileQuery $query
*
* @return array
2014-04-01 16:29:09 +02:00
*/
public function fetchColumn(FileQuery $query)
2014-03-19 16:57:11 +01:00
{
$column = array();
foreach ($this->fetchPairs($query) as $pair) {
foreach ($pair as $value) {
$column[] = $value;
break;
2014-03-19 16:57:11 +01:00
}
}
return $column;
2014-03-19 16:57:11 +01:00
}
2014-04-01 16:29:09 +02:00
/**
* Fetch first column value from first result row
2014-04-01 16:29:09 +02:00
*
* @param FileQuery $query
2014-04-01 16:29:09 +02:00
*
* @return mixed
2014-04-01 16:29:09 +02:00
*/
public function fetchOne(FileQuery $query)
{
$pairs = $this->fetchPairs($query);
if (isset($pairs[0])) {
foreach ($pairs[0] as $value) {
return $value;
2014-03-19 16:57:11 +01:00
}
}
return null;
2014-03-19 16:57:11 +01:00
}
2014-04-17 17:59:27 +02:00
}