PHPackages                             andy87/yii2-dnk-file-crafter - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. andy87/yii2-dnk-file-crafter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

andy87/yii2-dnk-file-crafter
============================

PHP library for Yii2 - developed by and\_y87

1.2.1(1y ago)17CC-BY-SA-4.0PHPPHP &gt;=8.0

Since Nov 15Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/andy87/yii2-file-crafter)[ Packagist](https://packagist.org/packages/andy87/yii2-dnk-file-crafter)[ Docs](https://andy87.ru)[ RSS](/packages/andy87-yii2-dnk-file-crafter/feed)WikiDiscussions master Synced 1mo ago

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

Yii2 File Crafter
=================

[](#yii2-file-crafter)

Yii2 File Crafter - library for generating a many templates with minimal differences

### Content

[](#content)

- [Setup](#yii2-file-crafter-setup)
- [Config](#yii2-file-crafter-config)
- [Using](#yii2-file-crafter-using)

---

 Setup
-------

[](#----setup)

### Requirements

[](#requirements)

- php &gt;=8.0
- Yii2

###  [Composer](https://getcomposer.org/download/)

[](#----composer)

Add package to project
----------------------

[](#add-package-to-project)

### Using console

[](#using-console)

*(Recommended)*- Composer ( global setup )

```
composer require andy87/yii2-file-crafter:dev-master --dev
```

- Composer.phar ( local setup )

```
php composer.phar require andy87/yii2-file-crafter:dev-master --dev
```

### Using: file `composer.json`

[](#using-file-composerjson)

Open file `composer.json`, in section with key `require` add line:
`"andy87/yii2-file-crafter": "dev-master"`

dont forget update composer

```
composer update
```

\- - - - -

 Config
--------

[](#----config)

Config in the configuration file.

- basic:`config/(web|web-local|local).php`
- advanced:`(frontend|backend)/config/main-local.php`

Minimum config

```
 $config['modules']['gii'] = [
    'class' => yii\gii\Module::class,
    'generators' => [
        'fileCrafter' => [
            'class' => andy87\yii2\file_crafter\Crafter::class,
            'options' => [
                'templates' => [
                    'group_name' => [
                        // 'template' => 'path/to/file.php',
                        'common/services/PascalCaseService' => '@app/common/services/items/{PascalCase}Service.php',
                        'template/test/unit/camelCaseService.tpl' => '@backend/test/unit/{{camelCase}}Service.php',
                        'templates/view/index.php' => 'custom/dir/{{snake_case}}/index.php',
                    ]
                ]
            ]
        ]
    ],
];
```

Full Config with all options

```
 $config['modules']['gii'] = [
    'class' => yii\gii\Module::class,
    'generators' => [
        'fileCrafter' => [
            'class' => andy87\yii2\file_crafter\Crafter::class,
            'options' => [
                'cache' => [
                    'dir' => '@runtime/yii2-file-crafter/cache',
                    'ext' => '.tpl'
                ],
                'source' => [
                    'dir' => '@runtime/yii2-file-crafter/templates/source',
                    'ext' => '.tpl'
                ],
                'custom_fields' => [
                    'singular' => 'label - one',
                    'plural' => 'label - many',
               ],
                'commands' => [
                    'php yii gii/model --tableName={{snake_case}} --modelClass={{PascalCase}} --ns="common\models" --interactive=0' //...
                ],
                'eventHandler' => app\composents\behavior\FileCrafterBehavior::class,
                'autoCompleteStatus' => true,
                'autoCompleteList' => [
                    'autocomplete name 1',
                    'autocomplete name 2',
                ],
                'previewStatus' => true,
                'canDelete' => true,
                'parseDataBase' => ['autocomplete','fakeCache'],
                'templates' => [
                    'common' => [
                        'common/services/PascalCaseService' => 'app/common/services/items/{[PascalCase]}Service.php',
                    ],
                    'backend' => [
                        'backend/test/unit/camelCaseService.tpl' => 'backend/test/unit/{{camelCase}}Service.php',
                    ],
                    'frontend' => [
                        'frontend/view/index.php' => 'app/frontend/view/{{snake_case}}/index.php',
                    ],
                    'all' => [
                        'common/services/PascalCaseService' => 'app/common/services/items/{PascalCase}Service.php',
                        'backend/test/unit/camelCaseService.tpl' => 'backend/test/unit/{{camelCase}}Service.php',
                        'frontend/view/index.php' => 'app/frontend/view/{{snake_case}}/index.php',
                    ]
                ],
            ]
        ]
    ],
];
```

 Using
-------

[](#----using)

- [Marks](#yii2-file-crafter-using-Marks)
- [Cache](#yii2-file-crafter-using-Cache)
- [Source](#yii2-file-crafter-using-Source)
- [Custom Fields](#yii2-file-crafter-using-CustomFields)
- [Autocomplete status](#yii2-file-crafter-autoCompleteStatus)
- [Autocomplete list](#yii2-file-crafter-autoCompleteList)
- [Preview status](#yii2-file-crafter-previewStatus)
- [Can delete](#yii2-file-crafter-canDelete)
- [Parse data base](#yii2-file-crafter-parseDataBase)
- [Commands](#yii2-file-crafter-using-Commands)
- [Events](#yii2-file-crafter-using-Events)
- [Templates](#yii2-file-crafter-using-Templates)

---

Marks
-----

[](#marks)

Module use marks for replace variables in templates.

- `{{PascalCase}}` - PascalCase by schema name
- `{{camelCase}}` - camelCase by schema name
- `{{snake_case}}` - snake\_case by schema name
- `{{kebab-case}}` - kebab-case by schema name
- `{{UPPERCASE}}` - UPPERCASE by schema name
- `{{lowercase}}` - lowercase by schema name
- `{{[key]}}` - custom key from property `custom_fields` on `config` ( see [Custom Fields](#yii2-file-crafter-using-Custom) )

#### Example

[](#example)

for schema name `Product Items` replace marks:

- `{{PascalCase}}` - `ProductItems`
- `{{camelCase}}` - `productItems`
- `{{snake_case}}` - `product_items`
- `{{kebab-case}}` - `product-items`
- `{{UPPERCASE}}` - `PRODUCT ITEMS`
- `{{lowercase}}` - `product items`

---

 Cache (optional)
------------------

[](#----cache----optional)

Configuration for the cache folder with schema data.

- `dir` - path to the cache directory with schema data
- `ext` - extension of the cache file

Default configuration:

```
$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ...
                'cache' => [
                    'dir' => '@runtime/yii2-file-crafter/cache',
                    'ext' => '.json'
                ],
                // ...
            ],
        ],
    ]
];
```

---

 Source (optional)
-------------------

[](#----source----optional)

Configuration for the source folder with templates files.

- `dir` - path to the directory with the templates files source for generation
- `ext` - extension of the templates file

Default configuration:

```
$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ...
                'source' => [
                    'dir' => '@runtime/yii2-file-crafter/templates/source',
                    'ext' => '.tpl'
                ],
                // ...
            ],
        ],
    ]
];
```

---

 Templates (required)
----------------------

[](#----templates----required)

Array with groups of templates for use on generate files.

```
[
    ['group1'] => [
        'template1' => 'path/from/project/root/to/resultFile.tpl',
        'template2.tpl' => 'path/from/project/root/to/resultFile.php',
        // ...
    ],
    ['group2'] => [
        'template1.php' => '@path/alias/to/resultFile.tpl',
        '@alias/to/template' => 'path/from/project/root/to/resultFile.php',
        // ...
    ],
]
```

The source path may contain:

- some `@` alias ( `source['dir']` - default container )
- `ext` for generate any file type ( `.php` default )
- some `{{variable}}` ( see [Marks](#yii2-file-crafter-using-Marks) )

File source-template will be searched in the `source` folder.
Source folder path can be set in the configuration file. ( see [Source](#yii2-file-crafter-using-Source) )

The resultFile path may contain:

- some `@` alias ( `@app/` - default prefix )
- `ext` for generate any file type ( `.php` default )
- some `{{variable}}` ( see [Marks](#yii2-file-crafter-using-Marks) )

Content of the templates file rendered with the `View` method `renderFile`
And prepared with the `$replaceList` array contains all marks. ( see [Marks](#yii2-file-crafter-using-Marks) )
And also passed to the render method:

- `$schema` - schema object
- `$generator` - self generator object

```
$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ...
                'templates' => [
                    'all' => [
                        '@backend/dir/by/alias/camelCaseService.tpl' => '@backend/generate/by/alias/{{camelCase}}Service.php',
                        'dir/on/source/dir/generate_file' => 'custom/dir/on/source/dir/{{snake_case}}/generate_file.tpl',
                    ],
                ],
                // ...
            ],
        ],
    ]
];
```

---

 Custom Fields (optional)
--------------------------

[](#----custom-fields----optional)

Array with custom fields for use custom variables in templates.
Using on template key wrapped in square brackets: `{{%key%}}`
Example: `{{key_one}}`, `{{key_two}}`...

Example simple config

```
$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ...
                'custom_fields' => [
                    'singular' => 'one',
                    'plural' => 'many',
                ],
                // ...
            ],
        ],
    ]
];
```

with template:

```
Value - ONE = {{singular}}
Value - MANY = ({{plural}})
```

 \_\_\_

Schema 1: `Product Items`
Field `one` = `!!product!!`
Field `many` = `>>> products  products  Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ...
                'autoCompleteStatus' => true,
                // ...
            ],
        ],
    ],
];
```

---

 Autocomplete list (optional)
------------------------------

[](#----autocomplete-list----optional)

Key `autoCompleteList` contain list of autocomplete field `Schema name` in the form self custom list.

Type: `array`

```
$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ...
                'autoCompleteList' => [
                    'Product Items',
                    'Category Group',
                    'User Profile',
                    // ...
                ],
                // ...
            ],
        ],
    ],
];
```

---

Preview status (optional)
-------------------------

[](#preview-statusoptional)

Key `previewStatus` contain status for preview file content on hover icon in the form.

Variants: `true`(default) or `false`

```
$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ...
                'previewStatus' => true,
                // ...
            ],
        ],
    ],
];
```

---

Can delete (optional)
---------------------

[](#can-deleteoptional)

Key `canDelete` contain status for delete schema from the form.

Variants: `true`(default) or `false`

```
$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ...
                'canDelete' => true,
                // ...
            ],
        ],
    ],
];
```

---

 Parse data base (optional)
----------------------------

[](#----parse-data-base----optional)

Key `parseDataBase` contain list of target for extend schema name list from database.

Variants: `array` with values:

- `autocomplete`
- `fakeCache`

Default `empty`;

```
$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ...
                'parseDataBase' => ['autocomplete','fakeCache'],
                // ...
            ],
        ],
    ],
];
```

 Commands (optional)
---------------------

[](#----commands----optional)

Key `commands` contain list `cli` command for call before generate any file.
command make use of the `{{variable}}` in the command string ( see [Marks](#yii2-file-crafter-using-Marks) )

Example: generate gii model for table name from schema name before generate fileContent

Default `empty`;

```
$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ...
                'commands' => [
                    'php yii gii/model --tableName={{snake_case}} --modelClass={{PascalCase}} --ns="common\models"  --interactive=0 --overwrite=1' // ...
                ],
                // ...
            ],
        ],
    ],
];
```

 Events (optional)
-------------------

[](#----events----optional)

Make use of the `eventHandlers` key to add a behavior to the module.

Example: add behavior `FileCrafterBehavior` to the module

Default `null`;

```
$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
        'options' => [
                // ...
                'eventHandlers' => FileCrafterBehavior::class,
                // ...
            ],
        ],
    ],
];
```

#### Before init

[](#before-init)

`CrafterEvent::BEFORE_INIT` before init module

#### After init

[](#after-init)

`CrafterEvent::AFTER_INIT` after init module

Come events has special properties...

#### Before generate

[](#before-generate)

`CrafterEventGenerate::BEFORE` before generate all files

```
//class FileCrafterBehavior extends Behavior
public function beforeGenerate(CrafterEventGenerate $crafterEventGenerate): void {
    Yii::info([ 'Generated files', [
        $crafterEventGenerate->files // empty (call before generate)
    ]]);
}
```

#### Before command

[](#before-command)

`CrafterEventCommand::BEFORE` before run cli command

```
//class FileCrafterBehavior extends Behavior
public function beforeCommand(CrafterEventCommand $crafterEventCommand): void {
       Yii::error([ __METHOD__, [
        $crafterEventCommand->cmd->exec,
        $crafterEventCommand->cmd->output, // empty (call before exec command)
        $crafterEventCommand->cmd->replaceList
    ]]);
}
```

##### Cmd

[](#cmd)

`\andy87\yii2\file_crafter\components\models\Dto`

- string **$exec** - *exec command*
- string **$output** - *exec output*
- array **$replaceList** - *replace map*

#### After command

[](#after-command)

`CrafterEventCommand::AFTER` after run cli command

```
//class FileCrafterBehavior extends Behavior
public function afterCommand(CrafterEventCommand $crafterEventCommand): void {
    Yii::error([ __METHOD__, [
        $crafterEventCommand->cmd->exec,
        $crafterEventCommand->cmd->output, // output command
        $crafterEventCommand->cmd->replaceList
    ]]);
}
```

#### Before render

[](#before-render)

`CrafterEventRender::BEFORE` before render file

```
//class FileCrafterBehavior extends Behavior
public function beforeRender(CrafterEventRender $crafterEventRender): void {
    Yii::error([ __METHOD__, [
        $crafterEventRender->schema,
        $crafterEventRender->sourcePath,
        $crafterEventRender->generatePath,
        $crafterEventRender->replaceList,
        $crafterEventRender->content // empty (call before render file)
    ]]);
}
```

#### After render

[](#after-render)

`CrafterEventRender::AFTER` after render file

```
//class FileCrafterBehavior extends Behavior
public function afterRender(CrafterEventRender $crafterEventRender): void {
    Yii::error([ __METHOD__, [
        $crafterEventRender->schema,
        $crafterEventRender->sourcePath,
        $crafterEventRender->generatePath,
        $crafterEventRender->replaceList,
        $crafterEventRender->content // content file
    ]]);
}
```

#### After generate

[](#after-generate)

`CrafterEventGenerate::AFTER` after generate all files

```
public function afterGenerate(CrafterEventGenerate $crafterEventGenerate): void {
    Yii::info([ 'Generated files', [
        $crafterEventGenerate->files // CodeFile[]
    ]]);
}
```

---

[Packagist](https://packagist.org/packages/andy87/yii2-file-crafter)

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance53

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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

Every ~0 days

Total

2

Last Release

542d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/548115774b8d117de69e58a5c7759f93d4cfcb37251e4eda2a41f46da85497b0?d=identicon)[andy87](/maintainers/andy87)

---

Top Contributors

[![andy87](https://avatars.githubusercontent.com/u/4658489?v=4)](https://github.com/andy87 "andy87 (103 commits)")

---

Tags

yii2yii2-extensionphplibrarygeneratoryii2and\_y87crafter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/andy87-yii2-dnk-file-crafter/health.svg)

```
[![Health](https://phpackages.com/badges/andy87-yii2-dnk-file-crafter/health.svg)](https://phpackages.com/packages/andy87-yii2-dnk-file-crafter)
```

###  Alternatives

[lambdish/phunctional

λ PHP functional library

3612.0M23](/packages/lambdish-phunctional)[akamon/phunctional

λ PHP functional library

3609.4k](/packages/akamon-phunctional)[fedemotta/yii2-widget-datatables

DataTables widget for Yii2

34179.4k1](/packages/fedemotta-yii2-widget-datatables)

PHPackages © 2026

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