<commit_before><?php
abstract class Kwf_Component_Renderer_Abstract
{
    protected $_enableCache = null;

    protected $_includedComponents = array();
    protected $_minLifetime;

    private $_helpers = array();

    const PLUGIN_TYPE_AFTER = 'A';
    const PLUGIN_TYPE_BEFORE = 'B';
    const PLUGIN_TYPE_USECACHE = 'C';
    const PLUGIN_TYPE_REPLACE = 'R';

    public function includedComponent($targetComponentId, $targetType)
    {
        $this->_includedComponents[] = $targetComponentId;
    }

    public function __construct()
    {
        $this->_enableCache = !Kwf_Config::getValue('debug.componentCache.disable');
        if (Kwf_Component_Data_Root::getShowInvisible()) {
            $this->_enableCache = false;
        }
    }

    public function setEnableCache($enableCache)
    {
        if ($enableCache === null) throw new Kwf_Exception('expected boolean parameter');
        $this->_enableCache = $enableCache;
    }

    /**
     * Renders a single component without master
     */
    public function renderComponent($component, &$hasDynamicParts = false)
    {
        $helper = new Kwf_Component_View_Helper_Component();
        $helper->setRenderer($this);
        $content = $helper->component($component);

        $ret = $this->_renderPass2($content, $hasDynamicParts);
        Kwf_Component_Cache::getInstance()->writeBuffer();
        return $ret;
    }

    protected abstract function _getRendererName();

    //overridden by Renderer_Mail to use mail templates
    public function getTemplate(Kwf_Component_Data $component, $type)
    {
        $template = Kwc_Abstract::getTemplateFile($component->componentClass, $type);
        if (!$template) throw new Kwf_Exception("No $type-Template found for '{$component->componentClass}'");
        return $template;
    }

    public function render($ret)
    {
        $ret = $this->_renderPass2($ret, $hasDynamicParts);
        return $ret;
    }

    private function _findAndExecutePlugins($ret, $pluginType, &$hasDynamicParts)
    {
        static $benchmarkEnabled;
        if (!isset($benchmarkEnabled)) $benchmarkEnabled = Kwf_Benchmark::isEnabled();

        while (($start = strpos($ret, '<plugin'.$pluginType.' ')) !== false) {
            $hasDynamicParts = true;
            $startEnd = strpos($ret, '>', $start);
            $args = explode(' ', substr($ret, $start+9, $startEnd-$start-9));
            $end = strpos($ret, '</plugin'.$pluginType.' '.$args[0].'>');
            $content = substr($ret, $startEnd+1, $end-$startEnd-1);
            if ($benchmarkEnabled) $startTime = microtime(true);
            $plugin = Kwf_Component_Plugin_Abstract::getInstance($args[1], $args[2]);
            if ($pluginType == self::PLUGIN_TYPE_USECACHE) {
                if (!$plugin->useViewCache($this)) {
                    $content = $this->_getHelper('component')->render($args[2], array());
                }
            } else if ($pluginType == self::PLUGIN_TYPE_AFTER || $pluginType == self::PLUGIN_TYPE_BEFORE) {
                $content = $plugin->processOutput($content, $this);
            } else if ($pluginType == self::PLUGIN_TYPE_REPLACE) {
                $c = $plugin->replaceOutput($this);
                if ($c !== false) {
                    $content = $c;
                }
            }

            if ($benchmarkEnabled) Kwf_Benchmark::subCheckpoint('plugin '.$args[1], microtime(true)-$startTime);
            $ret = substr($ret, 0, $start).$content.substr($ret, $end+11+strlen($args[0]));
        }
        return $ret;
    }

    private function _findSinglePlugin($pluginType, $content)
    {
        if (substr($content, 0, 9) == '<plugin'.$pluginType.' ') {
            $pluginEnd = strpos($content, '>');
            $args = explode(' ', substr($content, 10, $pluginEnd-10));
            $content = substr($content, $pluginEnd+1, strrpos($content, '</')-$pluginEnd-1);
            return array(
                'plugin' => Kwf_Component_Plugin_Abstract::getInstance($args[1], $args[2]),
                'content' => $content
            );
        }
        return null;
    }

