From ec4f53d56f9aa2f82b1cf327a7f0869251fb1dbb Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Thu, 19 Aug 2021 13:03:45 +0200 Subject: [PATCH] Add DI to ParsedLogIterator, replace constructors with fluent api --- src/DI.php | 8 ++++ src/Model/Log/ParsedLogIterator.php | 59 ++++++++++++++++++++++------- src/Module/Admin/Logs/View.php | 6 ++- src/Util/ReversedFileReader.php | 36 +++++++++++------- static/dependencies.config.php | 6 +++ 5 files changed, 88 insertions(+), 27 deletions(-) diff --git a/src/DI.php b/src/DI.php index 28ad130b4d..cbc353161c 100644 --- a/src/DI.php +++ b/src/DI.php @@ -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 // diff --git a/src/Model/Log/ParsedLogIterator.php b/src/Model/Log/ParsedLogIterator.php index 3e1832d969..bde07ee4a8 100644 --- a/src/Model/Log/ParsedLogIterator.php +++ b/src/Model/Log/ParsedLogIterator.php @@ -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; } /** diff --git a/src/Module/Admin/Logs/View.php b/src/Module/Admin/Logs/View.php index 26ad3a8b61..222052380a 100644 --- a/src/Module/Admin/Logs/View.php +++ b/src/Module/Admin/Logs/View.php @@ -71,7 +71,11 @@ class View extends BaseAdmin $error = DI::l10n()->t('Error trying to open %1$s log file.\r\n
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 %1$s log file.\r\n
Check to see if file %1$s is readable.', $f); } diff --git a/src/Util/ReversedFileReader.php b/src/Util/ReversedFileReader.php index c889b5f93d..64d41b25d6 100644 --- a/src/Util/ReversedFileReader.php +++ b/src/Util/ReversedFileReader.php @@ -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) { diff --git a/static/dependencies.config.php b/static/dependencies.config.php index 5efe78ddf3..f07a61807e 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -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], + ] + ] ];