Skip to content

Commit

Permalink
Merge pull request #229 from WebFiori/dev
Browse files Browse the repository at this point in the history
feat: Added Support for Setting Env Vars Using putenv()
  • Loading branch information
usernane authored Jul 22, 2024
2 parents 701548c + 880f8bf commit 3f107ce
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 17 deletions.
4 changes: 2 additions & 2 deletions tests/webfiori/framework/test/cli/SchedulerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ public function test05() {
"Line: 44\n",
"Stack Trace:\n",
"#0 At class app\\tasks\Fail2TestTask line 44\n",
"#1 At class webfiori\\framework\scheduler\AbstractTask line 1097\n",
"#2 At class webfiori\\framework\scheduler\AbstractTask line 418\n",
"#1 At class webfiori\\framework\scheduler\AbstractTask line 1101\n",
"#2 At class webfiori\\framework\scheduler\AbstractTask line 422\n",
"#3 At class webfiori\\framework\scheduler\AbstractTask line 951\n",
"#4 At class webfiori\\framework\scheduler\TasksManager line 673\n",
"#5 At class webfiori\\framework\scheduler\TasksManager line 139\n",
Expand Down
49 changes: 49 additions & 0 deletions tests/webfiori/framework/test/cli/UpdateSettingsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,56 @@ public function testUpdatePrimaryTheme01() {

$this->assertEquals('themes\\fioriTheme2\\NewTestTheme2', App::getConfig()->getTheme());
}
/**
* @test
*/
public function testUpdatePrimaryTheme02() {
$runner = App::getRunner();
$runner->setInputs([
'webfiori\\framework\\Privilege',
'themes\\fioriTheme2\\NewTestTheme2'
]);
$runner->setArgsVector([
'webfiori',
'update-settings',
'--w' => 'theme'
]);
$runner->start();
//$this->assertEquals(0, $runner->start());
$this->assertEquals([
"Enter theme class name with namespace:\n",
"Error: Invalid input is given. Try again.\n",
"Enter theme class name with namespace:\n",
"Success: Theme successfully updated.\n"
], $runner->getOutput());

$this->assertEquals('themes\\fioriTheme2\\NewTestTheme2', App::getConfig()->getTheme());
}
/**
* @test
*/
public function testUpdatePrimaryTheme03() {
$runner = App::getRunner();
$runner->setInputs([
'webfiori\\framework\\App',
'themes\\fioriTheme2\\NewTestTheme2'
]);
$runner->setArgsVector([
'webfiori',
'update-settings',
'--w' => 'theme'
]);
$runner->start();
//$this->assertEquals(0, $runner->start());
$this->assertEquals([
"Enter theme class name with namespace:\n",
"Error: Invalid input is given. Try again.\n",
"Enter theme class name with namespace:\n",
"Success: Theme successfully updated.\n"
], $runner->getOutput());

$this->assertEquals('themes\\fioriTheme2\\NewTestTheme2', App::getConfig()->getTheme());
}
/**
* @test
*/
Expand Down
42 changes: 40 additions & 2 deletions tests/webfiori/framework/test/config/JsonDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public function testAddEnvVar00() {
$driver->addEnvVar('COOL_OR_NOT', 'cool');
$driver->addEnvVar('DO_IT', false);
$driver->addEnvVar('MULTIPLY_BY', 4, 'A number to multiply by.');
$driver->addEnvVar('NUL');
}
/**
* @test
Expand Down Expand Up @@ -171,6 +172,10 @@ public function testAddEnvVar01() {
"value" => 4,
"description" => "A number to multiply by."
],
'NUL' => [
"value" => null,
"description" => null
]
], $driver->getEnvVars());
$driver->removeEnvVar('COOL_OR_NOT');
}
Expand Down Expand Up @@ -198,6 +203,10 @@ public function testAddEnvVar02() {
"value" => 4,
"description" => "A number to multiply by."
],
'NUL' => [
"value" => null,
"description" => null
]
], $driver->getEnvVars());
}
/**
Expand Down Expand Up @@ -409,6 +418,7 @@ public function testDatabaseConnections00() {
$conn = new ConnectionInfo('mysql', 'root', 'test@222', 'my_db', 'localhost', 3306);
$driver->addOrUpdateDBConnection($conn);
$this->assertEquals(1, count($driver->getDBConnections()));

}
/**
* @test
Expand All @@ -417,14 +427,42 @@ public function testDatabaseConnections00() {
public function testDatabaseConnections01() {
$driver = new JsonDriver();
$driver->initialize();
$account =$driver->getDBConnection('New_Connection');
$account = $driver->getDBConnection('New_Connection');
$this->assertEquals(3306, $account->getPort());
$this->assertEquals('my_db', $account->getDBName());
$this->assertEquals('mysql', $account->getDatabaseType());
$this->assertEquals('localhost', $account->getHost());
$this->assertEquals('test@222', $account->getPassword());
$this->assertEquals('root', $account->getUsername());

$driver->removeAllDBConnections();
$this->assertEquals(0, count($driver->getDBConnections()));
$this->assertNull($driver->getDBConnection('New_Connection'));
}
/**
* @test
* @depends testDatabaseConnections01
*/
public function testDatabaseConnections02() {
$driver = new JsonDriver();
$this->assertEquals(0, count($driver->getDBConnections()));
$this->assertNull($driver->getDBConnection('olf'));
$conn = new ConnectionInfo('mysql', 'root', 'test@222', 'my_db', 'localhost', 3306, [
'KG' => 9,
'OP' => 'hello'
]);
$driver->addOrUpdateDBConnection($conn);
$this->assertEquals(1, count($driver->getDBConnections()));
$account = $driver->getDBConnection('New_Connection');
$this->assertEquals(3306, $account->getPort());
$this->assertEquals('my_db', $account->getDBName());
$this->assertEquals('mysql', $account->getDatabaseType());
$this->assertEquals('localhost', $account->getHost());
$this->assertEquals('test@222', $account->getPassword());
$this->assertEquals('root', $account->getUsername());
$this->assertEquals([
'KG' => 9,
'OP' => 'hello'
], $account->getExtars());
}
/**
* @test
Expand Down
9 changes: 9 additions & 0 deletions tests/webfiori/framework/test/scheduler/SchedulerTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public function testAttributes05() {
* @test
*/
public function testAttributes06() {
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid argument type. Expected 'string' or 'webfiori\\framework\\scheduler\\TaskArgument'");
$job = new BaseTask();
$job->addExecutionArg(new \webfiori\framework\Privilege());
}
/**
* @test
*/
public function testAttributes07() {
$job = new BaseTask();
$job->addExecutionArgs([
'one' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,47 @@ public function test01() {
$this->assertEquals('A do arg', $arg->getDescription());
$this->assertTrue($arg->isOptional());
}
/**
* @test
*/
public function test02() {
$writer = new CLICommandClassWriter();
$this->assertFalse($writer->setCommandName('invalid name'));
$this->assertFalse($writer->setCommandName(' '));
$this->assertTrue($writer->setCommandName('Lets-Do-It'));
$this->assertEquals('Lets-Do-It', $writer->getCommandName());
$this->assertFalse($writer->setClassName('Invalid Name'));
$this->assertFalse($writer->setClassName(' '));
$this->assertTrue($writer->setClassName('DoItX2Command'));
$this->assertEquals('DoItX2Command', $writer->getName());
$this->assertEquals(ROOT_PATH.DS.APP_DIR.DS.'commands'.DS.'DoItX2Command.php', $writer->getAbsolutePath());
$this->assertEquals('app\\commands', $writer->getNamespace());
$this->assertEquals('Lets-Do-It', $writer->getCommandName());
$this->assertEquals('', $writer->getDescription());
$this->assertEquals([], $writer->getArgs());
$writer->setArgs([
new \webfiori\cli\Argument('--do', 'A do arg', true)
]);
$this->assertEquals('', $writer->getDescription());
$writer->setCommandDescription(' ');
$this->assertEquals('', $writer->getDescription());
$this->assertEquals([
'webfiori\cli\CLICommand'
], $writer->getUseStatements());
$writer->writeClass();
$clazz = $writer->getNamespace().'\\'.$writer->getName();
$this->assertTrue(class_exists($clazz));
$writer->removeClass();
$clazzObj = new $clazz();
$this->assertTrue($clazzObj instanceof CLICommand);
$this->assertEquals('Lets-Do-It', $clazzObj->getName());
$this->assertEquals('<NO DESCRIPTION>', $clazzObj->getDescription());
$this->assertEquals([
'--do'
], $clazzObj->getArgsNames());
$arg = $clazzObj->getArg('--do');
$this->assertTrue($arg instanceof \webfiori\cli\Argument);
$this->assertEquals('A do arg', $arg->getDescription());
$this->assertTrue($arg->isOptional());
}
}
1 change: 1 addition & 0 deletions webfiori/framework/cli/commands/UpdateSettingsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private function getThemeNs() {
if ($instance instanceof Theme) {
return true;
}
return false;
} catch (Throwable $exc) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion webfiori/framework/config/ClassDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function a($file, $str, $tabSize = 0) {
* @param string $description An optional description to describe the porpuse
* of the constant.
*/
public function addEnvVar(string $name, $value, string $description = null) {
public function addEnvVar(string $name, $value = null, string $description = null) {
$this->configVars['env-vars'][$name] = [
'value' => $value,
'description' => $description
Expand Down
5 changes: 3 additions & 2 deletions webfiori/framework/config/ConfigurationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ interface ConfigurationDriver {
* The variables which are added using this method will be defined as
* a named constant at run time using the function 'define'. This means
* the constant will be accesaable anywhere within the application's environment.
* Additionally, it will be added as environment variable using 'putenv()'.
*
* @param string $name The name of the named constant such as 'MY_CONSTANT'.
*
* @param mixed $value The value of the constant.
* @param mixed|null $value The value of the constant.
*
* @param string $description An optional description to describe the porpuse
* of the constant.
*/
public function addEnvVar(string $name, $value, string $description = null);
public function addEnvVar(string $name, $value = null, string $description = null);
/**
* Adds new database connections information or update existing connections.
*
Expand Down
18 changes: 17 additions & 1 deletion webfiori/framework/config/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public function __construct() {
$this->driver = new $driverClazz();
$this->driver->initialize();
}
/**
* Adds new environment variable to the configuration of the app.
*
* @param string $name The name of the variable such as 'MY_VAR'.
*
* @param mixed $value The value of the variable.
*
* @param string|null $description An optional text that describes the variable.
*/
public function addEnvVar(string $name, $value, string $description = null) {
$this->getDriver()->addEnvVar($name, $value, $description);
}
Expand Down Expand Up @@ -56,6 +65,7 @@ public function copy(ConfigurationDriver $new) {
}

foreach ($current->getEnvVars() as $name => $probs) {

$new->addEnvVar($name, $probs['value'], $probs['description']);
}
$new->setPrimaryLanguage($current->getPrimaryLanguage());
Expand Down Expand Up @@ -104,7 +114,13 @@ public static function setDriver(ConfigurationDriver $driver) {
public static function updateEnv() {
foreach (self::getDriver()->getEnvVars() as $name => $envVar) {
if (!defined($name)) {
define($name, $envVar['value']);
if (isset($envVar['value'])) {
define($name, $envVar['value']);
putenv($name.'='.$envVar['value']);
} else {
define($name, null);
putenv($name.'=');
}
}
}
}
Expand Down
34 changes: 27 additions & 7 deletions webfiori/framework/config/JsonDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __construct() {
* @param string $description An optional description to describe the porpuse
* of the constant.
*/
public function addEnvVar(string $name, $value, string $description = null) {
public function addEnvVar(string $name, $value = null, string $description = null) {
$this->json->get('env-vars')->add($name, new Json([
'value' => $value,
'description' => $description
Expand Down Expand Up @@ -193,6 +193,8 @@ public function getDBConnection(string $conName) {
foreach ($extras->getProperties() as $prop) {
$extrasArr[$prop->getName()] = $prop->getValue();
}
} else if (gettype($extras) == 'array') {
$extrasArr = $extras;
}

return new ConnectionInfo(
Expand Down Expand Up @@ -225,11 +227,16 @@ public function getDBConnections(): array {
$this->getProp($jsonObj, 'database', $name));
$extrasObj = $jsonObj->get('extras');

if ($extrasObj !== null && $extrasObj instanceof Json) {
if ($extrasObj !== null) {
$extrasArr = [];

foreach ($extrasObj->getProperties() as $prop) {
$extrasArr[$prop->getName()] = $prop->getValue();
if ($extrasObj instanceof Json) {


foreach ($extrasObj->getProperties() as $prop) {
$extrasArr[$prop->getName()] = $prop->getValue();
}
} else if (gettype($extrasObj) == 'array') {
$extrasArr = $extrasObj;
}
$acc->setExtras($extrasArr);
}
Expand Down Expand Up @@ -262,7 +269,15 @@ public function getDescriptions(): array {

return $retVal;
}

/**
* Returns an array that holds the information of defined application environment
* variables.
*
* @return array The returned array will be associative. The key will represent
* the name of the variable and its value is a sub-associative array with
* two indices, 'description' and 'value'. The description index is a text that describes
* the variable and the value index will hold its value.
*/
public function getEnvVars(): array {
$retVal = [];
$vars = $this->json->get('env-vars');
Expand Down Expand Up @@ -364,7 +379,12 @@ public function getSMTPConnections(): array {

return $retVal;
}

/**
* Returns the name or the namespace of default theme that the application
* will use in case a page does not have specific theme.
*
* @return string
*/
public function getTheme(): string {
return $this->json->get('theme') ?? '';
}
Expand Down
6 changes: 5 additions & 1 deletion webfiori/framework/scheduler/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace webfiori\framework\scheduler;

use Exception;
use InvalidArgumentException;
use Throwable;
use webfiori\collections\Queue;
use webfiori\error\TraceEntry;
Expand Down Expand Up @@ -205,6 +206,9 @@ public function __construct(string $taskName = '', string $when = '* * * * *', s
*
* @param string|TaskArgument $nameOrObj The name of the argument. This also can be an
* object of type TaskArgument.
*
* @throws InvalidArgumentException If provided argument is not a string or an object of type
* 'TaskArgument'.
*
* @since 1.0
*/
Expand All @@ -214,7 +218,7 @@ public function addExecutionArg($nameOrObj) {
} else if ($nameOrObj instanceof TaskArgument) {
$arg = $nameOrObj;
} else {
return;
throw new InvalidArgumentException('Invalid argument type. Expected \'string\' or \''.TaskArgument::class.'\'');;
}

if (!$this->hasArg($arg->getName())) {
Expand Down
2 changes: 1 addition & 1 deletion webfiori/framework/writers/CLICommandClassWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function setArgs(array $argsArr) {
public function setCommandDescription(string $desc) {
$trimmed = trim($desc);

if (strlen($desc) == 0) {
if (strlen($trimmed) == 0) {
return;
}
$this->desc = $trimmed;
Expand Down

0 comments on commit 3f107ce

Please sign in to comment.