Skip to content

Commit

Permalink
Merge pull request #22 from youzan/feature/local_env
Browse files Browse the repository at this point in the history
支持本地部署并载入本地变量
  • Loading branch information
xu42 authored Mar 9, 2021
2 parents 62ecdd3 + 516cc1f commit 0dbc177
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/Constant/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ class Env

const APOLLO_META_SERVER = 'APOLLO_METASERVER';

const APOLLO_FILE = '/tmp/apollo_all.yaml';
const APOLLO_FILE_CLOUD = '/tmp/apollo_all.yaml';

const APOLLO_FILE_LOCAL = '/env.local.yaml';


public static function getApolloFile(): string
{
if (isset($_SERVER['YOUZAN_ENV']) && !empty($_SERVER['YOUZAN_ENV'])) {
return self::APOLLO_FILE_CLOUD;
}

if (defined('YZCLOUD_BOOT_APP_DIR') && file_exists(YZCLOUD_BOOT_APP_DIR . self::APOLLO_FILE_LOCAL)) {
return YZCLOUD_BOOT_APP_DIR . self::APOLLO_FILE_LOCAL;
}

return self::APOLLO_FILE_CLOUD;
}
}
2 changes: 1 addition & 1 deletion src/Util/ApolloUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function writeToFile($reties = 3)
}

// write to file
$res = file_put_contents(Env::APOLLO_FILE, Yaml::dump($configAll));
$res = file_put_contents(Env::getApolloFile(), Yaml::dump($configAll));
if (false === $res) {
LogFacade::warn("Apollo writeToFile. write return false");
return $this->writeToFile(--$reties);
Expand Down
4 changes: 2 additions & 2 deletions src/Util/EnvUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function get(string $varName): ?string

private function getFromApollo(string $varName): ?string
{
if (empty($this->apolloConfig) && file_exists(Env::APOLLO_FILE)) {
if (empty($this->apolloConfig) && file_exists(Env::getApolloFile())) {
try {
$this->apolloConfig = Yaml::parseFile(Env::APOLLO_FILE);
$this->apolloConfig = Yaml::parseFile(Env::getApolloFile());
} catch (\Exception $e) {

}
Expand Down

0 comments on commit 0dbc177

Please sign in to comment.