-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathAbstractReader.php
More file actions
97 lines (79 loc) · 2.28 KB
/
Copy pathAbstractReader.php
File metadata and controls
97 lines (79 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* ua-parser
*
* Copyright (c) 2011-2012 Dave Olsen, http://dmolsen.com
*
* Released under the MIT license
*/
namespace UAParser\Util\Logfile;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use UAParser\Exception\ReaderException;
abstract class AbstractReader implements ReaderInterface
{
/** @var ReaderInterface[] */
private static $readers = array();
/**
* @param string $line
* @return ReaderInterface
*/
public static function factory($line)
{
foreach (static::getReaders() as $reader) {
if ($reader->test($line)) {
return $reader;
}
}
}
private static function getReaders()
{
if (static::$readers) {
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);
return isset($matches['userAgentString']);
}
public function read($line)
{
$matches = $this->match($line);
if (!isset($matches['userAgentString'])) {
throw ReaderException::userAgentParserError($line);
}
return $matches['userAgentString'];
}
protected function match($line)
{
if (preg_match($this->getRegex(), $line, $matches)) {
return $matches;
}
return array();
}
abstract protected function getRegex();
}
spl_autoload_register(array('UAParser\Util\Logfile\AbstractReader','autoloadCustomReaders'));