本文整理汇总了PHP中Symfony\Component\ExpressionLanguage\ExpressionLanguage类的典型用法代码示例。如果您正苦于以下问题:PHP ExpressionLanguage类的具体用法?PHP ExpressionLanguage怎么用?PHP ExpressionLanguage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ExpressionLanguage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getExpressionLanguage
protected function getExpressionLanguage()
{
$language = new ExpressionLanguage();
$language->register('ini', function ($value) {
return $value;
}, function ($arguments, $value) {
return ini_get($value);
});
return $language;
}
示例2: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$api = $this->getApplication()->getApi();
$analysis = $api->getProject($input->getArgument('project-uuid'))->getLastAnalysis();
if (!$analysis) {
$output->writeln('<error>There are no analyses</error>');
return 1;
}
$helper = new DescriptorHelper($api->getSerializer());
$helper->describe($output, $analysis, $input->getOption('format'));
if ('txt' === $input->getOption('format') && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
$output->writeln('');
$output->writeln('Re-run this command with <comment>-v</comment> option to get the full report');
}
if (!($expr = $input->getOption('fail-condition'))) {
return;
}
$el = new ExpressionLanguage();
$counts = array();
foreach ($analysis->getViolations() as $violation) {
if (!isset($counts[$violation->getCategory()])) {
$counts[$violation->getCategory()] = 0;
}
++$counts[$violation->getCategory()];
if (!isset($counts[$violation->getSeverity()])) {
$counts[$violation->getSeverity()] = 0;
}
++$counts[$violation->getSeverity()];
}
$vars = array('analysis' => $analysis, 'counts' => (object) $counts);
if ($el->evaluate($expr, $vars)) {
return 70;
}
}
示例3: testShortCircuitOperatorsCompile
/**
* @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);
}
示例4: postProcessConfigString
private function postProcessConfigString($string, $parameters)
{
$language = new ExpressionLanguage();
$language->register('env', function ($str) {
// This implementation is only needed if you want to compile
// not needed when simply using the evaluator
throw new RuntimeException("The 'env' method is not yet compilable.");
}, function ($arguments, $str, $required = false) {
$res = getenv($str);
if (!$res && $required) {
throw new RuntimeException("Required environment variable '{$str}' is not defined");
}
return $res;
});
preg_match_all('~\\{\\{(.*?)\\}\\}~', $string, $matches);
$variables = array();
//$variables['hello']='world';
foreach ($matches[1] as $match) {
$out = $language->evaluate($match, $variables);
$string = str_replace('{{' . $match . '}}', $out, $string);
}
// Inject parameters for strings between % characters
if (substr($string, 0, 1) == '%' && substr($string, -1, 1) == '%') {
$string = trim($string, '%');
if (!isset($parameters[$string])) {
throw new RuntimeException("Required parameter '{$string}' not defined");
}
$string = $parameters[$string];
}
return $string;
}
示例5: testConstantFunction
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")'));
}
示例6: decide
/**
* @param mixed $context
*
* @return mixed
* @throws \Exception
*/
public function decide($context = null)
{
if (null === $context) {
$context = [];
}
$visitor = new ClosureExpressionVisitor();
foreach ($this->rules as $rule) {
$expression = $rule->getExpression();
if ($expression instanceof Expression) {
if (null === $this->el) {
$this->el = new ExpressionLanguage();
}
$response = $this->el->evaluate($expression, $context);
} else {
$filter = $visitor->dispatch($expression);
$response = $filter($context);
}
if ($response) {
$return = $rule->getReturn();
if (is_callable($return)) {
return call_user_func($return, $context);
}
return $return;
}
}
throw new InvalidRuleException('No rules matched');
}
示例7: evaluate
public function evaluate($data, $expression = null)
{
if ($this->id === null) {
throw new \InvalidArgumentException('Policy not loaded!');
}
$expression = $expression === null ? $this->expression : $expression;
if (!is_array($data)) {
$data = array($data);
}
$context = array();
foreach ($data as $index => $item) {
if (is_numeric($index)) {
// Resolve it to a class name
$ns = explode('\\', get_class($item));
$index = str_replace('Model', '', array_pop($ns));
}
$context[strtolower($index)] = $item;
}
$language = new ExpressionLanguage();
try {
return $language->evaluate($expression, $context);
} catch (\Exception $e) {
throw new Exception\InvalidExpressionException($e->getMessage());
}
}
示例8: registerFunction
/**
* Register a new new ExpressionLanguage function.
*
* @param ExpressionFunctionInterface $function
*
* @return ExpressionEvaluator
*/
public function registerFunction(ExpressionFunctionInterface $function)
{
$this->expressionLanguage->register($function->getName(), $function->getCompiler(), $function->getEvaluator());
foreach ($function->getContextVariables() as $name => $value) {
$this->setContextVariable($name, $value);
}
return $this;
}
示例9: it_evaluates_the_rule_based_on_subject_to_false_when_exception_is_thrown
public function it_evaluates_the_rule_based_on_subject_to_false_when_exception_is_thrown(RuleInterface $rule, RuleSubjectInterface $subject, LoggerInterface $logger, ExpressionLanguage $expression)
{
$rule->getExpression()->shouldBeCalled();
$subject->getSubjectType()->shouldBeCalled();
$expression->evaluate(Argument::type('string'), Argument::type('array'))->willReturn(false);
$logger->error(Argument::type('string'))->shouldBeCalled();
$this->evaluate($rule, $subject)->shouldReturn(false);
}
示例10: evaluateExpression
/**
* @param ContentView $contentView
* @param string $queryParameterValue
*
* @return mixed
*/
private function evaluateExpression(ContentView $contentView, $queryParameterValue)
{
if (substr($queryParameterValue, 0, 2) === '@=') {
$language = new ExpressionLanguage();
return $language->evaluate(substr($queryParameterValue, 2), ['view' => $contentView, 'location' => $contentView->getLocation(), 'content' => $contentView->getContent()]);
} else {
return $queryParameterValue;
}
}
示例11: buildRootResource
/**
* Build root resource.
*
* @return array|\eZ\Publish\Core\REST\Common\Values\Root
*/
public function buildRootResource()
{
$language = new ExpressionLanguage();
$resources = array();
foreach ($this->resourceConfig as $name => $resource) {
$resources[] = new Values\Resource($name, $resource['mediaType'], $language->evaluate($resource['href'], ['router' => $this->router, 'templateRouter' => $this->templateRouter]));
}
return new Root($resources);
}
示例12: match
/**
* {@inheritDoc}
*/
public function match($value, $pattern)
{
$language = new ExpressionLanguage();
preg_match(self::MATCH_PATTERN, $pattern, $matches);
$expressionResult = $language->evaluate($matches[1], array('value' => $value));
if (!$expressionResult) {
$this->error = sprintf("\"%s\" expression fails for value \"%s\".", $pattern, new String($value));
}
return (bool) $expressionResult;
}
示例13: checkCondition
/**
* @see MetaborStd\Statemachine.ConditionInterface::checkCondition()
*/
public function checkCondition($subject, \ArrayAccess $context)
{
$values = $this->values;
$values['subject'] = $subject;
$values['context'] = $context;
return (bool) $this->expressionLanguage->evaluate($this->getExpression(), $values);
}
示例14: handle
public function handle($arg)
{
$expressionDetected = preg_match('/expr\\((.+)\\)/', $arg, $matches);
if (1 !== $expressionDetected) {
throw new NotResolvableValueException($arg);
}
return $this->expressionLanguage->evaluate($matches[1], $this->expressionContext->getData());
}
示例15: checkCondition
/**
* @param Rule $rule
* @param WorkingMemory $workingMemory
*
* @return bool
*/
public function checkCondition(Rule $rule, WorkingMemory $workingMemory)
{
try {
return (bool) $this->expressionLanguage->evaluate($rule->getCondition(), $workingMemory->getAllFacts());
} catch (SyntaxError $e) {
return false;
}
}