function it_should_parse_expression_with_parameters(Request $request, ExpressionLanguage $expression)
 {
     $request->get('foo')->willReturn('bar');
     $request->get('baz')->willReturn(1);
     $expression->evaluate('service("demo_service")')->willReturn('demo_object');
     $this->parseRequestValues(['factory' => ['method' => 'createByParameter', 'arguments' => ['expr:service("demo_service")']]], $request)->shouldReturn(['factory' => ['method' => 'createByParameter', 'arguments' => ['demo_object']]]);
     $expression->evaluate('service("demo_service")->getWith("bar")')->willReturn('demo_object->getWith("bar")');
     $this->parseRequestValues(['factory' => ['method' => 'createByParameter', 'arguments' => ['expr:service("demo_service")->getWith($foo)']]], $request)->shouldReturn(['factory' => ['method' => 'createByParameter', 'arguments' => ['demo_object->getWith("bar")']]]);
     $expression->evaluate('service("demo_service")->getWith("bar", 1)')->willReturn('demo_object->getWith("bar", 1)');
     $this->parseRequestValues(['factory' => ['method' => 'createByParameter', 'arguments' => ['expr:service("demo_service")->getWith($foo, $baz)']]], $request)->shouldReturn(['factory' => ['method' => 'createByParameter', 'arguments' => ['demo_object->getWith("bar", 1)']]]);
     $expression->evaluate('service("demo_service")->getWith("bar")->andGet(1)')->willReturn('demo_object->getWith("bar")->andGet(1)');
     $this->parseRequestValues(['factory' => ['method' => 'createByParameter', 'arguments' => ['expr:service("demo_service")->getWith($foo)->andGet($baz)']]], $request)->shouldReturn(['factory' => ['method' => 'createByParameter', 'arguments' => ['demo_object->getWith("bar")->andGet(1)']]]);
 }
Esempio n. 2
0
 /**
  * @param array   $parameters
  * @param Request $request
  *
  * @return array
  */
 public function parseRequestValues(array $parameters, Request $request)
 {
     foreach ($parameters as $key => $value) {
         if (is_array($value)) {
             $parameters[$key] = $this->parseRequestValues($value, $request);
         }
         if (is_string($value) && 0 === strpos($value, '$')) {
             $parameterName = substr($value, 1);
             $parameters[$key] = $request->get($parameterName);
         }
         if (is_string($value) && 0 === strpos($value, 'expr:')) {
             $parameters[$key] = $this->expression->evaluate(substr($value, 5));
         }
     }
     return $parameters;
 }
Esempio n. 3
0
 /**
  * @param array   $parameters
  * @param Request $request
  *
  * @return array
  */
 public function parse(array $parameters, Request $request)
 {
     if (!isset($parameterNames)) {
         $parameterNames = array();
     }
     foreach ($parameters as $key => $value) {
         if (is_array($value)) {
             list($parameters[$key], $parameterNames[$key]) = $this->parse($value, $request);
         }
         if (is_string($value) && 0 === strpos($value, '$')) {
             $parameterName = substr($value, 1);
             $parameters[$key] = $request->get($parameterName);
             $parameterNames[$key] = $parameterName;
         }
         if (is_string($value) && 0 === strpos($value, 'expr:')) {
             $parameters[$key] = $this->expression->evaluate(substr($value, 5));
         }
     }
     return array($parameters, $parameterNames);
 }
 /**
  * {@inheritdoc}
  */
 protected function registerFunctions()
 {
     parent::registerFunctions();
     $this->register('getCurrentUser', function ($arg) {
         return sprintf('$this->get("security.token_storage")->getToken()->getUser()', $arg);
     }, function (array $variables) {
         return $variables['container']->get('security.token_storage')->getToken()->getUser();
     });
     $this->register('getQueryParam', function ($limitKey, $default) {
         return sprintf('$this->get("request")->get(%s, %s)', $limitKey, $default);
     }, function (array $variables, $limitKey, $default) {
         return $variables['container']->get('request')->get($limitKey, $default);
     });
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function parseRequestValues(array $parameters, Request $request)
 {
     foreach ($parameters as $key => $value) {
         if (is_array($value)) {
             $parameters[$key] = $this->parseRequestValues($value, $request);
         }
         if (is_string($value) && 0 === strpos($value, '$')) {
             $parameterName = substr($value, 1);
             $parameters[$key] = $request->get($parameterName);
         }
         if (is_string($value) && 0 === strpos($value, 'expr:')) {
             $service = substr($value, 5);
             if (preg_match_all('/(\\$\\w+)\\W/', $service, $match)) {
                 foreach ($match[1] as $parameterName) {
                     $parameter = $request->get(substr(trim($parameterName), 1));
                     $parameter = is_string($parameter) ? sprintf('"%s"', $parameter) : $parameter;
                     $service = str_replace($parameterName, $parameter, $service);
                 }
             }
             $parameters[$key] = $this->expression->evaluate($service);
         }
     }
     return $parameters;
 }