kamranahmedse/smasher

一个PHP包,可以轻松地将目录转换为JSON

v1.0 2016-02-28 18:12 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:31:37 UTC


README

轻松将您的目录结构转换为JSON或数组,反之亦然

Build Status Scrutinizer Code Quality Code Coverage

简介

Smasher是一个PHP工具,可以让您从目录结构中获取JSON或数组表示形式,或者使用指定的表示形式来创建目录结构

当您“粉碎”一个目录时,所有子目录、文件和符号链接都会转换为您指定的表示形式,当您“构建”时,该表示形式将被处理以创建指定的结构,即目录、文件和符号链接将自动创建。

..所有子目录、文件和符号链接都会转换为所需的表示形式...并返回

在哪里使用?

最初是为一个朋友编写的,让他可以在云生成脚本中使用它。然而,您可以在任何需要的地方使用它。

以下是一些启动想法。例如,您可以使用它

  • 当您需要某种类型的虚拟文件系统时。
  • 当解析多个目录时,直接访问文件系统并遍历目录意味着更多的内存使用和资源消耗。
  • 索引您的目录并轻松找到您正在寻找的地方。
  • 或者也许您可以使用它根据某些关键字搜索文件或文件夹。

我很乐意知道您如何最终使用它。

要求

需要php >= 5.4.0

安装

推荐的安装方法是使用composer。更新您的项目composer.json文件并添加以下内容

{
    "require": {
        "kamranahmedse/smasher": "*"
    }
}

然后运行composer install或简单地运行

composer require kamranahmedse/smasher

入门

目前,仅支持jsonarray表示形式,但是您可以轻松扩展它以添加对其他格式(如XML或YML等)的支持,我将在稍后向您展示如何轻松实现这一点。

让我们回到主题,先来说说如何使用。

介绍类 首先,介绍我们将要使用的类。

// Introduce the classes into your scope
use KamranAhmed\Smasher\Scanner;
use KamranAhmed\Smasher\JsonResponse;

粉碎目录 从目录生成JSON表示形式

// Instantiate the scanner class while passing the object
// of which type you need the response. Currently there is
// only JsonResponse that we have so..
$scanner = new Scanner(new JsonResponse());

// Scan the directory and return the JSON for the available content
$dirJson = $scanner->scan('/directory/to-scan');

// Or you can provide the path at which the json representation should
// be stored i.e.
// $scanner->scan('directory/to-scan', 'output/to-scan.json');

..返回目录结构 将JSON表示形式返回到目录结构

// Instantiate the scanner class while passing the object
// of which representation that you are going to use. We are going
// to convert JSON back to directory structure
$scanner = new Scanner(new JsonResponse());

// Specify the path where you need to populate the content in JSON
// Let's populate the directory structure in the JSON representation
// inside the output directory
$scanner->populate('output/', 'path/to/representation/to-use.json');

示例

假设以下目录结构。

- output
- sample-path
    - child-item
        - grand-child
            - child-file.md         // A file with specified content
              (Some content in the child file.)
        - grand-child-sibling
            - empty-file            // A file having no content
    - child-sibling
        - nothing-in-this-file.txt  // A file having no content
    - child-sibling-2
        - sibling-child
            - empty-file            // A file with no content
            - some-link
            - some-file
             (Text inside some-file)

如果我想为sample-path生成JSON表示形式并将其保存到output目录中

use KamranAhmed\Smasher\Scanner;
use KamranAhmed\Smasher\JsonResponse;

$scanner = new Scanner(new JsonResponse);
$scanner->scan('sample-path', 'output/sample-path.json');

这将在output/sample-path.json中创建json文件,其表示形式类似于以下内容

Image of JSON

请注意:键开头的@符号表示属性,而没有@符号的键表示目录。如果您想查看完整的JSON表示形式,请查看此文件

扩展以支持其他格式

为了扩展smasher以支持其他格式,您需要创建一个实现KamranAhmed\Smasher\Contracts\ResponseContract的响应类,并将该类的实例传递给Scanner对象

class SuperResponse implements ResponseContract {

    /**
     * Formats the passed data for example a `JsonResponse` will encode to json, `XMLResponse`
     * will encode to xml etc
     * @param  array $data The data which is to be encoded
     * @return string
     */
    public function encode($data) {
        // Put your logic to convert a nested array to 
        // *super response format* here and return the resulting string
    }
    
    /**
     * Decodes the passed string and creates array from it
     * @param  string $response The existing response which is to be decoded to array
     * @return array
     */
    public function decode($response) {
        // Put your logic to convert the $response string to
        // to a nested array, like the one you encoded, and return
        // the array
    }
}


// Then pass it to scanner object
$scanner = new Scanner(new SuperResponse);

// Directory structure will be transformed to the format you need and 
`output/to-scan.super` file will be created
$scanner->scan('path/to-scan', 'output/to-scan.super');

// To create the directory structure from the response.
$scanner->populate('output/', 'to-scan.super');

贡献

  • 报告任何错误
  • 改进建议和任何附加功能
  • 添加对其他格式的支持并提交拉取请求
  • 增强或改进现有功能

反馈

我很乐意听听您的想法。如果您有任何功能请求或发现任何错误,请创建一个问题。您也可以通过以下邮箱联系我:kamranahmed.se@gmail.com,或者您也可以在twitter上找到我:@kamranahmed_se