/** * @dataProvider shortCircuitProviderCompile */ public function testShortCircuitOperatorsCompile($expression, array $names, $expected) { $result = null; $expressionLanguage = new ExpressionLanguage(); eval(sprintf('$result = %s;', $expressionLanguage->compile($expression, $names))); $this->assertSame($expected, $result); }
public function testConstantFunction() { $expressionLanguage = new ExpressionLanguage(); $this->assertEquals(PHP_VERSION, $expressionLanguage->evaluate('constant("PHP_VERSION")')); $expressionLanguage = new ExpressionLanguage(); $this->assertEquals('constant("PHP_VERSION")', $expressionLanguage->compile('constant("PHP_VERSION")')); }
public function testCompile() { $object = new \StdClass(); $linkHelperMock = $this->mockHelper('/foo', $object, 'self', false); $expressionLanguage = new ExpressionLanguage(); $expressionEvaluator = new ExpressionEvaluator($expressionLanguage); $expressionEvaluator->registerFunction(new LinkExpressionFunction($linkHelperMock)); $compiledExpression = $expressionLanguage->compile('link(object, "self", false)', array('object', 'link_helper')); // setup variables for expression eval $object = $object; $link_helper = $linkHelperMock; $this->assertEquals('/foo', eval(sprintf('return %s;', $compiledExpression))); }
/** * @param Request $request * @param array $matches * * @return \React\EventLoop\Timer\Timer|\React\EventLoop\Timer\TimerInterface */ protected function evalCode(Request $request, array $matches = []) { $request->deleteMessage($request->getMessage())->then(function () use($request, $matches) { $request->reply('Executing Code')->then(function (Message $message) use($request, $matches) { // Lets set some local variables for the eval $client = $this->getDiscord(); $container = $this->container; $server = $request->getServer(); $author = $request->getAuthor(); $channel = $request->getChannel(); $self = $this; $start = microtime(true); $_____responseContent = <<<'EOF' ```php # Executed the following code in %d ms %s # Resulted in the following: %s ``` EOF; $sprintf = []; $sprintf[] = $matches[2]; try { if ($matches[1] === ' --raw') { $response = eval($matches[2]); var_dump($response); } else { $language = new ExpressionLanguage(); $sprintf[0] = $language->compile($matches[2], array_keys(get_defined_vars())) . ' (' . $matches[2] . ')'; $response = @$language->evaluate($matches[2], get_defined_vars()); } } catch (\Exception $e) { $sprintf[] = $e->getMessage() . ' on line ' . $e->getLine() . ' in file ' . $e->getFile(); $sprintf[] = (microtime(true) - $start) * 1000; return $request->updateMessage($message, sprintf($_____responseContent, $sprintf[2], $sprintf[0], $sprintf[1])); } if (is_array($response) || is_object($response)) { $response = json_decode($response, true); } $sprintf[] = $response; $sprintf[] = (microtime(true) - $start) * 1000; $request->updateMessage($message, sprintf($_____responseContent, $sprintf[2], $sprintf[0], $sprintf[1])); }); }); }
protected function execute(InputInterface $input, OutputInterface $output) { $expressionLanguage = new ExpressionLanguage(); $expressionLanguage->registerProvider(new StringExpressionLanguageProvider()); var_dump($expressionLanguage->evaluate('1+2')); $compiled = $expressionLanguage->compile('"TEST"~" "~"aaa"'); var_dump($compiled); $testClass = new TestClass(); $testClass->aa = 123; var_dump($expressionLanguage->evaluate('test.aa~" "~test.hi()', ['test' => $testClass])); $language = new ExpressionLanguage(null, [new StringExpressionLanguageProvider()]); var_dump($language->evaluate('lowercase("AAA")')); eval('var_dump(' . $language->compile('lowercase("AAA")') . ');'); $expr = new Expression('(1+2)*test.aa'); $parsedExpression = $language->parse($expr, ['test']); var_dump($parsedExpression); var_dump($language->evaluate($parsedExpression, ['test' => $testClass])); $serializedExpression = new SerializedParsedExpression('(1+2)*test.aa', serialize($parsedExpression->getNodes())); var_dump($language->evaluate($serializedExpression, ['test' => $testClass])); }
public function testCachingWithDifferentNamesOrder() { $cacheMock = $this->getMock('Symfony\\Component\\ExpressionLanguage\\ParserCache\\ParserCacheInterface'); $expressionLanguage = new ExpressionLanguage($cacheMock); $savedParsedExpressions = array(); $cacheMock->expects($this->exactly(2))->method('fetch')->will($this->returnCallback(function ($key) use(&$savedParsedExpressions) { return isset($savedParsedExpressions[$key]) ? $savedParsedExpressions[$key] : null; })); $cacheMock->expects($this->exactly(1))->method('save')->will($this->returnCallback(function ($key, $expression) use(&$savedParsedExpressions) { $savedParsedExpressions[$key] = $expression; })); $expression = 'a + b'; $expressionLanguage->compile($expression, array('a', 'B' => 'b')); $expressionLanguage->compile($expression, array('B' => 'b', 'a')); }
public function testCachingWithDifferentNamesOrder() { $cacheMock = $this->getMock('Psr\\Cache\\CacheItemPoolInterface'); $cacheItemMock = $this->getMock('Psr\\Cache\\CacheItemInterface'); $expressionLanguage = new ExpressionLanguage($cacheMock); $savedParsedExpressions = array(); $cacheMock->expects($this->exactly(2))->method('getItem')->with('a%20%2B%20b%2F%2Fa%7CB%3Ab')->willReturn($cacheItemMock); $cacheItemMock->expects($this->exactly(2))->method('get')->will($this->returnCallback(function () use(&$savedParsedExpression) { return $savedParsedExpression; })); $cacheItemMock->expects($this->exactly(1))->method('set')->with($this->isInstanceOf(ParsedExpression::class))->will($this->returnCallback(function ($parsedExpression) use(&$savedParsedExpression) { $savedParsedExpression = $parsedExpression; })); $cacheMock->expects($this->exactly(1))->method('save')->with($cacheItemMock); $expression = 'a + b'; $expressionLanguage->compile($expression, array('a', 'B' => 'b')); $expressionLanguage->compile($expression, array('B' => 'b', 'a')); }
/** * Set the expression. * * @param string $expression The expression. * * @return $this */ public function setExpression($expression) { $this->expression = $expression; $this->compiled = 'return ' . $this->expressionLanguage->compile($expression, array('transition', 'item', 'entity', 'entityId', 'context', 'errorCollection')) . ';'; return $this; }
/** * Compile the filter rules. * * @param FilterRules $filterRules The filter rules. * * @return string */ public function compile(FilterRules $filterRules) { $expression = $this->parse($filterRules); return $this->language->compile($expression, array_keys($this->variables)); }
/** * {@inheritdoc} * * @SuppressWarnings(PHPMD.UnusedLocalVariable) * @SuppressWarnings(PHPMD.EvalExpression) */ public function accepts(ItemInterface $item) { if (null === $this->compiled) { $language = new ExpressionLanguage(); $expression = $this->getExpression(); $this->compiled = sprintf('return %s;', $language->compile($expression, array('item', 'variables'))); } $variables = $this->variables; // @codingStandardsIgnoreStart return eval($this->compiled); // @codingStandardsIgnoreEnd }