diff --git a/src/Util/Logfile/AbstractReader.php b/src/Util/Logfile/AbstractReader.php index 232a4a8..c824f64 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'); + $finder->name('*.php'); + + $readers = array(); + foreach ($finder as $file) { + $clazz = __NAMESPACE__.'\\Custom\\'.$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..dba0ea2 --- /dev/null +++ b/src/Util/Logfile/Custom/NginxCustomLogFormatReader.php @@ -0,0 +1,45 @@ +.*?)\" # User Agent + \s+ + \"(?:\S+)\" + $@x'; + } +}