PHP code example of redgnar / whisky

1. Go to this page and download the library: Download redgnar/whisky library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

redgnar / whisky example snippets


use PhpParser\ParserFactory;
...

$functionRepository = new FunctionRepository();
$builder = new BasicBuilder(
    new PhpParser((new ParserFactory())->create(ParserFactory::ONLY_PHP7)),
    new VariableHandler(),
    new FunctionHandler($functionRepository)
);
$builder->addExtension(new BasicSecurity());
$executor = new BasicExecutor($functionRepository);
$variables = new BasicScope(['collection' => ['a', 'b']]);
$functionRepository->addFunction('testIt', function (string $text) {return $text; });
$script = $builder->build(
            <<<'EOD'
    $result = [];
    foreach ($collection as $item) {
        if ("b" === $item) {
            continue;
        }
        $result[] = testIt($item."aaa4bbb");
    }
EOD
);
$executor->execute($script, $variables);
var_dump($variables->get('result'));