diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2142f38 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db +nppBackup + +## Test files +test +tests diff --git a/README.md b/README.md index 2dac9cf..a30bcfe 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,58 @@ JSON_OPTIONS is a bitmask consisting of: The behaviour of these constants is described on the JSON constants page: http://php.net/manual/en/json.constants.php +#### Help Info +``` +$ ./dir2json.php -h +------------------------------------------------------ +dir2json - v0.3.0 + +by Ryan & Tavinus, 2015-2018 +http://www.ryadel.com/ +https://github.com/Darkseal/dir2json +------------------------------------------------------ + +USAGE (from CLI): + > ./dir2json.php [JSON_OPTIONS] [SORT_ORDER] [SORT_FLAG] + +EXAMPLE: + > ./dir2json.php ./ ./cache.json JSON_PRETTY_PRINT SORT_DESC SORT_NATURAL + +HELP: + > ./dir2json.php -h + +------------------------------------------------------ + +JSON_OPTIONS is a bitmask consisting of: (default: 0) + JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, + JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, + JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR + +The behaviour of these constants is described on the JSON constants page: + http://php.net/manual/en/json.constants.php + +for further info on PHP's json_encode function, read here: + http://php.net/manual/en/function.json-encode.php + +------------------------------------------------------ + +Sorting is done by the PHP funcion array_multisort() +http://php.net/manual/en/function.array-multisort.php + +SORT_ORDER can be either: + SORT_ASC - ascendingly (default) + SORT_DESC - descendingly + +SORT_FLAGS defaults to SORT_LOCALE_STRING, always case insensitive: + SORT_REGULAR - compare items normally (don't change types) + SORT_NUMERIC - compare items numerically + SORT_STRING - compare items as strings + SORT_LOCALE_STRING - compare items as strings, based on the current locale. + SORT_NATURAL - compare items as strings using "natural ordering" like natsort() + +------------------------------------------------------ +``` + #### Example ``` > php dir2json ./images out.json JSON_PRETTY_PRINT diff --git a/dir2json.php b/dir2json.php index 58631a1..2242a31 100755 --- a/dir2json.php +++ b/dir2json.php @@ -13,11 +13,12 @@ // > php dir2json.php --help // ------------------------------------------------------ -$DIR2JSON = '0.2.1'; +$DIR2JSON = '0.3.0'; -function dir2json($dir) +function dir2json($dir, $sortOrder = SORT_ASC, $sortFlag = SORT_LOCALE_STRING) { $dirList = []; + $tmpList = []; // used to sort files and dirs together $excludes = [ ".", @@ -37,15 +38,20 @@ function dir2json($dir) { if (!in_array($content, $excludes)) { - if(is_file($dir."/".$content)) + $tmpList[] = strtolower($content); + //$tmpList[] = $content; + if(is_file($dir."/".$content)) $dirList[] = $content; else if(is_dir($dir."/".$content)) - $dirList[$content] = dir2json($dir."/".$content); + $dirList[$content] = dir2json($dir."/".$content, $sortOrder, $sortFlag); } } closedir($handler); - } - return $dirList; + } + + // sort based on file/folder name + array_multisort($tmpList, $sortOrder, $sortFlag, $dirList); + return $dirList; } function usage($str='', $ret=2) { @@ -68,15 +74,17 @@ https://github.com/Darkseal/dir2json ------------------------------------------------------ USAGE (from CLI): - > ./dir2json.php [JSON_OPTIONS] + > ./dir2json.php [JSON_OPTIONS] [SORT_ORDER] [SORT_FLAG] EXAMPLE: - > ./dir2json.php ./ ./cache.json JSON_PRETTY_PRINT + > ./dir2json.php ./ ./cache.json JSON_PRETTY_PRINT SORT_DESC SORT_NATURAL HELP: > ./dir2json.php -h - -JSON_OPTIONS is a bitmask consisting of: + +------------------------------------------------------ + +JSON_OPTIONS is a bitmask consisting of: (default: 0) JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR @@ -89,6 +97,22 @@ for further info on PHP's json_encode function, read here: ------------------------------------------------------ +Sorting is done by the PHP funcion array_multisort() +http://php.net/manual/en/function.array-multisort.php + +SORT_ORDER can be either: + SORT_ASC - ascendingly (default) + SORT_DESC - descendingly + +SORT_FLAGS defaults to SORT_LOCALE_STRING, always case insensitive: + SORT_REGULAR - compare items normally (don't change types) + SORT_NUMERIC - compare items numerically + SORT_STRING - compare items as strings + SORT_LOCALE_STRING - compare items as strings, based on the current locale. + SORT_NATURAL - compare items as strings using "natural ordering" like natsort() + +------------------------------------------------------ + EOT; exit(0); } @@ -99,6 +123,12 @@ $outputFile = isset($argv[2]) ? $argv[2] : null; $jsonOptions = isset($argv[3]) ? $argv[3] : null; $jsonOptions = empty($jsonOptions) ? 0 : constant($jsonOptions); +$sortOrder = isset($argv[4]) ? $argv[4] : null; +$sortOrder = empty($sortOrder) ? SORT_ASC : constant($sortOrder); + +$sortFlag = isset($argv[5]) ? $argv[5] : null; +$sortFlag = empty($sortFlag) ? SORT_LOCALE_STRING : constant($sortFlag); + // If we have a folder to read if (!is_dir($targetFolder)) { if (empty($targetFolder)) $targetFolder = '(empty)'; @@ -110,11 +140,13 @@ if (empty($outputFile)) { usage("Need a valid output file name (empty)", 3); } -$arr = dir2json($targetFolder); +$arr = dir2json($targetFolder, $sortOrder, $sortFlag); +//var_dump($arr); + $json = json_encode($arr, $jsonOptions); if (!file_put_contents($outputFile, $json)) { usage("Could not save output file: $outputFile", 4); } exit(0); -?> +?> \ No newline at end of file