init 5.81 KB
Newer Older
1
#!/usr/bin/env php
2
<?php
3 4 5 6 7
/**
 * Yii Application Initialization Tool
 *
 * In order to run in non-interactive mode:
 *
8
 * init --env=Development --overwrite=n
9 10 11 12 13 14 15 16
 *
 * @author Alexander Makarov <sam@rmcreative.ru>
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

17 18 19 20
if (!extension_loaded('mcrypt')) {
    die('The mcrypt PHP extension is required by Yii2.');
}

21
$params = getParams();
22
$root = str_replace('\\', '/', __DIR__);
23
$envs = require("$root/environments/index.php");
24 25
$envNames = array_keys($envs);

26 27 28
echo "Yii Application Initialization Tool v1.0\n\n";

$envName = null;
29
if (empty($params['env']) || $params['env'] === '1') {
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    echo "Which environment do you want the application to be initialized in?\n\n";
    foreach ($envNames as $i => $name) {
        echo "  [$i] $name\n";
    }
    echo "\n  Your choice [0-" . (count($envs) - 1) . ', or "q" to quit] ';
    $answer = trim(fgets(STDIN));

    if (!ctype_digit($answer) || !in_array($answer, range(0, count($envs) - 1))) {
        echo "\n  Quit initialization.\n";
        exit(0);
    }

    if (isset($envNames[$answer])) {
        $envName = $envNames[$answer];
    }
45
} else {
46
    $envName = $params['env'];
47
}
48 49

if (!in_array($envName, $envNames)) {
50 51 52
    $envsList = implode(', ', $envNames);
    echo "\n  $envName is not a valid environment. Try one of the following: $envsList. \n";
    exit(2);
53 54
}

55 56 57
$env = $envs[$envName];

if (empty($params['env'])) {
58 59 60 61 62 63
    echo "\n  Initialize the application under '{$envNames[$answer]}' environment? [yes|no] ";
    $answer = trim(fgets(STDIN));
    if (strncasecmp($answer, 'y', 1)) {
        echo "\n  Quit initialization.\n";
        exit(0);
    }
64 65
}

Qiang Xue committed
66
echo "\n  Start initialization ...\n\n";
67
$files = getFileList("$root/environments/{$env['path']}");
68 69
$all = false;
foreach ($files as $file) {
70 71 72
    if (!copyFile($root, "environments/{$env['path']}/$file", $file, $all, $params)) {
        break;
    }
73 74
}

75 76 77 78 79
$callbacks = ['setCookieValidationKey', 'setWritable', 'setExecutable'];
foreach ($callbacks as $callback) {
    if (!empty($env[$callback])) {
        $callback($root, $env[$callback]);
    }
80 81
}

Qiang Xue committed
82
echo "\n  ... initialization completed.\n\n";
83 84 85

function getFileList($root, $basePath = '')
{
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    $files = [];
    $handle = opendir($root);
    while (($path = readdir($handle)) !== false) {
        if ($path === '.svn' || $path === '.' || $path === '..') {
            continue;
        }
        $fullPath = "$root/$path";
        $relativePath = $basePath === '' ? $path : "$basePath/$path";
        if (is_dir($fullPath)) {
            $files = array_merge($files, getFileList($fullPath, $relativePath));
        } else {
            $files[] = $relativePath;
        }
    }
    closedir($handle);
    return $files;
102 103
}

104
function copyFile($root, $source, $target, &$all, $params)
105
{
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    if (!is_file($root . '/' . $source)) {
        echo "       skip $target ($source not exist)\n";
        return true;
    }
    if (is_file($root . '/' . $target)) {
        if (file_get_contents($root . '/' . $source) === file_get_contents($root . '/' . $target)) {
            echo "  unchanged $target\n";
            return true;
        }
        if ($all) {
            echo "  overwrite $target\n";
        } else {
            echo "      exist $target\n";
            echo "            ...overwrite? [Yes|No|All|Quit] ";


            $answer = !empty($params['overwrite']) ? $params['overwrite'] : trim(fgets(STDIN));
            if (!strncasecmp($answer, 'q', 1)) {
                return false;
            } else {
                if (!strncasecmp($answer, 'y', 1)) {
                    echo "  overwrite $target\n";
                } else {
                    if (!strncasecmp($answer, 'a', 1)) {
                        echo "  overwrite $target\n";
                        $all = true;
                    } else {
                        echo "       skip $target\n";
                        return true;
                    }
                }
            }
        }
        file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
        return true;
    }
    echo "   generate $target\n";
    @mkdir(dirname($root . '/' . $target), 0777, true);
    file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
    return true;
146
}
147 148 149

function getParams()
{
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    $rawParams = [];
    if (isset($_SERVER['argv'])) {
        $rawParams = $_SERVER['argv'];
        array_shift($rawParams);
    }

    $params = [];
    foreach ($rawParams as $param) {
        if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
            $name = $matches[1];
            $params[$name] = isset($matches[3]) ? $matches[3] : true;
        } else {
            $params[] = $param;
        }
    }
    return $params;
}

function setWritable($root, $paths)
{
    foreach ($paths as $writable) {
        echo "      chmod 0777 $writable\n";
        @chmod("$root/$writable", 0777);
    }
}

function setExecutable($root, $paths)
{
    foreach ($paths as $executable) {
        echo "      chmod 0755 $executable\n";
        @chmod("$root/$executable", 0755);
    }
}

function setCookieValidationKey($root, $paths)
{
    foreach ($paths as $file) {
187
        echo "   generate cookie validation key in $file\n";
188 189 190 191 192 193 194
        $file = $root . '/' . $file;
        $length = 32;
        $bytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
        $key = strtr(substr(base64_encode($bytes), 0, $length), '+/=', '_-.');
        $content = preg_replace('/(("|\')cookieValidationKey("|\')\s*=>\s*)(""|\'\')/', "\\1'$key'", file_get_contents($file));
        file_put_contents($file, $content);
    }
195
}
Qiang Xue committed
196 197 198 199 200 201 202 203 204 205

function createSymlink($links)
{
    foreach ($links as $link => $target) {
        echo "    symlink $target as $link\n";
        if (!is_link($link)) {
            symlink($target, $link);
        }
    }
}