瀏覽代碼

v0.3.0 - add gitignore; add help info to readme.md; sorting of results (files+folders); option to choose sorting order; option to choose sorting flags

master
父節點
當前提交
c92b8a959d
共有 3 個檔案被更改,包括 110 行新增12 行删除
  1. +14
    -0
      .gitignore
  2. +52
    -0
      README.md
  3. +44
    -12
      dir2json.php

+ 14
- 0
.gitignore 查看文件

@@ -0,0 +1,14 @@
# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
nppBackup

## Test files
test
tests

+ 52
- 0
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: The behaviour of these constants is described on the JSON constants page:
http://php.net/manual/en/json.constants.php 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 <targetFolder> <outputFile> [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 #### Example
``` ```
> php dir2json ./images out.json JSON_PRETTY_PRINT > php dir2json ./images out.json JSON_PRETTY_PRINT


+ 44
- 12
dir2json.php 查看文件

@@ -13,11 +13,12 @@
// > php dir2json.php --help // > 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 = []; $dirList = [];
$tmpList = []; // used to sort files and dirs together
$excludes = [ $excludes = [
".", ".",
@@ -37,15 +38,20 @@ function dir2json($dir)
{ {
if (!in_array($content, $excludes)) if (!in_array($content, $excludes))
{ {
if(is_file($dir."/".$content))
$tmpList[] = strtolower($content);
//$tmpList[] = $content;
if(is_file($dir."/".$content))
$dirList[] = $content; $dirList[] = $content;
else if(is_dir($dir."/".$content)) else if(is_dir($dir."/".$content))
$dirList[$content] = dir2json($dir."/".$content);
$dirList[$content] = dir2json($dir."/".$content, $sortOrder, $sortFlag);
} }
} }
closedir($handler); closedir($handler);
}
return $dirList;
}

// sort based on file/folder name
array_multisort($tmpList, $sortOrder, $sortFlag, $dirList);
return $dirList;
} }


function usage($str='', $ret=2) { function usage($str='', $ret=2) {
@@ -68,15 +74,17 @@ https://github.com/Darkseal/dir2json
------------------------------------------------------ ------------------------------------------------------
USAGE (from CLI): USAGE (from CLI):
> ./dir2json.php <targetFolder> <outputFile> [JSON_OPTIONS]
> ./dir2json.php <targetFolder> <outputFile> [JSON_OPTIONS] [SORT_ORDER] [SORT_FLAG]


EXAMPLE: EXAMPLE:
> ./dir2json.php ./ ./cache.json JSON_PRETTY_PRINT
> ./dir2json.php ./ ./cache.json JSON_PRETTY_PRINT SORT_DESC SORT_NATURAL


HELP: HELP:
> ./dir2json.php -h > ./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_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_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION,
JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR 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; EOT;
exit(0); exit(0);
} }
@@ -99,6 +123,12 @@ $outputFile = isset($argv[2]) ? $argv[2] : null;
$jsonOptions = isset($argv[3]) ? $argv[3] : null; $jsonOptions = isset($argv[3]) ? $argv[3] : null;
$jsonOptions = empty($jsonOptions) ? 0 : constant($jsonOptions); $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 we have a folder to read
if (!is_dir($targetFolder)) { if (!is_dir($targetFolder)) {
if (empty($targetFolder)) $targetFolder = '(empty)'; if (empty($targetFolder)) $targetFolder = '(empty)';
@@ -110,11 +140,13 @@ if (empty($outputFile)) {
usage("Need a valid output file name (empty)", 3); 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); $json = json_encode($arr, $jsonOptions);
if (!file_put_contents($outputFile, $json)) { if (!file_put_contents($outputFile, $json)) {
usage("Could not save output file: $outputFile", 4); usage("Could not save output file: $outputFile", 4);
} }


exit(0); exit(0);
?>
?>

Loading…
取消
儲存