PHP code example of andy87 / yii2-file-crafter

1. Go to this page and download the library: Download andy87/yii2-file-crafter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andy87 / yii2-file-crafter example snippets


 $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',
                    ]
                ]
            ]
        ]
    ],
];

 $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}}' //...
                ],
                'eventHandlers' => 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',
                    ]
                ],
            ]
        ]
    ],
];

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

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

[
    ['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',
        // ...
    ],
]

$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',
                    ],
                ],
                // ...
            ],
        ],
    ]
];

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

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

Value - ONE = !!product!!
Value - MANY = (>>> products <<<)

Value - ONE = --category--
Value - MANY = (+++categories+++)

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

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

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

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

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

$config['modules']['gii'] = [
    'class' => Module::class,
        'generators' => [
            'fileCrafter' => [
            'options' => [
                // ... 
                'commands' => [
                    'php ../../yii gii/model --tableName={{snake_case}} --modelClass={{PascalCase}}' // ... 
                ],
                // ...
            ],
        ],
    ],
];

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

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

//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
    ]]);
}

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

//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)
    ]]);
}

//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
    ]]);
}

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