<commit_before><?php namespace Codeception\Extension;

use Codeception\Configuration;
use Codeception\Exception\Extension as ExtensionException;
use Codeception\Platform\Extension;

/**
 * Class DrushRunserver
 * @package Codeception\Extension
 */
class DrushRunserver extends Extension
{
    /**
     * @var array
     */
    static $events = [
      'suite.before' => 'beforeSuite'
    ];

    /**
     * @var resource
     */
    private $resource;


    /**
     * @var
     */
    private $pipes;

    /**
     *  Construct a DrushRunserver instance.
     */
    public function __construct($config, $options)
    {
        // ToDo: Add in a config option to allow people to override this should they wish to try playing with PHP 5.3/CGI.
        if (version_compare(PHP_VERSION, '5.4', '<')) {
            throw new ExtensionException($this, 'Requires PHP 5.4 or above.');
        }

        parent::__construct($config, $options);
    }

    /**
     * Get the Drupal root directory.
     *
     * @return string
     *   The root directory of the Drupal installation.
     */
    private function getDrupalRoot()
    {
        // We can't get getcwd() as a default parameter, so this will have to do.
        if (is_null($this->config['root'])) {
            return codecept_root_dir();
        } else {
            // If a user has passed in a path to their Drupal root, then we'll still need to append the current working
            // directory to it.
            return codecept_root_dir($this->config['root']);
        }
    }

    /**
     * Build up the Drush command to run the server with.
     */
    private function getCommand()
    {
        // ToDo: Make this more configurable.
        // ToDo: Somehow find out which Drush the user is using as the commands are different.
        return 'drush runserver -r ' . $this->getDrupalRoot();
    }

    /**
     * Get a descriptorspec ready to pass to proc_open().
     *
     * @see proc_open()
     * @see startServer()
     *
     * @return array
     *   An array that can be passed to proc_open to be used as a descriptor spec.
     */
    private function getDescriptorSpec()
    {
        return [
          ['pipe', 'r'],
          ['file', Configuration::logDir() . 'drush.runserver.output.txt', 'w'],
          ['file', Configuration::logDir() . 'drush.runserver.errors.txt', 'a']
        ];
    }

    /**
     * Start the Drush server.
     */
    private function startServer()
    {
        if ($this->resource !== null) {
            return;
        }

        $this->writeln('Starting Drush server...');

        // Get the actual command and a descriptor spec to pass to proc_open().
        $command = $this->getCommand();
        $descriptorSpec = $this->getDescriptorSpec();

        // Start the process.
        $this->resource = proc_open($command, $descriptorSpec, $this->pipes, null, null, ['bypass_shell' => true]);

        // Check to ensure the process is actually running.
        if (!is_resource($this->resource)) {
            throw new ExtensionException($this, 'Failed to start server.');
        }
        if (!proc_get_status($this->resource)['running']) {
            proc_close($this->resource);
            throw new ExtensionException($this, 'Failed to start server.');
        }

        // If it is, then let the user know.
        $this->writeln('Started Drush server.');
    }

    /**
     * Stop the Drush server.
     */
    private function stopServer()
    {
        if ($this->resource !== null) {
            $this->writeln('Stopping Drush server...');
            foreach ($this->pipes AS $pipe) {
                if (is_resource($pipe)) {
                    fclose($pipe);
                }
            }
            proc_terminate($this->resource, 2);
            $this->writeln('Stopped Drush server.');
            unset($this->resource);
        }
    }

    /**<commit_msg>Added configurable Drush binary path.<commit_after><?php namespace Codeception\Extension;

use Codeception\Configuration;
use Codeception\Exception\Extension as ExtensionException;
use Codeception\Platform\Extension;

/**
 * Class DrushRunserver
 * @package Codeception\Extension
 */
class DrushRunserver extends Extension
{
    /**
     * @var array
     */
    static $events = [
      'suite.before' => 'beforeSuite'
    ];

    /**
     * @var resource
     */
    private $resource;


    /**
     * @var
     */
    private $pipes;

    /**
     * @var string
     */
    private $drushBinary = 'drush';

    /**
     *  Construct a DrushRunserver instance.
     */
    public function __construct($config, $options)
    {
        // ToDo: Add in a config option to allow people to override this should they wish to try playing with PHP 5.3/CGI.
        if (version_compare(PHP_VERSION, '5.4', '<')) {
            throw new ExtensionException($this, 'Requires PHP 5.4 or above.');
        }

        parent::__construct($config, $options);

        // Allow a configurable path to Drush in case it's not installed system-wide.
        if (is_null($this->config['drushBinary'])) {
            $this->drushBinary = $this->config['drushBinary'];
        }
    }

    /**
     * Get the Drupal root directory.
     *
     * @return string
     *   The root directory of the Drupal installation.
     */
    private function getDrupalRoot()
    {
        // We can't get getcwd() as a default parameter, so this will have to do.
        if (is_null($this->config['root'])) {
            return codecept_root_dir();
        } else {
            // If a user has passed in a path to their Drupal root, then we'll still need to append the current working
            // directory to it.
            return codecept_root_dir($this->config['root']);
        }
    }

    /**
     * Build up the Drush command to run the server with.
     */
    private function getCommand()
    {
        // ToDo: Make this more configurable.
        // ToDo: Somehow find out which Drush the user is using as the commands are different.
        return $this->drushBinary . ' runserver -r ' . $this->getDrupalRoot();
    }

    /**
     * Get a descriptorspec ready to pass to proc_open().
     *
     * @see proc_open()
     * @see startServer()
     *
     * @return array
     *   An array that can be passed to proc_open to be used as a descriptor spec.
     */
    private function getDescriptorSpec()
    {
        return [
          ['pipe', 'r'],
          ['file', Configuration::logDir() . 'drush.runserver.output.txt', 'w'],
          ['file', Configuration::logDir() . 'drush.runserver.errors.txt', 'a']
        ];
    }

    /**
     * Start the Drush server.
     */
    private function startServer()
    {
        if ($this->resource !== null) {
            return;
        }

        $this->writeln('Starting Drush server...');

        // Get the actual command and a descriptor spec to pass to proc_open().
        $command = $this->getCommand();
        $descriptorSpec = $this->getDescriptorSpec();

        // Start the process.
        $this->resource = proc_open($command, $descriptorSpec, $this->pipes, null, null, ['bypass_shell' => true]);

        // Check to see if $resource is actually a resource.
        if (!is_resource($this->resource)) {
            throw new ExtensionException($this, 'Failed to start server.');
        }

        // Check to ensure the process is actually running.
        if (!proc_get_status($this->resource)['running']) {
            proc_close($this->resource);
            throw new ExtensionException($this, 'Failed to start server.');
        }

        $this->writeln('Started Drush server.');
    }

    /**
     * Stop the Drush server.
     */
    private function stopServer()
    {
        if ($this->resource !== null) {
            $this->writeln('Stopping Drush server...');
            foreach ($this->pipes AS $pipe) {
                if (is_resource($pipe)) {
                    fclose($pipe);
                }
            }
            proc_terminate($this->resource, 2);
            $this->writeln('Stopped Drush server.');
            unset($this->resource);
        }
    }

    /**
{'fraction_non_alphanumeric': 0.1268418467583497, 'fraction_numerical': 0.001719056974459725, 'mean_word_length': 1.951086956521739, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, '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': '123917', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2225, 'n_words': 670}