Skip to content

Commit 4eb66f7

Browse files
author
Mark Hale
committed
Load config from URL.
1 parent 3ea6c18 commit 4eb66f7

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

model/GlobalConfig.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,21 @@ class GlobalConfig extends BaseConfig {
3232

3333
public function __construct($config_name='/../config.ttl')
3434
{
35+
// if present, use value from env instead
36+
$config_name_env = getenv('SKOSMOS_CONFIG');
37+
if ($config_name_env) {
38+
$config_name = $config_name_env;
39+
}
40+
3541
$this->cache = new Cache();
3642
try {
37-
$this->filePath = realpath( dirname(__FILE__) . $config_name );
38-
if (!file_exists($this->filePath)) {
39-
throw new Exception('config.ttl file is missing, please provide one.');
43+
if (str_starts_with($config_name, 'http://') || str_starts_with($config_name, 'https://')) {
44+
$this->filePath = $config_name;
45+
} else {
46+
$this->filePath = realpath( dirname(__FILE__) . $config_name );
47+
if (!file_exists($this->filePath)) {
48+
throw new Exception($config_name . ' is missing, please provide a config.ttl.');
49+
}
4050
}
4151
$this->initializeConfig();
4252
} catch (Exception $e) {
@@ -66,10 +76,13 @@ public function getConfigModifiedTime()
6676
private function initializeConfig()
6777
{
6878
try {
69-
// retrieve last modified time for config file (filemtime returns int|bool!)
70-
$configModifiedTime = filemtime($this->filePath);
71-
if (!is_bool($configModifiedTime)) {
72-
$this->configModifiedTime = $configModifiedTime;
79+
$configModifiedTime = null;
80+
if (!str_starts_with($this->filePath, 'http://') && !str_starts_with($this->filePath, 'https://')) {
81+
// retrieve last modified time for config file (filemtime returns int|bool!)
82+
$configModifiedTime = filemtime($this->filePath);
83+
if (!is_bool($configModifiedTime)) {
84+
$this->configModifiedTime = $configModifiedTime;
85+
}
7386
}
7487
// use APC user cache to store parsed config.ttl configuration
7588
if ($this->cache->isAvailable() && !is_null($this->configModifiedTime)) {
@@ -107,9 +120,18 @@ private function initializeConfig()
107120
*/
108121
private function parseConfig($filename)
109122
{
123+
if (str_starts_with($filename, 'http://') || str_starts_with($filename, 'https://')) {
124+
$ch = curl_init($filename);
125+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
126+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/turtle'));
127+
$contents = curl_exec($ch);
128+
curl_close($ch);
129+
} else {
130+
$contents = file_get_contents($filename);
131+
}
110132
$this->graph = new EasyRdf\Graph();
111133
$parser = new SkosmosTurtleParser();
112-
$parser->parse($this->graph, file_get_contents($filename), 'turtle', $filename);
134+
$parser->parse($this->graph, $contents, 'turtle', $filename);
113135
$this->namespaces = $parser->getNamespaces();
114136
}
115137

0 commit comments

Comments
 (0)