<?php
/**
 * This task is used to apply XSL transformations to schema.xml
 *
 * It will look for transformations in <project_dir>/data/transform/*.xsl
 * The schema.xml file will be backuped before transformation.
 *
 * @author  <trivoallan@clever-age.com>
 *
 * usage : 
 *   symfony transform-schema transformation
 * 
 * installation :
 *   Just drop it in SF_DATA_DIR/tasks/
 */


pake_desc( 'apply transformations to your data model' );
pake_task( 'transform-schema', 'project_exists' );

function run_transform_schema( $task, $args ) 
{

    // Check params
    // -- missing params ?
    if ( !count($args) ) {
        throw new Exception( 'You must provide a transformation to apply.' );
    }

    // -- tranformation exists ?
    $transformation_filename = sprintf( '%s/data/transform/%s.xsl', sfConfig::get('sf_root_dir'), $args[0] );
    if ( !file_exists($transformation_filename) ) {
        throw new Exception( "This transformation doesn't exist." );
    }

    // -- schema exists ?
    $schema_filename = sprintf( '%s/schema.xml', sfConfig::get('sf_config_dir') );
    if ( !file_exists($schema_filename) ) {
        throw new Exception( "Missing schema.xml" );
    }

    // Backup schema
    pake_copy( $schema_filename, $schema_filename . '.previous', array('override' => true) );
    
    // Transform schema
    $xslt = new xsltProcessor;
    $xslt->importStyleSheet( DomDocument::load($transformation_filename) );
    $out = $xslt->transformToXML( DomDocument::loadXML(file_get_contents($schema_filename)) );
    file_put_contents( $schema_filename, $out );
    

}

?>