Skip to content
Browse files

Merge pull request #1180 from amenophis/expression

Use expressionLanguage into ParametersParser
  • Loading branch information...
2 parents c918755 + 39d6e52 commit f80918715b7474681ecec9386e51c852b418a626 @pjedrzejewski pjedrzejewski committed Mar 8, 2014
View
6 src/Sylius/Bundle/ResourceBundle/Controller/ParametersParser.php
@@ -11,7 +11,7 @@
namespace Sylius\Bundle\ResourceBundle\Controller;
-use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
+use Sylius\Bundle\ResourceBundle\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PropertyAccess\PropertyAccess;
@@ -48,6 +48,10 @@ public function parse(array &$parameters, Request $request)
if (is_string($value) && 0 === strpos($value, '$')) {
$parameters[$key] = $request->get(substr($value, 1));
}
+
+ if (is_string($value) && 0 === strpos($value, 'expr:')) {
+ $parameters[$key] = $this->expression->evaluate(substr($value, 5));
+ }
}
return $parameters;
View
64 src/Sylius/Bundle/ResourceBundle/ExpressionLanguage/ExpressionLanguage.php
@@ -0,0 +1,64 @@
+<?php
+
+/*
+ * This file is part of the Sylius package.
+ *
+ * (c) Paweł Jędrzejewski
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sylius\Bundle\ResourceBundle\ExpressionLanguage;
+
+use Symfony\Component\DependencyInjection\ContainerAwareInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\ExpressionLanguage\ExpressionLanguage as BaseExpressionLanguage;
+
+/**
+ * Adds some function to the default ExpressionLanguage.
+ * @author Jérémy Leherpeur <jeremy@leherpeur.net>
+ */
+class ExpressionLanguage extends BaseExpressionLanguage implements ContainerAwareInterface
+{
+ /**
+ * @var ContainerInterface
+ */
+ protected $container;
+
+ /**
+ * Sets the Container.
+ *
+ * @param ContainerInterface|null $container A ContainerInterface instance or null
+ *
+ * @api
+ */
+ public function setContainer(ContainerInterface $container = null)
+ {
+ $this->container = $container;
+ }
+
+ public function evaluate($expression, $values = array())
+ {
+ $values["container"] = $this->container;
+
+ return parent::evaluate($expression, $values);
+ }
+
+ protected function registerFunctions()
+ {
+ parent::registerFunctions();
+
+ $this->register('service', function ($arg) {
+ return sprintf('$this->get(%s)', $arg);
+ }, function (array $variables, $value) {
+ return $variables['container']->get($value);
+ });
+
+ $this->register('parameter', function ($arg) {
+ return sprintf('$this->getParameter(%s)', $arg);
+ }, function (array $variables, $value) {
+ return $variables['container']->getParameter($value);
+ });
+ }
+}
View
8 src/Sylius/Bundle/ResourceBundle/Resources/config/container/services.xml
@@ -19,7 +19,7 @@
<parameters>
<parameter key="sylius.controller.configuration_factory.class">Sylius\Bundle\ResourceBundle\Controller\ConfigurationFactory</parameter>
<parameter key="sylius.controller.parameters_parser.class">Sylius\Bundle\ResourceBundle\Controller\ParametersParser</parameter>
- <parameter key="sylius.expression_language.class">Symfony\Component\ExpressionLanguage\ExpressionLanguage</parameter>
+ <parameter key="sylius.expression_language.class">Sylius\Bundle\ResourceBundle\ExpressionLanguage\ExpressionLanguage</parameter>
<parameter key="sylius.twig.resource.class">Sylius\Bundle\ResourceBundle\Twig\SyliusResourceExtension</parameter>
<parameter key="sylius.form.type.entity_to_identifier.class">Sylius\Bundle\ResourceBundle\Form\Type\EntityToIdentifierType</parameter>
@@ -35,7 +35,11 @@
<service id="sylius.controller.parameters_parser" class="%sylius.controller.parameters_parser.class%">
<argument type="service" id="sylius.expresssion_language" />
</service>
- <service id="sylius.expresssion_language" class="%sylius.expression_language.class%" />
+ <service id="sylius.expresssion_language" class="%sylius.expression_language.class%">
+ <call method="setContainer">
+ <argument type="service" id="service_container" />
+ </call>
+ </service>
<service id="sylius.twig.resource" class="%sylius.twig.resource.class%">
<argument type="service" id="service_container" />

0 comments on commit f809187

Please sign in to comment.
Something went wrong with that request. Please try again.