PHPackages                             kamranahmedse/smasher - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. kamranahmedse/smasher

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

kamranahmedse/smasher
=====================

A PHP package to easily smash directories and convert them to JSON

v1.0(10y ago)604022MITPHPPHP &gt;=5.3.0

Since Feb 28Pushed 10y ago6 watchersCompare

[ Source](https://github.com/kamranahmedse/smasher)[ Packagist](https://packagist.org/packages/kamranahmedse/smasher)[ Docs](https://github.com/kamranahmedse/smasher)[ RSS](/packages/kamranahmedse-smasher/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Smasher - Smash your directories
================================

[](#smasher---smash-your-directories)

> Easily turn your directory structure to JSON or Array and vice versa

[![Build Status](https://camo.githubusercontent.com/f45dabe64497fc95ab73322b90fddc53dd0cced000a9271107f9a6a0ab44a5e0/68747470733a2f2f7472617669732d63692e6f72672f6b616d72616e61686d656473652f736d61736865722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/kamranahmedse/smasher)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f2948164efba03fc347316107243563ef45eb398ed0ce2da48750e3ae163dab5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b616d72616e61686d656473652f736d61736865722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/kamranahmedse/smasher/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/370cfb22a6778109aef22b2535ec0ac1aa5a8fc8cc4265e672d6680d708f9027/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b616d72616e61686d656473652f736d61736865722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/kamranahmedse/smasher/?branch=master)

Introduction
------------

[](#introduction)

Smasher is a php utility that lets you **get a JSON or array~~ representation from your directory structure**, or **use the specified representations to create the directory structure**

When you *smash* a directory, all the subdirectories, files, symlinks are converted to the representation that you specify and when you *build*, the representaion is processed to create the specified structure i.e. directories, files and symlinks are automatically created.

> ..all the subdirectories, files, symlinks are converted to the representation that you need ...and back

Where to Use?
-------------

[](#where-to-use)

Originally it was written for a friend to let him use it in the cloud formation scripts. However you can use it wherever you want.

Here are some of the ideas to get you started. For example, you can use it:

- Where you need some sort of virtual filesystem.
- When parsing several directories, accessing filesystem directly and iterating through the directories means more memory usage and consumption of resources.
- To index your directories and easily locate the place you are looking for.
- Or may be you can use it to search files or folders based on some keywords.

I would love to know how you end up using it.

Requirements
------------

[](#requirements)

php &gt;= 5.4.0 is required

Installation
------------

[](#installation)

The recommended way of installation is using composer. Update your project's `composer.json` file and add the following:

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

And run `composer install` or simply run

```
composer require kamranahmedse/smasher
```

Getting Started
---------------

[](#getting-started)

Currently `json` and `array` are the only supported representations however you can easily extend it to add support for other formats such as XML or YML etc, I will show you how easy it is to do that in a moment.

Let's stick to the topic, how to use, for now.

**Introducing the classes** First things first, introduce the classes that we are going to use, in your scope.

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

**Smashing a directory** Generating JSON representation from the directory

```
// 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');
```

**..back to directory structure** Turning the JSON representation back to directory structure

```
// 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');
```

Example
-------

[](#example)

Assuming the following directory structure.

```
- 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)

```

If I want to generate the JSON representation for the `sample-path` and save it inside `output` directory

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

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

This will create the json file in `output/sample-path.json` with the representation similar to the following:

[![Image of JSON](https://camo.githubusercontent.com/4c2fb8bf88bdaded0804499b273be0ca483cc03bcca3b0d8514767af433cbc08/687474703a2f2f692e696d6775722e636f6d2f5a4e35635741592e706e67)](https://camo.githubusercontent.com/4c2fb8bf88bdaded0804499b273be0ca483cc03bcca3b0d8514767af433cbc08/687474703a2f2f692e696d6775722e636f6d2f5a4e35635741592e706e67)

Also note that: `@` symbol in the beginning of a key represents the property and the keys without the `@` symbol represents a directory. If you'd like to look at the full JSON representation, [have a look at this file](https://raw.githubusercontent.com/kamranahmedse/smasher/master/tests/data/scanned-samples/scanned-json.json)

### Extending to support other formats

[](#extending-to-support-other-formats)

In order to extend `smasher` for other formats, all you have to do is create a response class by implementing the `KamranAhmed\Smasher\Contracts\ResponseContract` and pass the instance of that class to the `Scanner` object

```
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');
```

### Contributing

[](#contributing)

- Report any bugs
- Suggestions for improvement and any additional functionality
- Add support for additional formats and open a pull request
- Enhancing or improve the available functionality

### Feedback

[](#feedback)

I'd love to hear what you have to say. Please open an issue for any feature requests that you may want or the bugs that you notice. Also you can contact me at  or you can also find me at twitter [@kamranahmed\_se](http://twitter.com/kamranahmed_se)

###  Health Score

31

—

LowBetter than 70% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3677d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e119260b816ceaf3e92000b32fc38122e255b463ec0c23b37568f46da2ac0b77?d=identicon)[kamranahmedse](/maintainers/kamranahmedse)

---

Top Contributors

[![kamranahmedse](https://avatars.githubusercontent.com/u/4921183?v=4)](https://github.com/kamranahmedse "kamranahmedse (24 commits)")

---

Tags

jsondirectoryfolderdirectoriesfoldersdirectory-to-jsondirectories-to-jsonfolders-to-jsonfolder-to-json

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/kamranahmedse-smasher/health.svg)

```
[![Health](https://phpackages.com/badges/kamranahmedse-smasher/health.svg)](https://phpackages.com/packages/kamranahmedse-smasher)
```

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k311.5M547](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k463.5M121](/packages/mtdowling-jmespathphp)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k133.8M804](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k88.3M590](/packages/jms-serializer-bundle)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30421.2M40](/packages/colinodell-json5)[clue/ndjson-react

Streaming newline-delimited JSON (NDJSON) parser and encoder for ReactPHP.

15063.4M13](/packages/clue-ndjson-react)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
