1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Esi\SimpleCounter. |
7
|
|
|
* |
8
|
|
|
* (c) Eric Sizemore <https://github.com/ericsizemore> |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the MIT license. For the full copyright and |
11
|
|
|
* license information, please view the LICENSE file that was distributed with |
12
|
|
|
* this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Esi\SimpleCounter\Configuration; |
16
|
|
|
|
17
|
|
|
use Esi\SimpleCounter\Interface\ConfigurationInterface; |
18
|
|
|
use Esi\Utility\Filesystem; |
19
|
|
|
use Esi\Utility\Strings; |
20
|
|
|
use Symfony\Component\OptionsResolver\Options; |
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
22
|
|
|
|
23
|
|
|
use function rtrim; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @phpstan-type FlatfileOptions = array{ |
27
|
|
|
* imageDir?: string, |
28
|
|
|
* imageExt?: string, |
29
|
|
|
* uniqueOnly?: bool, |
30
|
|
|
* asImage?: bool, |
31
|
|
|
* honorDnt?: bool, |
32
|
|
|
* visitorTextString?: string, |
33
|
|
|
* logDir?: string, |
34
|
|
|
* countFile?: string, |
35
|
|
|
* ipFile?: string |
36
|
|
|
* }|array{} |
37
|
|
|
* |
38
|
|
|
* @see \Esi\SimpleCounter\Tests\FlatfileStorageTest |
39
|
|
|
*/ |
40
|
|
|
final class FlatfileConfiguration implements ConfigurationInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var FlatfileOptions |
44
|
|
|
*/ |
45
|
|
|
private static array $options = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param FlatfileOptions $options |
49
|
|
|
*/ |
50
|
15 |
|
private function __construct(array $options = []) |
51
|
|
|
{ |
52
|
15 |
|
$optionsResolver = new OptionsResolver(); |
53
|
15 |
|
self::configureOptions($optionsResolver); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @psalm-var FlatfileOptions self::$options |
57
|
|
|
*/ |
58
|
15 |
|
self::$options = $optionsResolver->resolve($options); |
59
|
|
|
} |
60
|
|
|
|
61
|
15 |
|
public static function configureOptions(OptionsResolver $optionsResolver): void |
62
|
|
|
{ |
63
|
15 |
|
$optionsResolver->setDefaults([ |
64
|
15 |
|
'logDir' => \dirname(__DIR__, 2) . '/counter/logs/', |
65
|
15 |
|
'countFile' => 'counter.json', |
66
|
15 |
|
'ipFile' => 'ips.json', |
67
|
15 |
|
'imageDir' => \dirname(__DIR__, 2) . '/counter/images/', |
68
|
15 |
|
'imageExt' => '.png', |
69
|
15 |
|
'uniqueOnly' => true, |
70
|
15 |
|
'asImage' => false, |
71
|
15 |
|
'honorDnt' => false, |
72
|
15 |
|
'visitorTextString' => 'You are visitor #%s', |
73
|
15 |
|
]) |
74
|
15 |
|
->setAllowedTypes('logDir', 'string') |
75
|
15 |
|
->setAllowedTypes('countFile', 'string') |
76
|
15 |
|
->setAllowedTypes('ipFile', 'string') |
77
|
15 |
|
->setAllowedTypes('imageDir', 'string') |
78
|
15 |
|
->setAllowedTypes('imageExt', 'string') |
79
|
15 |
|
->setAllowedTypes('uniqueOnly', 'bool') |
80
|
15 |
|
->setAllowedTypes('asImage', 'bool') |
81
|
15 |
|
->setAllowedTypes('honorDnt', 'bool') |
82
|
15 |
|
->setAllowedTypes('visitorTextString', 'string') |
83
|
15 |
|
->setAllowedValues('logDir', static fn (string $value): bool => Filesystem::isDirectory($value)) |
84
|
15 |
|
->setAllowedValues('imageDir', static fn (string $value): bool => Filesystem::isDirectory($value)) |
85
|
15 |
|
->setAllowedValues('countFile', static fn (string $value): bool => Strings::endsWith($value, '.json')) |
86
|
15 |
|
->setAllowedValues('ipFile', static fn (string $value): bool => Strings::endsWith($value, '.json')) |
87
|
15 |
|
->setAllowedValues('visitorTextString', static fn (string $value): bool => Strings::doesContain($value, '%s')) |
88
|
15 |
|
->setNormalizer('logDir', static fn (Options $options, string $value): string => rtrim($value, '/\\')) |
89
|
15 |
|
->setNormalizer('imageDir', static fn (Options $options, string $value): string => rtrim($value, '/\\')) |
90
|
15 |
|
->setNormalizer('imageExt', static function (Options $options, string $value): string { |
91
|
11 |
|
if (!Strings::beginsWith($value, '.')) { |
92
|
1 |
|
$value = '.' . $value; |
93
|
|
|
} |
94
|
|
|
|
95
|
11 |
|
return $value; |
96
|
15 |
|
}); |
97
|
|
|
} |
98
|
|
|
|
99
|
10 |
|
public static function getOption(string $option): null|bool|string |
100
|
|
|
{ |
101
|
10 |
|
return self::$options[$option] ?? null; |
102
|
|
|
} |
103
|
|
|
|
104
|
15 |
|
public static function initOptions(array $options = []): FlatfileConfiguration |
105
|
|
|
{ |
106
|
15 |
|
return new self($options); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|