Created
July 14, 2015 16:07
-
-
Save lsloan/18c29f267f68aacb045f to your computer and use it in GitHub Desktop.
Using arrays from config files with Zend Framework
This file contains hidden or 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
analytic_sensor_id = "05D2D6AB-B9CE-4BAC-98FB-EB96F25F0D29" | |
analytic_endpoint_url[localhost8000] = "http://localhost:8000/" | |
analytic_api_key[localhost8000] = "snafu" | |
analytic_endpoint_url[localhost9000] = "http://localhost:9000/" | |
analytic_api_key[localhost9000] = "fubar" |
This file contains hidden or 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
<?php | |
$analyticConfigs = []; | |
$apiConfig = Zend_Registry::get('api_config'); | |
$sensorId = $apiConfig->analytic_sensor_id; | |
foreach ($apiConfig->analytic_api_key as $clientName => $apiKey){ | |
$analyticConfigs[$clientName]['apiKey'] = $apiKey; | |
} | |
foreach ($apiConfig->analytic_endpoint_url as $clientName => $endpointUrl){ | |
$analyticConfigs[$clientName]['endpointUrl'] = $endpointUrl; | |
} | |
// Another config file (not shown here) for non-secure data | |
$config = Zend_Registry::get('config'); | |
$configFromLti = filter_var($config->analytic_config_from_lti_request, FILTER_VALIDATE_BOOLEAN); | |
$debugMode = filter_var($config->analytic_debug_mode, FILTER_VALIDATE_BOOLEAN); | |
$connectionTimeoutMs = intval($config->analytic_connection_timeout_ms, 10); | |
$sensor = new Sensor($sensorId); | |
foreach ($analyticConfigs as $clientName => $clientParams) { | |
if (array_key_exists('apiKey', $clientParams) && array_key_exists('endpointUrl', $clientParams)) { | |
$sensor->registerClient($clientName . 'Http', | |
new Client($clientName . 'ClientId', | |
(new Options()) | |
->setApiKey($clientParams['apiKey']) | |
->setDebug($debugMode) | |
->setHost($clientParams['endpointUrl']) | |
->setConnectionTimeout($connectionTimeoutMs) | |
) | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is one way to do it. Each client is expected to have a name, so that's given as the index to various parameter names. Maybe a better way would be to specify them in the config as:
That would require different code to find and access all the values, but it's probably possible. I just don't know whether specifying multiple values is better this way or as coded above.