これは架空のスクリプトなので動作確認はしてません
Created
December 1, 2017 16:34
-
-
Save zonuexe/9abc2ac3a1425637edb8d9bc9a69ee09 to your computer and use it in GitHub Desktop.
架空のSDKセットアップスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* 架空のSDK | |
* | |
* @copyright 2017 USAMI Kenta <tadsan@zonu.me> | |
* @license http://www.wtfpl.net/ WTFPL | |
*/ | |
error_reporting(E_ALL); | |
set_error_handler('exception_error_handler'); | |
// Composerでセットされた include_path をチェックするために読み込む | |
$loader = null; | |
foreach ([ | |
__DIR__ . '/../vendor/autoload.php', | |
__DIR__ . '/../../../autoload.php', | |
] as $autoloadFile) { | |
if (file_exists($autoloadFile)) { | |
$loader = require $autoloadFile; | |
break; | |
} | |
} | |
if ($loader === null) { | |
exit('Autoloader not found. Try installing dependencies using composer install.'); | |
} | |
$env = isset($argv[1]) ? $argv[1] : false; | |
$log = isset($argv[2]) ? $argv[2] : false; | |
if (!in_array($env, ['production', 'development'])) { | |
fwrite(STDERR, "env には 'production' または 'development' を指定すること\n"); | |
exit(1); | |
} | |
if ($log === false) { | |
// デプロイサーバーと実行されるAPサーバーは別なので、ここでファイルの実在や書き込み可能かは検証しない | |
fwrite(STDERR, "log にはファイルパスを指定すること\n"); | |
exit(1); | |
} | |
$root = dirname(__DIR__); | |
$conf_dir = "{$root}/example_sdk/src"; | |
fwrite(STDERR, sprintf("[root_path]\t%s\n", $root)); | |
fwrite(STDERR, sprintf("[include_path]\t%s\n", get_include_path())); | |
$dest = "cfg/endpoint.ini"; | |
$from = [ | |
'production' => "cfg/endpoint.ini.production", | |
'development' => "cfg/endpoint.ini.development", | |
]; | |
// コピー前ファイルを include_path 内とフルパスの両方から読み込み、内容に相違がないか確認 | |
$from_file_1 = file_get_contents($from[$env], true); | |
$from_file_2 = file_get_contents("{$conf_dir}/{$from[$env]}"); | |
if ($from_file_1 !== $from_file_2) { | |
fwrite(STDERR, "include_path 内に {$from[$env]} が複数ある可能性があります\n"); | |
exit(1); | |
} | |
fwrite(STDERR, sprintf("[copy]\t%s => %s\n", str_replace("{$root}/", '', "{$conf_dir}/{$from[$env]}"), str_replace("{$root}/", '', "{$conf_dir}/{$dest}"))); | |
copy("{$conf_dir}/$from[$env]", "{$conf_dir}/{$dest}"); | |
fwrite(STDERR, "[check]\t"); | |
$dest_file = file_get_contents($dest, true); | |
// コピー後ファイルを include_path 内から読み込み、内容に相違がないか確認 | |
if ($dest_file !== $from_file_2) { | |
fwrite(STDERR, "include_path 内に {$dest} が複数ある可能性があります\n"); | |
exit(1); | |
} | |
fwrite(STDERR, "OK.\n\n"); | |
fwrite(STDERR, sprintf("[log_path]\t%s\n", $log)); | |
$log_ini_tpl = file_get_contents($conf_dir . '/cfg/log.ini.tpl'); | |
$log_ini_path = $conf_dir . '/cfg/log.ini'; | |
$log_ini_content = str_replace('{{fileName}}', $log, $log_ini_tpl); | |
fwrite(STDERR, sprintf("[log_ini]\t%s\n", $log_ini_path)); | |
file_put_contents($log_ini_path, $log_ini_content); | |
// 書き込まれた設定ファイルをロードする | |
$ini = parse_ini_file('cfg/log.ini'); | |
fwrite(STDERR, sprintf("[log_ini]\t%s", print_r($ini, true))); | |
exit(0); | |
function exception_error_handler($severity, $message, $file, $line) { | |
if (!(error_reporting() & $severity)) { | |
return; | |
} | |
throw new \ErrorException($message, 0, $severity, $file, $line); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment