1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matecat\EmojiParser\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
9
|
|
|
|
10
|
|
|
class EmojiUpdateCommand extends Command |
11
|
|
|
{ |
12
|
|
|
protected function configure() { |
13
|
|
|
$this |
14
|
|
|
->setName( 'emoji:update' ) |
15
|
|
|
->setDescription( 'Update the emoji static map.' ) |
16
|
|
|
->setHelp( "Update the emoji static map with emoji-api.com API." ); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param InputInterface $input |
21
|
|
|
* @param OutputInterface $output |
22
|
|
|
* @return int |
23
|
|
|
*/ |
24
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
25
|
|
|
{ |
26
|
|
|
// SymfonyStyle |
27
|
|
|
$io = new SymfonyStyle( $input, $output ); |
28
|
|
|
$io->title('Update the emoji static map with emoji-api.com API'); |
29
|
|
|
|
30
|
|
|
$apiKey = parse_ini_file(__DIR__.'/../../config/credentials.ini'); |
31
|
|
|
$url = 'https://emoji-api.com/emojis?access_key='.$apiKey['emoji_api_key']; |
32
|
|
|
|
33
|
|
|
$emojis = json_decode(file_get_contents($url)); |
34
|
|
|
|
35
|
|
|
$i = 1; |
36
|
|
|
$updates = 0; |
37
|
|
|
$skipped = 0; |
38
|
|
|
|
39
|
|
|
foreach ($emojis as $emoji){ |
40
|
|
|
$this->importEmoji($emoji, $io, $i, $updates, $skipped); |
41
|
|
|
|
42
|
|
|
if(isset($emoji->variants) and is_array($emoji->variants)){ |
43
|
|
|
foreach ($emoji->variants as $variant){ |
44
|
|
|
$this->importEmoji($variant, $io, $i, $updates, $skipped); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$io->newLine(); |
50
|
|
|
$io->writeln("========================================"); |
51
|
|
|
$io->writeln("UPDATED: <fg=cyan>".$updates."</> SKIPPED: <fg=red>".$skipped."</>"); |
52
|
|
|
$io->writeln("========================================"); |
53
|
|
|
$io->newLine(); |
54
|
|
|
|
55
|
|
|
return Command::SUCCESS; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $emoji |
60
|
|
|
* @param $i |
61
|
|
|
* @param SymfonyStyle $io |
62
|
|
|
* @param $updates |
63
|
|
|
* @param $skipped |
64
|
|
|
*/ |
65
|
|
|
private function importEmoji($emoji, SymfonyStyle $io, &$i, &$updates, &$skipped) |
66
|
|
|
{ |
67
|
|
|
$htmlEntities = $this->convertEmojiToHtmlEntities($emoji->character); |
68
|
|
|
|
69
|
|
|
$chmapFile = __DIR__ . '/../chmap.php'; |
70
|
|
|
$chmap = include $chmapFile; |
71
|
|
|
$inverseChmap = array_flip($chmap); |
72
|
|
|
|
73
|
|
|
foreach ($htmlEntities as $character => $htmlEntity){ |
74
|
|
|
if(strlen($htmlEntity) >= 8){ |
75
|
|
|
if(!isset($inverseChmap[$htmlEntity])){ |
76
|
|
|
$outcome = 'UPDATED'; |
77
|
|
|
$outcomeColor = 'cyan'; |
78
|
|
|
$updates++; |
79
|
|
|
$chmap[$character] = $htmlEntity; |
80
|
|
|
} else { |
81
|
|
|
$outcome = 'SKIPPED'; |
82
|
|
|
$outcomeColor = 'red'; |
83
|
|
|
$skipped++; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$io->writeln(($i).'. Importing <fg=green>'.$emoji->slug.'</>...........<fg='.$outcomeColor.'>'.$outcome.'</>'); |
87
|
|
|
$i++; |
88
|
|
|
|
89
|
|
|
file_put_contents($chmapFile, $this->generateTheChmapArray($chmap)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $chmap |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function generateTheChmapArray($chmap) |
99
|
|
|
{ |
100
|
|
|
$chmapArray = "<?php "; |
101
|
|
|
$chmapArray .= PHP_EOL; |
102
|
|
|
$chmapArray .= PHP_EOL; |
103
|
|
|
$chmapArray .= "/**"; |
104
|
|
|
$chmapArray .= PHP_EOL; |
105
|
|
|
$chmapArray .= " * Note: for not visible characters:"; |
106
|
|
|
$chmapArray .= PHP_EOL; |
107
|
|
|
$chmapArray .= " *"; |
108
|
|
|
$chmapArray .= PHP_EOL; |
109
|
|
|
$chmapArray .= " * Launch IDE debug, and evaluate the expression:"; |
110
|
|
|
$chmapArray .= PHP_EOL; |
111
|
|
|
$chmapArray .= " *"; |
112
|
|
|
$chmapArray .= PHP_EOL; |
113
|
|
|
$chmapArray .= " * html_entity_decode('xxxx');"; |
114
|
|
|
$chmapArray .= PHP_EOL; |
115
|
|
|
$chmapArray .= " *"; |
116
|
|
|
$chmapArray .= PHP_EOL; |
117
|
|
|
$chmapArray .= " * and then copy the value"; |
118
|
|
|
$chmapArray .= PHP_EOL; |
119
|
|
|
$chmapArray .= " *"; |
120
|
|
|
$chmapArray .= PHP_EOL; |
121
|
|
|
$chmapArray .= " * @var array"; |
122
|
|
|
$chmapArray .= PHP_EOL; |
123
|
|
|
$chmapArray .= " */"; |
124
|
|
|
$chmapArray .= PHP_EOL; |
125
|
|
|
$chmapArray .= "return ". var_export($chmap, true) .";"; |
126
|
|
|
$chmapArray .= PHP_EOL; |
127
|
|
|
|
128
|
|
|
return $chmapArray; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $emoji |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
private function convertEmojiToHtmlEntities($emoji) |
136
|
|
|
{ |
137
|
|
|
$letters = preg_split( '//u', $emoji, null, PREG_SPLIT_NO_EMPTY ); |
138
|
|
|
$entities = []; |
139
|
|
|
|
140
|
|
|
foreach ( $letters as $letter ) { |
141
|
|
|
|
142
|
|
|
$utf32 = mb_convert_encoding($letter, 'UTF-32', 'UTF-8'); |
143
|
|
|
$hex4 = bin2hex($utf32); |
|
|
|
|
144
|
|
|
$dec = hexdec($hex4); |
145
|
|
|
|
146
|
|
|
$entities[$letter] = '&#'.$dec.';'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $entities; |
150
|
|
|
} |
151
|
|
|
} |