for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* @author Gerard van Helden <[email protected]>
* @copyright Zicht Online <http://zicht.nl>
*/
namespace Zicht\Tool\Script;
use Zicht\Tool\Util;
use Zicht\Tool\Script\Node\Node;
* Wraps the tokenization, parsing and compiling into one convenience class.
class Compiler
{
* Constructor.
*
* @param ParserInterface $parser
* @param TokenizerInterface $tokenizer
public function __construct($parser = null, $tokenizer = null)
$this->tokenizer = (null === $tokenizer ? new Tokenizer() : $tokenizer);
tokenizer
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
$this->parser = (null === $parser ? new Parser() : $parser);
parser
}
* Parses the raw string input and returns the resulting root node.
* @param string $input
* @return Node
public function parse($input)
if (strlen($input) == 0) {
return null;
return $this->parser->parse(new TokenStream($this->tokenizer->getTokens($input)));
* Compile an input string to PHP code
* @return string
public function compile($input)
try {
$buffer = new Buffer();
$this->parse($input)->compile($buffer);
$code = $buffer->getResult();
return $code;
} catch (\UnexpectedValueException $e) {
throw new \UnexpectedValueException('Error while compiling input: ' . Util::toPhp($input), 0, $e);
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: