Created
February 23, 2010 10:05
-
-
Save nickdunn/312047 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
public function frontendInitialised(&$context) { | |
$uri = getCurrentPage(); | |
$uri = trim($uri, '/'); | |
// is the request for a specific render resource (HTML or RDF) | |
if (preg_match('/(rdf|html)$/', $uri)) { | |
$extension = end(explode('.', $uri)); | |
} | |
// translate URL into a possible RDF file name | |
$rdf_file = preg_replace('/\//', '-', $uri); | |
// remove file extension from end of URI | |
if ($extension) $rdf_file = preg_replace('/.' . $extension . '$/', '', $rdf_file); | |
$rdf_file = trim($rdf_file, '-'); | |
$rdf_file .= '.rdf'; | |
$rdf_file_path = WORKSPACE . '/rdf/' . $rdf_file; | |
// does the client accept application/rdf+xml? | |
$accepts_rdf = preg_match('/application\/rdf\+xml/', $_SERVER['HTTP_ACCEPT']); | |
// if the RDF file exists, this URI has been semanticised... | |
if (file_exists($rdf_file_path)) { | |
// no extension (this is the resource identifier URI), so perform content negotiation | |
if (!$extension) { | |
$resource_type = 'html'; | |
if ($accepts_rdf) $resource_type = 'rdf'; | |
header('Location: /' . $uri . '.' . $resource_type, true, 303); | |
} | |
// this is not a resource identifier URI, but a rendering, so serve correct content | |
else { | |
switch ($extension) { | |
// read RDF file | |
case 'rdf': | |
$rdf_data = fopen($rdf_file_path, 'r'); | |
header('Content-Type: text/xml'); | |
echo fread($rdf_data, filesize($rdf_file_path)); | |
fclose($rdf_data); | |
exit(); | |
break; | |
// manipulate URL and allow Symphony to serve | |
case 'html': | |
$context['page'] = preg_replace('/.' . $extension . '$/', '', $uri); | |
break; | |
} | |
} | |
} | |
// no RDF exists | |
else { | |
// but client is expecting RDF | |
if ($accepts_rdf) header("HTTP/1.1 406 Not acceptable"); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment