<commit_before><?php
/**
 * @Author: <NAME>
 *        Date: 7/15/13
 *        Time: 3:41 PM
 */

namespace Xiidea\Installer;

use Composer\Script\Event;
use Xiidea\Installer\Services\Colors;
use Xiidea\Installer\Services\CoreLibrary;
use Xiidea\Installer\Services\DataBase;
use Xiidea\Installer\Services\DatabaseConfig;
use Xiidea\Helper\Filesystem;

class Manager
{
    private static $requires = array(
        'twig/twig' => 'Twig',
        'swiftmailer/swiftmailer' => 'Swift Mailer'
    );

    private static $twigInstalled = false;
    private static $newCopy = false;
    private static $environment = 'production';
    private static $validEnvironments = array('production', 'development', 'testing');

    public static function postUpdate(Event $event)
    {
        self::configureEnvironment($event);
    }

    public static function postInstall(Event $event)
    {
        self::configureEnvironment($event);
    }

    public static function preInstall(Event $event)
    {
        self::updateRequiresLibraries($event);
    }

    public static function preUpdate(Event $event)
    {
        self::updateRequiresLibraries($event);
    }

    private static function getStatusKeyName($require)
    {
        if(!self::$requires[$require]){
            return null;
        }

        return "use-".str_replace(' ', '-', strtolower(self::$requires[$require]));
    }

    private static function updateRequiresLibraries(Event $event)
    {
        $package = $event->getComposer()->getPackage();
        $requiresList = $package->getRequires();
        $extras = $event->getComposer()->getPackage()->getExtra();

        foreach (self::$requires as $require => $label) {
            $installStatus = self::isInstalled($requiresList[$require]);

            if($require == 'twig/twig'){
                self::$twigInstalled = $installStatus;
            }


            $statusKeyName = self::getStatusKeyName($require);

            if ($statusKeyName && !($extras[$statusKeyName])) {
                unset($requiresList[$require]);
            }
        }

        $package->setRequires($requiresList);
    }

    private static function isInstalled($link)
    {
        return file_exists("vendor/" . $link->getTarget());
    }

    private static function configureEnvironment($event)
    {
        $extras = $event->getComposer()->getPackage()->getExtra();

        $bootstrap = $extras['ci-web-dir'] . "/app.config.php";
        $webDirectory = realpath(".") . DIRECTORY_SEPARATOR . $extras['ci-web-dir'];
        $appDirectory = realpath(".") . DIRECTORY_SEPARATOR . $extras['ci-app-dir'];
        $ciBasePath = 'vendor/' . $extras['ci-package-name'];

        self::copyAppDir($ciBasePath, $appDirectory);
        self::writeConfig($event, $appDirectory);
        self::buildBootstrap($event, $bootstrap, $extras, $webDirectory, $ciBasePath);

        CoreLibrary::manage($event, $extras, self::$newCopy);
        DatabaseConfig::write($event, $appDirectory);

        self::generateDatabaseSchema($appDirectory, $event);

        $uploadDir = realpath($webDirectory) . DIRECTORY_SEPARATOR . "uploads";

        if (!file_exists($uploadDir)) {
            mkdir($uploadDir);
        }
    }


    private static function generateDatabaseSchema($appDirectory, Event $event)
    {
        $io = $event->getIO();

        $current_config = DatabaseConfig::getDatabaseConfigurationData($appDirectory . '/config/database.php');

        $db = new DataBase($current_config);

        if(!$db->isConnected()){<commit_msg>Cache directory created on install<commit_after><?php
/**
 * @Author: <NAME>
 *        Date: 7/15/13
 *        Time: 3:41 PM
 */

namespace Xiidea\Installer;

use Composer\Script\Event;
use Xiidea\Installer\Services\Colors;
use Xiidea\Installer\Services\CoreLibrary;
use Xiidea\Installer\Services\DataBase;
use Xiidea\Installer\Services\DatabaseConfig;
use Xiidea\Helper\Filesystem;

class Manager
{
    private static $requires = array(
        'twig/twig' => 'Twig',
        'swiftmailer/swiftmailer' => 'Swift Mailer'
    );

    private static $twigInstalled = false;
    private static $newCopy = false;
    private static $environment = 'production';
    private static $validEnvironments = array('production', 'development', 'testing');

    public static function postUpdate(Event $event)
    {
        self::configureEnvironment($event);
    }

    public static function postInstall(Event $event)
    {
        self::configureEnvironment($event);
    }

    public static function preInstall(Event $event)
    {
        self::updateRequiresLibraries($event);
    }

    public static function preUpdate(Event $event)
    {
        self::updateRequiresLibraries($event);
    }

    private static function getStatusKeyName($require)
    {
        if(!self::$requires[$require]){
            return null;
        }

        return "use-".str_replace(' ', '-', strtolower(self::$requires[$require]));
    }

    private static function updateRequiresLibraries(Event $event)
    {
        $package = $event->getComposer()->getPackage();
        $requiresList = $package->getRequires();
        $extras = $event->getComposer()->getPackage()->getExtra();

        foreach (self::$requires as $require => $label) {
            $installStatus = self::isInstalled($requiresList[$require]);

            if($require == 'twig/twig'){
                self::$twigInstalled = $installStatus;
            }


            $statusKeyName = self::getStatusKeyName($require);

            if ($statusKeyName && !($extras[$statusKeyName])) {
                unset($requiresList[$require]);
            }
        }

        $package->setRequires($requiresList);
    }

    private static function isInstalled($link)
    {
        return file_exists("vendor/" . $link->getTarget());
    }

    private static function configureEnvironment($event)
    {
        $extras = $event->getComposer()->getPackage()->getExtra();

        $bootstrap = $extras['ci-web-dir'] . "/app.config.php";
        $webDirectory = realpath(".") . DIRECTORY_SEPARATOR . $extras['ci-web-dir'];
        $appDirectory = realpath(".") . DIRECTORY_SEPARATOR . $extras['ci-app-dir'];
        $ciBasePath = 'vendor/' . $extras['ci-package-name'];

        self::copyAppDir($ciBasePath, $appDirectory);
        self::writeConfig($event, $appDirectory);
        self::buildBootstrap($event, $bootstrap, $extras, $webDirectory, $ciBasePath);

        CoreLibrary::manage($event, $extras, self::$newCopy);
        DatabaseConfig::write($event, $appDirectory);

        self::generateDatabaseSchema($appDirectory, $event);

        $webDirectory = realpath($webDirectory) . DIRECTORY_SEPARATOR;

        $uploadDir = $webDirectory . "uploads";

        if (!file_exists($uploadDir)) {
            @mkdir($uploadDir);
        }

        foreach(array('js', 'css') as $dir){
            $cacheDir = $webDirectory . "assets".DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR."cache";
            if (!file_exists($cacheDir)) {
                @mkdir($cacheDir);
            }
        }
    }


    private static function generateDatabaseSchema($appDirectory, Event $event)
    {
        $io = $event->getIO();

        $current_config = DatabaseConfig::getDatabaseConfigurationData($appDirectory . '/config/database.php');

        $db = new DataBase($current_config);

        if(!$db->isConnected()){
{'fraction_non_alphanumeric': 0.14511980506294841, 'fraction_numerical': 0.002165967239745499, 'mean_word_length': 2.904862579281184, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, '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': '6423251', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2062, 'n_words': 375}