PhpSandbox\PHPSandbox::prepare PHP Method

prepare() public method

This function validates your code and automatically whitelists it according to your specified configuration
public prepare ( callable $code, boolean $skip_validation = false ) : string
$code callable The callable to prepare for execution
$skip_validation boolean Boolean flag to indicate whether the sandbox should skip validation. Default is false.
return string The generated code (this can also be accessed via $sandbox->generated_code)
    public function prepare($code, $skip_validation = false)
    {
        $this->prepare_time = microtime(true);
        if ($this->allow_constants && !$this->isDefinedFunc('define') && ($this->hasWhitelistedFuncs() || !$this->hasBlacklistedFuncs())) {
            $this->whitelistFunc('define');
            //makes no sense to allow constants if you can't define them!
        }
        if (!$skip_validation) {
            $this->validate($code);
        }
        static::$sandboxes[$this->name] = $this;
        $this->generated_code = $this->prepareNamespaces() . $this->prepareAliases() . $this->prepareConsts() . "\r\n" . '$closure = function(){' . "\r\n" . $this->prepareVars() . $this->prepended_code . ($skip_validation ? $code : $this->prepared_code) . $this->appended_code . "\r\n" . '};' . "\r\n" . 'if(method_exists($closure, "bindTo")){ $closure = $closure->bindTo(null); }' . "\r\n" . 'return $closure();';
        usleep(1);
        //guarantee at least some time passes
        $this->prepare_time = microtime(true) - $this->prepare_time;
        return $this->generated_code;
    }
PHPSandbox