<commit_before><?php
/**
 * Nooku Framework - http://www.nooku.org
 *
 * @copyright	Copyright (C) 2007 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		git://git.assembla.com/nooku-framework.git for the canonical source repository
 */

namespace Nooku\Library;

/**
 * Abstract Template View
 *
 * @author  <NAME> <http://nooku.assembla.com/profile/johanjanssens>
 * @package Nooku\Library\View
 */
abstract class ViewTemplate extends ViewAbstract
{
    /**
     * Template object or identifier
     *
     * @var string|object
     */
    protected $_template;

    /**
     * Auto assign
     *
     * @var boolean
     */
    protected $_auto_assign;

    /**
     * Layout name
     *
     * @var string
     */
    protected $_layout;

    /**
     * Constructor
     *
     * @param  ObjectConfig $config  An optional ObjectConfig object with configuration options
     */
    public function __construct(ObjectConfig $config)
    {
        parent::__construct($config);

        //Set the auto assign state
        $this->_auto_assign = $config->auto_assign;

        //Set the layout
        $this->setLayout($config->layout);

        //Set the template object
        $this->_template = $config->template;

        //Attach the template filters
        $filters = (array)ObjectConfig::unbox($config->template_filters);

        foreach ($filters as $key => $value)
        {
            if (is_numeric($key)) {
                $this->getTemplate()->attachFilter($value);
            } else {
                $this->getTemplate()->attachFilter($key, $value);
            }
        }
    }

    /**
     * Initializes the config for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param   ObjectConfig $config  An optional ObjectConfig object with configuration options
     * @return  void
     */
    protected function _initialize(ObjectConfig $config)
    {
        //Clone the identifier
        $identifier = clone $this->getIdentifier();

        $config->append(array(
            'layout'           => '',
            'template'         => $this->getName(),
            'template_filters' => array('shorttag', 'function', 'url', 'decorator'),
            'auto_assign'      => true,
        ));

        parent::_initialize($config);
    }

    /**
     * Return the views output
     *
     * @param ViewContext	$context A view context object
     * @return string  The output of the view
     */
    protected function _actionRender(ViewContext $context)
    {
        $layout     = $this->getLayout();
        $format     = $this->getFormat();
        $data       = $this->getData();

        $identifier = clone $this->getIdentifier();
        $identifier->name = $layout.'.'.$format;

        $this->_content = (string) $this->getTemplate()
            ->load($identifier)
            ->compile()
            ->evaluate($data)
            ->render();

        return parent::_actionRender($context);
    }

    /**
     * Get the layout
     *
     * @return string The layout name
     */
    public function getLayout()
    {
        return empty($this->_layout) ? 'default' : $this->_layout;
    }
<commit_msg>Renamed auto_assign config option to auto_fetch. Implemented new fetchData() callback to be triggered beforeRender to fetch the data and inject it into the view through the context.<commit_after><?php
/**
 * Nooku Framework - http://www.nooku.org
 *
 * @copyright	Copyright (C) 2007 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		git://git.assembla.com/nooku-framework.git for the canonical source repository
 */

namespace Nooku\Library;

/**
 * Abstract Template View
 *
 * @author  <NAME> <http://nooku.assembla.com/profile/johanjanssens>
 * @package Nooku\Library\View
 */
abstract class ViewTemplate extends ViewAbstract
{
    /**
     * Template object or identifier
     *
     * @var string|object
     */
    protected $_template;

    /**
     * Auto assign
     *
     * @var boolean
     */
    protected $_auto_fetch;

    /**
     * Layout name
     *
     * @var string
     */
    protected $_layout;

    /**
     * Constructor
     *
     * @param  ObjectConfig $config  An optional ObjectConfig object with configuration options
     */
    public function __construct(ObjectConfig $config)
    {
        parent::__construct($config);

        //Set the auto assign state
        $this->_auto_fetch = $config->auto_fetch;

        //Set the layout
        $this->setLayout($config->layout);

        //Set the template object
        $this->setTemplate($config->template);

        //Attach the template filters
        $filters = (array)ObjectConfig::unbox($config->template_filters);

        foreach ($filters as $key => $value)
        {
            if (is_numeric($key)) {
                $this->getTemplate()->attachFilter($value);
            } else {
                $this->getTemplate()->attachFilter($key, $value);
            }
        }

        //Fetch the view data before rendering
        $this->registerCallback('before.render' , array($this, 'fetchData'));
    }

    /**
     * Initializes the config for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param   ObjectConfig $config  An optional ObjectConfig object with configuration options
     * @return  void
     */
    protected function _initialize(ObjectConfig $config)
    {
        //Clone the identifier
        $identifier = clone $this->getIdentifier();

        $config->append(array(
            'layout'           => '',
            'template'         => $this->getName(),
            'template_filters' => array('shorttag', 'function', 'url', 'decorator'),
            'auto_fetch'       => true,
        ));

        parent::_initialize($config);
    }

    /**
     * Return the views output
     *
     * @param ViewContext	$context A view context object
     * @return string  The output of the view
     */
    protected function _actionRender(ViewContext $context)
    {
        $layout     = $this->getLayout();
        $format     = $this->getFormat();
        $data       = $this->getData();

        //Handle partial layout paths
        if (is_string($layout) && strpos($layout, '.') === false)
        {
            $identifier = clone $this->getIdentifier();
            $identifier->name = $layout;

            $layout = (string) $identifier;
        }

        $this->_content = (string) $this->getTemplate()
            ->load((string) $layout.'.'.$format)
            ->compile()
            ->evaluate($data)
            ->render();

        return parent::_actionRender($context);
    }

    /**
     * Fetch the view data
     *
     * This function will always fetch the model state. Model data will only be fetched if the auto_fetch property is
     * set to TRUE.
     *
     * @param ViewContext	$context A view context object
     * @return void
     */
    public function fetchData(ViewContext $context)
    {
        $model = $this->getModel();

        //Auto-assign the state to the view
        $context->data->state = $model->getState();

        //Auto-assign the data from the model
        if($this->_auto_fetch)
        {
            //Get the view name
            $name = $this->getName();

            //Assign the data of the model to the view
            if(StringInflector::isPlural($name))
            {
                $context->data->$name = $model->getRowset();
                $context->data->total = $model->getTotal();
            }
            else $context->data->$name = $model->getRow();
        }
    }

    /**
     * Get the layout
     *
     * @return string The layout name
     */
    public function getLayout()
    {
        return empty($this->_layout) ? 'default' : $this->_layout;
    }

{'fraction_non_alphanumeric': 0.12999373825923607, 'fraction_numerical': 0.0022542266750156543, 'mean_word_length': 1.9932533733133433, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5597985', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2169, 'n_words': 561}