Location: PHPKode > projects > Rthree > r3-1.9.0/r3/generate/GenerateContext.php
<?php

/**
 * provides {@link r3_generate_GenerateContext} class
 *
 *
 * Copyright (c) 2007 Yahoo! Inc.  All rights reserved.
 * The copyrights embodied in the content of this file are licensed under the BSD
 * open source license
 *
 * @package r3
 * @subpackage generate
 */

/**
 * requires
 */
require_once("r3/domain/TemplateDomain.php");
require_once("r3/domain/TemplateObject.php");
require_once("r3/parse/Parser.php");
require_once("r3/parse/ParseController.php");
require_once("r3/generate/StdoutOutput.php");
require_once("r3/generate/FileOutput.php");

/**
 * provides {@link r3_generate_GenerateContext} class
 *
 * @package r3
 * @subpackage generate
 */
class r3_generate_GenerateContext {

    private $active=true;
    private $emitter;
    private $templateArgs=array();
    private $template_stack = array();
    private $templateObject = null;
    private $values = array();

    function __construct( r3_api_Target $target, 
                          r3_parse_Emitter $emitter
                          ) {
        $this->target = $target;
        $this->emitter = $emitter;
        $this->start();
    }

    function getEmitter() {
        return $this->emitter;
    }

    private function start() {
        $outputman = r3_util_Registry::inst()->outputManager();    
        $outputman->open( $this );
        $this->active = true;
    }

    private function confirmActive() {
        if ( ! $this->active ) {
            throw new r3_util_Exception("not active");
        }
    }

    function pushOutput( r3_generate_Output $output ) {
        $this->confirmActive();
        $outputman = r3_util_Registry::inst()->outputManager();    
        $outputman->add( $output );
        $outputman->open( $this );
    }

    function popOutput( ) {
        $this->confirmActive();
        $outputman = r3_util_Registry::inst()->outputManager();    
        return $outputman->remove( );
    }

    function output( $str ) {
        $this->confirmActive();
        $outputman = r3_util_Registry::inst()->outputManager();    
        $outputman->output( $str );
    }

    function finish() {
        $outputman = r3_util_Registry::inst()->outputManager();    
        $outputman->closeAll(  );
        $this->active = false;
    }

    //function templateOpen( r3_util_UTF8 $path ) {
       
    function visitsPerCurrentTarget( r3_domain_TemplateObject $template ) {
        if ( is_null( $this->getTarget() ) ) {
            return 0;
        }
        $tarname = $this->getTarget()->toString();
        $tplname = $template->getName()->toString();
        if ( ! isset( $this->visited[$tarname][$tplname] ) ) {
            $this->visited[$tarname][$tplname]=0;
        }
        return $this->visited[$tarname][$tplname];
    }

    function templateOpen( r3_domain_TemplateObject $template ) {
        $tarname = $this->getTarget()->toString();
        $tplname = $template->getName()->toString();
        if ( ! isset( $this->visited[$tarname][$tplname] ) ) {
            $this->visited[$tarname][$tplname]=0;
        }
        $this->visited[$tarname][$tplname]++;

        $this->confirmActive();
        $path = $template->getPath();
        $item = new r3_generate_TemplateStackItem( $template, $path->toString(), 0 );
        array_push( $this->template_stack, $item );
    }

    function templateClose( ) {
        $this->confirmActive();
        $closing = array_pop( $this->template_stack );
    }

/*
    function setTemplateObject( r3_domain_TemplateObject $obj ) {
        $this->templateObj = $obj;
    }
*/

    function getTemplateApiObject( ) {
        $domain = $this->getTemplateObject();
        if ( is_null( $domain ) ) { return null; }
        $target = $this->getTarget();
        if ( is_null( $target ) ) { return null; }
        return new r3_api_Template( $target->startLocationFull(), $domain );
    }

    function getTemplateStackItem() {
        if ( ! count( $this->template_stack ) ) {
            return null;
        }
        return $this->template_stack[ count( $this->template_stack ) -1 ];
    }

    function getTemplateObject( ) {
        $item = $this->getTemplateStackItem();
        if ( is_null( $item ) ) { return null; }
        return $item->getTemplate();
    }

    function setEmitter( r3_parse_Emitter $emitter ) {
        $this->emitter = $emitter;
    }

    function template() {
        $item = $this->getTemplateStackItem();
        if ( is_null( $item ) ) { return null; }
        return $item->getFilePath();
        /*
        if ( count( $this->template_stack ) ) {
            return $this->template_stack[count( $this->template_stack )-1];
        }
        return null;
        */
    }

