<commit_before><?php

namespace ForkCMS\Bundle\InstallerBundle\Service;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

use Backend\Core\Engine\Model;
use Backend\Core\Installer\CoreInstaller;
use Backend\Core\Installer\ModuleInstaller;
use Symfony\Component\DependencyInjection\Container;

/**
 * This service installs fork
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class ForkInstaller
{
    /**
     * The root dir of our project
     *
     * @var string
     */
    private $rootDir;

    /**
     * The Dependency injection container
     *
     * @var Container
     */
    private $container;

    /**
     * @todo: make sure the Container doesn't have to be injected
     */
    public function __construct(Container $container, $rootDir)
    {
        $this->container = $container;
        $this->rootDir = $rootDir;
    }

    /**
     * Installs Fork
     *
     * @param  array  $data The collected data required for Fork
     * @return bool         Is Fork successfully installed?
     */
    public function install(array $data)
    {
        if (!$this->isValidData($data)) {
            return false;
        }

        // extend execution limit
        set_time_limit(0);
        ini_set('memory_limit', '16M');

        $this->definePaths();
        $this->deleteCachedData();

        $this->buildDatabase($data);
        $this->installCore($data);
        var_dump('ok');exit;
    }

    /**
     * Checks if our given data is complete and valid
     *
     * @param  array $data The collected data required to install Fork
     * @return bool        Do we have all the needed data
     */
    protected function isValidData(array $data)
    {
        if (
            !in_array('db_hostname', $data)
            || !in_array('db_username', $data)
            || !in_array('db_password', $data)
            || !in_array('db_database', $data)
            || !in_array('db_port', $data)

            || !in_array('languages', $data)
            || !in_array('interface_languages', $data)
            || !in_array('default_language', $data)
            || !in_array('default_interface_language', $data)

            || !in_array('modules', $data)
            || !in_array('example_data', $data)

            || !in_array('email', $data)
            || !in_array('password', $data)
        ) {
            return false;
        }

        return true;
    }

    /**
     * Define paths also used in frontend/backend, to be used in installer.
     * @deprecated This is done in different places in Fork. This should be centralized
     */
    private function definePaths()
    {
        // general paths
        define('BACKEND_PATH', $this->rootDir . 'src/Backend');
        define('BACKEND_CACHE_PATH', BACKEND_PATH . '/Cache');
        define('BACKEND_CORE_PATH', BACKEND_PATH . '/Core');
        define('BACKEND_MODULES_PATH', BACKEND_PATH . '/Modules');

        define('FRONTEND_PATH', $this->rootDir . 'src/Frontend');
        define('FRONTEND_CACHE_PATH', FRONTEND_PATH . '/Cache');
        define('FRONTEND_CORE_PATH', FRONTEND_PATH . '/Core');
        define('FRONTEND_MODULES_PATH', FRONTEND_PATH . '/Modules');
        define('FRONTEND_FILES_PATH', FRONTEND_PATH . '/Files');
    }

    /**
     * Delete the cached data
     */
    private function deleteCachedData()
    {
        $finder = new Finder();
        $fs = new Filesystem();
        foreach ($finder->files()->in(BACKEND_CACHE_PATH)->in(FRONTEND_CACHE_PATH) as $file) {
            /** @var $file \SplFileInfo */
            $fs->remove($file->getRealPath());
        }
    }

    protected function installCore($data)
    {
        // install the core
        $installer = $this->getCoreInstaller($data);
        $installer->install();<commit_msg>Fetch warnings and defaultExtras from the CoreInstaller.<commit_after><?php

namespace ForkCMS\Bundle\InstallerBundle\Service;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

use Backend\Core\Engine\Model;
use Backend\Core\Installer\CoreInstaller;
use Backend\Core\Installer\ModuleInstaller;
use Symfony\Component\DependencyInjection\Container;

/**
 * This service installs fork
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class ForkInstaller
{
    /**
     * The root dir of our project
     *
     * @var string
     */
    private $rootDir;

    /**
     * The Dependency injection container
     *
     * @var Container
     */
    private $container;

    /**
     * @var array
     */
    private $warnings = array();
    private $defaultExtras = array();

    /**
     * @todo: make sure the Container doesn't have to be injected
     */
    public function __construct(Container $container, $rootDir)
    {
        $this->container = $container;
        $this->rootDir = $rootDir;
    }

    /**
     * Installs Fork
     *
     * @param  array  $data The collected data required for Fork
     * @return bool         Is Fork successfully installed?
     */
    public function install(array $data)
    {
        if (!$this->isValidData($data)) {
            return false;
        }

        // extend execution limit
        set_time_limit(0);
        ini_set('memory_limit', '16M');

        $this->definePaths();
        $this->deleteCachedData();

        $this->buildDatabase($data);
        $this->installCore($data);
        var_dump($this);exit;
    }

    /**
     * Checks if our given data is complete and valid
     *
     * @param  array $data The collected data required to install Fork
     * @return bool        Do we have all the needed data
     */
    protected function isValidData(array $data)
    {
        if (
            !in_array('db_hostname', $data)
            || !in_array('db_username', $data)
            || !in_array('db_password', $data)
            || !in_array('db_database', $data)
            || !in_array('db_port', $data)

            || !in_array('languages', $data)
            || !in_array('interface_languages', $data)
            || !in_array('default_language', $data)
            || !in_array('default_interface_language', $data)

            || !in_array('modules', $data)
            || !in_array('example_data', $data)

            || !in_array('email', $data)
            || !in_array('password', $data)
        ) {
            return false;
        }

        return true;
    }

    /**
     * Define paths also used in frontend/backend, to be used in installer.
     * @deprecated This is done in different places in Fork. This should be centralized
     */
    private function definePaths()
    {
        // general paths
        define('BACKEND_PATH', $this->rootDir . 'src/Backend');
        define('BACKEND_CACHE_PATH', BACKEND_PATH . '/Cache');
        define('BACKEND_CORE_PATH', BACKEND_PATH . '/Core');
        define('BACKEND_MODULES_PATH', BACKEND_PATH . '/Modules');

        define('FRONTEND_PATH', $this->rootDir . 'src/Frontend');
        define('FRONTEND_CACHE_PATH', FRONTEND_PATH . '/Cache');
        define('FRONTEND_CORE_PATH', FRONTEND_PATH . '/Core');
        define('FRONTEND_MODULES_PATH', FRONTEND_PATH . '/Modules');
        define('FRONTEND_FILES_PATH', FRONTEND_PATH . '/Files');
    }

    /**
     * Delete the cached data
     */
    private function deleteCachedData()
    {
        $finder = new Finder();
        $fs = new Filesystem();
        foreach ($finder->files()->in(BACKEND_CACHE_PATH)->in(FRONTEND_CACHE_PATH) as $file) {
            /** @var $file \SplFileInfo */
            $fs->remove($file->getRealPath());
        }
    }

    protected function installCore($data)
    {
        // install the core
        $installer = $this->getCoreInstaller($data);
        $installer->install();

        // add the warnings
        $moduleWarnings = $installer->getWarnings();
        if (!empty($moduleWarnings)) {
            $this->warnings[] = array('module' => 'Core', 'warnings' => $moduleWarnings);
        }

        // add the default extras
        $moduleDefaultExtras = $installer->getDefaultExtras();
        if (!empty($moduleDefaultExtras)) {
            $this->defaultExtras = array_merge($defaultExtras, $moduleDefaultExtras);
        }
{'fraction_non_alphanumeric': 0.13901079350017792, 'fraction_numerical': 0.0007116593523899894, 'mean_word_length': 2.3236105636578634, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 77, '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': '2012791', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2495, 'n_words': 558}