    private function _addPluginPlaceholders($content, $plugins, $componentId, $pluginTypes)
    {
        static $pluginNr = 0;
        if (in_array('before', $pluginTypes) && isset($plugins['before'])) {
            foreach ($plugins['before'] as $pluginClass) {
                $pluginNr++;
                $content = "<plugin".self::PLUGIN_TYPE_BEFORE." $pluginNr $pluginClass $componentId>$content</plugin".self::PLUGIN_TYPE_BEFORE." $pluginNr>";
            }
        }

        if (in_array('after', $pluginTypes) && isset($plugins['after'])) {
            foreach ($plugins['after'] as $pluginClass) {
                $pluginNr++;
                $content = "<plugin".self::PLUGIN_TYPE_AFTER." $pluginNr $pluginClass $componentId>$content</plugin".self::PLUGIN_TYPE_AFTER." $pluginNr>";
            }
        }

        if (in_array('replace', $pluginTypes) && isset($plugins['replace'])) {
            foreach ($plugins['replace'] as $pluginClass) {
                $pluginNr++;
                $content = "<plugin".self::PLUGIN_TYPE_REPLACE." $pluginNr $pluginClass $componentId>$content</plugin".self::PLUGIN_TYPE_REPLACE." $pluginNr>";
            }
        }

        if (in_array('useCache', $pluginTypes) && isset($plugins['useCache'])) {
            foreach ($plugins['useCache'] as $pluginClass) {
                $pluginNr++;
                $content = "<plugin".self::PLUGIN_TYPE_USECACHE." $pluginNr $pluginClass $componentId>$content</plugin".self::PLUGIN_TYPE_USECACHE." $pluginNr>";
            }
        }
        return $content;
    }

    protected static function _getGroupedViewPlugins(Kwf_Component_Data $component)
    {
        $plugins = array();
        foreach ($component->getComponent()->getActiveViewPlugins() as $p) {
            if (is_instance_of($p, 'Kwf_Component_Plugin_Interface_ViewBeforeCache')) {
                $plugins['beforeCache'][] = $p;
            } else if (is_instance_of($p, 'Kwf_Component_Plugin_Interface_ViewBeforeChildRender')) {
                $plugins['before'][] = $p;
            } else if (is_instance_of($p, 'Kwf_Component_Plugin_Interface_ViewAfterChildRender')) {
                $plugins['after'][] = $p;
            } else if (is_instance_of($p, 'Kwf_Component_Plugin_Interface_ViewReplace')) {
                $plugins['replace'][] = $p;
            } else if (is_instance_of($p, 'Kwf_Component_Plugin_Interface_UseViewCache')) {
                $plugins['useCache'][] = $p;
            }
        }
        return $plugins;
    }

    private function _executePlugins($content, $plugins, $componentId, $pluginTypes)
    {
        if (in_array('replace', $pluginTypes) && isset($plugins['replace'])) {
            foreach ($plugins['replace'] as $pluginClass) {
                $plugin = Kwf_Component_Plugin_Abstract::getInstance($pluginClass, $componentId);
                $r = $plugin->replaceOutput($this);
                if ($r !== false) {
                    $content = $r;
                }
            }
        }
        if (in_array('beforeCache', $pluginTypes) && isset($plugins['beforeCache'])) {
            foreach ($plugins['beforeCache'] as $pluginClass) {
                $plugin = Kwf_Component_Plugin_Abstract::getInstance($pluginClass, $componentId);
                $content = $plugin->processOutput($content, $this);
            }
        }
        if (in_array('before', $pluginTypes) && isset($plugins['before'])) {
            foreach ($plugins['before'] as $pluginClass) {
                $plugin = Kwf_Component_Plugin_Abstract::getInstance($pluginClass, $componentId);
                $content = $plugin->processOutput($content, $this);
            }
        }
        return $content;
    }

    private function _renderUncached($componentId, $type, $config)
    {
        $plugins = array();
        if ($type == 'component') {
            $c = Kwf_Component_Data_Root::getInstance()
                ->getComponentById($componentId, array('ignoreVisible'=>true));
            $plugins = $this->_getGroupedViewPlugins($c);
        }
        $content = $this->_executePlugins(null, $plugins, $componentId, array('replace'));

        if (is_null($content)) {
            $content = $this->_getHelper($type)->render($componentId, $config);
            $content = $this->_executePlugins($content, $plugins, $componentId, array('beforeCache'));
        }
        $content = $this->_executePlugins($content, $plugins, $componentId, array('before'));
        $content = $this->_addPluginPlaceholders($content, $plugins, $componentId, array('after'));

        return $content;
    }

