/** * @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; } } }
private function __collectResourcesInternal($template) { $res = array(); if (is_object($template)) { $classname = get_class($template); // first collect statics from the class definitions $static = ResourceAttribute::ResolveAll(ResourceAttribute::Collect($classname)); $res = array_merge($res, $static); if ($template instanceof Renderable) { // then check all contents and collect theis includes foreach ($template->__getContentVars() as $varname) { $sub = array(); foreach ($template->{$varname} as $var) { if (is_object($var) || is_array($var)) { $sub = array_merge($sub, $this->__collectResourcesInternal($var)); } } $res = array_merge($res, $sub); } // for Template class check the template file too if ($template instanceof Template) { $fnl = strtolower(array_shift(explode(".", basename($template->file)))); if (get_class_simple($template, true) != $fnl) { if (resourceExists("{$fnl}.css")) { $res[] = resFile("{$fnl}.css"); } elseif (resourceExists("{$fnl}.less")) { $res[] = resFile("{$fnl}.less"); } if (resourceExists("{$fnl}.js")) { $res[] = resFile("{$fnl}.js"); } } } // finally include the 'self' stuff (<classname>.js,...) // Note: these can be forced to be loaded in static if they require to be loaded before the contents resources $classname = get_class_simple($template); $parents = array(); $cnl = strtolower($classname); do { if (resourceExists("{$cnl}.css")) { $parents[] = resFile("{$cnl}.css"); } elseif (resourceExists("{$cnl}.less")) { $parents[] = resFile("{$cnl}.less"); } if (resourceExists("{$cnl}.js")) { $parents[] = resFile("{$cnl}.js"); } $classname = array_pop(explode('\\', get_parent_class(fq_class_name($classname)))); $cnl = strtolower($classname); } while ($classname != ""); $res = array_merge($res, array_reverse($parents)); } } elseif (is_array($template)) { foreach ($template as $var) { if (is_object($var) || is_array($var)) { $res = array_merge($res, $this->__collectResourcesInternal($var)); } } } return array_unique($res); }