This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoic-install-userbase
112 lines (84 loc) · 3.7 KB
/
stoic-install-userbase
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env php
<?php
if (version_compare('7.2.0', PHP_VERSION, '>')) {
fwrite(STDERR, sprintf("Stoic is only supported on versions of PHP 7.2 or newer." . PHP_EOL . "You are using PHP %s (%s)" . PHP_EOL, PHP_VERSION, PHP_BINARY));
die(1);
}
if (!ini_get('date.timezone')) {
ini_set('date.timezone', 'UTC');
}
foreach (['../../' => '../../', '../vendor/' => '../', 'vendor/' => './'] as $file => $relPath) {
$path = "{$file}autoload.php";
if (file_exists($path)) {
define('STOIC_COMPOSER_INSTALL', $path);
define('STOIC_COMPOSER_PARENT', $relPath);
break;
}
}
if (!defined('STOIC_COMPOSER_INSTALL')) {
fwrite(STDERR, "You need to set up the project dependencies using Composer:" . PHP_EOL . PHP_EOL . " composer install" . PHP_EOL);
die(1);
}
require(STOIC_COMPOSER_INSTALL);
use AndyM84\Config\ConfigContainer;
use Stoic\Pdo\PdoDrivers;
use Stoic\Pdo\PdoHelper;
use Stoic\Utilities\ConsoleHelper;
use Stoic\Utilities\FileHelper;
use Stoic\Web\Resources\SettingsStrings;
use Stoic\Web\Resources\StoicStrings;
// Script constants
define('SCRIPT_NAME', 'Stoic Framework Migration Utility');
define('SCRIPT_DESCRIPTION', 'Script that attempts to apply any available configuration\nand database migration files');
define('SCRIPT_USAGE', ' vendor/bin/stoic-install-userbase // Installs the migration files that match your DSN configuration');
$ch = new ConsoleHelper($argv);
$fh = new FileHelper(STOIC_COMPOSER_PARENT);
if ($ch->hasShortLongArg('h', 'help', true)) {
$ch->putLine(SCRIPT_NAME);
$ch->putLine(SCRIPT_DESCRIPTION);
$ch->putLine();
$ch->putLine(SCRIPT_USAGE);
$ch->putLine();
exit;
}
if (!$fh->fileExists(StoicStrings::SETTINGS_FILE_PATH)) {
fwrite(STDERR, "You must initialize your application:" . PHP_EOL . PHP_EOL . " vendor/bin/stoic-create" . PHP_EOL);
die(1);
}
$supportedDrivers = [
PdoDrivers::PDO_MYSQL => 'mysql'
];
$migrationFiles = [
'0001-StUsr-UserTables.sql'
];
$settings = new ConfigContainer($fh->getContents(StoicStrings::SETTINGS_FILE_PATH));
$ch->putLine(SCRIPT_NAME);
$ch->putLine();
$user = $settings->get(SettingsStrings::DB_USER, null);
$pass = $settings->get(SettingsStrings::DB_PASS, null);
$db = new PdoHelper($settings->get(SettingsStrings::DB_DSN), (empty($user) || $user == '<changeme>') ? null : $user, (empty($pass) || $pass == '<changeme>') ? null : $pass);
if ($db->isActive() && array_key_exists($db->getDriver()->getValue(), $supportedDrivers) !== false) {
$ch->putLine("Installing migration files based on configuration.. ");
$ch->putLine();
$basePkgPath = $fh->pathJoin('~/vendor/stoic/userbase/Migrations/MYSQL/');
$baseMigPath = $fh->pathJoin($settings->get(SettingsStrings::ASSETS_PATH), $settings->get(SettingsStrings::MIGRATE_DB_PATH));
foreach (array_values($migrationFiles) as $file) {
$ch->put("Copying migration file '{$file}'.. ");
try {
$fh->copyFile($fh->pathJoin($basePkgPath, 'drop', $file), $fh->pathJoin($baseMigPath, 'drop', $file));
$fh->copyFile($fh->pathJoin($basePkgPath, 'up', $file), $fh->pathJoin($baseMigPath, 'up', $file));
$ch->putLine('DONE');
} catch (Exception $ex) {
$ch->putLine('ERROR: ' . $ex->getMessage());
}
}
$ch->putLine();
$ch->putLine("Finished installing migration files");
} else {
$ch->putLine("Cannot use stoic-php-userbase, only available for the following db engines: " . implode(', ', array_values($supportedDrivers)));
}
$ch->putLine();
$ch->putLine("To complete userbase installation, run the following command:");
$ch->putLine();
$ch->putLine(" vendor/bin/stoic-migrate");
$ch->putLine();