Completed
Push — master ( f3ba84...536f4c )
by Kacper
02:36
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 24 and the first side effect is on line 87.

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