Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates for SensioFrameworkExtraBundle latest version(^5.2) (#4348)
* routing annotations deprecation fix * Twig_Extension deprecations fix * Update configurations * Add legacy implementation for templates backward compatibility * review code changes * Fix tests * review fixes * Refactored template engine in LegacyTemplateListener * Symfony 4 compatibility
- Loading branch information
Showing
21 changed files
with
388 additions
and
46 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
bundles/CoreBundle/DependencyInjection/Compiler/LegacyTemplatePass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Enterprise License (PEL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PEL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\CoreBundle\DependencyInjection\Compiler; | ||
|
||
use Pimcore\Bundle\CoreBundle\EventListener\LegacyTemplateListener; | ||
use Pimcore\Bundle\CoreBundle\Templating\LegacyTemplateGuesser; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class LegacyTemplatePass implements CompilerPassInterface | ||
{ | ||
/** | ||
* Replace SensioFrameworkExtraBundle template guesser & view listener with our implementation to support PHP templates | ||
* | ||
* @inheritDoc | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if ($container->hasDefinition('sensio_framework_extra.view.guesser')) { | ||
$definition = $container->getDefinition('sensio_framework_extra.view.guesser'); | ||
$definition | ||
->setPublic(true) | ||
->setClass(LegacyTemplateGuesser::class) | ||
->addArgument(new Reference('templating')); | ||
} | ||
|
||
|
||
if ($container->hasDefinition('sensio_framework_extra.view.listener')) { | ||
$definition = $container->getDefinition('sensio_framework_extra.view.listener'); | ||
$definition | ||
->setClass(LegacyTemplateListener::class) | ||
->addMethodCall('setTemplateEngine', [ | ||
new Reference('templating') | ||
]); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
bundles/CoreBundle/DependencyInjection/Compiler/RoutingLoaderPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Enterprise License (PEL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PEL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\CoreBundle\DependencyInjection\Compiler; | ||
|
||
use Pimcore\Routing\Loader\AnnotatedRouteControllerLoader; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Set annotation loader to our own implementation normalizing admin routes: converts the prefix | ||
* pimcore_pimcoreadmin_ to just pimcore_admin_ | ||
* | ||
*/ | ||
class RoutingLoaderPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* Replace Annotated Routing loader with your implementation to normalizes autogenerated admin routes | ||
* @inheritDoc | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('routing.loader.annotation')) { | ||
return; | ||
} | ||
|
||
$definition = $container->getDefinition('routing.loader.annotation'); | ||
$definition->setClass(AnnotatedRouteControllerLoader::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
bundles/CoreBundle/EventListener/LegacyTemplateListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace Pimcore\Bundle\CoreBundle\EventListener; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | ||
use Sensio\Bundle\FrameworkExtraBundle\Templating\TemplateGuesser; | ||
use Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener; | ||
use Symfony\Component\Templating\EngineInterface; | ||
|
||
/** | ||
* Provides backward compatibility for PHP templates | ||
* | ||
*/ | ||
class LegacyTemplateListener extends TemplateListener | ||
{ | ||
/** | ||
* @var EngineInterface | ||
*/ | ||
private $templateEngine; | ||
|
||
/** | ||
* @return EngineInterface | ||
*/ | ||
public function getTemplateEngine(): EngineInterface | ||
{ | ||
return $this->templateEngine; | ||
} | ||
|
||
/** | ||
* @param EngineInterface $templateEngine | ||
*/ | ||
public function setTemplateEngine(EngineInterface $templateEngine): void | ||
{ | ||
$this->templateEngine = $templateEngine; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function onKernelView(GetResponseForControllerResultEvent $event) | ||
{ | ||
|
||
/* @var Template $template */ | ||
$request = $event->getRequest(); | ||
$template = $request->attributes->get('_template'); | ||
|
||
if (!$template instanceof Template) { | ||
return; | ||
} | ||
|
||
|
||
$parameters = $event->getControllerResult(); | ||
$owner = $template->getOwner(); | ||
list($controller, $action) = $owner; | ||
|
||
// when the annotation declares no default vars and the action returns | ||
// null, all action method arguments are used as default vars | ||
if (null === $parameters) { | ||
$parameters = $this->resolveDefaultParameters($request, $template, $controller, $action); | ||
} | ||
|
||
// attempt to render the actual response | ||
$templating = $this->getTemplateEngine(); | ||
|
||
if ($template->isStreamable()) { | ||
$callback = function () use ($templating, $template, $parameters) { | ||
return $templating->stream($template->getTemplate(), $parameters); | ||
}; | ||
|
||
$event->setResponse(new StreamedResponse($callback)); | ||
} | ||
|
||
// make sure the owner (controller+dependencies) is not cached or stored elsewhere | ||
$template->setOwner([]); | ||
|
||
$event->setResponse($templating->renderResponse($template->getTemplate(), $parameters)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
KernelEvents::CONTROLLER => ['onKernelController', -128], | ||
KernelEvents::VIEW => 'onKernelView', | ||
]; | ||
} | ||
|
||
private function resolveDefaultParameters(Request $request, Template $template, $controller, $action) | ||
{ | ||
$parameters = []; | ||
$arguments = $template->getVars(); | ||
|
||
if (0 === \count($arguments)) { | ||
$r = new \ReflectionObject($controller); | ||
|
||
$arguments = []; | ||
foreach ($r->getMethod($action)->getParameters() as $param) { | ||
$arguments[] = $param; | ||
} | ||
} | ||
|
||
// fetch the arguments of @Template.vars or everything if desired | ||
// and assign them to the designated template | ||
foreach ($arguments as $argument) { | ||
if ($argument instanceof \ReflectionParameter) { | ||
$parameters[$name = $argument->getName()] = !$request->attributes->has($name) && $argument->isDefaultValueAvailable() ? $argument->getDefaultValue() : $request->attributes->get($name); | ||
} else { | ||
$parameters[$argument] = $request->attributes->get($argument); | ||
} | ||
} | ||
|
||
return $parameters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -499,3 +499,7 @@ presta_sitemap: | |
lastmod: ~ | ||
priority: ~ | ||
changefreq: ~ | ||
|
||
sensio_framework_extra: | ||
router: | ||
annotations: false |
Oops, something went wrong.