Skip to content
Permalink
Browse files

Fix EZP-22767: ezscript_require not taken into account on modules

  • Loading branch information...
yannickroger committed Apr 29, 2014
1 parent 8c772e6 commit 2c010c4580eac15421f5acd38fc52517834250f4
@@ -12,6 +12,7 @@
use eZ\Bundle\EzPublishLegacyBundle\LegacyResponse;
use eZ\Bundle\EzPublishLegacyBundle\LegacyResponse\LegacyResponseManager;
use eZ\Publish\Core\MVC\Legacy\Kernel\URIHelper;
use eZ\Publish\Core\MVC\Legacy\Templating\LegacyHelper;
use Symfony\Component\HttpFoundation\Request;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use ezpKernelRedirect;
@@ -52,13 +53,23 @@ class LegacyKernelController
*/
private $legacyResponseManager;
public function __construct( \Closure $kernelClosure, ConfigResolverInterface $configResolver, URIHelper $uriHelper, LegacyResponseManager $legacyResponseManager )
/** @var \eZ\Publish\Core\MVC\Legacy\Templating\LegacyHelper; */
private $legacyHelper;
public function __construct(
\Closure $kernelClosure,
ConfigResolverInterface $configResolver,
URIHelper $uriHelper,
LegacyResponseManager $legacyResponseManager,
LegacyHelper $legacyHelper
)
{
$this->kernel = $kernelClosure();
$this->legacyLayout = $configResolver->getParameter( 'module_default_layout', 'ezpublish_legacy' );
$this->configResolver = $configResolver;
$this->uriHelper = $uriHelper;
$this->legacyResponseManager = $legacyResponseManager;
$this->legacyHelper = $legacyHelper;
}
public function setRequest( Request $request = null )
@@ -86,6 +97,9 @@ public function indexAction()
}
$result = $this->kernel->run();
$this->legacyHelper->loadDataFromModuleResult( $result->getAttribute( 'module_result' ) );

This comment has been minimized.

Copy link
@lolautruche

lolautruche May 26, 2014

Contributor

