Commit 182a44f0 authored by Tobias Munk's avatar Tobias Munk

added fixOutput parameter to run php-cs-fixer on generated files

parent 047c7393
Pipeline #16246 passed with stages
in 5 minutes and 11 seconds
......@@ -179,6 +179,11 @@ class BatchController extends Controller
*/
public $crudTidyOutput = true;
/**
* @var bool whether to fix generated code (PSR-2)
*/
public $crudFixOutput = true;
/**
* @var string the namespace of the ActiveQuery class to be generated
*/
......@@ -240,6 +245,7 @@ class BatchController extends Controller
'modelGenerateLabelsFromComments',
'modelGenerateHintsFromComments',
'crudTidyOutput',
'crudFixOutput',
'crudControllerNamespace',
'crudSearchModelNamespace',
'crudSearchModelSuffix',
......@@ -383,6 +389,7 @@ class BatchController extends Controller
'accessFilter' => $this->crudAccessFilter,
'baseTraits' => $this->crudBaseTraits,
'tidyOutput' => $this->crudTidyOutput,
'fixOutput' => $this->crudFixOutput,
'template' => $this->crudTemplate,
'indexWidgetType' => $this->crudIndexWidgetType,
'indexGridClass' => $this->crudIndexGridClass,
......
......@@ -123,6 +123,11 @@ class Generator extends \yii\gii\generators\crud\Generator
*/
public $tidyOutput = true;
/**
* @var bool whether to use php-cs-fixer to generate PSR compatible output
*/
public $fixOutput = false;
/**
* @var string form field for selecting and loading saved gii forms
*/
......@@ -388,18 +393,28 @@ class Generator extends \yii\gii\generators\crud\Generator
public function render($template, $params = [])
{
$code = parent::render($template, $params);
// create temp file for code formatting
$tmpDir = Yii::getAlias('@runtime/giiant');
FileHelper::createDirectory($tmpDir);
$tmpFile = $tmpDir.'/'.md5($template);
file_put_contents($tmpFile, $code);
if ($this->tidyOutput) {
$tmpDir = Yii::getAlias('@runtime/giiant');
FileHelper::createDirectory($tmpDir);
$tmpFile = $tmpDir.'/'.md5($template);
file_put_contents($tmpFile, $code);
$command = Yii::getAlias('@vendor/bin/phptidy').' replace '.$tmpFile;
shell_exec($command);
$code = file_get_contents($tmpFile);
}
return file_get_contents($tmpFile);
} else {
return $code;
if ($this->fixOutput) {
$command = Yii::getAlias('@vendor/bin/php-cs-fixer').' fix '.$tmpFile;
shell_exec($command);
$code = file_get_contents($tmpFile);
}
unlink($tmpFile);
return $code;
}
public function validateClass($attribute, $params)
......
......@@ -22,6 +22,7 @@ $batch = <<<CMD
--crudPathPrefix={$name}/ \
--crudSkipRelations=Variant,Variants \
--crudProviders=schmunk42\\\\giiant\\\\generators\\\\crud\\\\providers\\\\extensions\\\\EditorProvider \
--crudFixOutput=1 \
--tables=country
CMD;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment