Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Util/Logfile/AbstractReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -68,3 +93,5 @@ protected function match($line)

abstract protected function getRegex();
}

spl_autoload_register(array('UAParser\Util\Logfile\AbstractReader','autoloadCustomReaders'));
45 changes: 45 additions & 0 deletions src/Util/Logfile/Custom/NginxCustomLogFormatReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* ua-parser
*
* Copyright (c) 2011-2012 Dave Olsen, http://dmolsen.com
*
* Released under the MIT license
*/
namespace UAParser\Util\Logfile\Custom;

use UAParser\Util\Logfile\AbstractReader;

class NginxCustomLogFormatReader extends AbstractReader
{
protected function getRegex()
{
return '@^
\[(?:[^:]+):(?:\d+:\d+:\d+) \s+ (?:[^\]]+)\] # Date/time
\s+
(?:\S+)
\s+
(?:\S+) # IP
\s+
(?:\S+)
\s+
(?:\S+)
\s+
(?:\S+)
\s+
\"(?:\S+)\s(?:.*?) # Verb
\s+
(?:\S+)\" # Path
\s+
(?:\S+) # Response
\s+
(?:\S+) # Length
\s+
(?:\".*?\") # Referrer
\s+
\"(?P<userAgentString>.*?)\" # User Agent
\s+
\"(?:\S+)\"
$@x';
}
}