Regression: If $result is an ezpKerneRedirect and request is POST, this will trigger a form token exception because of the `runCallback() done in the legacy helper.

See https://jira.ez.no/browse/EZP-22910

This comment has been minimized.

Copy link
@lolautruche

lolautruche May 26, 2014

Contributor

Fixed in #875

$this->kernel->setUseExceptions( true );
if ( $result instanceof ezpKernelRedirect )
@@ -114,6 +114,7 @@ services:
- @ezpublish.config.resolver
- @ezpublish_legacy.uri_helper
- @ezpublish_legacy.response_manager
- @ezpublish_legacy.templating.legacy_helper
calls:
- [setRequest, [@?request=]]

@@ -1,5 +1,7 @@
parameters:
ezpublish_legacy.twig.extension.class: eZ\Publish\Core\MVC\Legacy\Templating\Twig\Extension\LegacyExtension
ezpublish_legacy.twig.extension.template.js: 'EzPublishLegacyBundle::ez_legacy_render_js.html.twig'
ezpublish_legacy.twig.extension.template.css: 'EzPublishLegacyBundle::ez_legacy_render_css.html.twig'
ezpublish_legacy.templating.api_content_converter.class: eZ\Publish\Core\MVC\Legacy\Templating\Converter\ApiContentConverter
ezpublish_legacy.templating.pageparts_converter.class: eZ\Publish\Core\MVC\Legacy\Templating\Converter\PagePartsConverter
ezpublish_legacy.templating.delegating_converter.class: eZ\Publish\Core\MVC\Legacy\Templating\Converter\DelegatingConverter
@@ -15,7 +17,11 @@ parameters:
services:
ezpublish_legacy.twig.extension:
class: %ezpublish_legacy.twig.extension.class%
arguments: [@templating.engine.eztpl]
arguments:
- @templating.engine.eztpl
- @ezpublish_legacy.templating.legacy_helper
- %ezpublish_legacy.twig.extension.template.js%
- %ezpublish_legacy.twig.extension.template.css%
tags:
- {name: twig.extension}

@@ -44,6 +50,7 @@ services:

ezpublish_legacy.templating.legacy_helper:
class: %ezpublish_legacy.templating.legacy_helper.class%
arguments: [@ezpublish_legacy.kernel]

ezpublish.templating.global_helper.legacy:
class: %ezpublish.templating.global_helper.legacy.class%
@@ -0,0 +1,12 @@
{# Template used by ez_legacy_render_css #}
{% block ez_legacy_render_css %}
{% for item in css_files %}
<link rel="stylesheet" type="text/css" href="{{ asset( item ) }}" />
{%endfor %}

{% for item in css_code_lines %}
<style type="text/css">
{{ item|raw }}
</style>
{%endfor %}
{% endblock %}
@@ -0,0 +1,12 @@
{# Template used by ez_legacy_render_js #}
{% block ez_legacy_render_js %}
{% for item in js_files %}
<script type="text/javascript" src="{{ asset( item ) }}"></script>
{%endfor %}

{% for item in js_code_lines %}
<script type="text/javascript">
{{item|raw}}
</script>
{%endfor %}
{% endblock %}
@@ -10,7 +10,92 @@
namespace eZ\Publish\Core\MVC\Legacy\Templating;
use Symfony\Component\HttpFoundation\ParameterBag;
use ezjscPacker;
use eZINI;
use ezjscPackerTemplateFunctions;
class LegacyHelper extends ParameterBag
{
/**
* @var callable
*/
private $legacyKernelClosure;
public function __construct( \Closure $legacyKernelClosure )
{
$this->legacyKernelClosure = $legacyKernelClosure;

This comment has been minimized.

Copy link
@lolautruche

lolautruche Jul 22, 2014

Contributor

Regression: EZP-23184

Not calling parent constructor prevents inner parameters array to be initialized.

This comment has been minimized.

Copy link
@lolautruche

lolautruche Jul 22, 2014

Contributor

Fixed in #935

}
/**
* Fills up the LegacyHelper with data from a given moduleResult
*
* @param array $moduleResult
*/
public function loadDataFromModuleResult( array $moduleResult )
{
$kernelClosure = $this->legacyKernelClosure;
$that = $this;
$kernelClosure()->runCallback(
function () use ( $moduleResult, $that )
{
// Injecting all $moduleResult entries in the legacy helper
foreach ( $moduleResult as $key => $val )
{
if ( $key === 'content' )
continue;
$that->set( $key, $val );
}
// Adding ezjscore data to module result if not present for support in legacy modules
if ( !isset( $moduleResult['content_info']['persistent_variable'] ) )
{
$moduleResult['content_info']['persistent_variable'] = ezjscPackerTemplateFunctions::getPersistentVariable();
}
// Javascript/CSS files required with ezcss_require/ezscript_require
// Compression level is forced to 0 to only get the files list
if ( isset( $moduleResult['content_info']['persistent_variable']['css_files'] ) )
{
$that->set(
'css_files',
ezjscPacker::buildStylesheetFiles(
$moduleResult['content_info']['persistent_variable']['css_files'],
0
)
);
}
if ( isset( $moduleResult['content_info']['persistent_variable']['js_files'] ) )
{
$that->set(
'js_files',
ezjscPacker::buildJavascriptFiles(
$moduleResult['content_info']['persistent_variable']['js_files'],
0
)
);
}
// Now getting configured JS/CSS files, in design.ini
// Will only take FrontendCSSFileList/FrontendJavascriptList
$designINI = eZINI::instance( 'design.ini' );
$that->set(
'css_files_configured',
ezjscPacker::buildStylesheetFiles(
$designINI->variable( 'StylesheetSettings', 'FrontendCSSFileList' ),
0
)
);
$that->set(
'js_files_configured',
ezjscPacker::buildJavascriptFiles(
$designINI->variable( 'JavaScriptSettings', 'FrontendJavaScriptList' ),
0
)
);
},
false
);
}
}
@@ -22,7 +22,15 @@ class GlobalHelperTest extends BaseGlobalHelperTest
protected function setUp()
{
parent::setUp();
$this->legacyHelper = $this->getMock( 'eZ\\Publish\\Core\\MVC\\Legacy\\Templating\\LegacyHelper' );
$this->legacyHelper = $this->getMock(
'eZ\\Publish\\Core\\MVC\\Legacy\\Templating\\LegacyHelper',
array(),
array(
function ()
{
}
)
);
// Force to use Legacy GlobalHelper
$this->helper = new GlobalHelper( $this->configResolver, $this->locationService, $this->router, $this->translationHelper );
$this->helper->setLegacyHelper( $this->legacyHelper );
@@ -9,9 +9,12 @@
namespace eZ\Publish\Core\MVC\Legacy\Templating\Twig\Extension;
use eZ\Publish\Core\MVC\Legacy\Templating\LegacyHelper;
use eZ\Publish\Core\MVC\Legacy\Templating\Twig\TokenParser\LegacyIncludeParser;
use eZ\Publish\Core\MVC\Legacy\Templating\LegacyEngine;
use Twig_Extension;
use Twig_SimpleFunction;
use Twig_Environment;
/**
* Twig extension for eZ Publish legacy
@@ -23,9 +26,33 @@ class LegacyExtension extends Twig_Extension
*/
private $legacyEngine;
public function __construct( LegacyEngine $legacyEngine )
/** @var \eZ\Publish\Core\MVC\Legacy\Templating\LegacyHelper */
private $legacyHelper;
/** @var string */
private $jsTemplate;
/** @var string */
private $cssTemplate;
/**
* The Twig environment
*
* @var \Twig_Environment
*/
protected $environment;
public function __construct(
LegacyEngine $legacyEngine,
LegacyHelper $legacyHelper,
$jsTemplate,
$cssTemplate
)
{
$this->legacyEngine = $legacyEngine;
$this->legacyHelper = $legacyHelper;
$this->jsTemplate = $jsTemplate;
$this->cssTemplate = $cssTemplate;
}
/**
@@ -60,4 +87,122 @@ public function getName()
{
return 'ezpublish.legacy';
}
/**
* Initializes the template runtime (aka Twig environment).
*
* @param \Twig_Environment $environment
*/
public function initRuntime( Twig_Environment $environment )
{
$this->environment = $environment;
}
/**
* Returns a list of functions to add to the existing list.
*
* @return array
*/
public function getFunctions()
{
return array(
new Twig_SimpleFunction(
'ez_legacy_render_js',
array( $this, 'renderLegacyJs' ),
array( 'is_safe' => array( 'html' ) )
),
new Twig_SimpleFunction(
'ez_legacy_render_css',
array( $this, 'renderLegacyCss' ),
array( 'is_safe' => array( 'html' ) )
),
);
}
/**
* Generates style tags to be embedded in the page
*
* @return string html script and style tags
*/
public function renderLegacyJs()
{
$jsFiles = array();
$jsCodeLines = array();
foreach ( $this->legacyHelper->get( 'js_files' ) as $jsItem )

This comment has been minimized.

Copy link
@lolautruche

lolautruche Jul 22, 2014

Contributor

If js_files is not set, an E_WARNING will be triggered as value will be null.
A default value must be set.

{
// List of items can contain empty elements, path to files or code
if ( !empty( $jsItem ) )
{
if ( isset( $jsItem[4] ) && $this->isFile( $jsItem, '.js' ) )
{
$jsFiles[] = $jsItem;
}
else
{
$jsCodeLines[] = $jsItem;
}
}
}
return $this->environment->render(
$this->jsTemplate,
array(
'js_files' => $jsFiles,
'js_code_lines' => $jsCodeLines
)
);
}
/**
* Generates script tags to be embedded in the page
*
* @return string html script and style tags
*/
public function renderLegacyCss( )
{
$cssFiles = array();
$cssCodeLines = array();
foreach ( $this->legacyHelper->get( 'css_files' ) as $cssItem )

This comment has been minimized.

Copy link
@lolautruche

lolautruche Jul 22, 2014

Contributor

If css_files is not set, an E_WARNING will be triggered as value will be null.
A default value must be set.

{
// List of items can contain empty elements, path to files or code
if ( !empty( $cssItem ) )
{
if ( isset( $cssItem[5] ) && $this->isFile( $cssItem, '.css' ) )
{
$cssFiles[] = $cssItem;
}
else
{
$cssCodeLines[] = $cssItem;
}
}
}
return $this->environment->render(
$this->cssTemplate,
array(
'css_files' => $cssFiles,
'css_code_lines' => $cssCodeLines
)
);
}
/**
* Is the provided item (path or link) a file or code. Based on legacy's rules (ezjscpacker.php)
*
* @param $item string to be tested
* @param $extension string extension of the file
*
* @return bool true if item is a file
*/
private function isFile( $item, $extension )
{
return
strpos( $item, 'http://' ) === 0
|| strpos( $item, 'https://' ) === 0
|| strripos( $item, $extension ) === ( strlen( $item ) - strlen( $extension ) );
}
}

0 comments on commit 2c010c4

Please sign in to comment.
You can’t perform that action at this time.