| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Metricool\Support\Helpers\Storages; |
| 6 |
|
| 7 |
use Metricool\Support\Helpers\DeferredObject; |
| 8 |
use Metricool\Support\Helpers\Storage; |
| 9 |
|
| 10 |
/** |
| 11 |
* Environment configuration helper used in DI container. |
| 12 |
* |
| 13 |
* @mixin Storage This class acts as a proxy to Storage. All method calls are |
| 14 |
* resolved dynamically through {@see DeferredObject::__get()} |
| 15 |
*/ |
| 16 |
final class EnvironmentConfig extends DeferredObject |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @inheritDoc |
| 20 |
*/ |
| 21 |
protected function deferredClassString(): string |
| 22 |
{ |
| 23 |
return Storage::class; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @inheritDoc |
| 28 |
*/ |
| 29 |
protected function deferredConstructArguments(): array |
| 30 |
{ |
| 31 |
return [ |
| 32 |
'items' => $this->getStorageItems(), |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Method automatically resolves the environment configuration file. |
| 38 |
*/ |
| 39 |
private function getStorageItems(): array |
| 40 |
{ |
| 41 |
$items = require dirname(__FILE__, 5) . '/config/env.php'; |
| 42 |
|
| 43 |
return $this->overrideEnvironmentConfigItems($items); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Developers can override rsp_auth_url with constant RSP_AUTH_URL and |
| 48 |
* base_api_domain with constant RSP_SB_BASE_API_DOMAIN. Set the constants |
| 49 |
* preferably in wp-config.php. |
| 50 |
* |
| 51 |
* Overrides values for: |
| 52 |
* |
| 53 |
* $this->env->getUrl('metricool.rsp_auth_url') |
| 54 |
* $this->env->getString('metricool.base_api_domain') |
| 55 |
*/ |
| 56 |
private function overrideEnvironmentConfigItems(array $items): array |
| 57 |
{ |
| 58 |
if (defined('RSP_AUTH_URL')) { |
| 59 |
$items['metricool']['rsp_auth_url'] = constant('RSP_AUTH_URL'); |
| 60 |
} |
| 61 |
|
| 62 |
if (defined('RSP_MC_BASE_API_DOMAIN')) { |
| 63 |
$items['metricool']['base_api_domain'] = constant('RSP_MC_BASE_API_DOMAIN'); |
| 64 |
} |
| 65 |
|
| 66 |
if (defined('RSP_MC_OAUTH_CLIENT_ID')) { |
| 67 |
$items['metricool']['oauth_client_id'] = constant('RSP_MC_OAUTH_CLIENT_ID'); |
| 68 |
} |
| 69 |
|
| 70 |
return $items; |
| 71 |
} |
| 72 |
} |
| 73 |
|