]> git.mxchange.org Git - friendica.git/commitdiff
Add DI to ParsedLogIterator, replace constructors with fluent api
authorfabrixxm <fabrix.xm@gmail.com>
Thu, 19 Aug 2021 11:03:45 +0000 (13:03 +0200)
committerfabrixxm <fabrix.xm@gmail.com>
Thu, 19 Aug 2021 12:56:51 +0000 (14:56 +0200)
src/DI.php
src/Model/Log/ParsedLogIterator.php
src/Module/Admin/Logs/View.php
src/Util/ReversedFileReader.php
static/dependencies.config.php

index 28ad130b4da93719beddf67b594732b38f967974..cbc353161cef34017a4f10a796982104d6ba5e2e 100644 (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
        //
index 3e1832d969998cc46e10c200fd0e6cbd7119da55..bde07ee4a83a31aef33e2610d701a3fe21c99b4a 100644 (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 ReversedFileReader $reader
+        */
+       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
-        * @param array $filter         filters per column
-        * @param string $search        string to search to filter lines
+        * @return $this
         */
-       public function __construct(string $filename, int $limit = 0, array $filters = [], string $search = "")
+       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;
        }
 
        /**
index 26ad3a8b61f28f19a473060f2ac82ff1b9f17a3d..222052380af089a415ccbd28cc068ada8f3a8422 100644 (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);
                        }
index c889b5f93d6bab033e533ba23f43cab69da6bb29..64d41b25d66115763006cd1d8d7828a6273c26fd 100644 (file)
@@ -31,25 +31,34 @@ class ReversedFileReader implements \Iterator
        const BUFFER_SIZE = 4096;
        const SEPARATOR   = "\n";
 
+       /** @var resource */
+       private $fh = null;
+
        /** @var int */
-       private $filesize;
+       private $filesize = -1;
 
        /** @var int */
-       private $pos;
+       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) {
index 5efe78ddf329bd4b89592ffa2708fb7aeb1e9005..f07a61807e8aed3921c6afbb577db3192cb8da11 100644 (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],
+               ]
+       ]
 ];