    private function _renderAndCache($componentId, $type, $value, $config, $addPluginPlaceholders, &$cacheSaved = true)
    {
        $plugins = array();
        if ($type == 'component') {
            $c = Kwf_Component_Data_Root::getInstance()
                ->getComponentById($componentId, array('ignoreVisible'=>true));
            $plugins = $this->_getGroupedViewPlugins($c);
        }

        $cacheSaved = true;
        if (isset($plugins['useCache'])) {
            foreach ($plugins['useCache'] as $p) {
                $p = Kwf_Component_Plugin_Abstract::getInstance($p, $componentId);<commit_msg>show better exception msg when component can't be found<commit_after><?php
abstract class Kwf_Component_Renderer_Abstract
{
    protected $_enableCache = null;

    protected $_includedComponents = array();
    protected $_minLifetime;

    private $_helpers = array();

    const PLUGIN_TYPE_AFTER = 'A';
    const PLUGIN_TYPE_BEFORE = 'B';
    const PLUGIN_TYPE_USECACHE = 'C';
    const PLUGIN_TYPE_REPLACE = 'R';

    public function includedComponent($targetComponentId, $targetType)
    {
        $this->_includedComponents[] = $targetComponentId;
    }

    public function __construct()
    {
        $this->_enableCache = !Kwf_Config::getValue('debug.componentCache.disable');
        if (Kwf_Component_Data_Root::getShowInvisible()) {
            $this->_enableCache = false;
        }
    }

    public function setEnableCache($enableCache)
    {
        if ($enableCache === null) throw new Kwf_Exception('expected boolean parameter');
        $this->_enableCache = $enableCache;
    }

    /**
     * Renders a single component without master
     */
    public function renderComponent($component, &$hasDynamicParts = false)
    {
        $helper = new Kwf_Component_View_Helper_Component();
        $helper->setRenderer($this);
        $content = $helper->component($component);

        $ret = $this->_renderPass2($content, $hasDynamicParts);
        Kwf_Component_Cache::getInstance()->writeBuffer();
        return $ret;
    }

    protected abstract function _getRendererName();

    //overridden by Renderer_Mail to use mail templates
    public function getTemplate(Kwf_Component_Data $component, $type)
    {
        $template = Kwc_Abstract::getTemplateFile($component->componentClass, $type);
        if (!$template) throw new Kwf_Exception("No $type-Template found for '{$component->componentClass}'");
        return $template;
    }

    public function render($ret)
    {
        $ret = $this->_renderPass2($ret, $hasDynamicParts);
        return $ret;
    }

    private function _findAndExecutePlugins($ret, $pluginType, &$hasDynamicParts)
    {
        static $benchmarkEnabled;
        if (!isset($benchmarkEnabled)) $benchmarkEnabled = Kwf_Benchmark::isEnabled();

        while (($start = strpos($ret, '<plugin'.$pluginType.' ')) !== false) {
            $hasDynamicParts = true;
            $startEnd = strpos($ret, '>', $start);
            $args = explode(' ', substr($ret, $start+9, $startEnd-$start-9));
            $end = strpos($ret, '</plugin'.$pluginType.' '.$args[0].'>');
            $content = substr($ret, $startEnd+1, $end-$startEnd-1);
            if ($benchmarkEnabled) $startTime = microtime(true);
            $plugin = Kwf_Component_Plugin_Abstract::getInstance($args[1], $args[2]);
            if ($pluginType == self::PLUGIN_TYPE_USECACHE) {
                if (!$plugin->useViewCache($this)) {
                    $content = $this->_getHelper('component')->render($args[2], array());
                }
            } else if ($pluginType == self::PLUGIN_TYPE_AFTER || $pluginType == self::PLUGIN_TYPE_BEFORE) {
                $content = $plugin->processOutput($content, $this);
            } else if ($pluginType == self::PLUGIN_TYPE_REPLACE) {
                $c = $plugin->replaceOutput($this);
                if ($c !== false) {
                    $content = $c;
                }
            }

            if ($benchmarkEnabled) Kwf_Benchmark::subCheckpoint('plugin '.$args[1], microtime(true)-$startTime);
            $ret = substr($ret, 0, $start).$content.substr($ret, $end+11+strlen($args[0]));
        }
        return $ret;
    }

    private function _findSinglePlugin($pluginType, $content)
    {
        if (substr($content, 0, 9) == '<plugin'.$pluginType.' ') {
            $pluginEnd = strpos($content, '>');
            $args = explode(' ', substr($content, 10, $pluginEnd-10));
            $content = substr($content, $pluginEnd+1, strrpos($content, '</')-$pluginEnd-1);
            return array(
                'plugin' => Kwf_Component_Plugin_Abstract::getInstance($args[1], $args[2]),
                'content' => $content
            );
        }
        return null;
    }

    private function _addPluginPlaceholders($content, $plugins, $componentId, $pluginTypes)
    {
        static $pluginNr = 0;
        if (in_array('before', $pluginTypes) && isset($plugins['before'])) {
            foreach ($plugins['before'] as $pluginClass) {
                $pluginNr++;
                $content = "<plugin".self::PLUGIN_TYPE_BEFORE." $pluginNr $pluginClass $componentId>$content</plugin".self::PLUGIN_TYPE_BEFORE." $pluginNr>";
            }
        }

        if (in_array('after', $pluginTypes) && isset($plugins['after'])) {
            foreach ($plugins['after'] as $pluginClass) {
                $pluginNr++;
                $content = "<plugin".self::PLUGIN_TYPE_AFTER." $pluginNr $pluginClass $componentId>$content</plugin".self::PLUGIN_TYPE_AFTER." $pluginNr>";
            }
        }

        if (in_array('replace', $pluginTypes) && isset($plugins['replace'])) {
            foreach ($plugins['replace'] as $pluginClass) {
                $pluginNr++;
                $content = "<plugin".self::PLUGIN_TYPE_REPLACE." $pluginNr $pluginClass $componentId>$content</plugin".self::PLUGIN_TYPE_REPLACE." $pluginNr>";
            }
        }

        if (in_array('useCache', $pluginTypes) && isset($plugins['useCache'])) {
            foreach ($plugins['useCache'] as $pluginClass) {
                $pluginNr++;
                $content = "<plugin".self::PLUGIN_TYPE_USECACHE." $pluginNr $pluginClass $componentId>$content</plugin".self::PLUGIN_TYPE_USECACHE." $pluginNr>";
            }
        }
        return $content;
    }

    protected static function _getGroupedViewPlugins(Kwf_Component_Data $component)
    {
        $plugins = array();
        foreach ($component->getComponent()->getActiveViewPlugins() as $p) {
            if (is_instance_of($p, 'Kwf_Component_Plugin_Interface_ViewBeforeCache')) {
                $plugins['beforeCache'][] = $p;
            } else if (is_instance_of($p, 'Kwf_Component_Plugin_Interface_ViewBeforeChildRender')) {
                $plugins['before'][] = $p;
            } else if (is_instance_of($p, 'Kwf_Component_Plugin_Interface_ViewAfterChildRender')) {
                $plugins['after'][] = $p;
            } else if (is_instance_of($p, 'Kwf_Component_Plugin_Interface_ViewReplace')) {
                $plugins['replace'][] = $p;
            } else if (is_instance_of($p, 'Kwf_Component_Plugin_Interface_UseViewCache')) {
                $plugins['useCache'][] = $p;
            }
        }
        return $plugins;
    }

    private function _executePlugins($content, $plugins, $componentId, $pluginTypes)
    {
        if (in_array('replace', $pluginTypes) && isset($plugins['replace'])) {
            foreach ($plugins['replace'] as $pluginClass) {
                $plugin = Kwf_Component_Plugin_Abstract::getInstance($pluginClass, $componentId);
                $r = $plugin->replaceOutput($this);
                if ($r !== false) {
                    $content = $r;
                }
            }
        }
        if (in_array('beforeCache', $pluginTypes) && isset($plugins['beforeCache'])) {
            foreach ($plugins['beforeCache'] as $pluginClass) {
                $plugin = Kwf_Component_Plugin_Abstract::getInstance($pluginClass, $componentId);
                $content = $plugin->processOutput($content, $this);
            }
        }
        if (in_array('before', $pluginTypes) && isset($plugins['before'])) {
            foreach ($plugins['before'] as $pluginClass) {
                $plugin = Kwf_Component_Plugin_Abstract::getInstance($pluginClass, $componentId);
                $content = $plugin->processOutput($content, $this);
            }
        }
        return $content;
    }

    private function _renderUncached($componentId, $type, $config)
    {
        $plugins = array();
        if ($type == 'component') {
            $c = Kwf_Component_Data_Root::getInstance()
                ->getComponentById($componentId, array('ignoreVisible'=>true));
            if (!$c) {
                throw new Kwf_Exception("Didn't get '$componentId'");
            }
            $plugins = $this->_getGroupedViewPlugins($c);
        }
        $content = $this->_executePlugins(null, $plugins, $componentId, array('replace'));

        if (is_null($content)) {
            $content = $this->_getHelper($type)->render($componentId, $config);
            $content = $this->_executePlugins($content, $plugins, $componentId, array('beforeCache'));
        }
        $content = $this->_executePlugins($content, $plugins, $componentId, array('before'));
        $content = $this->_addPluginPlaceholders($content, $plugins, $componentId, array('after'));

        return $content;
    }

    private function _renderAndCache($componentId, $type, $value, $config, $addPluginPlaceholders, &$cacheSaved = true)
    {
        $plugins = array();
        if ($type == 'component') {
            $c = Kwf_Component_Data_Root::getInstance()
                ->getComponentById($componentId, array('ignoreVisible'=>true));
            $plugins = $this->_getGroupedViewPlugins($c);
        }

        $cacheSaved = true;
        if (isset($plugins['useCache'])) {
            foreach ($plugins['useCache'] as $p) {
                $p = Kwf_Component_Plugin_Abstract::getInstance($p, $componentId);
{'fraction_non_alphanumeric': 0.1500293442885344, 'fraction_numerical': 0.0027743690977965105, 'mean_word_length': 2.6046153846153848, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5197729', 'n_tokens_mistral': 5382, 'n_tokens_neox': 5203, 'n_words': 950}