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

NodeBundle/EventListener/RenderContextListener.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\NodeBundle\EventListener;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
10
use Symfony\Component\Templating\EngineInterface;
11
12
class RenderContextListener
13
{
14
    /**
15
     * @var EngineInterface
16
     */
17
    protected $templating;
18
19
    /**
20
     * @var EntityManagerInterface
21
     */
22
    protected $em;
23
24
    /**
25
     * @param EngineInterface        $templating
26
     * @param EntityManagerInterface $em
27
     */
28
    public function __construct(/* EngineInterface|EntityManagerInterface */ $em, EntityManagerInterface $emOld = null)
29
    {
30
        if ($em instanceof EngineInterface) {
31
            // NEXT_MAJOR Also remove the symfony/templating dependency as it is unused after the removal of the templating parameter.
32
            @trigger_error(sprintf('Passing a templating engine as the first argument of "%s" is deprecated since KunstmaanNodeBundle 5.1 and will be removed in KunstmaanNodeBundle 6.0. Remove the template engine service argument.', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
34
            $this->templating = $em;
35
            $this->em = $emOld;
36
37
            return;
38
        }
39
40
        $this->em = $em;
41
    }
42
43
    /**
44
     * @param GetResponseForControllerResultEvent $event
45
     */
46
    public function onKernelView(GetResponseForControllerResultEvent $event)
47
    {
48
        $response = $event->getControllerResult();
49
        if ($response instanceof Response) {
50
            // If it's a response, just continue
51
            return;
52
        }
53
54
        $request = $event->getRequest();
55
        if ($request->attributes->has('_template')) { //template is already set
56
            return;
57
        }
58
59
        $nodeTranslation    = $request->attributes->get('_nodeTranslation');
60
        if ($nodeTranslation) {
61
            $entity     = $request->attributes->get('_entity');
62
            $url        = $request->attributes->get('url');
63
            $nodeMenu   = $request->attributes->get('_nodeMenu');
64
            $parameters = $request->attributes->get('_renderContext');
65
66
            if ($request->get('preview') === true) {
67
                $version = $request->get('version');
68
                if (!empty($version) && is_numeric($version)) {
69
                    $nodeVersion = $this->em->getRepository('KunstmaanNodeBundle:NodeVersion')->find($version);
70
                    if (!is_null($nodeVersion)) {
71
                        $entity = $nodeVersion->getRef($this->em);
72
                    }
73
                }
74
            }
75
76
            $renderContext = array(
77
                'nodetranslation' => $nodeTranslation,
78
                'slug'            => $url,
79
                'page'            => $entity,
80
                'resource'        => $entity,
81
                'nodemenu'        => $nodeMenu,
82
            );
83
84
            if (is_array($parameters) || $parameters instanceof \ArrayObject) {
85
                $parameters = array_merge($renderContext, (array) $parameters);
86
            } else {
87
                $parameters = $renderContext;
88
            }
89
90
            if (is_array($response)) {
91
                // If the response is an array, merge with rendercontext
92
                $parameters = array_merge($parameters, $response);
93
            }
94
95
            //set the rendercontext with all params as response, plus the template in the request attribs
96
            //the SensioFrameworkExtraBundle kernel.view will handle everything else
97
            $event->setControllerResult((array) $parameters);
98
99
            $template = new Template(array());
100
            $template->setTemplate($entity->getDefaultView());
101
102
            $request->attributes->set('_template', $template);
103
        }
104
    }
105
}
106