diff --git a/bhl.inc.php b/bhl.inc.php index 882930c..148c7d7 100644 --- a/bhl.inc.php +++ b/bhl.inc.php @@ -272,16 +272,26 @@ function bhl_validate_func_ref(string $module_file, string $func_full_name, arra if(sizeof($signature) == 0) throw new Exception("Signature is invalid"); - //NOTE: we can't validate if the function is actually in a namespace - // but was passed without a namespace prefix $name_items = explode('.', $func_full_name); - $func = end($name_items); + $func = array_pop($name_items); + $namespace = implode('.', $name_items); $module_src = file_get_contents($module_file); if(!$module_src) throw new Exception("Bad module file '{$module_file}'"); + $module_chunks = array(); + if($namespace) + { + $module_chunks = preg_split('~namespace\s+'.preg_quote($namespace).'\s*{~', $module_src); + if(count($module_chunks) < 2) + throw new Exception("Namespace '$namespace' not found in '$module_file'"); + $module_chunks = array_filter($module_chunks, function($item) { return strlen($item) > 0;}); + } + else + $module_chunks[] = $module_src; + $signature_pattern = ''; $signature_pattern .= '~func\s+'; if($signature[0] !== 'void') @@ -303,8 +313,13 @@ function bhl_validate_func_ref(string $module_file, string $func_full_name, arra $signature_pattern .= '\s*\)'; $signature_pattern .= '~'; - if(!preg_match($signature_pattern, $module_src)) - throw new Exception("Func '$func' signature '".implode(',', $signature)."' not found in module '$module_file'"); + foreach($module_chunks as $module_chunk_src) + { + if(preg_match($signature_pattern, $module_chunk_src)) + return; + } + + throw new Exception("Func '$func' signature '".implode(',', $signature)."' not found in module '$module_file'"); } function bhl_upm_path() : string