Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load config from URL #1252

Open
wants to merge 1 commit into
base: skosmos-2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions model/GlobalConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,26 @@ class GlobalConfig extends BaseConfig {
*/
private $configModifiedTime = null;

const HTTP_URL_PREFIX = 'http://';
const HTTPS_URL_PREFIX = 'https://';

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, self::HTTP_URL_PREFIX) || str_starts_with($config_name, self::HTTPS_URL_PREFIX)) {
$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 +79,12 @@ 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;
if (!str_starts_with($this->filePath, self::HTTP_URL_PREFIX) && !str_starts_with($this->filePath, self::HTTPS_URL_PREFIX)) {
// 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 +122,18 @@ private function initializeConfig()
*/
private function parseConfig($filename)
{
if (str_starts_with($filename, self::HTTP_URL_PREFIX) || str_starts_with($filename, self::HTTPS_URL_PREFIX)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could modify parseConfig to pass a flag telling it whether it is a local config, or a remote config? This way we wouldn't have to check if it starts with http or https here and in the caller function ☝️

$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