Add DI to ParsedLogIterator, replace constructors with fluent api

This commit is contained in:
fabrixxm 2021-08-19 13:03:45 +02:00
parent a62124285d
commit ec4f53d56f
5 changed files with 88 additions and 27 deletions

View File

@ -394,6 +394,14 @@ abstract class DI
return self::$dice->create(Model\Storage\IWritableStorage::class);
}
/**
* @return Model\Log\ParsedLogIterator
*/
public static function parsedLogIterator()
{
return self::$dice->create(Model\Log\ParsedLogIterator::class);
}
//
// "Network" namespace
//

View File

@ -36,31 +36,64 @@ class ParsedLogIterator implements \Iterator
private $reader;
/** @var ParsedLog current iterator value*/
private $value;
private $value = null;
/** @var int max number of lines to read */
private $limit;
private $limit = 0;
/** @var array filters per column */
private $filters;
private $filters = [];
/** @var string search term */
private $search;
private $search = "";
/**
* @param string $filename File to open
* @param int $limit Max num of lines to read
* @param array $filter filters per column
* @param string $search string to search to filter lines
* @param ReversedFileReader $reader
*/
public function __construct(string $filename, int $limit = 0, array $filters = [], string $search = "")
public function __construct(ReversedFileReader $reader)
{
$this->reader = $reader;
}
/**
* @param string $filename File to open
* @return $this
*/
public function open(string $filename)
{
$this->reader->open($filename);
return $this;
}
/**
* @param int $limit Max num of lines to read
* @return $this
*/
public function withLimit(int $limit)
{
$this->limit = $limit;
return $this;
}
/**
* @param array $filters filters per column
* @return $this
*/
public function withFilters(array $filters)
{
$this->reader = new ReversedFileReader($filename);
$this->value = null;
$this->limit = $limit;
$this->filters = $filters;
$this->search = $search;
return $this;
}
/**
* @param string $search string to search to filter lines
* @return $this
*/
public function withSearch(string $search)
{
$this->search = $search;
return $this;
}
/**

View File

@ -71,7 +71,11 @@ class View extends BaseAdmin
$error = DI::l10n()->t('Error trying to open <strong>%1$s</strong> log file.\r\n<br/>Check to see if file %1$s exist and is readable.', $f);
} else {
try {
$data = new ParsedLogIterator($f, self::LIMIT, $filters, $search);
$data = DI::parsedLogIterator()
->open($f)
->withLimit(self::LIMIT)
->withFilters($filters)
->withSearch($search);
} catch (Exception $e) {
$error = DI::l10n()->t('Couldn\'t open <strong>%1$s</strong> log file.\r\n<br/>Check to see if file %1$s is readable.', $f);
}

View File

@ -31,25 +31,34 @@ class ReversedFileReader implements \Iterator
const BUFFER_SIZE = 4096;
const SEPARATOR = "\n";
/** @var int */
private $filesize;
/** @var resource */
private $fh = null;
/** @var int */
private $pos;
private $filesize = -1;
/** @var int */
private $pos = -1;
/** @var array */
private $buffer;
private $buffer = null;
/** @var int */
private $key;
private $key = -1;
/** @var string */
private $value;
private $value = null;
public function __construct($filename)
/**
* Open $filename for read and reset iterator
*
* @param string $filename File to open
* @return $this
*/
public function open(string $filename)
{
$this->_fh = fopen($filename, 'r');
if (!$this->_fh) {
$this->fh = fopen($filename, 'r');
if (!$this->fh) {
// this should use a custom exception.
throw \Exception("Unable to open $filename");
}
@ -58,16 +67,17 @@ class ReversedFileReader implements \Iterator
$this->buffer = null;
$this->key = -1;
$this->value = null;
return $this;
}
public function _read($size)
private function _read($size)
{
$this->pos -= $size;
fseek($this->_fh, $this->pos);
return fread($this->_fh, $size);
fseek($this->fh, $this->pos);
return fread($this->fh, $size);
}
public function _readline()
private function _readline()
{
$buffer = & $this->buffer;
while (true) {

View File

@ -46,6 +46,7 @@ use Friendica\Database\Database;
use Friendica\Factory;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Model\User\Cookie;
use Friendica\Model\Log\ParsedLogIterator;
use Friendica\Network;
use Friendica\Util;
use Psr\Log\LoggerInterface;
@ -227,4 +228,9 @@ return [
$_SERVER
],
],
ParsedLogIterator::class => [
'constructParams' => [
[Dice::INSTANCE => Util\ReversedFileReader::class],
]
]
];