Add DI to ParsedLogIterator, replace constructors with fluent api
This commit is contained in:
parent
a62124285d
commit
ec4f53d56f
|
@ -394,6 +394,14 @@ abstract class DI
|
||||||
return self::$dice->create(Model\Storage\IWritableStorage::class);
|
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
|
// "Network" namespace
|
||||||
//
|
//
|
||||||
|
|
|
@ -36,31 +36,64 @@ class ParsedLogIterator implements \Iterator
|
||||||
private $reader;
|
private $reader;
|
||||||
|
|
||||||
/** @var ParsedLog current iterator value*/
|
/** @var ParsedLog current iterator value*/
|
||||||
private $value;
|
private $value = null;
|
||||||
|
|
||||||
/** @var int max number of lines to read */
|
/** @var int max number of lines to read */
|
||||||
private $limit;
|
private $limit = 0;
|
||||||
|
|
||||||
/** @var array filters per column */
|
/** @var array filters per column */
|
||||||
private $filters;
|
private $filters = [];
|
||||||
|
|
||||||
/** @var string search term */
|
/** @var string search term */
|
||||||
private $search;
|
private $search = "";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $filename File to open
|
* @param ReversedFileReader $reader
|
||||||
* @param int $limit Max num of lines to read
|
|
||||||
* @param array $filter filters per column
|
|
||||||
* @param string $search string to search to filter lines
|
|
||||||
*/
|
*/
|
||||||
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->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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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);
|
$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 {
|
} else {
|
||||||
try {
|
try {
|
||||||
$data = new ParsedLogIterator($f, self::LIMIT, $filters, $search);
|
$data = DI::parsedLogIterator()
|
||||||
|
->open($f)
|
||||||
|
->withLimit(self::LIMIT)
|
||||||
|
->withFilters($filters)
|
||||||
|
->withSearch($search);
|
||||||
} catch (Exception $e) {
|
} 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);
|
$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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,25 +31,34 @@ class ReversedFileReader implements \Iterator
|
||||||
const BUFFER_SIZE = 4096;
|
const BUFFER_SIZE = 4096;
|
||||||
const SEPARATOR = "\n";
|
const SEPARATOR = "\n";
|
||||||
|
|
||||||
/** @var int */
|
/** @var resource */
|
||||||
private $filesize;
|
private $fh = null;
|
||||||
|
|
||||||
/** @var int */
|
/** @var int */
|
||||||
private $pos;
|
private $filesize = -1;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private $pos = -1;
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $buffer;
|
private $buffer = null;
|
||||||
|
|
||||||
/** @var int */
|
/** @var int */
|
||||||
private $key;
|
private $key = -1;
|
||||||
|
|
||||||
/** @var string */
|
/** @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');
|
$this->fh = fopen($filename, 'r');
|
||||||
if (!$this->_fh) {
|
if (!$this->fh) {
|
||||||
// this should use a custom exception.
|
// this should use a custom exception.
|
||||||
throw \Exception("Unable to open $filename");
|
throw \Exception("Unable to open $filename");
|
||||||
}
|
}
|
||||||
|
@ -58,16 +67,17 @@ class ReversedFileReader implements \Iterator
|
||||||
$this->buffer = null;
|
$this->buffer = null;
|
||||||
$this->key = -1;
|
$this->key = -1;
|
||||||
$this->value = null;
|
$this->value = null;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function _read($size)
|
private function _read($size)
|
||||||
{
|
{
|
||||||
$this->pos -= $size;
|
$this->pos -= $size;
|
||||||
fseek($this->_fh, $this->pos);
|
fseek($this->fh, $this->pos);
|
||||||
return fread($this->_fh, $size);
|
return fread($this->fh, $size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function _readline()
|
private function _readline()
|
||||||
{
|
{
|
||||||
$buffer = & $this->buffer;
|
$buffer = & $this->buffer;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
|
@ -46,6 +46,7 @@ use Friendica\Database\Database;
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
use Friendica\Model\Storage\IWritableStorage;
|
use Friendica\Model\Storage\IWritableStorage;
|
||||||
use Friendica\Model\User\Cookie;
|
use Friendica\Model\User\Cookie;
|
||||||
|
use Friendica\Model\Log\ParsedLogIterator;
|
||||||
use Friendica\Network;
|
use Friendica\Network;
|
||||||
use Friendica\Util;
|
use Friendica\Util;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
@ -227,4 +228,9 @@ return [
|
||||||
$_SERVER
|
$_SERVER
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
ParsedLogIterator::class => [
|
||||||
|
'constructParams' => [
|
||||||
|
[Dice::INSTANCE => Util\ReversedFileReader::class],
|
||||||
|
]
|
||||||
|
]
|
||||||
];
|
];
|
||||||
|
|
Loading…
Reference in a new issue