<commit_before><?php
/*
 * This file is part of the CSBill package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CSBill\InstallBundle\Installer\Step;

use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Exception\ParseException;
use CSBill\InstallBundle\Installer\AbstractFormStep;
use CSBill\InstallBundle\Form\Step\DatabaseConfigForm;

class DatabaseConfig extends AbstractFormStep
{
    /**
     * Array of currently implemented database drivers
     *
     * @var array
     */
    protected $implementedDrivers = array(
        'mysql'
    );

    /**
     * @var string
     */
    private $rootDir;

    /**
     * @var string
     */
    private $environment;

    /**
     * @var bool
     */
    private $debug;

    /**
     * @var array
     */
    protected $drivers = array();

    /**
     * @return DatabaseConfigForm
     */
    public function getForm()
    {
        return new DatabaseConfigForm();
    }

    /**
     * @return array
     */
    public function getFormData()
    {
        $this->drivers = array_intersect($this->implementedDrivers, \PDO::getAvailableDrivers());

        return array(
            'drivers'            => $this->drivers,
            'host'               => 'localhost',
            'port'               => 3306,
            'connection_factory' => $this->get('doctrine.dbal.connection_factory')
        );
    }

    /**
     * Writes the database configuration to the parameters.yml file, and runs all the database migrations and fixtures
     */
    public function process()
    {
        $form = $this->buildForm();
        $data = $form->getData();

        $data['driver'] = sprintf('pdo_%s', $this->drivers[$data['driver']]);

        $kernel = $this->get('kernel');

        $this->rootDir = $kernel->getRootDir();
        $this->environment = $kernel->getEnvironment();
        $this->debug = $kernel->isDebug();

        $this->writeConfigFile($data);

        // @TODO stream the response back to the user, so they can get feedback on the process running
        $this->executeMigrations();
        $this->executeFixtures();
    }

    /**
     * Writes all the configuration values to the paramaters file
     *
     * @param  array             $params
     * @throws \RuntimeException
     * @return void;
     */
    public function writeConfigFile($params = array())
    {
        $config = $this->rootDir . '/config/parameters.yml';

        $yaml = new Parser();

        try {
            $value = $yaml->parse(file_get_contents($config));
        } catch (ParseException $e) {
            throw new \RuntimeException(
                "Unable to parse the YAML string: %s. Your installation might be corrupt.",
                $e->getCode(),
                $e
            );
        }

        foreach ($params as $key => $param) {
            $key = sprintf('database_%s', $key);

            // sets the database details
            $value['parameters'][$key] = $param;
        }

        // Sets a unique value for the secret token.
        // We do this when writing the database configuration,
        // as this is the only time (for now) that we modify the parameters.yml file.
        // We still need to add an extra step so we can write smtp settings
        // @TODO add a more secure random string generator for enhanced security
        $value['parameters']['secret'] = md5(uniqid(php_uname(), true));

        $dumper = new Dumper();

        $yaml = $dumper->dump($value, 2);

        // @TODO : check if we have permission to write to file, otherwise check if we can update permission on file
        file_put_contents($config, $yaml);<commit_msg>[InstallBundle] Added check if config is writable, and attempt to change permissions when writing config file<commit_after><?php
/*
 * This file is part of the CSBill package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CSBill\InstallBundle\Installer\Step;

use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Exception\ParseException;
use CSBill\InstallBundle\Installer\AbstractFormStep;
use CSBill\InstallBundle\Form\Step\DatabaseConfigForm;

class DatabaseConfig extends AbstractFormStep
{
    /**
     * Array of currently implemented database drivers
     *
     * @var array
     */
    protected $implementedDrivers = array(
        'mysql'
    );

    /**
     * @var string
     */
    private $rootDir;

    /**
     * @var string
     */
    private $environment;

    /**
     * @var bool
     */
    private $debug;

    /**
     * @var array
     */
    protected $drivers = array();

    /**
     * @return DatabaseConfigForm
     */
    public function getForm()
    {
        return new DatabaseConfigForm();
    }

    /**
     * @return array
     */
    public function getFormData()
    {
        $this->drivers = array_intersect($this->implementedDrivers, \PDO::getAvailableDrivers());

        return array(
            'drivers'            => $this->drivers,
            'host'               => 'localhost',
            'port'               => 3306,
            'connection_factory' => $this->get('doctrine.dbal.connection_factory')
        );
    }

    /**
     * Writes the database configuration to the parameters.yml file, and runs all the database migrations and fixtures
     */
    public function process()
    {
        $form = $this->buildForm();
        $data = $form->getData();

        $data['driver'] = sprintf('pdo_%s', $this->drivers[$data['driver']]);

        $kernel = $this->get('kernel');

        $this->rootDir = $kernel->getRootDir();
        $this->environment = $kernel->getEnvironment();
        $this->debug = $kernel->isDebug();

        $this->writeConfigFile($data);

        // @TODO stream the response back to the user, so they can get feedback on the process running
        $this->executeMigrations();
        $this->executeFixtures();
    }

    /**
     * Writes all the configuration values to the paramaters file
     *
     * @param  array             $params
     * @throws \RuntimeException
     * @return void;
     */
    public function writeConfigFile($params = array())
    {
        $config = $this->rootDir . '/config/parameters.yml';

        $yaml = new Parser();

        try {
            $value = $yaml->parse(file_get_contents($config));
        } catch (ParseException $e) {
            throw new \RuntimeException(
                "Unable to parse the YAML string: %s. Your installation might be corrupt.",
                $e->getCode(),
                $e
            );
        }

        foreach ($params as $key => $param) {
            $key = sprintf('database_%s', $key);

            // sets the database details
            $value['parameters'][$key] = $param;
        }

        // Sets a unique value for the secret token.
        // We do this when writing the database configuration,
        // as this is the only time (for now) that we modify the parameters.yml file.
        // We still need to add an extra step so we can write smtp settings
        // @TODO add a more secure random string generator for enhanced security
        $value['parameters']['secret'] = md5(uniqid(php_uname(), true));

        $dumper = new Dumper();

        $yaml = $dumper->dump($value, 2);

        if (!is_writable($config)) {
            if(!@chmod($config, 0777)) {
                throw new \Exception('Unable to write config file');
            }
        }

        file_put_contents($config, $yaml);

        @chmod($config, 0644);
{'fraction_non_alphanumeric': 0.12336590937936286, 'fraction_numerical': 0.002538393197106232, 'mean_word_length': 2.106030744974379, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 61, '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': '2978979', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2123, 'n_words': 620}