@@ -78,7 +78,9 @@ private function initializeConfig()
7878 $ nskey = "namespaces of " . $ key ;
7979 $ this ->graph = $ this ->cache ->fetch ($ key );
8080 $ this ->namespaces = $ this ->cache ->fetch ($ nskey );
81- if ($ this ->graph === false || $ this ->namespaces === false ) { // was not found in cache
81+ if ($ this ->graph && $ this ->namespaces ) { // found in cache
82+ $ this ->resource = $ this ->configResource ($ this ->graph , "cache " );
83+ } else {
8284 $ this ->parseConfig ($ this ->filePath );
8385 $ this ->cache ->store ($ key , $ this ->graph );
8486 $ this ->cache ->store ($ nskey , $ this ->namespaces );
@@ -88,29 +90,80 @@ private function initializeConfig()
8890 $ this ->parseConfig ($ this ->filePath );
8991 }
9092
91- $ configResources = $ this ->graph ->allOfType ("skosmos:Configuration " );
92- if (is_null ($ configResources ) || !is_array ($ configResources ) || count ($ configResources ) !== 1 ) {
93- throw new Exception ("config.ttl must have exactly one skosmos:Configuration " );
94- }
95-
96- $ this ->resource = $ configResources [0 ];
9793 $ this ->initializeNamespaces ();
9894 } catch (Exception $ e ) {
9995 echo "Error: " . $ e ->getMessage ();
10096 }
10197 }
10298
99+ /**
100+ * Ensure there is exactely one skosmos:Configuration and return it.
101+ */
102+ private function configResource ($ graph , $ source ) {
103+ $ configResources = $ graph ->allOfType ("skosmos:Configuration " );
104+ if (is_null ($ configResources ) || !is_array ($ configResources ) || count ($ configResources ) !== 1 ) {
105+ throw new Exception ("$ source must have exactly one skosmos:Configuration " );
106+ }
107+ return $ configResources [0 ];
108+ }
109+
110+ /**
111+ * Retrieves, parses and includes configuration in existing configuration.
112+ * @param \EasyRdf\Resource URL or file of configuration in Turtle syntax.
113+ */
114+ private function includeConfig ($ location ) {
115+ $ location = $ location ->getUri ();
116+
117+ if (str_starts_with ($ location , "http:// " ) || str_starts_with ($ location , "https:// " )) {
118+ $ ch = curl_init ($ location );
119+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
120+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , array ('Content-type: text/turtle ' ));
121+ $ turtle = curl_exec ($ ch );
122+ $ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
123+ if ($ httpCode != 200 && $ httpCode != 303 ) {
124+ throw new Exception ("Failed to include configuration from $ location " );
125+ }
126+ curl_close ($ ch );
127+ } else {
128+ if (file_exists ($ location )) {
129+ $ turtle = file_get_contents ($ location );
130+ } else {
131+ throw new Exception ("Included config file $ location does not exist! " );
132+ }
133+ }
134+
135+ $ parser = new SkosmosTurtleParser ();
136+ try {
137+ $ graph = $ parser ->parseGraph ($ turtle , $ location );
138+ } catch (Exception $ e ) {
139+ throw new Exception ("Failed to parse $ location: " . $ e ->getMessage ());
140+ }
141+
142+ $ configResource = $ this ->configResource ($ graph , $ location );
143+ foreach ($ graph ->properties ($ configResource ) as $ property ) {
144+ foreach ($ configResource ->all ($ property ) as $ value ) {
145+ $ this ->graph ->add ($ this ->resource , $ property , $ value );
146+ }
147+ }
148+ }
149+
103150 /**
104151 * Parses configuration from the config.ttl file
105152 * @param string $filename path to config.ttl file
106153 * @throws \EasyRdf\Exception
107154 */
108155 private function parseConfig ($ filename )
109156 {
110- $ this ->graph = new EasyRdf \Graph ();
111157 $ parser = new SkosmosTurtleParser ();
112- $ parser -> parse ( $ this ->graph , file_get_contents ($ filename ), ' turtle ' , $ filename );
158+ $ this ->graph = $ parser -> parseGraph ( file_get_contents ($ filename ), $ filename );
113159 $ this ->namespaces = $ parser ->getNamespaces ();
160+
161+ $ this ->resource = $ this ->configResource ($ this ->graph , $ filename );
162+
163+ $ includes = $ this ->graph ->allResources ($ this ->resource , "skosmos:includeConfig " );
164+ foreach ($ includes as $ location ) {
165+ $ this ->includeConfig ($ location );
166+ }
114167 }
115168
116169 /**
0 commit comments