1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file was originally part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
9
|
|
|
* |
10
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
11
|
|
|
* - (c) John MacFarlane |
12
|
|
|
* |
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
14
|
|
|
* file that was distributed with this source code. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace UnicornFail\Emoji\Environment; |
18
|
|
|
|
19
|
|
|
use UnicornFail\Emoji\Dataset\Dataset; |
20
|
|
|
use UnicornFail\Emoji\Dataset\DatasetInterface; |
21
|
|
|
use UnicornFail\Emoji\Emojibase\ShortcodeInterface; |
22
|
|
|
use UnicornFail\Emoji\Exception\LocalePresetException; |
23
|
|
|
use UnicornFail\Emoji\Extension\CoreExtension; |
24
|
|
|
use UnicornFail\Emoji\Parser\Parser; |
25
|
|
|
use UnicornFail\Emoji\Parser\ParserInterface; |
26
|
|
|
|
27
|
|
|
final class Environment extends AbstractConfigurableEnvironment implements EmojiEnvironmentInterface |
28
|
|
|
{ |
29
|
|
|
/** @var ?DatasetInterface */ |
30
|
|
|
private $dataset; |
31
|
|
|
|
32
|
|
|
/** @var ?ParserInterface */ |
33
|
|
|
private $parser; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param mixed[]|\Traversable $configuration |
37
|
|
|
* |
38
|
|
|
* @return static |
39
|
|
|
*/ |
40
|
|
|
public static function create(?iterable $configuration = null) |
41
|
|
|
{ |
42
|
|
|
$environment = new static($configuration); |
43
|
|
|
|
44
|
|
|
foreach (static::defaultExtensions() as $extension) { |
45
|
|
|
$environment->addExtension($extension); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $environment; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
|
protected static function defaultExtensions(): iterable |
55
|
|
|
{ |
56
|
|
|
return [new CoreExtension()]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string[] $presets |
61
|
|
|
*/ |
62
|
|
|
protected static function loadLocalePreset(string $locale = 'en', array $presets = ShortcodeInterface::DEFAULT_PRESETS): DatasetInterface |
63
|
|
|
{ |
64
|
|
|
$throwables = []; |
65
|
|
|
$presets = \array_filter($presets); |
66
|
|
|
$remaining = $presets; |
67
|
|
|
while (\count($remaining) > 0) { |
68
|
|
|
$preset = \array_shift($remaining); |
69
|
|
|
try { |
70
|
|
|
return Dataset::unarchive(\sprintf('%s/%s/%s.gz', DatasetInterface::DIRECTORY, $locale, $preset)); |
71
|
|
|
} catch (\Throwable $throwable) { |
72
|
|
|
$throwables[$preset] = $throwable; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
throw new LocalePresetException($locale, $throwables); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getDataset(): DatasetInterface |
80
|
|
|
{ |
81
|
|
|
if ($this->dataset === null) { |
82
|
|
|
$locale = $this->getConfiguration()->get('locale'); |
83
|
|
|
\assert(\is_string($locale)); |
84
|
|
|
|
85
|
|
|
/** @var string[] $preset */ |
86
|
|
|
$preset = $this->getConfiguration()->get('preset'); |
87
|
|
|
|
88
|
|
|
$this->dataset = self::loadLocalePreset($locale, $preset); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->dataset; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getParser(): ParserInterface |
95
|
|
|
{ |
96
|
|
|
if ($this->parser === null) { |
97
|
|
|
$this->parser = new Parser($this); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->parser; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setDataset(DatasetInterface $dataset): void |
104
|
|
|
{ |
105
|
|
|
$this->dataset = $dataset; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function setParser(ParserInterface $parser): void |
109
|
|
|
{ |
110
|
|
|
$this->parser = $parser; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|