Skip to content

Commit

Permalink
Load config from URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Hale committed Dec 1, 2021
1 parent 3ea6c18 commit 4eb66f7
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions model/GlobalConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ class GlobalConfig extends BaseConfig {

public function __construct($config_name='/../config.ttl')
{
// if present, use value from env instead
$config_name_env = getenv('SKOSMOS_CONFIG');
if ($config_name_env) {
$config_name = $config_name_env;
}

$this->cache = new Cache();
try {
$this->filePath = realpath( dirname(__FILE__) . $config_name );
if (!file_exists($this->filePath)) {
throw new Exception('config.ttl file is missing, please provide one.');
if (str_starts_with($config_name, 'http://') || str_starts_with($config_name, 'https://')) {
$this->filePath = $config_name;
} else {
$this->filePath = realpath( dirname(__FILE__) . $config_name );
if (!file_exists($this->filePath)) {
throw new Exception($config_name . ' is missing, please provide a config.ttl.');
}
}
$this->initializeConfig();
} catch (Exception $e) {
Expand Down Expand Up @@ -66,10 +76,13 @@ public function getConfigModifiedTime()
private function initializeConfig()
{
try {
// retrieve last modified time for config file (filemtime returns int|bool!)
$configModifiedTime = filemtime($this->filePath);
if (!is_bool($configModifiedTime)) {
$this->configModifiedTime = $configModifiedTime;
$configModifiedTime = null;
if (!str_starts_with($this->filePath, 'http://') && !str_starts_with($this->filePath, 'https://')) {
// retrieve last modified time for config file (filemtime returns int|bool!)
$configModifiedTime = filemtime($this->filePath);
if (!is_bool($configModifiedTime)) {
$this->configModifiedTime = $configModifiedTime;
}
}
// use APC user cache to store parsed config.ttl configuration
if ($this->cache->isAvailable() && !is_null($this->configModifiedTime)) {
Expand Down Expand Up @@ -107,9 +120,18 @@ private function initializeConfig()
*/
private function parseConfig($filename)
{
if (str_starts_with($filename, 'http://') || str_starts_with($filename, 'https://')) {
$ch = curl_init($filename);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/turtle'));
$contents = curl_exec($ch);
curl_close($ch);
} else {
$contents = file_get_contents($filename);
}
$this->graph = new EasyRdf\Graph();
$parser = new SkosmosTurtleParser();
$parser->parse($this->graph, file_get_contents($filename), 'turtle', $filename);
$parser->parse($this->graph, $contents, 'turtle', $filename);
$this->namespaces = $parser->getNamespaces();
}

Expand Down

0 comments on commit 4eb66f7

Please sign in to comment.