Passed
Push — master ( 778310...fbc9ab )
by Kacper
05:38
created

KeyLighter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 83.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2015, 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
23
class KeyLighter
24
{
25
    const VERSION = '0.2.0';
26
27
    /** @var */
28
    private static $_languages = [];
29
    /** @var FormatterInterface */
30
    private static $_formatter = null;
31
32
    /**
33
     * @param string $name
34
     *
35
     * @return Language
36
     */
37
    public static function getLanguage($name) {
38
        $embedded = [];
39
        if(($pos = strpos($name, '>')) !== false) {
40
            $embedded[] = self::getLanguage(trim(substr($name, $pos + 1)));
41
            $name       = trim(substr($name, 0, $pos));
42
        }
43
44
        $lang = isset(self::$_languages[$name]) ? self::$_languages[$name] : 'Kadet\\Highlighter\\Language\\PlainText';
45
        return new $lang([
46
            'embedded' => $embedded
47
        ]);
48
    }
49
50
    /**
51
     * @param Language|callable|string $language
52
     * @param $aliases
53
     */
54
    public static function registerLanguage($language, $aliases) {
55
        self::$_languages = array_merge(self::$_languages, array_fill_keys($aliases, $language));
56
    }
57
58
    public static function setDefaultFormatter(FormatterInterface $formatter) {
59
        self::$_formatter = $formatter;
60
    }
61
62
    public static function registeredLanguages() {
63
        return self::$_languages;
64
    }
65
66
    public static function getDefaultFormatter() {
67
        return self::$_formatter;
68
    }
69
70
    public static function highlight($source, $language, FormatterInterface $formatter = null) {
71
        $formatter = $formatter ?: self::getDefaultFormatter();
72
73
        if(!$language instanceof Language) {
74
            $language = self::getLanguage($language);
75
        }
76
77
        return $formatter->format($language->parse($source));
78
    }
79
}
80
81
# Acts like static constructor
82
83
KeyLighter::setDefaultFormatter(
84
    php_sapi_name() === 'cli' ?
85
        new CliFormatter() :
86
        new HtmlFormatter()
87
);
88
89
KeyLighter::registerLanguage('Kadet\\Highlighter\\Language\\PhpLanguage', ['php']);
90
KeyLighter::registerLanguage('Kadet\\Highlighter\\Language\\XmlLanguage', ['xml', 'xaml']);
91
KeyLighter::registerLanguage('Kadet\\Highlighter\\Language\\HtmlLanguage', ['html', 'htm']);
92
KeyLighter::registerLanguage('Kadet\\Highlighter\\Language\\PowerShellLanguage', ['powershell', 'posh', 'ps1']);
93
KeyLighter::registerLanguage('Kadet\\Highlighter\\Language\\PlainText', ['plaintext', 'text', 'none', 'txt']);
94
KeyLighter::registerLanguage('Kadet\\Highlighter\\Language\\LatexLanguage', ['tex', 'latex']);
95
KeyLighter::registerLanguage('Kadet\\Highlighter\\Language\\IniLanguage', ['ini', 'cfg']);
96
KeyLighter::registerLanguage('Kadet\\Highlighter\\Language\\JavaScriptLanguage', ['js', 'jscript', 'javascript']);