Skip to content

Instantly share code, notes, and snippets.

@Rendez
Created January 2, 2011 22:34
Show Gist options
  • Save Rendez/762886 to your computer and use it in GitHub Desktop.
Save Rendez/762886 to your computer and use it in GitHub Desktop.
<?php
/**
* @package StaticFiles
*
* @author Luis Merino <mail@luismerino.name>
* @copyright Author
*/
class StaticFilesConfigHandler extends AgaviXmlConfigHandler
{
const XML_NAMESPACE = 'http://company.com/projectname/config/parts/static_files/1.0';
public function execute(AgaviXmlConfigDomDocument $document)
{
// setting up our default namespace
$document->setDefaultNamespace(self::XML_NAMESPACE, 'static_files');
$$routing = $this->context->getRouting();
$data = array();
// let's do our fancy work
foreach($document->getConfigurationElements() as $cfg) {
if($cfg->has('routes')) {
$this->parseRoutesAndFiles($routing, $cfg->get('routes'), $data);
}
}
$code = sprintf('$this->files = %s;', var_export($data, true));
return $this->generate($code, $document->documentURI);
}
protected function parseRoutesAndFiles(AgaviRouting $routing, $routes, &$data)
{
$controller = $this->context->getController();
$request = $this->context->getRequest();
foreach($routes as $route) {
$outputTypes = array();
$routeName = $route->getAttribute('name');
if($routeName !== '*' && is_null($routing->getRoute($routeName))) {
throw new AgaviConfigurationException('Route name "' . $routeName . '" does not exist or is not correct.');
}
if($route->hasAttribute('output_type')) {
foreach(explode(' ', $route->getAttribute('output_type')) as $ot) {
if($controller->getOutputType($ot)){
$outputTypes[] = $ot;
}
}
} else {
$outputTypes[] = $controller->getOutputType()->getName(); // Defaults to HTML
}
foreach($route->get('filelist') as $filelist){
$metatype = $filelist->getAttribute('metatype');
foreach($filelist->getElementsByTagName('file') as $file) {
foreach($outputTypes as $ot) {
if ($file->hasAttribute('name')) {
$data[$routeName][$ot][$metatype][$file->getAttribute('name')] = AgaviToolkit::expandDirectives($file->getValue());
} else {
$data[$routeName][$ot][$metatype][] = AgaviToolkit::expandDirectives($file->getValue());
}
}
}
}
}
}
}
?>
@Rendez
Copy link
Author

Rendez commented Jan 3, 2011

Line 23: Checking if is defined – otherwise it would mean node is empty. Each node will be parsed in parseRoutesAndFiles() and the output will be appended to $data.

Line 38: The loop will look into every defined node, thanks to the xpath feature.

Line 41: It's possible to validate each route name against the ones defined in routing.xml!… Perhaps a typo, or you forgot to type it? Not a problem, Agavi Exception Templates are among the most good-looking to inform you.

Line 47: It needs two separate type of files, and so far those are scripts and styles, as well as the defined output types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment