Skip to content

Commit 633006b

Browse files
committed
Merge branch 'refs/heads/b-7.3.x-env-variables-example-OXDEV-9496' into b-7.3.x
2 parents d655221 + 76d2e0e commit 633006b

16 files changed

Lines changed: 98 additions & 19 deletions

File tree

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Move this file to the root directory of your project or merge it with the existing one
2+
SHOW_GENERAL_GREETING=true
3+
OEEM_SHOP_NAME='OXID eShop'

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ The repository contains examples of following cases and more:
9494
* [Composer aliases for easy running of tests and quality tools](https://github.com/OXID-eSales/examples-module/blob/b-7.3.x/composer.json#L48)
9595
* [Using the github actions as CI tool with all recommended tools preconfigured for you.](https://github.com/OXID-eSales/examples-module/tree/b-7.3.x/.github)
9696

97+
* [Using variables from .env file](https://github.com/OXID-eSales/examples-module/tree/b-7.3.x/.env)
98+
* [Access via `getenv()` function](https://github.com/OXID-eSales/examples-module/tree/b-7.3.x/src/Extension/Controller/StartController.php)
99+
* Note: Changes to environment variables take effect immediately — no cache clearing is required.
100+
* [Access via DI container](https://github.com/OXID-eSales/examples-module/tree/b-7.3.x/src/Greeting/services.yaml)
101+
* Note: After updating environment variables, you must clear the cache for changes to take effect.
102+
97103
**HINTS**:
98104
* Only extend the shop core if there is no other way like listen and handle shop events,
99105
decorate/replace some DI service.

src/Core/Module.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ final class Module
1717

1818
public const OEEM_COUNTER_TEMPLATE_VARNAME = 'oeem_greeting_counter';
1919

20+
public const GENERAL_GREETING_LANGUAGE_CONST = 'OEEXAMPLESMODULE_GREETING';
21+
2022
public const DEFAULT_PERSONAL_GREETING_LANGUAGE_CONST = 'OEEXAMPLESMODULE_GREETING_GENERIC';
2123

2224
public const OEEM_ADMIN_GREETING_TEMPLATE_VARNAME = 'greeting_message';

src/Extension/Controller/StartController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ class StartController extends StartController_parent
3333
* NOTE: only leaf classes can be extended this way. The FrontendController class which
3434
* many Controllers inherit from cannot be extended this way.
3535
*/
36+
public function getOeemGeneralGreeting(): string
37+
{
38+
$service = $this->getService(GreetingMessageServiceInterface::class);
39+
return $service->getGeneralGreeting();
40+
}
41+
42+
public function showOeemGeneralGreeting(): bool
43+
{
44+
return strtolower((string)getenv('SHOW_GENERAL_GREETING')) == 'true';
45+
}
46+
3647
public function getOeemGreeting(): string
3748
{
3849
$service = $this->getService(GreetingMessageServiceInterface::class);

src/Greeting/Service/GreetingMessageService.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ public function __construct(
2222
private ModuleSettingsServiceInterface $moduleSettings,
2323
private EshopRequest $shopRequest,
2424
private EshopLanguage $shopLanguage,
25+
private ?string $shopName,
2526
) {
2627
}
2728

29+
public function getGeneralGreeting(): string
30+
{
31+
return sprintf($this->translate(ModuleCore::GENERAL_GREETING_LANGUAGE_CONST), $this->shopName);
32+
}
33+
2834
public function getGreeting(?EshopModelUser $user = null): string
2935
{
3036
$result = ModuleCore::DEFAULT_PERSONAL_GREETING_LANGUAGE_CONST;

src/Greeting/Service/GreetingMessageServiceInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
interface GreetingMessageServiceInterface
1515
{
16+
public function getGeneralGreeting(): string;
17+
1618
public function getGreeting(?EshopModelUser $user = null): string;
1719

1820
public function saveGreeting(EshopModelUser $user): bool;

src/Greeting/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ services:
1212

1313
OxidEsales\ExamplesModule\Greeting\Service\GreetingMessageServiceInterface:
1414
class: OxidEsales\ExamplesModule\Greeting\Service\GreetingMessageService
15+
arguments:
16+
$shopName: '%env(OEEM_SHOP_NAME)%'
1517
public: true
1618

1719
OxidEsales\ExamplesModule\Greeting\Infrastructure\UserModelFactoryInterface:

tests/Codeception/Acceptance/GreetingCest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testGreetingModeGeneric(AcceptanceTester $I): void
3737

3838
$I->openShop();
3939

40-
$I->waitForText(Translator::translate('OEEXAMPLESMODULE_GREETING'));
40+
$I->waitForText(sprintf(Translator::translate('OEEXAMPLESMODULE_GREETING'), getenv('OEEM_SHOP_NAME')));
4141
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_GENERIC'));
4242
$I->dontSeeElement('#oeem_update_greeting');
4343
}
@@ -51,7 +51,7 @@ public function testGreetingModeGenericWithUser(AcceptanceTester $I): void
5151
$I->openShop()
5252
->loginUser($I->getDemoUserName(), $I->getDemoUserPassword());
5353

54-
$I->waitForText(Translator::translate('OEEXAMPLESMODULE_GREETING'));
54+
$I->waitForText(sprintf(Translator::translate('OEEXAMPLESMODULE_GREETING'), getenv('OEEM_SHOP_NAME')));
5555
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_GENERIC'));
5656
$I->dontSee('Hi there sweetie'); //no personal greeting even if user has one set
5757
$I->dontSeeElement('#oeem_update_greeting');
@@ -64,7 +64,7 @@ public function testGreetingModePersonalWithoutUser(AcceptanceTester $I): void
6464
$I->setGreetingModePersonal();
6565
$I->openShop();
6666

67-
$I->waitForText(Translator::translate('OEEXAMPLESMODULE_GREETING'));
67+
$I->waitForText(sprintf(Translator::translate('OEEXAMPLESMODULE_GREETING'), getenv('OEEM_SHOP_NAME')));
6868
$I->dontSee(Translator::translate('OEEXAMPLESMODULE_GREETING_GENERIC'));
6969
$I->dontSeeElement('#oeem_update_greeting');
7070
}
@@ -79,7 +79,7 @@ public function testGreetingModePersonalUser(AcceptanceTester $I): void
7979
$I->openShop()
8080
->loginUser($I->getDemoUserName(), $I->getDemoUserPassword());
8181

82-
$I->waitForText(Translator::translate('OEEXAMPLESMODULE_GREETING'));
82+
$I->waitForText(sprintf(Translator::translate('OEEXAMPLESMODULE_GREETING'), getenv('OEEM_SHOP_NAME')));
8383
$I->dontSee(Translator::translate('OEEXAMPLESMODULE_GREETING_GENERIC'));
8484
$I->see('Hi there sweetie');
8585
$I->seeElement('#oeem_update_greeting');

tests/Codeception/Acceptance/ModuleCest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public function testCanDeactivateModule(AcceptanceTester $I): void
2424
$I->wantToTest('that deactivating the module does not destroy the shop');
2525

2626
$I->openShop();
27-
$I->waitForText(Translator::translate('OEEXAMPLESMODULE_GREETING'));
27+
$I->waitForText(sprintf(Translator::translate('OEEXAMPLESMODULE_GREETING'), getenv('OEEM_SHOP_NAME')));
2828

2929
$I->deactivateModule(Module::MODULE_ID);
3030
$I->reloadPage();
3131

3232
$I->waitForPageLoad();
33-
$I->dontSee(Translator::translate('OEEXAMPLESMODULE_GREETING'));
33+
$I->dontSee(sprintf(Translator::translate('OEEXAMPLESMODULE_GREETING'), getenv('OEEM_SHOP_NAME')));
3434
}
3535
}

tests/Codeception/Acceptance/UpdateGreetingCest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testSetGreetingMode(AcceptanceTester $I): void
4343
$I->openShop()
4444
->loginUser($I->getDemoUserName(), $I->getDemoUserPassword());
4545

46-
$I->waitForText(Translator::translate('OEEXAMPLESMODULE_GREETING'));
46+
$I->waitForText(sprintf(Translator::translate('OEEXAMPLESMODULE_GREETING'), getenv('OEEM_SHOP_NAME')));
4747
$I->dontSee(Translator::translate('OEEXAMPLESMODULE_GREETING_GENERIC'));
4848
$I->see('Hi there sweetie');
4949

0 commit comments

Comments
 (0)