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 ...@@ -179,6 +179,11 @@ class BatchController extends Controller
*/ */
public $crudTidyOutput = true; 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 * @var string the namespace of the ActiveQuery class to be generated
*/ */
...@@ -240,6 +245,7 @@ class BatchController extends Controller ...@@ -240,6 +245,7 @@ class BatchController extends Controller
'modelGenerateLabelsFromComments', 'modelGenerateLabelsFromComments',
'modelGenerateHintsFromComments', 'modelGenerateHintsFromComments',
'crudTidyOutput', 'crudTidyOutput',
'crudFixOutput',
'crudControllerNamespace', 'crudControllerNamespace',
'crudSearchModelNamespace', 'crudSearchModelNamespace',
'crudSearchModelSuffix', 'crudSearchModelSuffix',
...@@ -383,6 +389,7 @@ class BatchController extends Controller ...@@ -383,6 +389,7 @@ class BatchController extends Controller
'accessFilter' => $this->crudAccessFilter, 'accessFilter' => $this->crudAccessFilter,
'baseTraits' => $this->crudBaseTraits, 'baseTraits' => $this->crudBaseTraits,
'tidyOutput' => $this->crudTidyOutput, 'tidyOutput' => $this->crudTidyOutput,
'fixOutput' => $this->crudFixOutput,
'template' => $this->crudTemplate, 'template' => $this->crudTemplate,
'indexWidgetType' => $this->crudIndexWidgetType, 'indexWidgetType' => $this->crudIndexWidgetType,
'indexGridClass' => $this->crudIndexGridClass, 'indexGridClass' => $this->crudIndexGridClass,
......
...@@ -123,6 +123,11 @@ class Generator extends \yii\gii\generators\crud\Generator ...@@ -123,6 +123,11 @@ class Generator extends \yii\gii\generators\crud\Generator
*/ */
public $tidyOutput = true; 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 * @var string form field for selecting and loading saved gii forms
*/ */
...@@ -388,18 +393,28 @@ class Generator extends \yii\gii\generators\crud\Generator ...@@ -388,18 +393,28 @@ class Generator extends \yii\gii\generators\crud\Generator
public function render($template, $params = []) public function render($template, $params = [])
{ {
$code = parent::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) { 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; $command = Yii::getAlias('@vendor/bin/phptidy').' replace '.$tmpFile;
shell_exec($command); shell_exec($command);
$code = file_get_contents($tmpFile);
}
return file_get_contents($tmpFile); if ($this->fixOutput) {
} else { $command = Yii::getAlias('@vendor/bin/php-cs-fixer').' fix '.$tmpFile;
return $code; shell_exec($command);
$code = file_get_contents($tmpFile);
} }
unlink($tmpFile);
return $code;
} }
public function validateClass($attribute, $params) public function validateClass($attribute, $params)
......
...@@ -22,6 +22,7 @@ $batch = <<<CMD ...@@ -22,6 +22,7 @@ $batch = <<<CMD
--crudPathPrefix={$name}/ \ --crudPathPrefix={$name}/ \
--crudSkipRelations=Variant,Variants \ --crudSkipRelations=Variant,Variants \
--crudProviders=schmunk42\\\\giiant\\\\generators\\\\crud\\\\providers\\\\extensions\\\\EditorProvider \ --crudProviders=schmunk42\\\\giiant\\\\generators\\\\crud\\\\providers\\\\extensions\\\\EditorProvider \
--crudFixOutput=1 \
--tables=country --tables=country
CMD; 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