Skip to content

Commit 5a8a15e

Browse files
authored
Merge pull request #283 from WebFiori/dev
chore: Enhancements to CLI Commands
2 parents a36adc0 + 4554cca commit 5a8a15e

11 files changed

Lines changed: 321 additions & 33 deletions

File tree

WebFiori/Framework/Autoload/ClassLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public static function getCacheArray(): array {
292292
* @throws Exception
293293
*/
294294
public static function getCachePath() : string {
295-
return self::get()->getRoot().DIRECTORY_SEPARATOR.APP_DIR.DIRECTORY_SEPARATOR.'sto'.DIRECTORY_SEPARATOR.self::CACHE_NAME;
295+
return self::get()->getRoot().DIRECTORY_SEPARATOR.APP_DIR.DIRECTORY_SEPARATOR.'Storage'.DIRECTORY_SEPARATOR.self::CACHE_NAME;
296296
}
297297

298298
/**
@@ -838,7 +838,7 @@ private function parseCacheString($str) {
838838
*
839839
*/
840840
private function readCache() {
841-
$autoloadCachePath = $this->getRoot().DIRECTORY_SEPARATOR.APP_DIR.DIRECTORY_SEPARATOR.'sto';
841+
$autoloadCachePath = $this->getRoot().DIRECTORY_SEPARATOR.APP_DIR.DIRECTORY_SEPARATOR.'Storage';
842842
$autoloadCache = $autoloadCachePath.DIRECTORY_SEPARATOR.self::CACHE_NAME;
843843
//For first run, the cache file might not exist.
844844
if (file_exists($autoloadCache)) {

WebFiori/Framework/Cli/Commands/AddDbConnectionCommand.php

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
namespace WebFiori\Framework\Cli\Commands;
1212

13+
use WebFiori\Cli\Argument;
1314
use WebFiori\Cli\Command;
1415
use WebFiori\Database\ConnectionInfo;
1516
use WebFiori\Database\DatabaseException;
@@ -24,34 +25,80 @@
2425
*/
2526
class AddDbConnectionCommand extends Command {
2627
public function __construct() {
27-
parent::__construct('add:db-connection', [], 'Add a database connection.');
28+
parent::__construct('add:db-connection', [
29+
new Argument('--db-type', 'The type of the database server. Supported values: '.implode(', ', ConnectionInfo::SUPPORTED_DATABASES).'.', true),
30+
new Argument('--host', 'The address of the database host.', true),
31+
new Argument('--port', 'Port number of the database server.', true),
32+
new Argument('--user', 'The username to use when connecting to the database.', true),
33+
new Argument('--password', 'The password to use when connecting to the database.', true),
34+
new Argument('--database', 'The name of the database to connect to.', true),
35+
new Argument('--name', 'A friendly name to identify the connection.', true),
36+
new Argument('--extras', 'A JSON string of key-value pairs with extra connection information.', true),
37+
new Argument('--no-check', 'If provided, the connection will be added without the attempt to check if provided credentials are valid.', true),
38+
], 'Add a database connection.');
2839
}
2940
/**
3041
* Execute the command.
3142
*
3243
* @return int
3344
*/
3445
public function exec() : int {
35-
$dbType = $this->select('Select database type:', ConnectionInfo::SUPPORTED_DATABASES);
46+
$dbTypeArg = $this->getArgValue('--db-type');
47+
$supportedDbs = ConnectionInfo::SUPPORTED_DATABASES;
3648

37-
$connInfoObj = new ConnectionInfo('mysql', 'root', 'pass', 'ok');
49+
if ($dbTypeArg !== null && in_array($dbTypeArg, $supportedDbs)) {
50+
$dbType = $dbTypeArg;
51+
} else {
52+
if ($dbTypeArg !== null && !in_array($dbTypeArg, $supportedDbs)) {
53+
$this->warning("Database not supported: $dbTypeArg");
54+
}
55+
$dbType = $this->select('Select database type:', $supportedDbs);
56+
}
57+
58+
$connInfoObj = new ConnectionInfo($dbType, 'root', 'pass', 'ok');
59+
60+
$hostArg = $this->getArgValue('--host');
61+
$connInfoObj->setHost($hostArg !== null ? $hostArg : $this->getInput('Database host:', '127.0.0.1'));
62+
63+
$portArg = $this->getArgValue('--port');
64+
$connInfoObj->setPort($portArg !== null ? (int) $portArg : $this->getInput('Port number:', 3306));
65+
66+
$userArg = $this->getArgValue('--user');
67+
$connInfoObj->setUsername($userArg !== null ? $userArg : $this->getInput('Username:'));
68+
69+
$passArg = $this->getArgValue('--password');
70+
$connInfoObj->setPassword($passArg !== null ? $passArg : $this->getMaskedInput('Password:'));
71+
72+
$dbArg = $this->getArgValue('--database');
73+
$connInfoObj->setDBName($dbArg !== null ? $dbArg : $this->getInput('Database name:'));
74+
75+
$defaultName = 'db-connection-'.(count(App::getConfig()->getDBConnections()) + 1);
76+
$nameArg = $this->getArgValue('--name');
77+
$connInfoObj->setName($nameArg !== null ? $nameArg : $this->getInput('Give your connection a friendly name:', $defaultName));
3878

39-
if ($dbType == 'mssql') {
40-
$connInfoObj = new ConnectionInfo('mssql', 'root', 'pass', 'ok');
79+
$extrasArg = $this->getArgValue('--extras');
80+
81+
if ($extrasArg !== null) {
82+
$decoded = json_decode($extrasArg, true);
83+
84+
if (is_array($decoded)) {
85+
$connInfoObj->setExtras($decoded);
86+
}
87+
}
88+
89+
if ($this->isArgProvided('--no-check')) {
90+
App::getConfig()->addOrUpdateDBConnection($connInfoObj);
91+
$this->success('Connection information was stored in application configuration.');
92+
93+
return 0;
4194
}
4295

43-
$connInfoObj->setHost($this->getInput('Database host:', '127.0.0.1'));
44-
$connInfoObj->setPort($this->getInput('Port number:', 3306));
45-
$connInfoObj->setUsername($this->getInput('Username:'));
46-
$connInfoObj->setPassword($this->getMaskedInput('Password:'));
47-
$connInfoObj->setDBName($this->getInput('Database name:'));
48-
$connInfoObj->setName($this->getInput('Give your connection a friendly name:', 'db-connection-'.(count(App::getConfig()->getDBConnections()) + 1)));
4996
$this->println('Trying to connect to the database...');
5097

5198
$addConnection = $this->tryConnect($connInfoObj);
5299
$orgHost = $connInfoObj->getHost();
53100
$orgErr = $addConnection !== true ? $addConnection->getMessage() : '';
54-
101+
55102
if ($addConnection !== true) {
56103
if ($connInfoObj->getHost() == '127.0.0.1') {
57104
$this->println("Trying with 'localhost'...");

WebFiori/Framework/Cli/Commands/AddSmtpConnectionCommand.php

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
namespace WebFiori\Framework\Cli\Commands;
1212

13+
use WebFiori\Cli\Argument;
1314
use WebFiori\Cli\Command;
1415
use WebFiori\Mail\Exceptions\SMTPException;
1516
use WebFiori\Mail\SMTPAccount;
@@ -24,7 +25,17 @@
2425
*/
2526
class AddSmtpConnectionCommand extends Command {
2627
public function __construct() {
27-
parent::__construct('add:smtp-connection', [], 'Add an SMTP account.');
28+
parent::__construct('add:smtp-connection', [
29+
new Argument('--host', 'The address of SMTP server host.', true),
30+
new Argument('--port', 'Port number of the SMTP server.', true),
31+
new Argument('--user', 'The username to use when connecting to the server.', true),
32+
new Argument('--password', 'The password to use when connecting to the server.', true),
33+
new Argument('--sender-address', 'The email address that will appear as sender.', true),
34+
new Argument('--sender-name', 'The name that will appear as sender.', true),
35+
new Argument('--name', 'A friendly name to identify the connection.', true),
36+
new Argument('--oauth-token', 'OAuth access token to use for authentication instead of password.', true),
37+
new Argument('--no-check', 'If provided, the connection will be added without the attempt to check if provided credentials are valid.', true),
38+
], 'Add an SMTP account.');
2839
}
2940
/**
3041
* Execute the command.
@@ -33,9 +44,12 @@ public function __construct() {
3344
*/
3445
public function exec() : int {
3546
$smtpConn = new SMTPAccount();
36-
$smtpConn->setServerAddress($this->getInput('SMTP Server address:', '127.0.0.1'));
47+
48+
$hostArg = $this->getArgValue('--host');
49+
$smtpConn->setServerAddress($hostArg !== null ? $hostArg : $this->getInput('SMTP Server address:', '127.0.0.1'));
50+
3751
$smtpConn->setPort(25);
38-
$addr = $smtpConn->getAddress();
52+
$addr = $smtpConn->getServerAddress();
3953

4054
if ($addr == 'smtp.outlook.com'
4155
|| $addr == 'outlook.office365.com'
@@ -45,12 +59,39 @@ public function exec() : int {
4559
|| $addr == 'smtp.mail.yahoo.com') {
4660
$smtpConn->setPort(465);
4761
}
48-
$smtpConn->setPort($this->getInput('Port number:', $smtpConn->getPort()));
49-
$smtpConn->setUsername($this->getInput('Username:'));
50-
$smtpConn->setPassword($this->getMaskedInput('Password:'));
51-
$smtpConn->setAddress($this->getInput('Sender email address:', $smtpConn->getUsername()));
52-
$smtpConn->setSenderName($this->getInput('Sender name:', $smtpConn->getAddress()));
53-
$smtpConn->setAccountName($this->getInput('Give your connection a friendly name:', 'smtp-connection-'.count(App::getConfig()->getSMTPConnections())));
62+
63+
$portArg = $this->getArgValue('--port');
64+
$smtpConn->setPort($portArg !== null ? (int) $portArg : $this->getInput('Port number:', $smtpConn->getPort()));
65+
66+
$userArg = $this->getArgValue('--user');
67+
$smtpConn->setUsername($userArg !== null ? $userArg : $this->getInput('Username:'));
68+
69+
$passArg = $this->getArgValue('--password');
70+
$smtpConn->setPassword($passArg !== null ? $passArg : $this->getMaskedInput('Password:'));
71+
72+
$senderAddrArg = $this->getArgValue('--sender-address');
73+
$smtpConn->setAddress($senderAddrArg !== null ? $senderAddrArg : $this->getInput('Sender email address:', $smtpConn->getUsername()));
74+
75+
$senderNameArg = $this->getArgValue('--sender-name');
76+
$smtpConn->setSenderName($senderNameArg !== null ? $senderNameArg : $this->getInput('Sender name:', $smtpConn->getAddress()));
77+
78+
$defaultName = 'smtp-connection-'.count(App::getConfig()->getSMTPConnections());
79+
$nameArg = $this->getArgValue('--name');
80+
$smtpConn->setAccountName($nameArg !== null ? $nameArg : $this->getInput('Give your connection a friendly name:', $defaultName));
81+
82+
$tokenArg = $this->getArgValue('--oauth-token');
83+
84+
if ($tokenArg !== null) {
85+
$smtpConn->setAccessToken($tokenArg);
86+
}
87+
88+
if ($this->isArgProvided('--no-check')) {
89+
App::getConfig()->addOrUpdateSMTPAccount($smtpConn);
90+
$this->success('Connection information was stored in application configuration.');
91+
92+
return 0;
93+
}
94+
5495
$this->println('Trying to connect. This can take up to 1 minute...');
5596
$server = new SMTPServer($smtpConn->getServerAddress(), $smtpConn->getPort());
5697

WebFiori/Framework/Cli/Commands/DryRunMigrationsCommand.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ public function exec(): int {
4949
$namespace = APP_DIR.'\\Database\\Migrations';
5050
$count = $this->runner->discoverFromPath($migrationsPath, $namespace);
5151

52+
// Discover seeders
53+
$seedersPath = APP_PATH.'Database'.DS.'Seeders';
54+
$seedersNs = APP_DIR.'\\Database\\Seeders';
55+
$count += $this->runner->discoverFromPath($seedersPath, $seedersNs);
56+
5257
if ($count === 0) {
53-
$this->info('No migrations found.');
58+
$this->info('No migrations/seeders found.');
5459
return 0;
5560
}
5661

@@ -90,18 +95,21 @@ private function dryRun(): int {
9095
$pending = $this->runner->getPendingChanges(true);
9196

9297
if (empty($pending)) {
93-
$this->info('No pending migrations.');
98+
$this->info('No pending migrations/seeders.');
9499
return 0;
95100
}
96101

97-
$this->println('Pending migrations:');
102+
$this->println('Pending migrations/seeders:');
98103
foreach ($pending as $item) {
99104
$this->println(' - ' . $item['change']->getName());
100105
if (!empty($item['queries'])) {
101106
$this->println(' Queries:');
102107
foreach ($item['queries'] as $query) {
103108
$this->println(' ' . $query);
104109
}
110+
} else {
111+
$this->println(' Queries:');
112+
$this->println(' No Queries');
105113
}
106114
}
107115

WebFiori/Framework/Scheduler/TaskStatusEmail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function __construct($sendAccName, array $receivers = []) {
8888
$this->insert('p')->text('Technical Info:');
8989
$this->insert($this->createTaskInfoTable($activeTask));
9090
$logTxt = implode("\r\n", TasksManager::getLogArray()) . "\r\n";
91-
$file = new File(APP_PATH.'sto'.DS.'logs'.DS.'scheduler'.DS.$activeTask->getTaskName().'-ExecLog-'.date('Y-m-d H-i-s').'.log');
91+
$file = new File(APP_PATH.'Storage'.DS.'Logs'.DS.'Scheduler'.DS.$activeTask->getTaskName().'-ExecLog-'.date('Y-m-d H-i-s').'.log');
9292
$file->setRawData($logTxt);
9393
$file->write(false, true);
9494
$this->addAttachment($file);

WebFiori/Framework/Scheduler/TasksManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ private function logExecHelper($forced, $task, File $file) {
941941
*/
942942
private function logTaskExecution($task,$forced = false) {
943943
if ($this->isLogEnabled) {
944-
$logsPath = ROOT_PATH.DS.APP_DIR.DS.'sto'.DS.'logs';
944+
$logsPath = ROOT_PATH.DS.APP_DIR.DS.'Storage'.DS.'Logs';
945945
$logFile = $logsPath.DS.'tasks.log';
946946
$file = new File($logFile);
947947
$file->create(true);

WebFiori/Framework/Scheduler/WebUI/ListTasksPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function __construct() {
7070
'cols' => 12
7171
])->addChild('v-card');
7272
$card->addChild('v-card-title')->text('Tasks Execution Log');
73-
$file = new File(APP_PATH.'sto'.DS.'logs'.DS.'tasks-execution.log');
73+
$file = new File(APP_PATH.'Storage'.DS.'Logs'.DS.'tasks-execution.log');
7474

7575
if ($file->isExist()) {
7676
$file->read();

WebFiori/Framework/Session/DefaultSessionStorage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* The default sessions storage engine.
1919
*
2020
* This storage engine will store session state as a file in the folder
21-
* 'app/sto/sessions'. The name of the file that contains session state
21+
* '[APP_DIR]/Storage/Sessions'. The name of the file that contains session state
2222
* will be the ID of the session.
2323
*
2424
* @author Ibrahim
@@ -31,8 +31,8 @@ class DefaultSessionStorage implements SessionStorage {
3131
*
3232
*/
3333
public function __construct() {
34-
$sessionsDirName = 'sessions';
35-
$sessionsStoragePath = APP_PATH.'sto';
34+
$sessionsDirName = 'Sessions';
35+
$sessionsStoragePath = APP_PATH.'Storage';
3636
$this->storeLoc = $sessionsStoragePath.DS.$sessionsDirName;
3737

3838
if (!file_exists($this->storeLoc) && is_writable($sessionsStoragePath)) {

0 commit comments

Comments
 (0)