Skip to content

Commit 335c525

Browse files
committed
autoload Reader classes from a custom folder
1 parent 3004c49 commit 335c525

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

src/Util/Logfile/AbstractReader.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99
namespace UAParser\Util\Logfile;
1010

11+
use Symfony\Component\Finder\Finder;
12+
use Symfony\Component\Finder\SplFileInfo;
1113
use UAParser\Exception\ReaderException;
1214

1315
abstract class AbstractReader implements ReaderInterface
@@ -34,11 +36,34 @@ private static function getReaders()
3436
return static::$readers;
3537
}
3638

39+
static::$readers = static::getCustomReaders();
3740
static::$readers[] = new ApacheCommonLogFormatReader();
3841

3942
return static::$readers;
4043
}
4144

45+
public static function autoloadCustomReaders($clazz)
46+
{
47+
$parts = explode('\\', $clazz);
48+
$path = __DIR__.DIRECTORY_SEPARATOR.'Custom'.DIRECTORY_SEPARATOR.end($parts).'.php';
49+
if (is_file($path)) {
50+
require $path;
51+
}
52+
}
53+
54+
private static function getCustomReaders()
55+
{
56+
$finder = Finder::create()->in(__DIR__.DIRECTORY_SEPARATOR.'Custom');
57+
array_map(array($finder, 'name'), array('*.php'));
58+
59+
$readers = array();
60+
foreach ($finder as $file) {
61+
$clazz = __NAMESPACE__.'\\'.$file->getBasename('.php');
62+
$readers[] = new $clazz;
63+
}
64+
return $readers;
65+
}
66+
4267
public function test($line)
4368
{
4469
$matches = $this->match($line);
@@ -68,3 +93,5 @@ protected function match($line)
6893

6994
abstract protected function getRegex();
7095
}
96+
97+
spl_autoload_register(array('UAParser\Util\Logfile\AbstractReader','autoloadCustomReaders'));
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* ua-parser
4+
*
5+
* Copyright (c) 2011-2012 Dave Olsen, http://dmolsen.com
6+
*
7+
* Released under the MIT license
8+
*/
9+
namespace UAParser\Util\Logfile;
10+
11+
class NginxCustomLogFormatReader extends AbstractReader
12+
{
13+
protected function getRegex()
14+
{
15+
return '@^
16+
\[(?:[^:]+):(?:\d+:\d+:\d+) \s+ (?:[^\]]+)\] # Date/time
17+
\s+
18+
(?:\S+)
19+
\s+
20+
(?:\S+) # IP
21+
\s+
22+
(?:\S+)
23+
\s+
24+
(?:\S+)
25+
\s+
26+
(?:\S+)
27+
\s+
28+
\"(?:\S+)\s(?:.*?) # Verb
29+
\s+
30+
(?:\S+)\" # Path
31+
\s+
32+
(?:\S+) # Response
33+
\s+
34+
(?:\S+) # Length
35+
\s+
36+
(?:\".*?\") # Referrer
37+
\s+
38+
\"(?P<userAgentString>.*?)\" # User Agent
39+
\s+
40+
\"(?:\S+)\"
41+
$@x';
42+
}
43+
}

0 commit comments

Comments
 (0)