<commit_before><?php
class Vps_Component_Abstract
{
    private static $_settings = null;
    private static $_rebuildingSettings = false;
    private static $_cacheSettings = array();
    private static $_modelsCache = array(
        'own' => array(),
        'child' => array(),
        'form' => array(),
        'table' => array()
    );

    public function __construct()
    {
        $this->_init();
    }

    /**
     * Wird nach dem Konstruktor aufgerufen. Initialisierungscode in Unterklassen ist hier richtig.
     */
    protected function _init()
    {
    }

    public static function hasSettings($class)
    {
        static $prefix;
        if (!isset($prefix)) $prefix = Vps_Cache::getUniquePrefix();

        $cacheId = 'hasSettings-'.$class;
        $ret = Vps_Cache_Simple::fetch($cacheId, $success);
        if ($success) {
            return $ret;
        }

        //& für performance
        $s =& self::_getSettingsCached();
        $ret = isset($s[$class]);
        Vps_Cache_Simple::add($cacheId, $ret);
        return $ret;
    }

    public static function hasSetting($class, $setting)
    {
        if (self::$_rebuildingSettings) {
            //um endlosschleife in settingsCache zu verhindern

            self::_verifyComponentClass($class);
            $settings = self::_loadCacheSettings($class);
            return isset($settings[$setting]);
        }

        $cacheId = 'has-'.$class.'-'.$setting;
        $ret = Vps_Cache_Simple::fetch($cacheId, $success);
        if ($success) {
            return $ret;
        }

        //& für performance
        $s =& self::_getSettingsCached();
        if (!isset($s[$class])) {
            throw new Vps_Exception("No Settings for component '$class' found; it is probably not in allComponentClasses. Requested setting: $setting");
        }
        $ret = array_key_exists($setting, $s[$class]);
        Vps_Cache_Simple::add($cacheId, $ret);
        return $ret;
    }

    private static function _verifyComponentClass($class)
    {
        $c = strpos($class, '.') ? substr($class, 0, strpos($class, '.')) : $class;
        if (!class_exists($c)) {
            $file = str_replace('_', '/', $c).'.yml';
            if (file_exists($file)) {
                $input = file_get_contents($file);
                $yaml = new sfYamlParser();
                try {
                    $settings = $yaml->parse($input);
                } catch (Exception $e) {
                    throw new Vps_Exception(sprintf('Unable to parse %s: %s', $file, $e->getMessage()));
                }
                if (!isset($settings['base'])) {
                    throw new Vps_Exception("'base' setting is required in '$file'");
                }
                if (!class_exists($settings['base'])) {
                    throw new Vps_Exception("'$file' base class '$settings[base]' does not exist");
                }
                $code = "<?php\nclass $c extends $settings[base]\n{\n";
                $code .= "    public static \$YAML_CONFIG_FILE = '$file';\n";
                $code .= "}\n";
                $classFile = 'application/cache/generated/'.str_replace('_', '/', $c).'.php';
                mkdir(substr($classFile, 0, strrpos($classFile, '/')), 0777, true);
                file_put_contents($classFile, $code);
                if (!class_exists($c)) {
                    throw new Vps_Exception("just generated class still does not exist");
                }
            } else {
                throw new Vps_Exception("Invalid component '$class'");
            }
        }
    }

    private static function _processYamlSettings(&$settings)
    {
        foreach ($settings as $k=>$i) {
            if (is_string($i) && preg_match('#^\\s*(trl|trlVps)\\(\'(.*)\'\\)\s*$#', $i, $m)) {
                $settings[$k] = Vps_Trl::getInstance()->trl($m[2], array(), $m[1]=='trlVps' ? Vps_Trl::SOURCE_VPS : Vps_Trl::SOURCE_WEB);
            } else if (is_string($i) && preg_match('#^\\.\'(.*)\'$#', $i, $m)) {
                if (isset($settings[$k])) {
                    $settings[$k] = $settings[$k] . $m[1];
                } else {
                    $settings[$k] = $m[1];
                }
            }
            if (is_array($i)) {
                self::_processYamlSettings($settings[$k]);
            }
        }
    }

