PATH:
var
/
www
/
vhosts
/
sandbox.dos-group.com
/
beacons.sandbox.dos-group.com
/
vendor
/
dos
/
common
/
src
/
Dos
/
Cloud
<?php namespace Dos\Cloud; use Dos\Phalcon\Enum; use Momentum\Bootstrap\Configuration; use Momentum\Log\Logstash; use Momentum\Log\LogstashEnum; use Monolog\Logger; class Environment { const DEFAULT_DB_PORT = 3306; const KEY_DB = 'db'; const KEY_CACHE = 'cache'; const KEY_LOGGER = 'logger'; const KEY_LOGSTASH = 'logstash'; const KEY_VCAP = 'VCAP_SERVICES'; /** * Cloud services key names */ const KEY_VCAP_CONNECTION_DB = 'mariadb'; const KEY_VCAP_CONNECTION_REDIS = 'redis'; const KEY_VCAP_CONNECTION_ELK = 'elk'; // names used in cloud ELK service const KEY_LOGSTASH_CONNECTION_HOST = 'logstashHost'; const KEY_LOGSTASH_CONNECTION_PORT = 'logstashPort'; // names used in cloud Redis service const KEY_REDIS_CONNECTION_HOST = 'host'; const KEY_REDIS_CONNECTION_PORT = 'port'; const KEY_REDIS_CONNECTION_PASSWORD = 'password'; /** * @param Configuration $config * @param array $defaultDbConnection Settings for project database connection * @param array $defaultRedisConnection Redis connection - can be empty if Xcache should be used instead * @param array $defaultLogstashConnection Logstash connection params - if not empty - logger Logstash will be used * @param string $cacheNamespace Prefix, relative path to use cache items * @param string $loggerChannel Prefix - chanel info to put to syslog * @param string $applicationDir Path to application root * @param string|null $frontendCache Cache engine to use * @param int $logLevel Default log level for logger * @param bool $skipPhpunitDbSettings Not apply DB settings from PHPUNIT_* Env variables * @return array * @throws \Exception */ public static function getConnections( Configuration $config, array $dbConnection, array $redisConnection, array $logstashConnection, $cacheNamespace, $loggerChannel, $applicationDir, $frontendCache = NULL, $logLevel = NULL, $skipPhpunitDbSettings = FALSE ) { if ($logLevel === NULL) { $logLevel = Logger::INFO; } if (isset($_ENV[self::KEY_VCAP])) { $services = json_decode($_ENV[self::KEY_VCAP], TRUE); $dbConnection = self::getDbConnection($config, $services); $redisConnection = self::getRedisConnection($config, $services); $logstashConnection = self::getLogstashConnection($config, $services); } $connections = [ self::KEY_DB => [ 'adapter' => 'Mysql', Enum::KEY_CONNECTION_HOST => defined('PHPUNIT_DB_HOST') && !$skipPhpunitDbSettings ? PHPUNIT_DB_HOST : $dbConnection[Enum::KEY_CONNECTION_HOST], Enum::KEY_CONNECTION_USER => defined('PHPUNIT_DB_USER') && !$skipPhpunitDbSettings ? PHPUNIT_DB_USER : $dbConnection[Enum::KEY_CONNECTION_USER], Enum::KEY_CONNECTION_PASSWORD => defined('PHPUNIT_DB_PASSWORD') && !$skipPhpunitDbSettings ? PHPUNIT_DB_PASSWORD : $dbConnection[Enum::KEY_CONNECTION_PASSWORD], Enum::KEY_CONNECTION_DBNAME => defined('PHPUNIT_DB_DATABASE') && !$skipPhpunitDbSettings ? PHPUNIT_DB_DATABASE : $dbConnection[Enum::KEY_CONNECTION_DATABASE], 'persistent' => TRUE, //'tableMapping'=> __DIR__ . '/tables.json', ], self::KEY_CACHE => [ 'backend' => !empty($redisConnection) ? Enum::CACHE_BACKEND_REDIS : Enum::CACHE_BACKEND_XCACHE, Enum::KEY_CACHE_PREFIX => $config->getCachePrefix() . $cacheNamespace, Enum::KEY_CACHE_LIFETIME => $config->getCacheLifetime(), //Volt cache dir Enum::KEY_CACHE_DIR => $applicationDir . '/../cache/', ], self::KEY_LOGGER => [ Enum::KEY_LOG_DRIVER => Enum::LOG_DRIVER_FILE, 'config' => [ 'channel' => $loggerChannel, 'handler' => $applicationDir . '/../logs/' . $config->getDatabaseName() . '.log', 'files' => 5, 'level' => $logLevel, ], ], ]; if (!empty($dbConnection[Enum::KEY_CONNECTION_PORT])) { $connections[self::KEY_DB][Enum::KEY_CONNECTION_PORT] = $dbConnection[Enum::KEY_CONNECTION_PORT]; } else { $connections[self::KEY_DB][Enum::KEY_CONNECTION_PORT] = self::DEFAULT_DB_PORT; } if (!empty($redisConnection)) { $connections[self::KEY_CACHE][Enum::KEY_CONNECTION_HOST] = $redisConnection[Enum::KEY_CONNECTION_HOST]; $connections[self::KEY_CACHE][Enum::KEY_CONNECTION_PORT] = $redisConnection[Enum::KEY_CONNECTION_PORT]; //$connections[self::KEY_CACHE][Enum::KEY_CONNECTION_PASSWORD] = $redisConnection[Enum::KEY_CONNECTION_PASSWORD]; $connections[self::KEY_CACHE][Enum::KEY_REDIS_CONNECTION_PERSISTENT] = FALSE; $connections[self::KEY_CACHE][Enum::KEY_REDIS_CONNECTION_INDEX] = 1; } if ($frontendCache !== NULL) { if (!in_array($frontendCache, [Enum::CACHE_FRONTEND_DATA, Enum::CACHE_FRONTEND_NONE, Enum::CACHE_FRONTEND_OUTPUT, Enum::CACHE_FRONTEND_BASE64, Enum::CACHE_FRONTEND_IGBINARY])) { throw new \Exception('Improper cache value ' . $frontendCache); } $connections[self::KEY_CACHE]['frontend'] = $frontendCache; } if (!empty($logstashConnection)) { $app = strtolower(explode('.', $loggerChannel, 2)[0]); $proto = isset($logstashConnection[Enum::KEY_CONNECTION_PROTO]) ? $logstashConnection[Enum::KEY_CONNECTION_PROTO] : Logstash::PROTO_TCP; if (!in_array($app, [LogstashEnum::APP_CMS, LogstashEnum::APP_API, LogstashEnum::APP_CLI])) { throw new \Exception('Unknown application: ' . $app); } if (!in_array($proto, [Logstash::PROTO_TCP, Logstash::PROTO_UDP])) { throw new \Exception('Unknown protocol: ' . $proto); } $connections[self::KEY_LOGGER] = [ Enum::KEY_LOG_DRIVER => Enum::LOG_DRIVER_LOGSTASH, 'config' => array_merge($logstashConnection, [ LogstashEnum::PARAM_HOSTNAME => $config->getCustomerIdentifier(), LogstashEnum::PARAM_CHANNEL => $config->getAppIdentifier(), LogstashEnum::PARAM_APPLICATION => $app, Enum::KEY_CONNECTION_PROTO => $proto, ]), ]; } return $connections; } /** * @param Configuration $config * @param array $services * @return array * @throws \Exception */ private static function getDbConnection(Configuration $config, array $services) { $dbConnection = []; $availableDb = []; foreach ($services[self::KEY_VCAP_CONNECTION_DB] as $item) { if ($config->getDatabaseName() == $item['name']) { $dbConnection = $item['credentials']; break; } $availableDb[] = $item['name']; } // cloud database is not attached if (empty($dbConnection)) { throw new \Exception('Database ' . $config->getDatabaseName() . ' not present in connection list - ' . implode(', ', $availableDb) . '.'); } return $dbConnection; } /** * @param Configuration $config * @param array $services * @return array */ private static function getRedisConnection(Configuration $config, array $services) { $redisConnection = []; if (!empty($services[self::KEY_VCAP_CONNECTION_REDIS])) { $redisConnection = [ Enum::KEY_CONNECTION_HOST => $services[self::KEY_VCAP_CONNECTION_REDIS][0]['credentials'][self::KEY_REDIS_CONNECTION_HOST], Enum::KEY_CONNECTION_PORT => $services[self::KEY_VCAP_CONNECTION_REDIS][0]['credentials'][self::KEY_REDIS_CONNECTION_PORT], Enum::KEY_CONNECTION_PASSWORD => $services[self::KEY_VCAP_CONNECTION_REDIS][0]['credentials'][self::KEY_REDIS_CONNECTION_PASSWORD], ]; } return $redisConnection; } /** * @param Configuration $config * @param array $services * @return array */ private static function getLogstashConnection(Configuration $config, array $services) { $logstashConnection = []; if (!empty($services[self::KEY_VCAP_CONNECTION_ELK])) { $logstashConnection = [ Enum::KEY_CONNECTION_HOST => $services[self::KEY_VCAP_CONNECTION_ELK][0]['credentials'][self::KEY_LOGSTASH_CONNECTION_HOST], Enum::KEY_CONNECTION_PORT => $services[self::KEY_VCAP_CONNECTION_ELK][0]['credentials'][self::KEY_LOGSTASH_CONNECTION_PORT], ]; } return $logstashConnection; } }
[+]
..
[-] Environment.php
[open]