-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add ConfigService and GlobalsRegistrar helpers #7
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenCoreEMR\ModuleConfig; | ||
|
|
||
| use OpenEMR\Common\Crypto\CryptoGen; | ||
| use OpenEMR\Common\Database\QueryUtils; | ||
|
|
||
| /** | ||
| * Persist module settings to the OpenEMR globals table via upsert. | ||
| * | ||
| * @author Michael A. Smith <michael@opencoreemr.com> | ||
| * @copyright Copyright (c) 2026 OpenCoreEMR Inc. <https://www.opencoreemr.com> | ||
| */ | ||
| class ConfigService | ||
| { | ||
| private const UPSERT_SQL = <<<'SQL' | ||
| INSERT INTO `globals` (`gl_name`, `gl_index`, `gl_value`) | ||
| VALUES (?, 0, ?) | ||
| ON DUPLICATE KEY UPDATE `gl_value` = ? | ||
| SQL; | ||
|
kojiromike marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Save a setting to the globals table (plaintext). | ||
| */ | ||
| public function saveSetting(string $key, string $value): void | ||
| { | ||
| QueryUtils::sqlStatementThrowException(self::UPSERT_SQL, [$key, $value, $value]); | ||
| } | ||
|
|
||
| /** | ||
| * Encrypt a value with CryptoGen and save it to the globals table. | ||
| */ | ||
| public function saveEncryptedSetting(string $key, string $value): void | ||
| { | ||
| $encrypted = (new CryptoGen())->encryptStandard($value); | ||
| $this->saveSetting($key, $encrypted); | ||
|
kojiromike marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenCoreEMR\ModuleConfig; | ||
|
|
||
| use OpenEMR\Events\Globals\GlobalsInitializedEvent; | ||
| use OpenEMR\Services\Globals\GlobalSetting; | ||
| use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
| use Symfony\Component\HttpFoundation\ParameterBag; | ||
|
|
||
| /** | ||
| * Register a module's globals section: enable/disable toggle and settings page link. | ||
| * | ||
| * Eliminates the boilerplate every OCE module copy-pastes in its Bootstrap class. | ||
| * | ||
| * @author Michael A. Smith <michael@opencoreemr.com> | ||
| * @copyright Copyright (c) 2026 OpenCoreEMR Inc. <https://www.opencoreemr.com> | ||
| */ | ||
| class GlobalsRegistrar | ||
| { | ||
| public function __construct( | ||
| private readonly ParameterBag $globalsBag, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Subscribe to GlobalsInitializedEvent and register the module's globals section. | ||
| */ | ||
| public function register( | ||
| EventDispatcherInterface $eventDispatcher, | ||
| GlobalsSectionDescriptor $descriptor, | ||
| ): void { | ||
| $globalsBag = $this->globalsBag; | ||
|
|
||
| $eventDispatcher->addListener( | ||
| GlobalsInitializedEvent::EVENT_HANDLE, | ||
| static function (GlobalsInitializedEvent $event) use ($descriptor, $globalsBag): void { | ||
| $service = $event->getGlobalsService(); | ||
|
|
||
| $service->createSection($descriptor->sectionName); | ||
|
|
||
| // Enable/disable toggle | ||
| $enableSetting = new GlobalSetting( | ||
| sprintf(xlt('Enable %s'), $descriptor->sectionName), | ||
| GlobalSetting::DATA_TYPE_BOOL, | ||
| '0', | ||
| sprintf(xlt('Enable or disable the %s module'), $descriptor->sectionName), | ||
| ); | ||
| $service->appendToSection( | ||
| $descriptor->sectionName, | ||
| $descriptor->enableKey, | ||
| $enableSetting, | ||
| ); | ||
|
|
||
| // Settings page link | ||
| $webroot = $globalsBag->getString('webroot'); | ||
| $settingsPath = $webroot | ||
| . '/interface/modules/custom_modules/' . $descriptor->moduleDirName | ||
| . '/public/settings.php'; | ||
|
|
||
| $linkSetting = new GlobalSetting( | ||
| xlt('Module Settings'), | ||
| GlobalSetting::DATA_TYPE_HTML_DISPLAY_SECTION, | ||
| '', | ||
| xlt('Link to the module settings page'), | ||
| ); | ||
| $linkSetting->addFieldOption( | ||
| GlobalSetting::DATA_TYPE_OPTION_RENDER_CALLBACK, | ||
| static function () use ($settingsPath, $descriptor): string { | ||
| $url = attr($settingsPath); | ||
| $label = xlt('Open Module Settings'); | ||
| $description = xlt($descriptor->settingsDescription); | ||
| return <<<HTML | ||
| <p>{$description}</p> | ||
| <a href="{$url}" class="btn btn-secondary btn-sm" | ||
| onclick="top.restoreSession()">{$label}</a> | ||
| HTML; | ||
| }, | ||
| ); | ||
| $service->appendToSection( | ||
| $descriptor->sectionName, | ||
| $descriptor->enableKey . '_settings_link', | ||
| $linkSetting, | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenCoreEMR\ModuleConfig; | ||
|
|
||
| /** | ||
| * Parameters for registering a module's globals section (enable toggle + settings link). | ||
| * | ||
| * @author Michael A. Smith <michael@opencoreemr.com> | ||
| * @copyright Copyright (c) 2026 OpenCoreEMR Inc. <https://www.opencoreemr.com> | ||
| */ | ||
| final readonly class GlobalsSectionDescriptor | ||
| { | ||
| /** | ||
| * @param string $sectionName Display name for the globals section (e.g. "OpenCoreEMR Sinch Conversations") | ||
| * @param string $moduleDirName Module directory name under custom_modules/ (e.g. "oce-module-sinch-conversations") | ||
| * @param string $enableKey Globals key for the enable/disable toggle (e.g. "oce_sinch_conversations_enabled") | ||
| * @param string $settingsDescription Help text shown above the settings link | ||
| */ | ||
| public function __construct( | ||
| public string $sectionName, | ||
| public string $moduleDirName, | ||
| public string $enableKey, | ||
| public string $settingsDescription, | ||
| ) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenEMR\Common\Crypto; | ||
|
|
||
| /** | ||
| * Mock CryptoGen — uses base64 to simulate encryption without real crypto dependencies. | ||
| */ | ||
| class CryptoGen | ||
| { | ||
| public function encryptStandard(string $value): string | ||
| { | ||
| return base64_encode($value); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenEMR\Common\Database; | ||
|
|
||
| /** | ||
| * Mock QueryUtils to avoid database calls during tests. | ||
| */ | ||
| class QueryUtils | ||
| { | ||
| /** @var list<array{sql: string, binds: list<mixed>}> */ | ||
| private static array $queries = []; | ||
|
|
||
| private static ?\Throwable $nextException = null; | ||
|
|
||
| /** | ||
| * @param list<mixed> $binds | ||
| */ | ||
| public static function sqlStatementThrowException(string $sql, array $binds = []): true | ||
| { | ||
| self::$queries[] = ['sql' => $sql, 'binds' => $binds]; | ||
| if (self::$nextException !== null) { | ||
| $e = self::$nextException; | ||
| self::$nextException = null; | ||
| throw $e; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public static function setNextException(\Throwable $e): void | ||
| { | ||
| self::$nextException = $e; | ||
| } | ||
|
|
||
| /** @return list<array{sql: string, binds: list<mixed>}> */ | ||
| public static function getQueries(): array | ||
| { | ||
| return self::$queries; | ||
| } | ||
|
|
||
| public static function reset(): void | ||
| { | ||
| self::$queries = []; | ||
| self::$nextException = null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| // Stubs for OpenEMR global functions used by library classes (xlt, attr, etc.). | ||
| // Loaded by the test bootstrap before any test runs. | ||
|
|
||
| if (!function_exists('xlt')) { | ||
| function xlt(string $text): string | ||
| { | ||
| return $text; | ||
| } | ||
| } | ||
|
|
||
| if (!function_exists('attr')) { | ||
| function attr(string $text): string | ||
| { | ||
| return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.