    function emitter() {
        return $this->emitter;
    }

    function controller() {
        return $this->controller;
    }

    function location() {
        return $this->target->startLocationFull();
    }

    function getTarget() {
        return $this->target;
    }

    function setTemplateArgs( array $args ) {
        $this->confirmActive();
        $item = $this->getTemplateStackItem();
        if ( is_null( $item ) ) { 
            throw new r3_util_GenerateException( $this, "attempting to set args on non-existent template" );
        }
        $item->setTemplateArgs( $args );
    }

    function getTemplateArgs( ) {
        $item = $this->getTemplateStackItem();
        if ( is_null( $item ) ) { 
            throw new r3_util_GenerateException( $this, "no template from which to get args" );
        }
        return $item->getTemplateArgs();
    }

    function set( $key, $value) {
        $this->values[$key] = $value;
        return $value;
    }

    function get( $key ) {
        if ( ! isset( $this->values[$key] ) ) {
            return null;
        }
        return $this->values[$key]; 
    }

/*
    function setFileName( $file_name ) {
        //$this->fileName = $file_name;
        $item = $this->getTemplateStackItem();
        $item->setFileName( $file_name );
    }
*/

    function getFileName() {
        $item = $this->getTemplateStackItem();
        if ( is_null( $item ) ) { 
            throw new r3_util_GenerateException( $this, "no template from which to get filename" );
        }
        return $item->getFileName( );
        /*
        if ( ! isset( $this->fileName ) ) {
            return null;
        }
        return $this->fileName;
        */
    }
/*
    function setFilePath( $file_path ) {
        // $this->filePath = $file_path;
        $item = $this->getTemplateStackItem();
        if ( is_null( $item ) ) { 
            throw new r3_util_GenerateException( $this, "no template on which to set filepath" );
        }
        $item->setFilePath( $file_path );
    }
*/

    function getFilePath() {
        $item = $this->getTemplateStackItem();
        if ( is_null( $item ) ) { 
            throw new r3_util_GenerateException( $this, "no template from which to get filepath" );
        }
        return $item->getFilePath();
        /*
        if ( ! isset( $this->filePath ) ) {
            return null;
        }
        return $this->filePath;
        */
    }

    function setLineNo( $line_no ) {
        $item = $this->getTemplateStackItem();
        if ( is_null( $item ) ) { 
            throw new r3_util_GenerateException( $this, "no template on which to set line number" );
        }
        return $item->setLineNo( $line_no );
        //$this->lineno = $line_no;
    }

    function getLineNo() {
        $item = $this->getTemplateStackItem();
        if ( is_null( $item ) ) { 
            throw new r3_util_GenerateException( $this, "no template on which to get line number" );
        }
        return $item->getLineNo();
    /*
        if ( ! isset( $this->lineno ) ) {
            return 0;
        }
        return $this->lineno;
    */
    }

    function trans( $source, $esc = null ) {

        $tr_api_mgr = r3_util_Registry::inst()->translationManager();
        $location = $this->location();
        $source_utf8 = utf8($source);
        
        if ( is_null($esc) ) {
            $trans = $tr_api_mgr->getTranslation($location, $source_utf8);
            if( ! is_null($trans) ) {
                return $trans->getTranslation()->toString();
            } else {
                return $source;
            }
        } else {
            $trans = $tr_api_mgr->inTemplateTranslation( $source_utf8, $source_utf8, $location, $esc );
            return $trans->toString();
        }
    }
}

class r3_generate_TemplateStackItem {
    private $template;
    private $filepath;
    private $filename;
    private $line_no;
    private $args;

    function __construct( $template, $filepath, $args=array(), $line_no=0 ) {
        $this->filepath = $filepath;
        $this->line_no = $line_no;
        $this->template = $template;
        $this->args = $args;

    }

    function setTemplateArgs( array $args ) {
        $this->args = $args;
    }

    function getTemplateArgs() {
        return $this->args;
    }

    function getTemplate() {
        return $this->template; 
    }

    function setFilePath( $path ) {
        return $this->filepath=$path;
    }
    function getFileName() {
        return $this->template->getName()->toString();
    }

    function setLineNo( $no ) {
        $this->line_no = $no;
    }

    function getLineNo() {
        return $this->line_no;
    }

    function getFilePath() {
        return $this->filepath;
    }
}
?>
Return current item: Rthree