/** * Creates a template with layout only. * * Sometimes you just want to separate parts of your layout without giving them some special logic. * You may just store them as *.tpl.php files and create a template from them like this: * <code php> * // assuming template file is 'templates/my.tpl.php' * $tpl = Template::Make('my'); * $tpl->set('myvar','I am just layout'); * <code> * @param string $template_basename Name of the template * @return Template The created template */ static function Make($template_basename) { if (file_exists($template_basename)) { $tpl_file = $template_basename; } else { foreach (array_reverse(cfg_get('system', 'tpl_ext')) as $tpl_ext) { $tpl_file = __search_file_for_class($template_basename, $tpl_ext); if ($tpl_file) { break; } } } if (!$tpl_file) { WdfException::Raise("Template not found: {$template_basename}"); } $res = new Template($tpl_file); return $res; }
/** * @internal Collects dependencies from a file */ function minify_collect_from_file($kind, $f, $debug_path = '') { global $dependency_info, $res_file_storage, $ext_resources; if (!$f) { return; } $classname = fq_class_name(array_shift(explode('.', basename($f)))); if (isset($res_file_storage[$classname]) || minify_forbidden($classname)) { return; } $order = array('static', 'inherited', 'instanciated', 'self'); //:array('self','incontent','instanciated','inherited'); $res_file_storage[$classname] = array(); $content = file_get_contents($f); // remove block-comments $content = preg_replace("|/\\*.*\\*/|sU", "", $content); do { $c2 = preg_replace("|(.*)//.*\$|m", "\$1", $content); if ($content == $c2) { break; } $content = $c2; } while (true); foreach ($order as $o) { switch ($o) { case 'inherited': if (preg_match_all('/class\\s+[^\\s]+\\s+extends\\s+([^\\s]+)/', $content, $matches, PREG_SET_ORDER)) { foreach ($matches as $m) { $file_for_class = __search_file_for_class($m[1]); if (!$file_for_class) { continue; } $dependency_info[$classname][] = strtolower($m[1]); minify_collect_from_file($kind, $file_for_class, $debug_path . '/' . $classname); } } break; case 'instanciated': $matches = array(); if (preg_match_all('/new\\s+([^\\(]+)\\(/', $content, $by_new, PREG_SET_ORDER)) { $matches = array_merge($matches, $by_new); } if (preg_match_all('/\\s+([^:\\s\\(\\)]+)::Make\\(/Ui', $content, $by_make, PREG_SET_ORDER)) { $matches = array_merge($matches, $by_make); } if (count($matches) > 0) { foreach ($matches as $m) { $file_for_class = __search_file_for_class($m[1]); if (!$file_for_class) { continue; } $dependency_info[$classname][] = strtolower($m[1]); minify_collect_from_file($kind, $file_for_class, $debug_path . '/' . $classname); } } break; case 'self': $simplecls = array_pop(explode("\\", $classname)); if (resourceExists(strtolower("{$simplecls}.{$kind}"))) { $tmp = resFile(strtolower("{$simplecls}.{$kind}")); if (!in_array($tmp, $res_file_storage[$classname])) { $res_file_storage[$classname][] = $tmp; } } break; case 'static': try { foreach (ResourceAttribute::Collect($classname) as $resource) { $b = $resource->Resolve(); if ($resource instanceof \ScavixWDF\Reflection\ExternalResourceAttribute) { $ext_resources[] = $b; continue; } if (!ends_with($b, $kind)) { continue; } $b = strtolower($b); if (!in_array($b, $res_file_storage[$classname])) { $res_file_storage[$classname][] = $b; } } } catch (Exception $ex) { } break; } } }
/** * @internal Collects dependencies from a file */ function minify_collect_from_file($kind, $f, $debug_path = '') { global $dependency_info, $res_file_storage; if (!$f) { return; } $classname = strtolower(basename($f, ".class.php")); if (isset($res_file_storage[$classname]) || minify_forbidden($classname)) { return; } $order = array('static', 'inherited', 'instanciated', 'self'); //:array('self','incontent','instanciated','inherited'); $res_file_storage[$classname] = array(); $content = file_get_contents($f); // remove block-comments $content = preg_replace("|/\\*.*\\*/|sU", "", $content); do { $c2 = preg_replace("|(.*)//.*\$|m", "\$1", $content); if ($content == $c2) { break; } $content = $c2; } while (true); foreach ($order as $o) { switch ($o) { case 'inherited': if (preg_match_all('/class\\s+[^\\s]+\\s+extends\\s+([^\\s]+)/', $content, $matches, PREG_SET_ORDER)) { // log_debug("minify_collect_from_file [$debug_path/$classname]: INHERITED",$matches); foreach ($matches as $m) { $file_for_class = __search_file_for_class($m[1]); if (!$file_for_class) { continue; } $dependency_info[$classname][] = strtolower($m[1]); minify_collect_from_file($kind, $file_for_class, $debug_path . '/' . $classname); } } break; case 'instanciated': if (preg_match_all('/new\\s+([^\\(]+)\\(/', $content, $matches, PREG_SET_ORDER)) { // log_debug("minify_collect_from_file [$debug_path/$classname]: INSTANCIATED",$matches); foreach ($matches as $m) { $file_for_class = __search_file_for_class($m[1]); if (!$file_for_class) { continue; } $dependency_info[$classname][] = strtolower($m[1]); minify_collect_from_file($kind, $file_for_class, $debug_path . '/' . $classname); } } break; case 'self': if (resourceExists(strtolower("{$classname}.{$kind}"))) { $tmp = resFile(strtolower("{$classname}.{$kind}")); if (!in_array($tmp, $res_file_storage[$classname])) { $res_file_storage[$classname][] = $tmp; } } break; case 'static': try { $buf = ResourceAttribute::ResolveAll(ResourceAttribute::Collect($classname)); foreach ($buf as $b) { if (!ends_with($b, $kind)) { continue; } $b = strtolower($b); if (!in_array($b, $res_file_storage[$classname])) { $res_file_storage[$classname][] = $b; } } } catch (Exception $ex) { } break; } } }
/** * Tries to load the template for the calling class * @param object|string $controller Object or class to load template for * @param string $template_name Pass '' (empty string) for this. * @return bool|string Returns the filename if found, else false */ function __autoload__template($controller, $template_name) { global $CONFIG; if (is_object($controller)) { $class = strtolower(get_class($controller)); } else { $class = $controller; } if ($template_name != "") { $key = "autoload_template-" . getAppVersion('nc') . $template_name; $r = cache_get($key); if ($r != false && file_exists($r)) { return $r; } if (file_exists($template_name)) { cache_set($key, $template_name, $CONFIG['system']['cache_ttl']); return $template_name; } $template_name2 = dirname(__search_file_for_class($class)) . "/" . $template_name; if (file_exists($template_name2)) { cache_set($key, $template_name2, $CONFIG['system']['cache_ttl']); return $template_name2; } $template_name2 = dirname(__search_file_for_class($class)) . "/base/" . $template_name; if (file_exists($template_name2)) { cache_set($key, $template_name2, $CONFIG['system']['cache_ttl']); return $template_name2; } } $key = "autoload_template_class-" . $class; $r = cache_get($key); if ($r != false && file_exists($r)) { return $r; } $file = __search_file_for_class($class); foreach (array_reverse($CONFIG['system']['tpl_ext']) as $tpl_ext) { $tpl_file = str_replace("class.php", $tpl_ext, $file ? $file : ""); if (file_exists($tpl_file)) { cache_set($key, $tpl_file, $CONFIG['system']['cache_ttl']); return $tpl_file; } } $pclass = get_parent_class($class); if ($pclass !== false && strtolower($pclass) != "template") { return __autoload__template($pclass, ""); } return false; }
private function _getAttributes($comment, $filter, $object = false, $method = false, $field = false, $allowAttrInheritance = true) { $pattern = '/@attribute\\[([^\\]]*)\\]/im'; if (!preg_match_all($pattern, $comment, $matches)) { return array(); } if (!is_array($filter)) { $filter = array($filter); } foreach ($filter as $i => $f) { $filter[$i] = strtolower(str_replace("Attribute", "", $f)); } $res = array(); $pattern = '/([^\\(]*)\\((.*)\\)/im'; foreach ($matches[1] as $m) { $m = trim($m); if (preg_match_all($pattern, $m, $inner)) { $name = str_replace("Attribute", "", $inner[1][0]); $attr = $name . "Attribute({$inner[2][0]})"; } else { $name = str_replace("Attribute", "", $m); $attr = $name . "Attribute()"; } if (!__search_file_for_class($name . "Attribute")) { if ($name != 'NoMinify') { log_trace("Invalid Attribute: {$m} ({$name}Attribute) found in Comment '{$comment}'"); } continue; } $parts = explode("(", $attr, 2); $attr = fq_class_name($parts[0]) . "(" . $parts[1]; eval('$attr = new ' . $attr . ';'); $name = strtolower($name); $add = count($filter) == 0; foreach ($filter as $f) { if ($f == $name || $allowAttrInheritance && is_subclass_of($attr, $f . "Attribute")) { $add = true; break; } } if ($add) { $attr->Reflector = $this; $attr->Class = $this->Classname; if ($object && is_object($object)) { $attr->Object = $object; } if ($method) { $attr->Method = $method; } if ($field) { $attr->Field = $field; } $res[] = $attr; } } return $res; }