Completed
Push — master ( 6054a8...c11aa8 )
by Kacper
04:06
created

Http::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
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\Language;
17
18
use Kadet\Highlighter\Matcher\RegexMatcher;
19
use Kadet\Highlighter\Parser\Rule;
20
use Kadet\Highlighter\Parser\Token\LanguageToken;
21
use Kadet\Highlighter\Parser\TokenFactory;
22
23
class Http extends GreedyLanguage
24
{
25
    private $_embeddedFactory;
26
27
    public function __construct(array $options)
28
    {
29
        parent::__construct($options);
30
31
        $this->_embeddedFactory = new TokenFactory(LanguageToken::class);
32
    }
33
34
    /**
35
     * Tokenization rules setup
36
     */
37
    public function setupRules()
38
    {
39
        $this->rules->addMany([
40
            'number.status'   => new Rule(new RegexMatcher('/^HTTP\/.+\s(\d+)/')),
41
            'constant.status' => new Rule(new RegexMatcher('/^HTTP\/.+\s+\d+\s+(.+?)\R/')),
42
43
            'call.method' => new Rule(new RegexMatcher('/^(\w+).*HTTP\//')),
44
            'string.path' => new Rule(new RegexMatcher('/^\w+\s(.*?)\sHTTP\//')),
45
46
            'symbol.version' => new Rule(new RegexMatcher('/^.*(HTTP\/\S+)/')),
47
48
            'symbol.header' => new Rule(new RegexMatcher('/^([\w-]+:)/m')),
49
        ]);
50
    }
51
52
    public function tokenize($source, $additional = [], $offset = 0, $embedded = false)
53
    {
54
        $split = preg_split('/\R\R/', $source, 2);
55
56
        $http = $split[0];
57
        if(isset($split[1]) && $payload = $split[1]) {
58
            if(preg_match('/Content-Type: ([^;]*)/', $http, $matches)) {
59
                $mime = $matches[1];
60
            } else {
61
                $mime = 'text/plain';
62
            }
63
64
            $injected = self::byMime($mime);
65
            $language = $this->_embeddedFactory->create('language.'.$injected->getIdentifier(), [
66
                'pos'    => strlen($http) + 4 + $offset,
67
                'length' => strlen($payload),
68
                'inject' => $injected,
69
                'rule'   => new Rule(null, [
70
                    'language' => $this,
71
                    'priority' => 900
72
                ])
73
            ]);
74
            $language->setValid(true);
75
76
            $additional = array_merge(
77
                $additional,
78
                $injected->tokenize($payload, [], $language->pos, Language::EMBEDDED_BY_PARENT)->getArrayCopy(),
79
                [$language, $language->getEnd()]
80
            );
81
        }
82
83
        return parent::tokenize($source, $additional, $offset, $embedded);
84
    }
85
86
87
    /**
88
     * Unique language identifier, for example 'php'
89
     *
90
     * @return string
91
     */
92
    public function getIdentifier()
93
    {
94
        return 'http';
95
    }
96
97
    public static function getMetadata()
98
    {
99
        return [
100
            'name'      => ['http'],
101
        ];
102
    }
103
}
104