Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add KeymanSiteEnvironment #12

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions _common/KeymanSiteEnvironment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);

namespace Keyman\Site\Common;

// Dynamic properties marked for deprecation: https://wiki.php.net/rfc/deprecate_dynamic_properties
#[AllowDynamicProperties]
class KeymanSiteEnvironment {
private array $data = [];

private static $instance;

public static function Instance($class = null): KeymanSiteEnvironment {
if(!self::$instance)
self::Rebuild($class);
return self::$instance;
}

public static function Rebuild($class) {
self::$instance = new KeymanSiteEnvironment($class);
}

protected function __construct($class) {
self::_Init($class);
}

protected function _Init($class) {
$env = getenv();
$props = get_class_vars($class);
foreach($props as $key => $value) {
if (isset($env[$key])) {
echo "key $key, env[key] $env[$key]\n";
self::__set($key, $env[$key]);
}
}
var_dump($this);
echo "__get: " . self::__get('LOGNAME') . "\n";
}

public function &__get($name) { return $this->data[$name]; }
public function __isset($name) { return isset($this->data[$name]); }
public function __set($name, $value) { $this->data[$name] = $value; }
public function __unset($name) { unset($this->data[$name]); }

public static function Debug($class) {
$props = get_class_vars($class);
echo "------- Debug -------\n";
var_dump($props);
echo "---------------------\n";
}
}
Copy link
Contributor Author

@darcywong00 darcywong00 Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. unset a property

I'd prefer to rewrite how that particular function works. It's suboptimal at present.

  1. return a boolean if the env var is ''?

No, if the env var is unsset, then the property value should be NULL (testable with is_null, isset, isempty etc).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, if the env var is unsset, then the property value should be NULL ...

I meant for 2. to check when vars are initialized to ''

https://github.com/keymanapp/help.keyman.com/blob/9169d57566ca64026b4421b2fc4a80cc7b7f38b1/_includes/HelpSiteEnvironment.php#L9-L12

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is needed?


class HelpSiteEnvironment extends KeymanSiteEnvironment {
public ?string
// Expected env vars for help.keyman.com
$HOSTNAME = null, // Do not commit this line
$KEYMANHOSTS_TIER = null,
$LOGNAME = null,
$KEYMAN_COM_PROXY_PORT = null;

const MY_KEYMAN = null;

public function __construct() {
return new KeymanSiteEnvironment(get_class());
}

public static function Init() {
KeymanSiteEnvironment::Instance(get_class());

// For troubleshooting
KeymanSiteEnvironment::Instance()->Debug(get_class());
}
}

// Code for testing
HelpSiteEnvironment::Init();
if (HelpSiteEnvironment::Instance()->__isset('LOGNAME')) {
echo "true\n";
} else {
echo "false\n";
}

1 change: 1 addition & 0 deletions bootstrap.inc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ function _bootstrap_configure_common() {
JsonApiFailure.php
KeymanHosts.php
KeymanSentry.php
KeymanSiteEnvironment.php
MarkdownHost.php
)
local common_file=
Expand Down