-
Notifications
You must be signed in to change notification settings - Fork 288
N°9597 - Switch environment to a build environment must not be available #925
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| namespace Combodo\iTop\Service\Startup; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| use Combodo\iTop\Application\Helper\Session; | ||||||||||||||||||||||||||||||||||||||||||||
| use CoreException; | ||||||||||||||||||||||||||||||||||||||||||||
| use IssueLog; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| class StartupService | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||
| * @param string|null $sSwitchEnv | ||||||||||||||||||||||||||||||||||||||||||||
| * @param bool $bAllowCache | ||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||
| * @return string | ||||||||||||||||||||||||||||||||||||||||||||
| * @throws CoreException | ||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||
| public static function SetItopEnvironment(?string $sSwitchEnv, bool &$bAllowCache): string | ||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe you could change all local method and remove staitc which is not so usefull. and not recommended by phpunit (no mock possible) |
||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| if (static::IsBuildEnvironment($sSwitchEnv)) { | ||||||||||||||||||||||||||||||||||||||||||||
| $oException = new CoreException("Switching to environment '$sSwitchEnv' is not allowed since it is a build environment"); | ||||||||||||||||||||||||||||||||||||||||||||
| IssueLog::Exception("Trying to switch to environment '$sSwitchEnv' is not allowed since it is a build environment", $oException); | ||||||||||||||||||||||||||||||||||||||||||||
| throw $oException; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||
| ($sSwitchEnv != null) | ||||||||||||||||||||||||||||||||||||||||||||
| && file_exists(APPCONF.$sSwitchEnv.'/'.ITOP_CONFIG_FILE) | ||||||||||||||||||||||||||||||||||||||||||||
| && (Session::Get('itop_env') !== $sSwitchEnv) | ||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||||||||||||||||||||||||||||
| Session::Set('itop_env', $sSwitchEnv); | ||||||||||||||||||||||||||||||||||||||||||||
| $sEnv = $sSwitchEnv; | ||||||||||||||||||||||||||||||||||||||||||||
| $bAllowCache = false; | ||||||||||||||||||||||||||||||||||||||||||||
| // Reset the opcache since otherwise the PHP "model" files may still be cached !! | ||||||||||||||||||||||||||||||||||||||||||||
| if (function_exists('opcache_reset')) { | ||||||||||||||||||||||||||||||||||||||||||||
| // Zend opcode cache | ||||||||||||||||||||||||||||||||||||||||||||
| opcache_reset(); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (function_exists('apc_clear_cache')) { | ||||||||||||||||||||||||||||||||||||||||||||
| // APC(u) cache | ||||||||||||||||||||||||||||||||||||||||||||
| apc_clear_cache(); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| // TODO: reset the credentials as well ?? | ||||||||||||||||||||||||||||||||||||||||||||
| } elseif (Session::IsSet('itop_env')) { | ||||||||||||||||||||||||||||||||||||||||||||
| $sEnv = Session::Get('itop_env'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||
| $sEnv = ITOP_DEFAULT_ENV; | ||||||||||||||||||||||||||||||||||||||||||||
| Session::Set('itop_env', ITOP_DEFAULT_ENV); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| return $sEnv; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| public static function IsBuildEnvironment(?string $sEnv): bool | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| return $sEnv != null && str_ends_with($sEnv, '-build'); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| namespace Service\Startup; | ||
|
|
||
| use Combodo\iTop\Service\Startup\StartupService; | ||
| use Combodo\iTop\Test\UnitTest\ItopTestCase; | ||
| use CoreException; | ||
|
|
||
| class StartupServiceTest extends ItopTestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $this->RequireOnceItopFile('application/utils.inc.php'); | ||
| } | ||
|
|
||
| public function testSetItopEnvironmentUsesDefaultWhenEnvironmentIsNull(): void | ||
| { | ||
| $bAllowCache = true; | ||
| $sEnv = StartupService::SetItopEnvironment(null, $bAllowCache); | ||
| $this->assertEquals(ITOP_DEFAULT_ENV, $sEnv); | ||
| $this->assertTrue($bAllowCache); | ||
| } | ||
|
|
||
| public function testSetItopEnvironmentWithValidEnvironment(): void | ||
| { | ||
| $bAllowCache = true; | ||
| $sEnv = StartupService::SetItopEnvironment('test', $bAllowCache); | ||
| $this->assertEquals('test', $sEnv); | ||
| $this->assertFalse($bAllowCache); | ||
| } | ||
|
|
||
| public function testSetItopEnvironmentThrowsForBuildEnvironment() | ||
| { | ||
| $bAllowCache = true; | ||
| $this->expectException(CoreException::class); | ||
| $this->expectExceptionMessage("Switching to environment 'test-build' is not allowed since it is a build environment"); | ||
| StartupService::SetItopEnvironment('test-build', $bAllowCache); | ||
| } | ||
|
|
||
| public function testIsBuildEnvironment() | ||
| { | ||
| $this->assertTrue(StartupService::IsBuildEnvironment('test-build')); | ||
| $this->assertFalse(StartupService::IsBuildEnvironment('test')); | ||
| $this->assertFalse(StartupService::IsBuildEnvironment(null)); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the meaning of $bAllowCache? it seems not related to opcache/apccache.
maybe you could document what is its purpose.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a boolean that determines whether the APC cache can be used or not, in conjunction with the
apc_cache.enabledparameterIn the case of the environment switch, the cache must not be used
I've updated the documentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a small explanation in the php doc for both parameters would be handy?