-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from jr-cologne/diff-envs
Add functionality to differentiate between production and testing environment
- Loading branch information
Showing
16 changed files
with
92 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -51,3 +51,5 @@ vendor | |
|
||
# Exclude PHPStorm stuff | ||
.idea | ||
|
||
.env |
This file contains 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
@@ -29,12 +29,18 @@ | |
use Google\Cloud\Datastore\DatastoreClient as GoogleCloudDatastore; | ||
|
||
$env = new Env; | ||
$env->loadEnvVarsFromDatastore(new GoogleCloudDatastore([ | ||
'projectId' => getenv('GOOGLE_CLOUD_PROJECT'), | ||
])); | ||
|
||
if ($env->isProduction()) { | ||
$env->loadEnvVarsFromDatastore(new GoogleCloudDatastore([ | ||
'projectId' => getenv('GOOGLE_CLOUD_PROJECT'), | ||
])); | ||
} else { | ||
$env->loadEnvVarsFromDotenvFile(__DIR__ . '/../.env'); | ||
} | ||
|
||
$app = new CryptoStatus( | ||
(new Config())->load(__DIR__ . '/config/config.php') | ||
(new Config())->load(__DIR__ . '/config/config.php'), | ||
!$env->isProduction() | ||
); | ||
|
||
$app->run(); |
This file contains 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
@@ -37,6 +37,13 @@ class CryptoStatus | |
*/ | ||
protected $config; | ||
|
||
/** | ||
* Defines whether testing mode is enabled | ||
* | ||
* @var bool $testing | ||
*/ | ||
protected $testing; | ||
|
||
/** | ||
* The Twitter client instance | ||
* | ||
|
@@ -73,9 +80,10 @@ class CryptoStatus | |
* @throws Exceptions\TwitterClientException | ||
* @throws Exceptions\ConfigException | ||
*/ | ||
public function __construct(Config $config) | ||
public function __construct(Config $config, bool $testing = false) | ||
{ | ||
$this->config = $config; | ||
$this->testing = $testing; | ||
|
||
$this->init(); | ||
} | ||
|
@@ -110,6 +118,11 @@ public function run() | |
|
||
$tweets = $this->createTweets(); | ||
|
||
if ($this->testing) { | ||
echo implode(PHP_EOL . PHP_EOL, $tweets); | ||
die(); | ||
} | ||
|
||
if (!$this->postTweets($tweets)) { | ||
$this->deleteTweets($this->failed_tweets); | ||
|
||
|
This file contains 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
@@ -29,6 +29,16 @@ | |
class Env | ||
{ | ||
|
||
/** | ||
* Check if the current environment is the production environment | ||
* | ||
* @return bool | ||
*/ | ||
public function isProduction() : bool | ||
{ | ||
return !empty(getenv('GOOGLE_CLOUD_PROJECT')); | ||
} | ||
|
||
/** | ||
* Load environment variables from gcloud datastore | ||
* | ||
|
@@ -55,13 +65,28 @@ public function loadEnvVarsFromDatastore(DatastoreClient $datastore) | |
throw new EnvException("Failed to read environment variables from datastore"); | ||
} | ||
|
||
foreach ($env_vars as $env_var) { | ||
$setting = $env_var['name'] . '=' . $env_var['value']; | ||
$this->putEnvVars($env_vars); | ||
} | ||
|
||
if (!self::put($setting)) { | ||
throw new EnvException('Failed to load environment variables'); | ||
} | ||
/** | ||
* Load environment variables from .env file | ||
* | ||
* @param string $dotenv_file | ||
* @throws EnvException | ||
*/ | ||
public function loadEnvVarsFromDotenvFile(string $dotenv_file) | ||
{ | ||
if (!file_exists($dotenv_file)) { | ||
throw new EnvException("The file ({$dotenv_file}) to load the environment variables from does not exist"); | ||
} | ||
|
||
$env_vars = file($dotenv_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | ||
|
||
if (!$env_vars) { | ||
throw new EnvException("Failed to read environment variables file ({$dotenv_file})"); | ||
} | ||
|
||
$this->putEnvVars($env_vars); | ||
} | ||
|
||
/** | ||
|
@@ -92,4 +117,25 @@ public function get(string $varname) | |
|
||
return $env; | ||
} | ||
|
||
/** | ||
* Set an array of environment variables | ||
* | ||
* @param array $env_vars | ||
* @throws EnvException | ||
*/ | ||
protected function putEnvVars(array $env_vars) : void | ||
{ | ||
foreach ($env_vars as $env_var) { | ||
$setting = $env_var; | ||
|
||
if (is_array($env_var)) { | ||
$setting = $env_var['name'] . '=' . $env_var['value']; | ||
} | ||
|
||
if (!$this->put($setting)) { | ||
throw new EnvException('Failed to load environment variables'); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|
This file contains 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* @author JR Cologne <[email protected]> | ||
* @copyright 2019 JR Cologne | ||
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT | ||
* @version v0.6.8-beta | ||
* @version v0.7.0-beta | ||
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository | ||
* | ||
* ________________________________________________________________________________ | ||
|