From 435ead24130f37dd4637f96adeaf78356b7f9bba Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 25 Apr 2024 23:01:11 +0200 Subject: [PATCH] [ENH] refactor function to check file/directory exists --- config/function.php | 243 ++++++++++++++++++++++++++------------------ 1 file changed, 146 insertions(+), 97 deletions(-) diff --git a/config/function.php b/config/function.php index 3580c8c..1a8bbd2 100644 --- a/config/function.php +++ b/config/function.php @@ -3,122 +3,133 @@ * Copyright (c) 2023. Wepesi. */ -use Wepesi\Core\I18n; -use Wepesi\Core\Session; - -/** - * @param string $dir - * @return array - */ -function getSubDirectories(string $dir): array -{ - $subDir = []; - $directories = array_filter(glob($dir), 'is_dir'); - $subDir = array_merge($subDir, $directories); - foreach ($directories as $directory) $subDir = array_merge($subDir, getSubDirectories($directory . '/*')); - return $subDir; +if (! function_exists('getSubDirectories')) { + /** + * @param string $dir + * @return array + */ + function getSubDirectories(string $dir): array + { + $subDir = []; + $directories = array_filter(glob($dir), 'is_dir'); + $subDir = array_merge($subDir, $directories); + foreach ($directories as $directory) $subDir = array_merge($subDir, getSubDirectories($directory . '/*')); + return $subDir; + } } -/** - * @param $class - * @return false|mixed|string - */ -function extractNamespace($class) -{ - $class_arr = explode("\\", $class); - return end($class_arr); +if (! function_exists('extractNamespace')) { + /** + * @param $class + * @return mixed|string + */ + function extractNamespace($class) + { + $class_arr = explode("\\", $class); + return end($class_arr); + } } -/** - * @param $fileName - * @return mixed|string - */ -function checkFileExtension($fileName) -{ - $file_parts = pathinfo($fileName); - return (isset($file_parts['extension']) && $file_parts['extension'] == 'php') ? $fileName : $fileName . '.php'; +if (! function_exists('checkFileExtension')) { + /** + * @param $fileName + * @return string + */ + function checkFileExtension($fileName): string + { + $file_parts = pathinfo($fileName); + return (isset($file_parts['extension']) && $file_parts['extension'] == 'php') ? $fileName : $fileName . '.php'; + } } -/** - * @param array $exclude_folder - * @return void - */ -function autoIndexFolder(array $exclude_folder = []) -{ - $app_root = appDirSeparator(dirname(__DIR__)); - - // define folder to be excluded to not be affected by the process. - $exclude = ['vendor', 'test']; - if (count($exclude_folder)) $exclude = array_merge($exclude, $exclude_folder); - $implode = implode('|', $exclude); - $folder_struct = getSubDirectories($app_root); - - $filter = array_filter($folder_struct, function ($folder_name) use ($implode) { - $pattern = "/$implode/i"; - if (!preg_match($pattern, strtolower(trim($folder_name)))) { - return $folder_name; +if (! function_exists('autoIndexFolder')) { + /** + * @param array $exclude_folder + * @return void + */ + function autoIndexFolder(array $exclude_folder = []) + { + $app_root = appDirSeparator(dirname(__DIR__)); + // check if cache directory exists before processing + $cash_file_dir = appDirSeparator($app_root . '/cache'); + if (!file_exists($cash_file_dir)) { + mkdir($cash_file_dir, 0777, true); } - }); - if (!checkCacheContent($filter,$app_root)) { - foreach ($filter as $subFolder) { - if (!is_file($subFolder . '/index.php')) { - copy(__DIR__ . '/index.php', $subFolder . '/index.php'); + // define exclude folder to not be affected by the situation. + $exclude = ['vendor', 'test']; + if (count($exclude_folder)) $exclude = array_merge($exclude, $exclude_folder); + $implode = implode('|', $exclude); + $folder_struct = getSubDirectories($app_root); + $filter = array_filter($folder_struct, function ($folder_name) use ($implode) { + $pattern = "/$implode/i"; + if (!preg_match($pattern, strtolower(trim($folder_name)))) { + return $folder_name; + } + }); + + if (!checkCacheContent($cash_file_dir, $filter)) { + foreach ($filter as $subFolder) { + if (!is_file($subFolder . '/index.php')) { + copy(__DIR__ . '/index.php', $subFolder . '/index.php'); + } } } } } -/** - * check content from cache file - * @param array $filter - * @param string $app_root - * @return bool - */ -function checkCacheContent(array $filter,string $app_root): bool -{ - $status = true; - // check if cache directory exists before processing - $cash_file_dir = appDirSeparator($app_root . '/cache'); - if (!file_exists($cash_file_dir)) { - mkdir($cash_file_dir, 0777, true); - } - $cash_file_path = appDirSeparator($cash_file_dir . '/index_folder'); - - if (!file_exists($cash_file_path)){ - file_put_contents($cash_file_path, var_export($filter, true)); - } else { - $old_content = file_get_contents($cash_file_path); - if (json_encode($old_content, true) != json_encode($filter, true)) { - file_put_contents($cash_file_path, var_export($filter, true)); +if (! function_exists('checkCacheContent')){ + /** + * @param string $cash_file_dir + * @param array $filter + * @return bool + */ + function checkCacheContent(string $cash_file_dir, array $filter): bool + { + $status = true; + $cash_file_path = appDirSeparator($cash_file_dir . '/index_folder.txt'); + sort($filter); + $file_content = json_encode($filter, true); + $cache_file = fOpen($cash_file_path, 'a+'); + if (!is_file($cash_file_path) || filesize($cash_file_path) < 1) { + fwrite($cache_file, $file_content); } else { - $status = false; + $content = fread($cache_file, filesize($cash_file_path)); + if ($content != $file_content) { + $cache_file = fOpen($cash_file_path, 'w'); + fwrite($cache_file, $file_content); + } else { + $status = false; + } } + fclose($cache_file); + return $status; } - return $status; } -/** - * @param string $path - * @return string - */ -function appDirSeparator(string $path): string -{ - $new_path = $path; - if ((substr(PHP_OS, 0, 3)) === 'WIN') $new_path = str_replace("\\", '/', $path); - return $new_path; +if (! function_exists('appDirSeparator')) { + /** + * @param string $path + * @return string + */ + function appDirSeparator(string $path):string{ + $new_path = $path; + if ((substr(PHP_OS, 0, 3)) === 'WIN') $new_path = str_replace("\\", '/', $path); + return $new_path; + } } -/** - * translate your text - * @param string $message - * @param string|array $value - * @return string - */ -function tra(string $message, $value = null): string -{ - $i18n = new i18n(Session::get('lang')); - $translate_value = !is_array($value) ? [$value] : $value; - return $i18n->translate($message, $translate_value); +if (! function_exists('dumper')) { + /** + * @param $ex + * @return void + */ + function dumper($ex) + { + print('
');
+        print_r($ex);
+        print('
'); + exit(); + } } /** @@ -128,4 +139,42 @@ function tra(string $message, $value = null): string */ function route_path(string $path): string{ return WEB_ROOT . ltrim($path,'/'); +} + +if (! function_exists('fileExists')) { + /** + * Validate if the file exists, and in some case create it + * @param string $filename Path to the file or directory. to check files on network shares. + * @param bool $create if the file does not exist, create it + * @return bool + */ + function fileExists(string $filename, bool $create): bool + { + if (! is_file($filename) && !file_exists($filename)){ + if ($create) { + return (bool) file_put_contents($filename, var_export('',true)); + } + return false; + } + return true; + } +} + +if (! function_exists('directoryExists')) { + /** + * Validate if the file exists, and in some case create it + * @param string $filename Path to the file or directory. to check files on network shares. + * @param bool $create if the file does not exist, create it + * @return bool + */ + function directoryExists(string $filename, bool $create): bool + { + if (! file_exists($filename)){ + if ($create) { + return (bool) mkdir($filename,0777,true); + } + return false; + } + return true; + } } \ No newline at end of file