Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Twig/Extension/PagePartTwigExtension.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
2
3
namespace Kunstmaan\PagePartBundle\Twig\Extension;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\PagePartBundle\Entity\PagePartRef;
7
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
8
use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
9
use Kunstmaan\PagePartBundle\Repository\PagePartRefRepository;
10
11
/**
12
 * PagePartTwigExtension
13
 */
14
class PagePartTwigExtension extends \Twig_Extension
15
{
16
    /**
17
     * @var EntityManager
18
     */
19
    protected $em;
20
21
    /**
22
     * @param EntityManager $em
23
     */
24
    public function __construct(EntityManager $em)
25
    {
26
        $this->em = $em;
27
    }
28
29
    /**
30
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use \Twig_SimpleFunction[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
31
     */
32
    public function getFunctions()
33
    {
34
        return array(
35
            new \Twig_SimpleFunction('render_pageparts', array($this, 'renderPageParts'), array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('html'))),
36
            new \Twig_SimpleFunction('getpageparts', array('needs_environment' => true, $this, 'getPageParts')),
37
            new \Twig_SimpleFunction('has_page_parts', [$this, 'hasPageParts']),
38
        );
39
    }
40
41
    /**
42
     * @param \Twig_Environment     $env
43
     * @param array                 $twigContext The twig context
44
     * @param HasPagePartsInterface $page        The page
45
     * @param string                $contextName The pagepart context
46
     * @param array                 $parameters  Some extra parameters
47
     *
48
     * @return string
49
     */
50 View Code Duplication
    public function renderPageParts(\Twig_Environment $env, array $twigContext, HasPagePartsInterface $page, $contextName = "main", array $parameters = array())
51
    {
52
        $template = $env->loadTemplate("KunstmaanPagePartBundle:PagePartTwigExtension:widget.html.twig");
53
        /* @var $entityRepository PagePartRefRepository */
54
        $pageparts = $this->getPageParts($page, $contextName);
55
        $newTwigContext = array_merge($parameters, array(
56
            'pageparts' => $pageparts,
57
            'page' => $page
58
        ));
59
        $newTwigContext = array_merge($newTwigContext, $twigContext);
60
61
        return $template->render($newTwigContext);
62
    }
63
64
    /**
65
     * @param HasPagePartsInterface $page    The page
66
     * @param string                $context The pagepart context
67
     *
68
     * @return PagePartInterface[]
69
     */
70
    public function getPageParts(HasPagePartsInterface $page, $context = "main")
71
    {
72
        /**@var $entityRepository PagePartRefRepository */
73
        $entityRepository = $this->em->getRepository('KunstmaanPagePartBundle:PagePartRef');
74
        $pageparts = $entityRepository->getPageParts($page, $context);
75
76
        return $pageparts;
77
    }
78
79
    /**
80
     * @param HasPagePartsInterface $page
81
     * @param string $context
82
     *
83
     * @return bool
84
     */
85
    public function hasPageParts(HasPagePartsInterface $page, $context = 'main')
86
    {
87
        return $this->em->getRepository(PagePartRef::class)->hasPageParts($page, $context);
88
    }
89
}
90