PhpSandbox\PHPSandbox::prepend PHP Méthode

prepend() public méthode

Prepend trusted code
public prepend ( string | callable $code )
$code string | callable String or callable of trusted $code to prepend to generated code
    public function prepend($code)
    {
        if (!$code) {
            return $this;
        }
        $code = $this->disassemble($code);
        if ($this->auto_whitelist_trusted_code) {
            $this->autoWhitelist($code);
        }
        $this->prepended_code .= substr($code, 6) . "\r\n";
        //remove opening php tag
        return $this;
    }

Usage Example

 /**
  * Test whether sandbox autowhitelists trusted code
  */
 public function testAutowhitelistTrustedCode()
 {
     $this->sandbox->prepend(function () {
         function test2()
         {
             return 'Hello World!';
         }
     });
     $this->assertEquals('Hello World!', $this->sandbox->execute(function () {
         return test2();
     }));
     $this->setUp();
     //reset
 }
PHPSandbox