Completed
Pull Request — master (#84)
by Tobias
09:11
created

HttpMessageMarkupExtension::markup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Http\HttplugBundle\Collector\Twig;
4
5
/**
6
 * 
7
 *
8
 * @author Tobias Nyholm <[email protected]>
9
 */
10
class HttpMessageMarkupExtension extends \Twig_Extension
11
{
12
    /**
13
     * {@inheritdoc}
14
     *
15
     * @return array
16
     */
17
    public function getFilters()
18
    {
19
        return array(
20
            new \Twig_SimpleFilter('httplug_markup', [$this, 'markup'], ['is_safe' => ['html']]),
21
        );
22
    }
23
24
    /**
25
     * @param string $message http message
26
     */
27
    public function markup($message)
28
    {
29
        $safeMessage = htmlentities($message);
30
        list($headers, $body) = preg_split('|\\r?\\n\\r?\\n|', $safeMessage, 2);
31
32
        // make header names bold
33
        $headers = preg_replace("|\n(.*?): |si", "\n<b>$1</b>: ", $headers);
34
        
35
        return sprintf("%s\n\n<div class='httplug-http-body'>%s</div>", $headers, $body);
36
    }
37
38
    public function getName()
39
    {
40
        return 'httplug.message_markup';
41
    }
42
}