-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKeyLighter.php
179 lines (152 loc) · 4.43 KB
/
KeyLighter.php
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Highlighter
*
* Copyright (C) 2016, Some right reserved.
*
* @author Kacper "Kadet" Donat <[email protected]>
*
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
*
* From Kadet with love.
*/
namespace Kadet\Highlighter;
use Kadet\Highlighter\Formatter\CliFormatter;
use Kadet\Highlighter\Formatter\FormatterInterface;
use Kadet\Highlighter\Formatter\HtmlFormatter;
use Kadet\Highlighter\Language\Language;
use Kadet\Highlighter\Language\PlainText;
use Kadet\Highlighter\Utils\Singleton;
/**
* KeyLighter helper class, used to simplify usage.
*
* @package Kadet\Highlighter
*/
class KeyLighter
{
use Singleton;
public const VERSION = '0.9-dev';
/**
* Registered aliases
*
* @var array
*/
private $_languages = [
'name' => [],
'mime' => [],
'extension' => []
];
private $_formatters = [];
/** @var FormatterInterface */
private $_formatter = null;
/**
* @param string $name
*
* @return Language
*/
public function getLanguage($name)
{
$embedded = [];
if (($pos = strpos($name, '>')) !== false) {
$embedded[] = self::getLanguage(trim(substr($name, $pos + 1)));
$name = trim(substr($name, 0, $pos));
}
return $this->languageByName($name, [
'embedded' => $embedded
]);
}
public function languageByName($name, $params = [])
{
return isset($this->_languages['name'][$name]) ?
$this->_languages['name'][$name]($params) :
new PlainText($params);
}
public function languageByMime($mime, $params = [])
{
return isset($this->_languages['mime'][$mime]) ?
$this->_languages['mime'][$mime]($params) :
new PlainText($params);
}
public function languageByExt($filename, $params = [])
{
foreach($this->_languages['extension'] as $mask => $class) {
if(fnmatch($mask, $filename)) {
return $class($params);
}
}
return new PlainText($params);
}
/**
* @param callable|string $language
* @param array<string> $aliases
*
* @deprecated Will be removed in 1.0
*/
public function registerLanguage($language, $aliases)
{
$this->register($language, ['name' => $aliases]);
}
public function setDefaultFormatter(FormatterInterface $formatter)
{
$this->_formatter = $formatter;
}
public function registeredLanguages($by = 'name', $class = false)
{
return array_map(function ($e) use($class) {
return $e([])->getFQN($class);
}, $this->_languages[$by]);
}
public function getDefaultFormatter()
{
return $this->_formatter;
}
public function addFormatter($name, FormatterInterface $formatter)
{
$this->_formatters[$name] = $formatter;
}
public function getFormatter($name)
{
return isset($this->_formatters[$name]) ? $this->_formatters[$name] : false;
}
public function registeredFormatters()
{
return $this->_formatters;
}
public function highlight($source, Language $language, FormatterInterface $formatter = null)
{
$formatter = $formatter ?: $this->getDefaultFormatter();
return $formatter->format($language->parse($source));
}
public function __construct()
{
$this->setDefaultFormatter(
php_sapi_name() === 'cli' ? new CliFormatter() : new HtmlFormatter()
);
}
public function init()
{
foreach(include __DIR__.'/Config/metadata.php' as $alias) {
$class = $alias[0];
unset($alias[0]);
$this->register($class, $alias);
}
$this->_formatters = include __DIR__.'/Config/formatters.php';
}
/**
* @param string|callable $class
* @param array $options
*/
public function register($class, array $options)
{
if(!is_callable($class) && is_subclass_of($class, Language::class)) {
$class = function($arguments = []) use ($class) {
return new $class($arguments);
};
}
foreach(array_intersect_key($options, array_flip(['name', 'mime', 'extension'])) as $name => $values) {
$this->_languages[$name] = array_merge($this->_languages[$name], array_fill_keys($values, $class));
}
}
}