<commit_before><?php

function Dwoo_Plugin_sencha_bootstrap(Dwoo_Core $dwoo, $App = null, $classPaths = array(), $packages = array(), $patchLoader = true, $framework = 'ext', $frameworkVersion = null)
{
    // retrieve app if available
    if (!$App) {
        $App = $dwoo->data['App'];
    }

    // resolve nested dependencies for manually supplied packages
    if (!empty($packages)) {
        $packages = Sencha::crawlRequiredPackages($packages);
    }

    // if app provided, load classpaths and packages
    if ($App) {
        $framework = $App->getFramework();
        $frameworkVersion = $App->getFrameworkVersion();
        $appPath = 'sencha-workspace/'.$App->getName();

        // recursively merge app's required packages and their required packages into packages list
        $packages = array_merge($packages, $App->getRequiredPackages());

        // add theme to packages list
        if ($themeName = $App->getBuildCfg('app.theme')) {
            $packages[] = $themeName;
        }

        // include classpath files
        $classPaths = array_merge($classPaths, explode(',', $App->getBuildCfg('app.classpath')), Sencha::aggregateClassPathsForPackages($packages));
    }

    // apply default framework version and normalize
    if (!$frameworkVersion) {
        $frameworkVersion = Sencha::$frameworks[$framework]['defaultVersion'];
    }
    
    $frameworkVersion = Sencha::normalizeFrameworkVersion($framework, $frameworkVersion);
    $frameworkPath = "sencha-workspace/$framework-$frameworkVersion";

    // initialize output state
    $manifest = array();
    $autoLoadPaths = array();

    // set framework path if patching loader
    if ($patchLoader) {
        $manifest['Ext'] = "/app/$framework-$frameworkVersion/src";
    }

    // add paths for packages
    foreach (array_unique($packages) AS $packageName) {
        $packagePath = "sencha-workspace/packages/$packageName";
        array_push($classPaths, "$packagePath/src", "$packagePath/overrides");
    }

    // build list of all source trees, resolving CMD variables and children
    $sources = array();
    foreach (array_unique($classPaths) AS $classPath) {
        if (strpos($classPath, '${workspace.dir}/x/') === 0) {
            $classPath = substr($classPath, 19);
            $manifest[str_replace('/', '.', $classPath)] = '/app/x/' . $classPath;

            $classPath = 'ext-library/' . $classPath;
        } elseif (strpos($classPath, 'ext-library/') === 0) {
            $classPath = substr($classPath, 12);
            $manifest[str_replace('/', '.', $classPath)] = '/app/x/' . $classPath;

            $classPath = 'ext-library/' . $classPath;
        } elseif (strpos($classPath, '${app.dir}/') === 0) {
            $classPath = $appPath . substr($classPath, 10);
        } elseif (strpos($classPath, '${ext.dir}/') === 0) {
            $classPath = $frameworkPath . substr($classPath, 10);
        } elseif (strpos($classPath, '${touch.dir}/') === 0) {
            $classPath = $frameworkPath . substr($classPath, 12);
        }

        Emergence_FS::cacheTree($classPath);
        $sources = array_merge($sources, Emergence_FS::getTreeFiles($classPath, false, array('Type' => 'application/javascript')));
    }

    // skip patching loader if manifest will be empty
    if (empty($sources)) {
        return '';
    }

    // process all source files and build manifest and list of classes to automatically load
    foreach ($sources AS $path => &$source) {
        $autoLoad = false;

        // rewrite path to canonican external URL
        if ($appPath && strpos($path, "$appPath/") === 0) {
            $webPath = '/app/'.substr($path, 17);
        } elseif (strpos($path, 'ext-library/') === 0) {
            $webPath = '/app/x'.substr($path, 11);
        } elseif (strpos($path, 'sencha-workspace/packages/') === 0) {
            $webPath = '/app/'.substr($path, 17);

            // package overrides should automatically be loaded
            if (substr($path, strpos($path, '/', 26), 11) == '/overrides/') {
                $autoLoad = true;
            }
        } elseif (strpos($path, 'sencha-workspace/pages/') === 0) {
            $webPath = '/app/'.substr($path, 17);
        } elseif (strpos($path, $frameworkPath) === 0) {
            $webPath = '/app/'.substr($path, 17);
        } else {
            // this class was not in a recognized externally loadable collection
            continue;
        }

        // discover class name
        $sourceCacheKey = "sencha-class-name/$source[SHA1]";

        if (!$source['Class'] = Cache::fetch($sourceCacheKey)) {
            $sourceNode = Site::resolvePath($path);
            $sourceReadHandle = $sourceNode->get();

            while (($line = fgets($sourceReadHandle, 4096)) !== false) {
                if (preg_match('/\s*Ext\.define\(([\'"])([^\'"]+)\1/i', $line, $matches)) {
                    $source['Class'] = $matches[2];
                    break;
                }
            }

            fclose($sourceReadHandle);

            // cache class name
            Cache::store($sourceCacheKey, $source['Class']);
        }

        // skip if class name could not be determined
        if (!$source['Class']) {
            continue;
        }

        // apply fingerprint signature to path
        $webPath = "$webPath?_sha1=$source[SHA1]";

        // map class name to path
        $manifest[$source['Class']] = $webPath;

        // add path to autoLoad list
        if ($autoLoad) {
            $autoLoadPaths[] = $webPath;
        }
    }

    // output loader patch and manifest
    return
        '<script type="text/javascript">(function(){'
            .'var origLoadScript = Ext.Loader.loadScript'
                .',origLoadScriptFile = Ext.Loader.loadScriptFile'
                .',dcParam = Ext.Loader.getConfig("disableCachingParam")'
                .',now = Ext.Date.now();'
            .(
                $patchLoader ?
                    'function _versionScriptUrl(url) {'
                        .'if (url[0] != "/") {'
                            .'url = window.location.pathname + url;'
                            .'while (url.match(/\/\.\.\//)) url = url.replace(/\/[^\/]+\/\.\./g, "");'
                        .'}'
        
                        .'if(!url.match(/\?_sha1=/)) {'
                            .'console.warn("Fingerprinted URL not found for %o, it will be loaded with a cache-buster", url);'
                            .'url += "?" + dcParam + "=" + now;'
                        .'}'
        
                        .'return url;'
                    .'}'
        
                    .'Ext.Loader.loadScript = function(options) {'
                        .'if (typeof options == "string") {'
                            .'options = _versionScriptUrl(options);'
                        .'} else {'
                            .'options.url = _versionScriptUrl(options.url);'
                        .'}'
                        .'origLoadScript.call(Ext.Loader, options);'
                    .'};'
        
                    .'Ext.Loader.loadScriptFile = function(url, onLoad, onError, scope, synchronous) {'
                        .'origLoadScriptFile.call(Ext.Loader, _versionScriptUrl(url), onLoad, onError, scope, synchronous);'
                    .'};'
        
                    .'Ext.Loader.setConfig("disableCaching", false);'
                :
                    ''
            )

            .'Ext.Loader.addClassPathMappings('.json_encode($manifest).');'
            .( count($autoLoadPaths) ? 'Ext.Array.each('.json_encode($autoLoadPaths).', origLoadScript);' : '' )
        .'})()</script>';
}<commit_msg>vary Loader patch for ext/touch<commit_after><?php

