<commit_before><?php
namespace Christiaan\PhpSandbox\Child;
 
use Christiaan\PhpSandbox\RpcProtocol;

class SandboxChild
{
    private $protocol;
    private $parent;

    public function __construct(RpcProtocol $protocol)
    {
        $this->protocol = $protocol;
        $this->setupErrorHandlers();
        $this->parent = new SandboxParent($protocol);
        $this->setupCallbacks();
        $this->setupOutputBuffering();
    }

    private function setupCallbacks()
    {
        $parent = $this->parent;
        $data = array();
        $this->protocol->registerCallback(
            'execute',
            function ($code) use ($parent, &$data) {
                extract($data);
                return eval($code);
            }
        );
        $this->protocol->registerCallback(
            'assignVar',
            function ($key, $value) use (&$data) {
                $data[$key] = $value;
            }
        );
        $this->protocol->registerCallback(
            'assignObject',
            function ($key) use (&$data, $parent) {
                $data[$key] = new SandboxObjectProxy($parent, $key);
            }
        );
    }

    private function setupErrorHandlers()
    {
        $protocol = $this->protocol;
        $exceptionHandler = function (\Exception $exception) use ($protocol) {
            $protocol->sendError($exception->getMessage());
        };
        set_exception_handler($exceptionHandler);

        $errorHandler = function ($errno, $errstr, $errfile, $errline) use ($exceptionHandler) {
            $exceptionHandler(new \ErrorException($errstr, $errno, 0, $errfile, $errline));
        };
        set_error_handler($errorHandler);
    }

    private function setupOutputBuffering()
    {
        $protocol = $this->protocol;
        // see php.net/ob_start about chunksize
        ob_start(
            function ($output) use ($protocol) {
                $protocol->sendCall('output', array($output));
                return '';
            },<commit_msg>Better comment explaining the needed hack<commit_after><?php
namespace Christiaan\PhpSandbox\Child;
 
use Christiaan\PhpSandbox\RpcProtocol;

class SandboxChild
{
    private $protocol;
    private $parent;

    public function __construct(RpcProtocol $protocol)
    {
        $this->protocol = $protocol;
        $this->setupErrorHandlers();
        $this->parent = new SandboxParent($protocol);
        $this->setupCallbacks();
        $this->setupOutputBuffering();
    }

    private function setupCallbacks()
    {
        $parent = $this->parent;
        $data = array();
        $this->protocol->registerCallback(
            'execute',
            function ($code) use ($parent, &$data) {
                extract($data);
                return eval($code);
            }
        );
        $this->protocol->registerCallback(
            'assignVar',
            function ($key, $value) use (&$data) {
                $data[$key] = $value;
            }
        );
        $this->protocol->registerCallback(
            'assignObject',
            function ($key) use (&$data, $parent) {
                $data[$key] = new SandboxObjectProxy($parent, $key);
            }
        );
    }

    private function setupErrorHandlers()
    {
        $protocol = $this->protocol;
        $exceptionHandler = function (\Exception $exception) use ($protocol) {
            $protocol->sendError($exception->getMessage());
        };
        set_exception_handler($exceptionHandler);

        $errorHandler = function ($errno, $errstr, $errfile, $errline) use ($exceptionHandler) {
            $exceptionHandler(new \ErrorException($errstr, $errno, 0, $errfile, $errline));
        };
        set_error_handler($errorHandler);
    }

    private function setupOutputBuffering()
    {
        $protocol = $this->protocol;
        ob_start(
            function ($output) use ($protocol) {
                $protocol->sendCall('output', array($output));
                return '';
            },
            // Prior to PHP 5.4.0, the value 1 was a special case value that set the chunk size to 4096 bytes.
{'fraction_non_alphanumeric': 0.1323923095643709, 'fraction_numerical': 0.002433682161109759, 'mean_word_length': 2.0421909696521094, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5370', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1073, 'n_words': 231}