public static function handleTreeRequest($rootNode = null) { set_time_limit(1800); $rootPath = $rootNode ? $rootNode->getFullPath(null, false) : null; $collectionConditions = array(); $fileConditions = array(); // process excludes if (!empty($_REQUEST['exclude']) && method_exists('Emergence_FS', 'getNodesFromPattern')) { $excludes = is_array($_REQUEST['exclude']) ? $_REQUEST['exclude'] : array($_REQUEST['exclude']); $excludedCollections = array(); $excludedFiles = array(); foreach (Emergence_FS::getNodesFromPattern($excludes) as $node) { if ($node->Class == 'SiteCollection') { $excludedCollections[] = $node->ID; } else { $excludedFiles[] = $node->ID; } } if (count($excludedCollections)) { $collectionConditions['excludeTrees'] = $excludedCollections; } if (count($excludedFiles)) { $fileConditions[] = 'ID NOT IN (' . implode(',', $excludedFiles) . ')'; } } // get files $files = Emergence_FS::getTreeFiles($rootPath, false, $fileConditions, $collectionConditions); header('HTTP/1.1 300 Multiple Choices'); header('Content-Type: application/vnd.emergence.tree+json'); print json_encode(array('total' => count($files), 'files' => $files)); exit; }
function Dwoo_Plugin_sencha_bootstrap(Dwoo_Core $dwoo, $App = null, $classPaths = array(), $packages = array(), $patchLoader = true, $framework = 'ext', $frameworkVersion = null, $packageRequirers = null) { // retrieve app if available if (!$App) { $App = $dwoo->data['App']; } // load workspace classpaths $classPaths = array_merge($classPaths, explode(',', Sencha::getWorkspaceCfg('workspace.classpath'))); // 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(false)); // false to skip crawling decendents, we'll do it here later // 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'))); // include override files if ($overridesPath = $App->getBuildCfg('app.overrides')) { $classPaths = array_merge($classPaths, explode(',', $overridesPath)); } } // pull package requirements from source files if (!empty($packageRequirers)) { if (is_string($packageRequirers)) { $packageRequirers = array($packageRequirers); } foreach ($packageRequirers as $packageRequirer) { if ($sourceNode = Site::resolvePath($packageRequirer)) { $packages = array_merge($packages, Sencha::getRequiredPackagesForSourceFile($sourceNode->RealPath)); } } } // 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 $packages = array_unique(Sencha::crawlRequiredPackages(array_unique($packages), $framework, $frameworkVersion)); foreach ($packages as $packageName) { // check workspace and framework package dirs $packagePath = "sencha-workspace/packages/{$packageName}"; if (!Site::resolvePath($packagePath)) { $packagePath = "{$frameworkPath}/packages/{$packageName}"; if (!Site::resolvePath($packagePath)) { throw new Exception("Source for package {$packageName} not found in workspace or framework"); } } array_push($classPaths, "{$packagePath}/src", "{$packagePath}/overrides"); } // include classpaths from packages $classPaths = array_merge($classPaths, Sencha::aggregateClassPathsForPackages($packages)); // filter classpaths $classPaths = array_unique(array_filter($classPaths)); // build list of all source trees, resolving CMD variables and children $sources = array(); foreach ($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; $addToManifest = true; // rewrite path to canonican external URL if ($appPath && strpos($path, "{$appPath}/") === 0) { $webPath = '/app/' . substr($path, 17); // app overrides should automatically be loaded if (substr($path, strlen($appPath), 11) == '/overrides/') { $autoLoad = true; $addToManifest = false; } } 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; $addToManifest = false; } } elseif (strpos($path, $frameworkPath) === 0) { $webPath = "/app/{$framework}-{$frameworkVersion}/" . substr($path, strlen($frameworkPath) + 1); // package overrides should automatically be loaded if (substr($path, strpos($path, '/', strlen($frameworkPath) + 10), 11) == '/overrides/') { $autoLoad = true; $addToManifest = false; } } 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\\(\\s*([\'"])([^\\2]+)\\2|\\/\\/\\s*@define[ \\t]+(\\S+))/i', $line, $matches)) { $source['Class'] = empty($matches[4]) ? $matches[3] : $matches[4]; 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 if ($addToManifest) { $manifest[$source['Class']] = $webPath; } // add path to autoLoad list if ($autoLoad) { $autoLoadPaths[] = $webPath; } } // build loader overrides $loaderPatch = ''; if ($patchLoader) { $loaderPatch .= 'Ext.Loader.setConfig("disableCaching", false);'; $loaderPatch .= '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;' . '}'; $loaderPatch .= 'function _overrideMethod(cls, method, override) {' . 'var parent = cls[method] || Ext.emptyFn;' . 'cls[method] = function() {' . 'var me = this;' . 'callArgs = Array.prototype.slice.call(arguments, 0);' . 'callArgs.unshift(function() {' . 'parent.apply(me, arguments);' . '});' . 'return override.apply(this, callArgs);' . '};' . '}'; # if (Sencha::isVersionNewer('5', $frameworkVersion)) { if ($framework == 'ext') { $loaderPatch .= '_overrideMethod(Ext.Loader, "loadScript", function(parent, options) {' . 'if (typeof options == "string") {' . 'options = _versionScriptUrl(options);' . '} else {' . 'options.url = _versionScriptUrl(options.url);' . '}' . 'return parent(options);' . '});'; } else { $loaderPatch .= '_overrideMethod(Ext.Loader, "loadScriptFile", function(parent, url, onLoad, onError, scope, synchronous) {' . 'return parent(_versionScriptUrl(url), onLoad, onError, scope, synchronous);' . '});'; } } // output loader patch and manifest return '<script type="text/javascript">(function(){' . 'var dcParam = Ext.Loader.getConfig("disableCachingParam")' . ',now = Ext.Date.now();' . $loaderPatch . 'Ext.Loader.addClassPathMappings(' . json_encode($manifest) . ');' . '})()</script>' . (count($autoLoadPaths) ? implode('', array_map(function ($url) { return '<script type="text/javascript" src="' . $url . '"></script>'; }, $autoLoadPaths)) : ''); }
public static function getSourceNodes($paths, $root, $contentType = null) { $paths = static::splitMultipath($paths); if (is_string($root)) { $root = Site::splitPath($root); } $sourceFiles = array(); foreach ($paths as $path) { $path = array_merge($root, $path); list($filename) = array_slice($path, -1); if ($filename == '*') { array_pop($path); Emergence_FS::cacheTree($path); foreach (Emergence_FS::getTreeFiles($path, false, $contentType ? array('Type' => $contentType) : null) as $path => $fileData) { $sourceFiles[$path] = $fileData; } } else { $node = Site::resolvePath($path); if (!$node || !is_a($node, 'SiteFile')) { throw new Exception('Source file "' . implode('/', $path) . '" does not exist', self::ERROR_NOT_FOUND); } if ($node->Type != $contentType) { throw new Exception('Source file "' . implode('/', $path) . '" does not match requested content type "' . $contentType . '"', self::ERROR_TYPE_MISMATCH); } $sourceFiles[join('/', $path)] = array('ID' => $node->ID, 'SHA1' => $node->SHA1); } } return $sourceFiles; }
$patternTemplate = '/(_|gettext)\\(\\s*(\'|")(.*?)\\2\\s*\\)/si'; $patternTemplateShort = '/\\{(_|gettext)\\s+("|\'|)(.*?)\\2\\s*\\}/si'; $patternPHP = '/(_|gettext)\\s*\\(\\s*(\'|")(.*?)\\2\\s*\\)/si'; $patternPHPValidators = '/(\'|")errorMessage\\1\\s*=>\\s*(\'|")(.*?)\\2/s'; // create a memory handle to write pot file to $pot = fopen('php://memory', 'w+'); $strings = array(); // extract strings from templates $files = Emergence_FS::getTreeFiles('html-templates', false, array('Type' => 'text/x-html-template')); foreach ($files as $path => $fileData) { $contents = file_get_contents(SiteFile::getByID($fileData['ID'])->RealPath); _extractStrings($patternTemplate, $contents, $path, $strings); _extractStrings($patternTemplateShort, $contents, $path, $strings); } // extract strings from PHP files $files = Emergence_FS::getTreeFiles(null, false, array('Type' => 'application/php')); foreach ($files as $path => $fileData) { $contents = file_get_contents(SiteFile::getByID($fileData['ID'])->RealPath); _extractStrings($patternPHP, $contents, $path, $strings); _extractStrings($patternPHPValidators, $contents, $path, $strings); } // write pot file foreach ($strings as $string => $sources) { fwrite($pot, '#: ' . implode(' ', $sources) . PHP_EOL); // switch output format if embedded newlines found (see https://www.gnu.org/software/gettext/manual/html_node/Normalizing.html) if (preg_match('/[^\\n]\\n+[^\\n]/', $string)) { // multiline output format fwrite($pot, 'msgid ""' . PHP_EOL); fwrite($pot, str_replace('\\n', '\\n"' . PHP_EOL . '"', _encodeString($string)) . PHP_EOL); } else { fwrite($pot, 'msgid ' . _encodeString($string) . PHP_EOL);