function Dwoo_Plugin_sencha_bootstrap(Dwoo_Core $dwoo, $App = null, $classPaths = array(), $packages = array(), $patchLoader = true, $framework = 'ext', $frameworkVersion = null)
{
    // retrieve app if available
    if (!$App) {
        $App = $dwoo->data['App'];
    }

    // resolve nested dependencies for manually supplied packages
    if (!empty($packages)) {
        $packages = Sencha::crawlRequiredPackages($packages);
    }

    // if app provided, load classpaths and packages
    if ($App) {
        $framework = $App->getFramework();
        $frameworkVersion = $App->getFrameworkVersion();
        $appPath = 'sencha-workspace/'.$App->getName();

        // recursively merge app's required packages and their required packages into packages list
        $packages = array_merge($packages, $App->getRequiredPackages());

        // add theme to packages list
        if ($themeName = $App->getBuildCfg('app.theme')) {
            $packages[] = $themeName;
        }

        // include classpath files
        $classPaths = array_merge($classPaths, explode(',', $App->getBuildCfg('app.classpath')), Sencha::aggregateClassPathsForPackages($packages));
    }

    // apply default framework version and normalize
    if (!$frameworkVersion) {
        $frameworkVersion = Sencha::$frameworks[$framework]['defaultVersion'];
    }
    
    $frameworkVersion = Sencha::normalizeFrameworkVersion($framework, $frameworkVersion);
    $frameworkPath = "sencha-workspace/$framework-$frameworkVersion";

    // initialize output state
    $manifest = array();
    $autoLoadPaths = array();

    // set framework path if patching loader
    if ($patchLoader) {
        $manifest['Ext'] = "/app/$framework-$frameworkVersion/src";
    }

    // add paths for packages
    foreach (array_unique($packages) AS $packageName) {
        $packagePath = "sencha-workspace/packages/$packageName";
        array_push($classPaths, "$packagePath/src", "$packagePath/overrides");
    }

    // build list of all source trees, resolving CMD variables and children
    $sources = array();
    foreach (array_unique($classPaths) AS $classPath) {
        if (strpos($classPath, '${workspace.dir}/x/') === 0) {
            $classPath = substr($classPath, 19);
            $manifest[str_replace('/', '.', $classPath)] = '/app/x/' . $classPath;

            $classPath = 'ext-library/' . $classPath;
        } elseif (strpos($classPath, 'ext-library/') === 0) {
            $classPath = substr($classPath, 12);
            $manifest[str_replace('/', '.', $classPath)] = '/app/x/' . $classPath;

            $classPath = 'ext-library/' . $classPath;
        } elseif (strpos($classPath, '${app.dir}/') === 0) {
            $classPath = $appPath . substr($classPath, 10);
        } elseif (strpos($classPath, '${ext.dir}/') === 0) {
            $classPath = $frameworkPath . substr($classPath, 10);
        } elseif (strpos($classPath, '${touch.dir}/') === 0) {
            $classPath = $frameworkPath . substr($classPath, 12);
        }

        Emergence_FS::cacheTree($classPath);
        $sources = array_merge($sources, Emergence_FS::getTreeFiles($classPath, false, array('Type' => 'application/javascript')));
    }

    // skip patching loader if manifest will be empty
    if (empty($sources)) {
        return '';
    }

    // process all source files and build manifest and list of classes to automatically load
    foreach ($sources AS $path => &$source) {
        $autoLoad = false;

        // rewrite path to canonican external URL
        if ($appPath && strpos($path, "$appPath/") === 0) {
            $webPath = '/app/'.substr($path, 17);
        } elseif (strpos($path, 'ext-library/') === 0) {
            $webPath = '/app/x'.substr($path, 11);
        } elseif (strpos($path, 'sencha-workspace/packages/') === 0) {
            $webPath = '/app/'.substr($path, 17);

            // package overrides should automatically be loaded
            if (substr($path, strpos($path, '/', 26), 11) == '/overrides/') {
                $autoLoad = true;
            }
        } elseif (strpos($path, 'sencha-workspace/pages/') === 0) {
            $webPath = '/app/'.substr($path, 17);
        } elseif (strpos($path, $frameworkPath) === 0) {
            $webPath = '/app/'.substr($path, 17);
        } else {
            // this class was not in a recognized externally loadable collection
            continue;
        }

        // discover class name
        $sourceCacheKey = "sencha-class-name/$source[SHA1]";

        if (!$source['Class'] = Cache::fetch($sourceCacheKey)) {
            $sourceNode = Site::resolvePath($path);
            $sourceReadHandle = $sourceNode->get();

            while (($line = fgets($sourceReadHandle, 4096)) !== false) {
                if (preg_match('/\s*Ext\.define\(([\'"])([^\'"]+)\1/i', $line, $matches)) {
                    $source['Class'] = $matches[2];
                    break;
                }
            }

            fclose($sourceReadHandle);

            // cache class name
            Cache::store($sourceCacheKey, $source['Class']);
        }

        // skip if class name could not be determined
        if (!$source['Class']) {
            continue;
        }

        // apply fingerprint signature to path
        $webPath = "$webPath?_sha1=$source[SHA1]";

        // map class name to path
        $manifest[$source['Class']] = $webPath;

        // add path to autoLoad list
        if ($autoLoad) {
            $autoLoadPaths[] = $webPath;
        }
    }
    
    
    // build loader overrides
    $loaderOverrides = array();
    
    if ($patchLoader) {
        
#        if (Sencha::isVersionNewer('5', $frameworkVersion)) {
        if ($framework == 'ext') {
            $loaderOverrides['loadScript'] = 
                'function(options) {'
                    .'if (typeof options == "string") {'
                        .'options = _versionScriptUrl(options);'
                    .'} else {'
                        .'options.url = _versionScriptUrl(options.url);'
                    .'}'
                    .'this.callParent([options]);'
                .'}';
        }

        $loaderOverrides['loadScriptFile'] = 
            'function(url, onLoad, onError, scope, synchronous) {'
                .'this.callParent([_versionScriptUrl(url), onLoad, onError, scope, synchronous]);'
            .'}';

        // render to JS object
        array_walk($loaderOverrides, function(&$value, $key) {
            $value = "$key: $value";
        });
        
        $loaderOverrides = '{' . implode(',', $loaderOverrides) . '}';
    }

    // output loader patch and manifest
    return
        '<script type="text/javascript">(function(){'
            .'var origLoadScript = Ext.Loader.loadScript'
                .',origLoadScriptFile = Ext.Loader.loadScriptFile'
                .',dcParam = Ext.Loader.getConfig("disableCachingParam")'
                .',now = Ext.Date.now();'
            .(
                $patchLoader ?
                    'function _versionScriptUrl(url) {'
                        .'if (url[0] != "/") {'
                            .'url = window.location.pathname + url;'
                            .'while (url.match(/\/\.\.\//)) url = url.replace(/\/[^\/]+\/\.\./g, "");'
                        .'}'
        
                        .'if(!url.match(/\?_sha1=/)) {'
                            .'console.warn("Fingerprinted URL not found for %o, it will be loaded with a cache-buster", url);'
                            .'url += "?" + dcParam + "=" + now;'
                        .'}'
        
                        .'return url;'
                    .'}'

                    .(
                        count($loaderOverrides) ?
                            'Ext.override(Ext.Loader, '.$loaderOverrides.');'
                        :
                            ''
                    )
                    .'Ext.Loader.setConfig("disableCaching", false);'
                :
                    ''
            )

            .'Ext.Loader.addClassPathMappings('.json_encode($manifest).');'
            .( count($autoLoadPaths) ? 'Ext.Array.each('.json_encode($autoLoadPaths).', function(url) {Ext.Loader.loadScriptFile(url, Ext.emptyFn)});' : '' )
        .'})()</script>';
}
{'fraction_non_alphanumeric': 0.161, 'fraction_numerical': 0.0056875, 'mean_word_length': 1.8789132781576106, 'pattern_counts': {'":': 0, '<': 9, '<?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': '4491770', 'n_tokens_mistral': 4575, 'n_tokens_neox': 4324, 'n_words': 931}