Skip to content

Instantly share code, notes, and snippets.

@lsloan
Created July 14, 2015 16:07
Show Gist options
  • Save lsloan/18c29f267f68aacb045f to your computer and use it in GitHub Desktop.
Save lsloan/18c29f267f68aacb045f to your computer and use it in GitHub Desktop.
Using arrays from config files with Zend Framework
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"
<?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)
)
);
}
}
@lsloan
Copy link
Author

lsloan commented Jul 14, 2015

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:

analytic.sensor_id = "05D2D6AB-B9CE-4BAC-98FB-EB96F25F0D29"
analytic.localhost8000.endpoint_url = "http://localhost:8000/"
analytic.localhost8000.api_key = "snafu"
analytic.localhost9000.endpoint_url = "http://localhost:9000/"
analytic.localhost9000.api_key = "fubar"

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment