<commit_before><?php

namespace RubtsovAV\YandexWordstatParser\Browser\ReactPhantomJs;

use Evenement\EventEmitter;
use React\ChildProcess\Process as ReactProcess;
use React\EventLoop\LoopInterface as EventLoopInterface;

class Process extends EventEmitter
{
	const SCRIPT = 'scripts/index.js';

	protected $process;

	public function __construct($phantomJs, $options = [])
	{
		$command = $this->buildCommand($phantomJs, $options);
		$workingDirectory = realpath(__DIR__ . '/scripts');
		$this->process = new ReactProcess($command, $workingDirectory);
	}

	protected function buildCommand($phantomJs, $options = [])
	{
		$cmd = escapeshellcmd($phantomJs);
		$script = escapeshellarg(__DIR__ . '/' . static::SCRIPT);
		$optionsString = $this->buildOptionsString($options);
		return "$cmd $optionsString $script";
	}

	protected function buildOptionsString($options = [])
	{
		$optionsString = [];
		foreach ($options as $name => $value) {
			switch (gettype($value)) {
				case 'NULL':
					$optionsString[] = escapeshellarg($name);
					break;

				case 'boolean':
					$optionsString[] = escapeshellarg($name) . '=' . ($value ? 'true' : 'false');
					break;
				
				default:
					$optionsString[] = escapeshellarg($name) . '=' . escapeshellarg($value);
					break;
			}
		}
		return implode(' ', $optionsString);
	}

	public function start(EventLoopInterface $loop)
	{
		$this->process->start($loop);

		$this->process->on('exit', function ($code) {
			$this->emit('exit', [$code]);
		});

		$this->process->stderr->on('data', function ($data) {
			$this->emit('error', [$data]);
		});

		$parser = new MessageParser();
        $parser->on('message', function(Message $message) {
        	$this->emit('message', [$message]);
        });
        $listener = [$parser, 'feed'];
		$this->process->stdout->on('data', $listener);
	}

	public function stop() 
	{
		$this->process->terminate();
		
		$message = new Message('terminate');
		$this->sendMessage($message);
	}

	public function sendMessage(Message $message)
	{
		$this->process->stdin->write($message->encode());
	}
}<commit_msg>Fixed: sh zombi proxies<commit_after><?php

namespace RubtsovAV\YandexWordstatParser\Browser\ReactPhantomJs;

use Evenement\EventEmitter;
use React\ChildProcess\Process as ReactProcess;
use React\EventLoop\LoopInterface as EventLoopInterface;

class Process extends EventEmitter
{
	const SCRIPT = 'scripts/index.js';

	protected $process;

	public function __construct($phantomJs, $options = [])
	{
		$command = $this->buildCommand($phantomJs, $options);
		$workingDirectory = realpath(__DIR__ . '/scripts');
		$this->process = new ReactProcess($command, $workingDirectory);
	}

	protected function buildCommand($phantomJs, $options = [])
	{
		$cmd = escapeshellcmd($phantomJs);
		$script = escapeshellarg(__DIR__ . '/' . static::SCRIPT);
		$optionsString = $this->buildOptionsString($options);
		return "exec $cmd $optionsString $script";
	}

	protected function buildOptionsString($options = [])
	{
		$optionsString = [];
		foreach ($options as $name => $value) {
			switch (gettype($value)) {
				case 'NULL':
					$optionsString[] = escapeshellarg($name);
					break;

				case 'boolean':
					$optionsString[] = escapeshellarg($name) . '=' . ($value ? 'true' : 'false');
					break;
				
				default:
					$optionsString[] = escapeshellarg($name) . '=' . escapeshellarg($value);
					break;
			}
		}
		return implode(' ', $optionsString);
	}

	public function start(EventLoopInterface $loop)
	{
		$this->process->start($loop);

		$this->process->on('exit', function ($code) {
			$this->emit('exit', [$code]);
		});

		$this->process->stderr->on('data', function ($data) {
			$this->emit('error', [$data]);
		});

		$parser = new MessageParser();
        $parser->on('message', function(Message $message) {
        	$this->emit('message', [$message]);
        });
        $listener = [$parser, 'feed'];
		$this->process->stdout->on('data', $listener);
	}

	public function stop() 
	{
		$this->process->terminate();
		
		$message = new Message('terminate');
		$this->sendMessage($message);
	}

	public function sendMessage(Message $message)
	{
		$this->process->stdin->write($message->encode());
	}
}
{'fraction_non_alphanumeric': 0.17871629682653303, 'fraction_numerical': 0.0, 'mean_word_length': 4.220423412204235, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '4497550', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1294, 'n_words': 252}