    private static function _mergeSettings(&$settings, $mergeSettings)
    {
        foreach ($mergeSettings as $k=>$i) {
            if ($k=='_merge') {
                //ignore, not a setting; only for controling merging
                continue;
            }
            if (is_array($i)) {
                /*
                if (isset($i['_merge']) && $i['_merge'] == 'parentComponent') {
                    //keep $settings[$k]
                    if (!isset($settings[$k])) $settings[$k] = array();
                } else {
                    $settings[$k] = array(); //no merge; empty parent settings
                }
                */
                if (isset($i['_merge']) && $i['_merge'] == 'reset') {
                    $settings[$k] = array(); //no merge; empty parent settings
                } else {
                    $keys = array_keys($settings[$k]);
                    if ($keys[0] == 0) {
                        $settings[$k] = array(); //array; no merge; empty parent settings
                    } else {
                        //keep $settings[$k]
                        if (!isset($settings[$k])) $settings[$k] = array();
                    }
                }
                self::_mergeSettings($settings[$k], $i);
            } else {
                $settings[$k] = $i;
            }
        }
    }

    private static function _loadCacheSettings($class)
    {
        $c = strpos($class, '.') ? substr($class, 0, strpos($class, '.')) : $class;
        $param = strpos($class, '.') ? substr($class, strpos($class, '.')+1) : null;
        if (!isset(self::$_cacheSettings[$c][$param])) {
            $settings = call_user_func(array($c, 'getSettings'), $param);
            if (isset($c::$YAML_CONFIG_FILE)) {
                $input = file_get_contents($c::$YAML_CONFIG_FILE);
                $yaml = new sfYamlParser();
                try {
                    $mergeSettings = $yaml->parse($input);
                } catch (Exception $e) {
                    throw new Vps_Exception(sprintf('Unable to parse %s: %s', $c::$YAML_CONFIG_FILE, $e->getMessage()));<commit_msg>workaround parse error for php 5.2<commit_after><?php
class Vps_Component_Abstract
{
    private static $_settings = null;
    private static $_rebuildingSettings = false;
    private static $_cacheSettings = array();
    private static $_modelsCache = array(
        'own' => array(),
        'child' => array(),
        'form' => array(),
        'table' => array()
    );

    public function __construct()
    {
        $this->_init();
    }

    /**
     * Wird nach dem Konstruktor aufgerufen. Initialisierungscode in Unterklassen ist hier richtig.
     */
    protected function _init()
    {
    }

    public static function hasSettings($class)
    {
        static $prefix;
        if (!isset($prefix)) $prefix = Vps_Cache::getUniquePrefix();

        $cacheId = 'hasSettings-'.$class;
        $ret = Vps_Cache_Simple::fetch($cacheId, $success);
        if ($success) {
            return $ret;
        }

        //& für performance
        $s =& self::_getSettingsCached();
        $ret = isset($s[$class]);
        Vps_Cache_Simple::add($cacheId, $ret);
        return $ret;
    }

    public static function hasSetting($class, $setting)
    {
        if (self::$_rebuildingSettings) {
            //um endlosschleife in settingsCache zu verhindern

            self::_verifyComponentClass($class);
            $settings = self::_loadCacheSettings($class);
            return isset($settings[$setting]);
        }

        $cacheId = 'has-'.$class.'-'.$setting;
        $ret = Vps_Cache_Simple::fetch($cacheId, $success);
        if ($success) {
            return $ret;
        }

        //& für performance
        $s =& self::_getSettingsCached();
        if (!isset($s[$class])) {
            throw new Vps_Exception("No Settings for component '$class' found; it is probably not in allComponentClasses. Requested setting: $setting");
        }
        $ret = array_key_exists($setting, $s[$class]);
        Vps_Cache_Simple::add($cacheId, $ret);
        return $ret;
    }

    private static function _verifyComponentClass($class)
    {
        $c = strpos($class, '.') ? substr($class, 0, strpos($class, '.')) : $class;
        if (!class_exists($c)) {
            $file = str_replace('_', '/', $c).'.yml';
            if (file_exists($file)) {
                $input = file_get_contents($file);
                $yaml = new sfYamlParser();
                try {
                    $settings = $yaml->parse($input);
                } catch (Exception $e) {
                    throw new Vps_Exception(sprintf('Unable to parse %s: %s', $file, $e->getMessage()));
                }
                if (!isset($settings['base'])) {
                    throw new Vps_Exception("'base' setting is required in '$file'");
                }
                if (!class_exists($settings['base'])) {
                    throw new Vps_Exception("'$file' base class '$settings[base]' does not exist");
                }
                $code = "<?php\nclass $c extends $settings[base]\n{\n";
                $code .= "    public static _getYamlConfigFile() { return '$file'; }\n";
                $code .= "}\n";
                $classFile = 'application/cache/generated/'.str_replace('_', '/', $c).'.php';
                mkdir(substr($classFile, 0, strrpos($classFile, '/')), 0777, true);
                file_put_contents($classFile, $code);
                if (!class_exists($c)) {
                    throw new Vps_Exception("just generated class still does not exist");
                }
            } else {
                throw new Vps_Exception("Invalid component '$class'");
            }
        }
    }

    private static function _processYamlSettings(&$settings)
    {
        foreach ($settings as $k=>$i) {
            if (is_string($i) && preg_match('#^\\s*(trl|trlVps)\\(\'(.*)\'\\)\s*$#', $i, $m)) {
                $settings[$k] = Vps_Trl::getInstance()->trl($m[2], array(), $m[1]=='trlVps' ? Vps_Trl::SOURCE_VPS : Vps_Trl::SOURCE_WEB);
            } else if (is_string($i) && preg_match('#^\\.\'(.*)\'$#', $i, $m)) {
                if (isset($settings[$k])) {
                    $settings[$k] = $settings[$k] . $m[1];
                } else {
                    $settings[$k] = $m[1];
                }
            }
            if (is_array($i)) {
                self::_processYamlSettings($settings[$k]);
            }
        }
    }

    private static function _mergeSettings(&$settings, $mergeSettings)
    {
        foreach ($mergeSettings as $k=>$i) {
            if ($k=='_merge') {
                //ignore, not a setting; only for controling merging
                continue;
            }
            if (is_array($i)) {
                /*
                if (isset($i['_merge']) && $i['_merge'] == 'parentComponent') {
                    //keep $settings[$k]
                    if (!isset($settings[$k])) $settings[$k] = array();
                } else {
                    $settings[$k] = array(); //no merge; empty parent settings
                }
                */
                if (isset($i['_merge']) && $i['_merge'] == 'reset') {
                    $settings[$k] = array(); //no merge; empty parent settings
                } else {
                    $keys = array_keys($settings[$k]);
                    if ($keys[0] == 0) {
                        $settings[$k] = array(); //array; no merge; empty parent settings
                    } else {
                        //keep $settings[$k]
                        if (!isset($settings[$k])) $settings[$k] = array();
                    }
                }
                self::_mergeSettings($settings[$k], $i);
            } else {
                $settings[$k] = $i;
            }
        }
    }

    private static function _loadCacheSettings($class)
    {
        $c = strpos($class, '.') ? substr($class, 0, strpos($class, '.')) : $class;
        $param = strpos($class, '.') ? substr($class, strpos($class, '.')+1) : null;
        if (!isset(self::$_cacheSettings[$c][$param])) {
            $settings = call_user_func(array($c, 'getSettings'), $param);
            if (method_exists($c, '_getYamlConfigFile')) {
                $file = call_user_func(array($c, '_getYamlConfigFile'));
                $input = file_get_contents($file);
                $yaml = new sfYamlParser();
                try {
                    $mergeSettings = $yaml->parse($input);
                } catch (Exception $e) {
                    throw new Vps_Exception(sprintf('Unable to parse %s: %s', $file, $e->getMessage()));
{'fraction_non_alphanumeric': 0.16304347826086957, 'fraction_numerical': 0.002312673450508788, 'mean_word_length': 1.7043985824473629, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '1651436', 'n_tokens_mistral': 3742, 'n_tokens_neox': 3602, 'n_words': 780}