<commit_before><?php

namespace UseAllFive\DoctrineWebConsole;

use Silex\Application;
use Silex\ControllerProviderInterface;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\HttpFoundation\Request;
use UseAllFive\DoctrineWebConsole\Console\Tools\ConsoleRunner;

class ConsoleControllerProvider implements ControllerProviderInterface
{
    protected $customCommands;

    /**
     * Pass custom doctrine commands to this method
     * to enable them in the web console.
     *
     * @param array $customCommands Custom console commands
     */
    public function __construct(array $customCommands = array())
    {
        $this->customCommands = $customCommands;
    }

    /**
     * {@inheritdoc}
     */
    public function connect(Application $app)
    {
        $controllers = $app['controllers_factory'];

        $controllers->get('/', function (Application $app) {
            return file_get_contents(__DIR__ . '/Resources/views/Default/index.html');
        });

        $controllers->post('/execute', function (Request $request, Application $app) {
            $helperSet = ConsoleRunner::createHelperSet($app['orm.em']);
            ob_start();
            $output = new StreamOutput(fopen("php://output", "w"));
            $_SERVER['argv'] = explode(' ', "doctrine ".$request->request->get('command'));
            $_SERVER['argc'] = count($_SERVER['argv']);
            ConsoleRunner::run($helperSet, $this->customCommands, $output);
            return ob_get_clean();
        });

        return $controllers;
    }
}<commit_msg>Allow specification of a command path<commit_after><?php

namespace UseAllFive\DoctrineWebConsole;

use Silex\Application;
use Silex\ControllerProviderInterface;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\HttpFoundation\Request;
use UseAllFive\DoctrineWebConsole\Console\Tools\ConsoleRunner;

class ConsoleControllerProvider implements ControllerProviderInterface
{
    protected $commandPath;

    protected $customCommands;

    /**
     * If a command path is not specified, commands will be executed
     * in the web directory's path.
     *
     * @param array $commandPath Path in which commands will be executed
     * @param array $customCommands Extra console commands
     */
    public function __construct($commandPath = null, array $customCommands = array())
    {
        $this->commandPath = $commandPath;
        $this->customCommands = $customCommands;
    }

    /**
     * {@inheritdoc}
     */
    public function connect(Application $app)
    {
        $controllers = $app['controllers_factory'];

        $controllers->get('/', function (Application $app) {
            return file_get_contents(__DIR__ . '/Resources/views/Default/index.html');
        });

        $controllers->post('/execute', function (Request $request, Application $app) {
            $helperSet = ConsoleRunner::createHelperSet($app['orm.em']);
            ob_start();
            $output = new StreamOutput(fopen("php://output", "w"));
            $_SERVER['argv'] = explode(' ', "doctrine ".$request->request->get('command'));
            $_SERVER['argc'] = count($_SERVER['argv']);
            if (null !== $this->commandPath) {
                if (@!is_dir($this->commandPath)) {
                    throw new \InvalidArgumentException(
                        "Unable to change directories into the specified command directory"
                    );
                }
                chdir($this->commandPath);
            }
            ConsoleRunner::run($helperSet, $this->customCommands, $output);
            return ob_get_clean();
        });

        return $controllers;
    }
}
{'fraction_non_alphanumeric': 0.12447145877378435, 'fraction_numerical': 0.0, 'mean_word_length': 2.8465447154471546, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '1791571', 'n_tokens_mistral': 981, 'n_tokens_neox': 970, 'n_words': 217}