@@ 18-118 (lines=101) @@ | ||
15 | * @author Christoph Ziegenberg <[email protected]> |
|
16 | * @link https://github.com/crossjoin/browscap |
|
17 | */ |
|
18 | class AdapterFactory |
|
19 | { |
|
20 | /** |
|
21 | * @var array |
|
22 | */ |
|
23 | protected static $defaultAdapterClasses = [ |
|
24 | '\Crossjoin\Browscap\Parser\Sqlite\Adapter\Pdo', |
|
25 | '\Crossjoin\Browscap\Parser\Sqlite\Adapter\Sqlite3', |
|
26 | ]; |
|
27 | ||
28 | /** |
|
29 | * @var array|null |
|
30 | */ |
|
31 | protected static $adapterClasses; |
|
32 | ||
33 | /** |
|
34 | * @param string $fileName |
|
35 | * |
|
36 | * @return AdapterInterface |
|
37 | * @throws ParserConditionNotSatisfiedException |
|
38 | * @throws UnexpectedValueException |
|
39 | */ |
|
40 | public static function getInstance(string $fileName) : AdapterInterface |
|
41 | { |
|
42 | // Initialize adapter classes |
|
43 | if (static::$adapterClasses === null) { |
|
44 | self::setDefaultAdapterClasses(); |
|
45 | } |
|
46 | ||
47 | foreach (static::$adapterClasses as $className) { |
|
48 | $instance = static::getInstanceByClassName($className, $fileName); |
|
49 | if ($instance !== null) { |
|
50 | return $instance; |
|
51 | } |
|
52 | } |
|
53 | ||
54 | throw new ParserConditionNotSatisfiedException( |
|
55 | "No Sqlite extension found. Either 'pdo_sqlite' or 'sqlite3' extension required." |
|
56 | ); |
|
57 | } |
|
58 | ||
59 | public static function setDefaultAdapterClasses() |
|
60 | { |
|
61 | self::setAdapterClasses(self::$defaultAdapterClasses); |
|
62 | } |
|
63 | ||
64 | /** |
|
65 | * @param array $fullyQualifiedClassNames |
|
66 | * |
|
67 | * @throws InvalidArgumentException |
|
68 | */ |
|
69 | public static function setAdapterClasses(array $fullyQualifiedClassNames) |
|
70 | { |
|
71 | $rememberClasses = self::$adapterClasses; |
|
72 | ||
73 | self::$adapterClasses = []; |
|
74 | foreach ($fullyQualifiedClassNames as $className) { |
|
75 | if (is_string($className)) { |
|
76 | self::$adapterClasses[] = $className; |
|
77 | } else { |
|
78 | // Reset to previous value on error |
|
79 | self::$adapterClasses = $rememberClasses; |
|
80 | ||
81 | throw new InvalidArgumentException( |
|
82 | "A value in the class name array is of type '" . gettype($className) . "'. String expected." |
|
83 | ); |
|
84 | } |
|
85 | } |
|
86 | } |
|
87 | ||
88 | /** |
|
89 | * @param string $className |
|
90 | * @param string $fileName |
|
91 | * |
|
92 | * @return AdapterInterface|null |
|
93 | * @throws UnexpectedValueException |
|
94 | */ |
|
95 | protected static function getInstanceByClassName(string $className, string $fileName) |
|
96 | { |
|
97 | if (class_exists($className)) { |
|
98 | $interface = '\Crossjoin\Browscap\Parser\Sqlite\Adapter\AdapterFactoryInterface'; |
|
99 | $interfaces = class_implements($className); |
|
100 | if (array_key_exists(ltrim($interface, '\\'), $interfaces)) { |
|
101 | try { |
|
102 | return new $className($fileName); |
|
103 | } catch (BrowscapException $e) { |
|
104 | // Ignore exception, because we just return NULL on failure |
|
105 | } |
|
106 | } else { |
|
107 | throw new UnexpectedValueException( |
|
108 | "Class '$className' has to implement the interface '$interface'.", |
|
109 | 1459000689 |
|
110 | ); |
|
111 | } |
|
112 | } else { |
|
113 | throw new UnexpectedValueException("Class '$className' doesn't exist.", 1459000690); |
|
114 | } |
|
115 | ||
116 | return null; |
|
117 | } |
|
118 | } |
|
119 |
@@ 17-111 (lines=95) @@ | ||
14 | * @author Christoph Ziegenberg <[email protected]> |
|
15 | * @link https://github.com/crossjoin/browscap |
|
16 | */ |
|
17 | class SourceFactory |
|
18 | { |
|
19 | /** |
|
20 | * @var array |
|
21 | */ |
|
22 | protected static $defaultSourceClasses = [ |
|
23 | '\Crossjoin\Browscap\Source\Ini\BrowscapOrg', |
|
24 | '\Crossjoin\Browscap\Source\Ini\PhpSetting', |
|
25 | ]; |
|
26 | ||
27 | /** |
|
28 | * @var array|null |
|
29 | */ |
|
30 | protected static $sourceClasses; |
|
31 | ||
32 | /** |
|
33 | * @return SourceInterface |
|
34 | * @throws UnexpectedValueException |
|
35 | */ |
|
36 | public static function getInstance() : SourceInterface |
|
37 | { |
|
38 | // Initialize adapter classes |
|
39 | if (static::$sourceClasses === null) { |
|
40 | self::setDefaultSourceClasses(); |
|
41 | } |
|
42 | ||
43 | foreach (static::$sourceClasses as $className) { |
|
44 | $instance = static::getInstanceByClassName($className); |
|
45 | if ($instance !== null) { |
|
46 | return $instance; |
|
47 | } |
|
48 | } |
|
49 | ||
50 | return new None(); |
|
51 | } |
|
52 | ||
53 | public static function setDefaultSourceClasses() |
|
54 | { |
|
55 | self::setSourceClasses(self::$defaultSourceClasses); |
|
56 | } |
|
57 | ||
58 | /** |
|
59 | * @param array $fullyQualifiedClassNames |
|
60 | * |
|
61 | * @throws InvalidArgumentException |
|
62 | */ |
|
63 | public static function setSourceClasses(array $fullyQualifiedClassNames) |
|
64 | { |
|
65 | $rememberClasses = self::$sourceClasses; |
|
66 | ||
67 | self::$sourceClasses = []; |
|
68 | foreach ($fullyQualifiedClassNames as $className) { |
|
69 | if (is_string($className)) { |
|
70 | self::$sourceClasses[] = $className; |
|
71 | } else { |
|
72 | // Reset to previous value on error |
|
73 | self::$sourceClasses = $rememberClasses; |
|
74 | ||
75 | throw new InvalidArgumentException( |
|
76 | "A value in the class name array is of type '" . gettype($className) . "'. String expected." |
|
77 | ); |
|
78 | } |
|
79 | } |
|
80 | } |
|
81 | ||
82 | /** |
|
83 | * @param string $className |
|
84 | * |
|
85 | * @return SourceInterface|null |
|
86 | * @throws UnexpectedValueException |
|
87 | */ |
|
88 | protected static function getInstanceByClassName(string $className) |
|
89 | { |
|
90 | if (class_exists($className)) { |
|
91 | $interface = '\Crossjoin\Browscap\Source\SourceFactoryInterface'; |
|
92 | $interfaces = class_implements($className); |
|
93 | if (array_key_exists(ltrim($interface, '\\'), $interfaces)) { |
|
94 | try { |
|
95 | return new $className(); |
|
96 | } catch (BrowscapException $e) { |
|
97 | // Ignore exception, because we just return NULL on failure |
|
98 | } |
|
99 | } else { |
|
100 | throw new UnexpectedValueException( |
|
101 | "Class '$className' has to implement the interface '$interface'.", |
|
102 | 1459069587 |
|
103 | ); |
|
104 | } |
|
105 | } else { |
|
106 | throw new UnexpectedValueException("Class '$className' doesn't exist.", 1459069588); |
|
107 | } |
|
108 | ||
109 | return null; |
|
110 | } |
|
111 | } |
|
112 |