Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoload Reader classes from a custom folder #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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();
Xuphey marked this conversation as resolved.
Show resolved Hide resolved
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'));
Xuphey marked this conversation as resolved.
Show resolved Hide resolved

$readers = array();
foreach ($finder as $file) {
$clazz = __NAMESPACE__.'\\'.$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'));
43 changes: 43 additions & 0 deletions src/Util/Logfile/Custom/NginxCustomLogFormatReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* ua-parser
*
* Copyright (c) 2011-2012 Dave Olsen, http://dmolsen.com
*
* Released under the MIT license
*/
namespace UAParser\Util\Logfile;
Xuphey marked this conversation as resolved.
Show resolved Hide resolved

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';
}
}