Skip to content

Commit

Permalink
custom scenarioDefaultDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
siggi-k committed Aug 25, 2024
1 parent 5c7fa9b commit 775437f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/generator/ApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace cebe\yii2openapi\generator;

use cebe\yii2openapi\lib\items\DbModel;
use yii\db\mysql\Schema as MySqlSchema;
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
use yii\db\pgsql\Schema as PgSqlSchema;
Expand Down Expand Up @@ -133,6 +134,15 @@ class ApiGenerator extends Generator
*/
public $excludeModels = [];

/**
* @var array Map for custom dbModels
* @example
* 'dbModel' => [
* 'scenarioDefaultDescription' => " Scenario {name}", @see DbModel::$scenarioDefaultDescription
* ]
*/
public $dbModel = [];

/**
* @var array Map for custom controller names not based on model name for exclusive cases
* @example
Expand Down
9 changes: 9 additions & 0 deletions src/lib/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class Config extends BaseObject
*/
public $excludeModels = [];

/**
* @var array Map for custom dbModels
* @example
* 'dbModel' => [
* 'scenarioDefaultDescription' => " Scenario {name}", @see DbModel::$scenarioDefaultDescription
* ]
*/
public $dbModel = [];

/**
* @var array Map for custom controller names not based on model name for exclusive cases
* @example
Expand Down
3 changes: 3 additions & 0 deletions src/lib/generators/ModelsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public function generate():CodeFiles
}
foreach ($this->models as $model) {
$className = $model->getClassName();
if (!empty($this->config->dbModel['scenarioDefaultDescription'])) {
$model->scenarioDefaultDescription = $this->config->dbModel['scenarioDefaultDescription'];
}
if ($model->isNotDb === false) {
$this->files->add(new CodeFile(
Yii::getAlias("$modelPath/base/$className.php"),
Expand Down
16 changes: 13 additions & 3 deletions src/lib/items/DbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ class DbModel extends BaseObject
*/
private array $scenarios;

/**
* @var string
* Here, you can set your own default description for the scenario.
* You can use the {name} attribute from the schema for the YAML model.
*/
public string $scenarioDefaultDescription = " Scenario {name}";

public function getTableAlias():string
{
return '{{%' . $this->tableName . '}}';
Expand Down Expand Up @@ -227,9 +234,12 @@ private function getScenariosByOpenapiSchema(): array

foreach ($scenarios as $key => $scenario) {
$scenarios[$key]['const'] = 'SCENARIO_' . strtoupper($scenario['name']);
$scenarios[$key]['description'] = !empty($scenario['description']) ?
FormatHelper::getFormattedDescription($scenario['description'], 5)
: ' Scenario ' . $scenario['name'];
$scenarios[$key]['description'] = FormatHelper::getFormattedDescription(
!empty($scenario['description']) ?
$scenario['description']
: str_replace('{name}', $scenario['name'], $this->scenarioDefaultDescription
),
5);
}

return $scenarios;
Expand Down

0 comments on commit 775437f

Please sign in to comment.