<?php

namespace BtwInstaller;

class BlueTwilightUpdater
{
    private $baseDirectory;
    private $composerSignature;

    public function __construct()
    {
        $this->baseDirectory = dirname(__DIR__);
        chdir($this->baseDirectory);
        putenv('HOME=' . $this->baseDirectory);

        // Display errors so installer never gets a WSOD!
        ini_set('display_errors', true);
    }

    public function run()
    {
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
        {
            // Handle post
            $this->runUpdate();
            exit();
        }
        ?>
        <html>
        <head>
            <title>Blue Twilight Update</title>
        </head>
        <body>
        <h1>Blue Twilight Update</h1>
        <p>This update routine ensures your Blue Twilight Composer packages are up-to-date.</p>
        <p>To get started, simply click the button below.</p>
        <p style="font-weight: bold; color: #ff0000;">This can take a few minutes so please be patient, and only click the button once!</p>
        <form method="post">
            <button type="submit">Update Composer and dependencies for me</button>
        </form>

        <hr/>
        <h2>Got Composer?</h2>

        <p>If you already have Composer installed, however, you may want to use that instead. Just run the below commands on your server, changing the path to Composer as appropriate:</p>
        <p><em>Please note: &quot;composer.phar&quot; may actually be &quot;composer&quot; on your system.</em></p>
        <pre>
cd <?php echo $this->baseDirectory; ?>

php artisan clear-compiled
/path/to/composer.phar install
        </pre>
        </body>
        </html>
        <?php
    }

    private function runUpdate()
    {

        ?>
        <h1>Updating Blue Twilight Composer packages</h1>

        <ul>
            <?php
            $steps = [
                ['Removing compiled cache', 'removeCompiledCached'],
                ['Updating dependencies using Composer', 'runComposer']
            ];

            $successful = true;
            foreach ($steps as $step)
            {
                echo sprintf("<li>%s...</li>%s", $step[0], PHP_EOL);
                $result = call_user_func([$this, $step[1]]);

                if (!$result)
                {
                    $successful = false;
                    break;
                }
            }

            if ($successful)
            {
                header('Location: admin');
                exit();
            }

            ?>
        </ul>
        <?php
    }

    private function removeCompiledCached()
    {
        ob_start();
        system('php artisan clear-compiled', $rc);
        $result = ob_get_clean();
        echo nl2br($result);
        if ($rc != 0)
        {
            $this->echoError('clear-compiled command failed');
            return false;
        }

        ob_start();
        system('php artisan cache:clear', $rc);
        $result = ob_get_clean();
        echo nl2br($result);
        if ($rc != 0)
        {
            $this->echoError('cache:clear command failed');
            return false;
        }

        ob_start();
        system('php artisan config:clear', $rc);
        $result = ob_get_clean();
        echo nl2br($result);
        if ($rc != 0)
        {
            $this->echoError('config:clear command failed');
            return false;
        }

        ob_start();
        system('php artisan view:clear', $rc);
        $result = ob_get_clean();
        echo nl2br($result);
        if ($rc != 0)
        {
            $this->echoError('view:clear command failed');
            return false;
        }

        $this->echoOK();
        return true;
    }

    private function echoError($message)
    {
        echo sprintf("<span style=\"color: #ff0000;\">%s.</span>%s", $message, PHP_EOL);
    }

    private function echoOK($message = '')
    {
        echo "<span style=\"color: #008800;\">OK";

        if (strlen($message) > 0)
        {
            echo sprintf('... %s', $message);
        }

        echo '</span>' . PHP_EOL;
    }

    private function runComposer()
    {
        ob_start();
        system('php composer.phar install', $rc);
        $result = ob_get_clean();
        echo nl2br($result);
        if ($rc != 0)
        {
            $this->echoError('Updating Composer packages failed');
            return false;
        }

        $this->echoOK();
        return true;
    }
}

$installer = new BlueTwilightUpdater();
$installer->run();