Completed
Push — async ( 59a193 )
by Eric
14:44
created

CKEditorJavascriptExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory CKEditor package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\CKEditorBundle\Twig;
13
14
use Symfony\Component\Form\FormRendererInterface;
15
use Symfony\Component\Form\FormView;
16
17
/**
18
 * Form Javascript Twig extension.
19
 *
20
 * @author GeLo <[email protected]>
21
 */
22
class CKEditorJavascriptExtension extends \Twig_Extension
23
{
24
    /** @var \Symfony\Component\Form\FormRendererInterface */
25
    private $renderer;
26
27
    /**
28
     * Creates a CKEditor Javascript Twig extension.
29
     *
30
     * @param \Symfony\Component\Form\FormRendererInterface $renderer The form renderer
31
     */
32
    public function __construct(FormRendererInterface $renderer)
33
    {
34
        $this->renderer = $renderer;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getFunctions()
41
    {
42
        return array(
43
            new \Twig_SimpleFunction(
44
                'form_javascript',
45
                array($this, 'renderJavascript'),
46
                array('is_safe' => array('html'))
47
            ),
48
        );
49
    }
50
51
    /**
52
     * Renders a form javascript fragment.
53
     *
54
     * @param FormView $view      The form view.
55
     * @param bool     $prototype TRUE if the form view is a prototype else FALSE.
56
     *
57
     * @return string The rendered form javascript fragment.
58
     */
59
    public function renderJavascript(FormView $view, $prototype = false)
60
    {
61
        return $this->renderer->searchAndRenderBlock($view, $prototype ? 'javascript_prototype' : 'javascript');
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getName()
68
    {
69
        return 'ivory_ckeditor_javascript';
70
    }
71
}
72