暇人の寝室

ソフトウェアエンジニアのノート

【PHP】ディレクトリ構造をjsonにして遊ぶ
投稿者: ylafaro0310
2022.11.28
dir-json

プログラミング

目的

  • PHPで簡単なコマンド風スクリプトを書けるようになりたい。
  • PHPでjsonの読み書きをしたい。
  • (半分は趣味)。

コード

まずはディレクトリ構造からjsonを生成するdir-json.php。

<?php
// Scan current dir or specified dir
function scanDirectory($pathname){
  if(!is_dir($pathname) || basename($pathname) === "." || basename($pathname) === ".."){
    return basename($pathname);
  }

  $dirs = scandir($pathname);
  //var_dump($dirs);
  $result = [];
  foreach($dirs as $dir){
    $scanned_dir = scanDirectory($pathname . "/" . $dir);
    if(is_string($scanned_dir)){
      $result[$scanned_dir] = null;
    }else{
      $result[$dir] = $scanned_dir;
    }
  }
  return $result;
}

$json = json_encode(scanDirectory(realpath(".")),JSON_PRETTY_PRINT);
file_put_contents("directories.json",$json);

ついでにテスト用のディレクトリ・ファイルを作成するシェルスクリプト(make_test_file.sh)を書く。

#!/bin/sh

mkdir -p _dir00/_dir10 _dir00/_dir11
mkdir -p _dir01/_dir10 _dir01/_dir11

touch _dir00/_dir10/_file20
touch _dir01/_dir11/_file21

あとはjsonからディレクトリ構造を再現するjson-dir.php。

<?php

$json_path = $argv[count($argv)-1];
$json_file = file_get_contents($json_path);
$json = json_decode($json_file,true);

$options = getopt("o:");
if(array_key_exists("o",$options)){
    $output_path = rtrim($options["o"],"/");
}else{
    $output_path = getcwd();
}

jsonToDir($output_path,$json);

function jsonToDir($output_path,$json){
    foreach($json as $key => $value){
        $pathname = $output_path . "/" . $key;
        if($value){
            mkdir($pathname,0777,true);
            jsonToDir($pathname,$value);
        }else{
            touch($pathname);
        }
    }
}

実行結果

dir-json.phpでディレクトリ構造をjson化。

> ./make_test_file.sh 
> ls
_dir00                  _dir01                  dir-json.php            json-dir.php            make_test_file.sh
> php dir-json.php 
> ls
_dir00                  _dir01                  dir-json.php            directories.json        json-dir.php            make_test_file.sh
> cat directories.json 
{
    ".": null,
    "..": null,
    ".gitignore": null,
    "_dir00": {
        ".": null,
        "..": null,
        "_dir10": {
            ".": null,
            "..": null,
            "_file20": null
        },
        "_dir11": {
            ".": null,
            "..": null
        }
    },
    "_dir01": {
        ".": null,
        "..": null,
        "_dir10": {
            ".": null,
            "..": null
        },
        "_dir11": {
            ".": null,
            "..": null,
            "_file21": null
        }
    },
    "dir-json.php": null,
    "json-dir.php": null,
    "make_test_file.sh": null
}

json-dir.phpでjsonからディレクトリ・ファイルを生成。

> ls
dir-json.php            directories.json        json-dir.php            make_test_file.sh
> php json-dir.php directories.json 
> ls -d $(find `pwd`)
(作業ディレクトリ)/_dir01/_dir10
(作業ディレクトリ)/.gitignore              
(作業ディレクトリ)/_dir01/_dir11
(作業ディレクトリ)/_dir00                  
(作業ディレクトリ)/_dir01/_dir11/_file21
(作業ディレクトリ)/_dir00/_dir10             
(作業ディレクトリ)/dir-json.php
(作業ディレクトリ)/_dir00/_dir10/_file20     
(作業ディレクトリ)/directories.json
(作業ディレクトリ)/_dir00/_dir11             
(作業ディレクトリ)/json-dir.php
(作業ディレクトリ)/_dir01                    
(作業ディレクトリ)/make_test_file.sh
カテゴリ:PHP,プログラミング

コメントを残す

メールアドレスが公開されることはありません。

©2022 暇人の寝室