|
1 | 1 | <?php |
2 | 2 |
|
3 | | -if (php_sapi_name() !== 'cli' || !defined('_CONFIG')) { |
4 | | - exit("\n\tI can not run out of system!\n"); |
| 3 | +if (php_sapi_name() !== 'cli') { |
| 4 | + ret('It only runs in CLI mode!'); |
5 | 5 | } |
6 | 6 |
|
| 7 | +$composer_psr4 = dirname(dirname(__DIR__)).'/composer/autoload_psr4.php'; |
| 8 | +if (!file_exists($composer_psr4)) { |
| 9 | + ret("I can't find Composer data!"); |
| 10 | +} |
| 11 | + |
| 12 | +$composer = require_once $composer_psr4; |
| 13 | +if (!isset($composer['Config\\'][0])) { |
| 14 | + ret("I can't find Composer data!"); |
| 15 | +} |
| 16 | + |
| 17 | +//load composer.json and get the "name" of pack |
| 18 | +$namespace = @json_decode(file_get_contents(__DIR__.'/composer.json'))->name; |
| 19 | +$appConfig = $composer['Config\\'][0].'/'.$namespace.'/'; |
| 20 | + |
7 | 21 | $thisConfig = __DIR__.'/Config/'; |
| 22 | +//exit("\n\n\---> $appConfig\n"); |
8 | 23 |
|
9 | 24 | if (!is_dir($thisConfig)) { |
10 | | - return; |
| 25 | + ret("I can't find 'Config' directory in this pack!"); |
11 | 26 | } |
12 | 27 |
|
13 | | -$namespace = @json_decode(file_get_contents(__DIR__.'/composer.json'))->name; |
14 | | -/* OPTIONAL |
15 | | - * load composer.json and get the "name" of pack |
16 | | - * $appConfig = _CONFIG.$namespace; |
17 | | - */ |
18 | | - |
19 | | -$appConfig = _CONFIG; |
| 28 | +if (is_dir($appConfig)) { |
| 29 | + ret("Configuration already exists - ignored."); |
| 30 | +} |
20 | 31 |
|
21 | 32 | //Coping all files (and directorys) in /Config |
22 | | -$copy = \Lib\Cli\Main::copyDirectoryContents($thisConfig, $appConfig); |
| 33 | +$copy = copyDirectoryContents($thisConfig, $appConfig); |
23 | 34 |
|
24 | 35 | //Return to application installer |
25 | | -return "\n---".($copy === true ? " $namespace instaled!" : $copy); |
| 36 | +ret($copy === true ? " $namespace instaled!" : $copy); |
| 37 | + |
| 38 | +// THE END ... |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +/** |
| 43 | + * Check or create a directory |
| 44 | + * |
| 45 | + * @param string $dir path of the directory |
| 46 | + * @param boolean $create False/true for create |
| 47 | + * @param string $perm indiucates a permission - default 0777 |
| 48 | + * |
| 49 | + * @return bool status of directory (exists/created = false or true) |
| 50 | + */ |
| 51 | +function checkAndOrCreateDir($dir, $create = false, $perm = 0777) |
| 52 | +{ |
| 53 | + if (is_dir($dir) && is_writable($dir)) { |
| 54 | + return true; |
| 55 | + } elseif ($create === false) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + @mkdir($dir, $perm, true); |
| 60 | + @chmod($dir, $perm); |
| 61 | + |
| 62 | + if (is_writable($dir)) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + return false; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Copy entire content of the $dir[ectory] |
| 70 | + * |
| 71 | + * @param string $dir Origin |
| 72 | + * @param string $target Destination |
| 73 | + * |
| 74 | + * @return bool True/false success |
| 75 | + */ |
| 76 | +function copyDirectoryContents($dir, $target) |
| 77 | +{ |
| 78 | + $dir = rtrim($dir, "\\/ ").'/'; |
| 79 | + $target = rtrim($target, "\\/ ").'/'; |
| 80 | + |
| 81 | + if (!checkAndOrCreateDir($target, true, 0777)) { |
| 82 | + return "ERROR: can't create directory '$taget'!"; |
| 83 | + } |
| 84 | + |
| 85 | + foreach (scandir($dir) as $file) { |
| 86 | + if ($file == '.' || $file == '..') { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (is_dir($dir.$file)) { |
| 91 | + if (!checkAndOrCreateDir($target.$file, true, 0777)) { |
| 92 | + return "ERROR: can't create directory '$taget$file'!"; |
| 93 | + } else { |
| 94 | + $copy = copyDirectoryContents($dir.$file, $target.$file); |
| 95 | + if ($copy !== true) { |
| 96 | + return $copy; |
| 97 | + } |
| 98 | + } |
| 99 | + } elseif (is_file($dir.$file)) { |
| 100 | + if (!copy($dir.$file, $target.$file)) { |
| 101 | + echo "\n ERROR: can't copy '$target$file'!"; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return true; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Return with message |
| 110 | + * |
| 111 | + * @param string $msg message |
| 112 | + * |
| 113 | + * @return void print and exit |
| 114 | + */ |
| 115 | +function ret($msg) |
| 116 | +{ |
| 117 | + echo "\n - $msg"; |
| 118 | + return true; |
| 119 | +} |
0 commit comments