From 335c525b682a273e75305bae19ee3361dda55f8a Mon Sep 17 00:00:00 2001 From: Edward HeXuefei Date: Wed, 10 Apr 2019 14:19:08 +0800 Subject: [PATCH 1/2] autoload Reader classes from a custom folder --- src/Util/Logfile/AbstractReader.php | 27 ++++++++++++ .../Custom/NginxCustomLogFormatReader.php | 43 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/Util/Logfile/Custom/NginxCustomLogFormatReader.php diff --git a/src/Util/Logfile/AbstractReader.php b/src/Util/Logfile/AbstractReader.php index 232a4a8..772ccc1 100644 --- a/src/Util/Logfile/AbstractReader.php +++ b/src/Util/Logfile/AbstractReader.php @@ -8,6 +8,8 @@ */ namespace UAParser\Util\Logfile; +use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; use UAParser\Exception\ReaderException; abstract class AbstractReader implements ReaderInterface @@ -34,11 +36,34 @@ private static function getReaders() return static::$readers; } + static::$readers = static::getCustomReaders(); static::$readers[] = new ApacheCommonLogFormatReader(); return static::$readers; } + public static function autoloadCustomReaders($clazz) + { + $parts = explode('\\', $clazz); + $path = __DIR__.DIRECTORY_SEPARATOR.'Custom'.DIRECTORY_SEPARATOR.end($parts).'.php'; + if (is_file($path)) { + require $path; + } + } + + private static function getCustomReaders() + { + $finder = Finder::create()->in(__DIR__.DIRECTORY_SEPARATOR.'Custom'); + array_map(array($finder, 'name'), array('*.php')); + + $readers = array(); + foreach ($finder as $file) { + $clazz = __NAMESPACE__.'\\'.$file->getBasename('.php'); + $readers[] = new $clazz; + } + return $readers; + } + public function test($line) { $matches = $this->match($line); @@ -68,3 +93,5 @@ protected function match($line) abstract protected function getRegex(); } + +spl_autoload_register(array('UAParser\Util\Logfile\AbstractReader','autoloadCustomReaders')); diff --git a/src/Util/Logfile/Custom/NginxCustomLogFormatReader.php b/src/Util/Logfile/Custom/NginxCustomLogFormatReader.php new file mode 100644 index 0000000..156438e --- /dev/null +++ b/src/Util/Logfile/Custom/NginxCustomLogFormatReader.php @@ -0,0 +1,43 @@ +.*?)\" # User Agent + \s+ + \"(?:\S+)\" + $@x'; + } +} From 85aac6589ada1da6a4daa99f82d375a349614bb2 Mon Sep 17 00:00:00 2001 From: Edward HeXuefei Date: Wed, 10 Apr 2019 15:26:03 +0800 Subject: [PATCH 2/2] Sync namesapce and path simplify finder lookup --- src/Util/Logfile/AbstractReader.php | 4 ++-- src/Util/Logfile/Custom/NginxCustomLogFormatReader.php | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Util/Logfile/AbstractReader.php b/src/Util/Logfile/AbstractReader.php index 772ccc1..c824f64 100644 --- a/src/Util/Logfile/AbstractReader.php +++ b/src/Util/Logfile/AbstractReader.php @@ -54,11 +54,11 @@ public static function autoloadCustomReaders($clazz) private static function getCustomReaders() { $finder = Finder::create()->in(__DIR__.DIRECTORY_SEPARATOR.'Custom'); - array_map(array($finder, 'name'), array('*.php')); + $finder->name('*.php'); $readers = array(); foreach ($finder as $file) { - $clazz = __NAMESPACE__.'\\'.$file->getBasename('.php'); + $clazz = __NAMESPACE__.'\\Custom\\'.$file->getBasename('.php'); $readers[] = new $clazz; } return $readers; diff --git a/src/Util/Logfile/Custom/NginxCustomLogFormatReader.php b/src/Util/Logfile/Custom/NginxCustomLogFormatReader.php index 156438e..dba0ea2 100644 --- a/src/Util/Logfile/Custom/NginxCustomLogFormatReader.php +++ b/src/Util/Logfile/Custom/NginxCustomLogFormatReader.php @@ -6,7 +6,9 @@ * * Released under the MIT license */ -namespace UAParser\Util\Logfile; +namespace UAParser\Util\Logfile\Custom; + +use UAParser\Util\Logfile\AbstractReader; class NginxCustomLogFormatReader extends AbstractReader {