Ejemplo n.º 1
1
 protected function getExpressionLanguage()
 {
     $language = new ExpressionLanguage();
     $language->register('ini', function ($value) {
         return $value;
     }, function ($arguments, $value) {
         return ini_get($value);
     });
     return $language;
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }
 /**
  * Registers new function to expression language
  *
  * @param ExpressionFunction $expressionFunction
  *
  * @return void
  */
 public function registerFunction(ExpressionFunction $expressionFunction)
 {
     $functionToCall = $expressionFunction->expressionFunction();
     $this->expressionReader->register($expressionFunction->name(), null, function () use($functionToCall) {
         $arguments = func_get_args();
         array_shift($arguments);
         return $functionToCall($arguments);
     });
 }
 /**
  * Initializes the context for the expression language
  */
 private function initializeContext()
 {
     $this->expressionContext = [];
     // register the date function
     $this->language->register("date", function ($format) {
         return sprintf("date(%s)", $format);
     }, function ($arguments, $format) {
         return date($format);
     });
 }
Ejemplo n.º 5
0
 /**
  * Create a new compiler.
  */
 public function __construct($mobileDetect, $ikimeaBrowser)
 {
     $this->language = new ExpressionLanguage();
     $this->language->register('version_compare', function ($str) {
         return 'version_compare(' . $str . ')';
     }, function ($arguments, $str) {
         return call_user_func_array('version_compare', $arguments);
     });
     $this->variables = ['mobileDetect' => $mobileDetect, 'ikimeaBrowser' => $ikimeaBrowser];
 }
Ejemplo n.º 6
0
 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;
 }
Ejemplo n.º 7
0
 public function create()
 {
     $expressionLanguage = new ExpressionLanguage();
     $expressionLanguage->registerProvider(new StringProvider());
     $expressionLanguage->registerProvider(new FilesystemProvider());
     $expressionLanguage->register('get_config', function () {
         return '';
         // Not supported
     }, function (array $arguments, $name, $default = null) {
         return $this->config->get($name, $default);
     });
     $expressionLanguage->register('date', function () {
         return '';
         // Not supported
     }, function (array $arguments, $format) {
         return date($format);
     });
     return $expressionLanguage;
 }
Ejemplo n.º 8
0
 private function hydrates(ExpressionLanguage $language)
 {
     $language->register('count', function ($value) {
         return sprintf('(is_array(%1$s) ? sizeof(%1$s) : 0)', $value);
     }, function ($arguments, $value) {
         if (!is_array($value)) {
             return 0;
         }
         return sizeof($value);
     });
     $language->register('isMagicMethod', function ($value) {
         return sprintf('(strpos(%1$s, "__") === 0 ', $value);
     }, function ($arguments, $value) {
         if (!$value instanceof ReflectedMethod) {
             return false;
         }
         return strpos($value->getName(), "__") === 0;
     });
 }
 /**
  * @return \Symfony\Component\ExpressionLanguage\ExpressionLanguage
  */
 private function getExpressionLanguage()
 {
     $language = new ExpressionLanguage();
     $language->register('has', function ($str) {
         return sprintf('(in_array(%1$s, scope))', $str);
     }, function ($arguments, $str) {
         return in_array($str, $arguments['scope']);
     });
     return $language;
 }
Ejemplo n.º 10
0
 /**
  * Register function.
  *
  * @param ExpressionLanguage $expressionLanguage Expression language
  */
 public function registerFunction(ExpressionLanguage $expressionLanguage)
 {
     $expressionLanguage->register('p', function ($ids) {
         return sprintf('(purchasable.id in [%1$s])', $ids);
     }, function ($arguments, $ids) {
         $ids = explode(',', $ids);
         $purchasable = $arguments['purchasable'];
         return in_array($purchasable->getId(), $ids);
     });
 }
Ejemplo n.º 11
0
 /**
  * Register function.
  *
  * @param ExpressionLanguage $expressionLanguage Expression language
  */
 public function registerFunction(ExpressionLanguage $expressionLanguage)
 {
     $expressionLanguage->register('m', function ($ids) {
         return sprintf('(purchasable.manufacturer.id in [%1$s])', $ids);
     }, function ($arguments, $ids) {
         $ids = explode(',', $ids);
         $purchasable = $arguments['purchasable'];
         $manufacturer = $purchasable->getManufacturer();
         if (!$manufacturer instanceof ManufacturerInterface) {
             return false;
         }
         return in_array($manufacturer->getId(), $ids);
     });
 }
Ejemplo n.º 12
0
 /**
  * Register function.
  *
  * @param ExpressionLanguage $expressionLanguage Expression language
  */
 public function registerFunction(ExpressionLanguage $expressionLanguage)
 {
     $expressionLanguage->register('c', function ($ids) {
         return sprintf('(c(%1$s))', $ids);
     }, function ($arguments, $ids) {
         $ids = explode(',', $ids);
         $purchasable = $arguments['purchasable'];
         $categoryIds = $purchasable->getCategories()->map(function (CategoryInterface $category) {
             return $category->getId();
         })->toArray();
         $intersection = array_intersect($categoryIds, $ids);
         return !empty($intersection);
     });
 }
 private function addFunction(ExpressionLanguage $expressionLanguage, ExpressionFunction $function)
 {
     $expressionLanguage->register($function->getName(), $function->getCompiler(), $function->getEvaluator());
 }