Passed
Pull Request — master (#26)
by Maciej
03:01
created

KeyLighter.php (1 issue)

1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter;
17
18
use Kadet\Highlighter\Formatter\CliFormatter;
19
use Kadet\Highlighter\Formatter\FormatterInterface;
20
use Kadet\Highlighter\Formatter\HtmlFormatter;
21
use Kadet\Highlighter\Language\Language;
22
use Kadet\Highlighter\Language\PlainText;
23
use Kadet\Highlighter\Utils\Singleton;
24
25
/**
26
 * KeyLighter helper class, used to simplify usage.
27
 *
28
 * @package Kadet\Highlighter
29
 */
30
class KeyLighter
31
{
32
    use Singleton;
33
34
    public const VERSION = '0.9-dev';
35
36
    /**
37
     * Registered aliases
38
     *
39
     * @var array
40
     */
41
    private $_languages = [
42
        'name'      => [],
43
        'mime'      => [],
44
        'extension' => []
45
    ];
46
47
    private $_formatters = [];
48
49
    /** @var FormatterInterface */
50
    private $_formatter = null;
51
52
    /**
53
     * @param string $name
54
     *
55
     * @return Language
56
     */
57 3
    public function getLanguage($name)
58
    {
59 3
        $embedded = [];
60 3
        if (($pos = strpos($name, '>')) !== false) {
61 1
            $embedded[] = self::getLanguage(trim(substr($name, $pos + 1)));
0 ignored issues
show
Bug Best Practice introduced by
The method Kadet\Highlighter\KeyLighter::getLanguage() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            /** @scrutinizer ignore-call */ 
62
            $embedded[] = self::getLanguage(trim(substr($name, $pos + 1)));
Loading history...
62 1
            $name       = trim(substr($name, 0, $pos));
63
        }
64
65 3
        return $this->languageByName($name, [
66 3
            'embedded' => $embedded
67
        ]);
68
    }
69
70 6
    public function languageByName($name, $params = [])
71
    {
72 6
        return isset($this->_languages['name'][$name]) ?
73 6
            $this->_languages['name'][$name]($params) :
74 6
            new PlainText($params);
75
    }
76
77 3
    public function languageByMime($mime, $params = [])
78
    {
79 3
        return isset($this->_languages['mime'][$mime]) ?
80 3
            $this->_languages['mime'][$mime]($params) :
81 3
            new PlainText($params);
82
    }
83
84 2
    public function languageByExt($filename, $params = [])
85
    {
86 2
        foreach($this->_languages['extension'] as $mask => $class) {
87 2
            if(fnmatch($mask, $filename)) {
88 2
                return $class($params);
89
            }
90
        }
91
92 1
        return new PlainText($params);
93
    }
94
95
    /**
96
     * @param callable|string $language
97
     * @param array<string>   $aliases
98
     *
99
     * @deprecated Will be removed in 1.0
100
     */
101 1
    public function registerLanguage($language, $aliases)
102
    {
103 1
        $this->register($language, ['name' => $aliases]);
104 1
    }
105
106 36
    public function setDefaultFormatter(FormatterInterface $formatter)
107
    {
108 36
        $this->_formatter = $formatter;
109 36
    }
110
111 3
    public function registeredLanguages($by = 'name', $class = false)
112
    {
113
        return array_map(function ($e) use($class) {
114 3
            return $e([])->getFQN($class);
115 3
        }, $this->_languages[$by]);
116
    }
117
118 1
    public function getDefaultFormatter()
119
    {
120 1
        return $this->_formatter;
121
    }
122
123
    public function addFormatter($name, FormatterInterface $formatter)
124
    {
125
        $this->_formatters[$name] = $formatter;
126
    }
127
128
    public function getFormatter($name)
129
    {
130
        return isset($this->_formatters[$name]) ? $this->_formatters[$name] : false;
131
    }
132
133
    public function registeredFormatters()
134
    {
135
        return $this->_formatters;
136
    }
137
138 29
    public function highlight($source, Language $language, FormatterInterface $formatter = null)
139
    {
140 29
        $formatter = $formatter ?: $this->getDefaultFormatter();
141 29
        return $formatter->format($language->parse($source));
142
    }
143
144 36
    public function __construct()
145
    {
146 36
        $this->setDefaultFormatter(
147 36
            php_sapi_name() === 'cli' ? new CliFormatter() : new HtmlFormatter()
148
        );
149 36
    }
150
151
    public function init()
152
    {
153
        foreach(include __DIR__.'/Config/metadata.php' as $alias) {
154
            $class = $alias[0];
155
            unset($alias[0]);
156
157
            $this->register($class, $alias);
158
        }
159
160
        $this->_formatters = include __DIR__.'/Config/formatters.php';
161
    }
162
163
    /**
164
     * @param string|callable $class
165
     * @param array           $options
166
     */
167 13
    public function register($class, array $options)
168
    {
169 11
        if(!is_callable($class) && is_subclass_of($class, Language::class)) {
170
            $class = function($arguments = []) use ($class) {
171 13
                return new $class($arguments);
172 11
            };
173
        }
174
175 11
        foreach(array_intersect_key($options, array_flip(['name', 'mime', 'extension'])) as $name => $values) {
176 11
            $this->_languages[$name] = array_merge($this->_languages[$name], array_fill_keys($values, $class));
177
        }
178 11
    }
179
}
180