public function test_basic() { $this->assertFalse(wildcard_compare('regex are useful', '')); $this->assertFalse(wildcard_compare('regex are * useful', '')); $this->assertFalse(wildcard_compare('regex are * useful', 'regex')); $this->assertFalse(wildcard_compare('regex are * useful', 'regex are')); $this->assertFalse(wildcard_compare('regex are * useful', 'regex are useful')); $this->assertFalse(wildcard_compare('regex are ? useful', 'regex are useful')); $this->assertTrue(wildcard_compare('regex are * useful', 'regex are always useful')); $this->assertTrue(wildcard_compare('* are * useful', 'regex are always useful')); $this->assertTrue(wildcard_compare('* are * useful', ' are useful')); $this->assertTrue(wildcard_compare('regex are ? useful', 'regex are 1 useful')); $this->assertTrue(wildcard_compare('regex are ?? useful', 'regex are 12 useful')); $this->assertTrue(wildcard_compare('regex are ??? useful', 'regex are 123 useful')); $this->assertTrue(wildcard_compare('regex ?? are ???? useful', 'regex 12 are 1234 useful')); $this->assertTrue(wildcard_compare('????? are ?????? useful', 'regex are always useful')); $this->assertTrue(wildcard_compare('on[ce]* are ?????? useful', 'one are always useful')); $this->assertTrue(wildcard_compare('on[ce]* are ?????? useful', 'once are always useful')); $this->assertTrue(wildcard_compare('on[ce]* are ?????? useful', 'onccce are always useful')); $this->assertTrue(wildcard_compare(['one *', '* two *', 'three ???'], ' two ')); $this->assertTrue(wildcard_compare(['one *', '* two *', 'three ???'], 'one instance should match')); $this->assertTrue(wildcard_compare(['one *', '* two *', 'three ???'], 'try to match two two')); $this->assertTrue(wildcard_compare(['one *', '* two *', 'three ???'], 'three 123')); $this->assertTrue(wildcard_compare(['one *', '* two *', ['three ???']], 'three 123')); $this->assertTrue(wildcard_compare(['one *', '* two *', [['three ???']]], 'three 123')); $this->assertTrue(wildcard_compare(['one *', '* two *', [[['three ???']]]], 'three 123')); $this->assertTrue(wildcard_compare([[[['one *', '* two *', 'three ???']]]], 'three 123')); $this->assertFalse(wildcard_compare(['one *', '* two *', 'three ???'], '')); $this->assertFalse(wildcard_compare(['one *', '* two *', 'three ???'], 'one')); $this->assertFalse(wildcard_compare(['one *', '* two *', 'three ???'], 'two')); $this->assertFalse(wildcard_compare(['one *', '* two *', 'three ???'], 'three')); $this->assertFalse(wildcard_compare(['one *', '* two *', ['three ???']], 'three')); $this->assertFalse(wildcard_compare(['one *', '* two *', [['three ???']]], 'three')); $this->assertFalse(wildcard_compare(['one *', '* two *', [[['three ???']]]], 'three')); $this->assertFalse(wildcard_compare([[[['one *', '* two *', 'three ???']]]], 'three')); }
/** * Returns true when _ANY_ of passed fields will be non-empty * Examples: required_any[duration_*] or required_any[duration_day,duration_week,duration_month] */ public function required_any($in, $params = [], $fields = []) { $param = trim(is_array($params) ? $params['param'] : $params); $wildcard = false; // Example: duration_day,duration_week,duration_month if (false !== strpos($param, ',')) { $field_names = explode(',', $param); // Example: duration_* } else { $wildcard = $param; } foreach ((array) $fields as $k => $v) { $skip = true; if ($wildcard && wildcard_compare($wildcard, $k)) { $skip = false; } elseif ($field_names && in_array($k, $field_names)) { $skip = false; } if ($skip) { continue; } if ($this->required($v)) { return true; } } return false; }
/** */ function _preload_plugins_list($force = false) { $this->PROFILING && ($this->_timing[] = [microtime(true), __CLASS__, __FUNCTION__, $this->trace_string(), func_get_args()]); if (isset($this->_plugins) && !$force) { return $this->_plugins; } $white_list = (array) $this->_plugins_white_list; $black_list = (array) $this->_plugins_black_list; // Order matters for plugins_classes !! $sets = ['framework' => YF_PATH . 'plugins/*/', 'project' => PROJECT_PATH . 'plugins/*/', 'app' => APP_PATH . 'plugins/*/']; $_plen = strlen(YF_PREFIX); $plugins = []; $plugins_classes = []; $ext = YF_CLS_EXT; // default is .class.php foreach ((array) $sets as $set => $pattern) { foreach ((array) glob($pattern, GLOB_ONLYDIR | GLOB_NOSORT) as $d) { $pname = basename($d); if ($white_list && wildcard_compare($white_list, $pname)) { // result is good, do not check black list if name found here, inside white list } elseif ($black_list && wildcard_compare($black_list, $pname)) { // Do not load files from this plugin break; } $dlen = strlen($d); $classes = []; foreach (array_merge(glob($d . '*/*' . $ext), glob($d . '*/*/*' . $ext)) as $f) { $cname = str_replace($ext, '', basename($f)); $cdir = dirname(substr($f, $dlen)) . '/'; if (substr($cname, 0, $_plen) == YF_PREFIX) { $cname = substr($cname, $_plen); } $classes[$cname][$cdir] = $f; $plugins_classes[$cname] = $pname; } $plugins[$pname][$set] = $classes; } } ksort($plugins); $this->_plugins = $plugins; ksort($plugins_classes); $this->_plugins_classes = $plugins_classes; return $this->_plugins; }
/** */ function grep($pattern_find, $start_dirs, $pattern_path = '*', $extra = []) { if (!$pattern_find) { return false; } if (!$start_dirs) { $start_dirs = APP_PATH; } if (!is_array($start_dirs)) { $start_dirs = [$start_dirs]; } if (!is_array($pattern_find)) { $pattern_find = [$pattern_find]; } $files = []; foreach ((array) $start_dirs as $start_dir) { $start_dir = rtrim($start_dir, '/'); foreach ((array) $this->rglob($start_dir, $pattern_path) as $path) { $files[] = $path; } } $matched = []; foreach ((array) $files as $_id => $path) { if (isset($extra['exclude_paths']) && wildcard_compare($extra['exclude_paths'], $path)) { continue; } $contents = file_get_contents($path); foreach ((array) $pattern_find as $p_find) { if (preg_match_all($p_find, $contents, $m)) { $matched[$files[$_id]] = $extra['return_match'] && isset($m[$extra['return_match']]) ? $m[$extra['return_match']] : $m[0]; continue; } } } return $matched; }