Skip to content

Commit 0df2645

Browse files
committed
Fix merge conflict
1 parent ca8e804 commit 0df2645

File tree

93 files changed

+1362
-310
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1362
-310
lines changed

.github/workflows/test.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@ jobs:
3131
# Run every tests inside Docker container
3232
- name: Docker Compose Setup
3333
uses: ndeloof/install-compose-action@v0.0.1
34-
with:
35-
# version: v3.5 # defaults to 'latest'
36-
legacy: true # will also install in PATH as `docker-compose`
3734

3835
- name: Clean
3936
run: make clean_all
4037

41-
- name: docker-compose up
38+
- name: docker compose up
4239
run: make up
4340

4441
# https://github.com/shivammathur/setup-php?tab=readme-ov-file#cache-composer-dependencies
@@ -57,7 +54,7 @@ jobs:
5754
restore-keys: ${{ runner.os }}-composer-
5855

5956
- name: Install Docker and composer dependencies
60-
run: docker-compose exec php php -v && make installdocker
57+
run: docker compose exec php php -v && make installdocker
6158

6259
- name: Migrate
6360
run: make UID=0 migrate

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Creating yii2-openapi_maria_1 ... done
8484
Creating yii2-openapi_mysql_1 ... done
8585
Creating yii2-openapi_postgres_1 ... done
8686
Creating yii2-openapi_php_1 ... done
87-
docker-compose exec php bash
87+
docker compose exec php bash
8888
8989
root@f9928598f841:/app# php -v
9090

Makefile

+15-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ check-style:
1010
vendor/bin/php-cs-fixer fix --diff --dry-run
1111

1212
check-style-from-host:
13-
docker-compose run --rm php sh -c 'vendor/bin/php-cs-fixer fix --diff --dry-run'
13+
docker compose run --rm php sh -c 'vendor/bin/php-cs-fixer fix --diff --dry-run'
1414

1515
fix-style:
1616
vendor/bin/indent --tabs composer.json
@@ -24,46 +24,46 @@ test:
2424
php $(PHPARGS) vendor/bin/phpunit
2525

2626
clean_all:
27-
docker-compose down
27+
docker compose down --remove-orphans
2828
sudo rm -rf tests/tmp/*
2929

3030
clean:
3131
sudo rm -rf tests/tmp/app/*
3232
sudo rm -rf tests/tmp/docker_app/*
3333

3434
down:
35-
docker-compose down --remove-orphans
35+
docker compose down --remove-orphans
3636

3737
up:
38-
docker-compose up -d
38+
docker compose up -d
3939
echo "Waiting for mariadb to start up..."
40-
docker-compose exec -T mysql timeout 60s sh -c "while ! (mysql -udbuser -pdbpass -h maria --execute 'SELECT 1;' > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker-compose ps; docker-compose logs; exit 1)
40+
docker compose exec -T mysql timeout 60s sh -c "while ! (mysql -udbuser -pdbpass -h maria --execute 'SELECT 1;' > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker compose ps; docker compose logs; exit 1)
4141

4242
echo "Waiting for Mysql to start up..."
43-
docker-compose exec -T mysql timeout 60s sh -c "while ! (mysql -udbuser -pdbpass -h mysql --execute 'SELECT 1;' > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker-compose ps; docker-compose logs; exit 1)
43+
docker compose exec -T mysql timeout 60s sh -c "while ! (mysql -udbuser -pdbpass -h mysql --execute 'SELECT 1;' > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker compose ps; docker compose logs; exit 1)
4444

4545
cli:
46-
docker-compose exec --user=$(UID) php bash
46+
docker compose exec --user=$(UID) php bash
4747

4848
cli_root:
49-
docker-compose exec --user="root" php bash
49+
docker compose exec --user="root" php bash
5050

5151
cli_mysql:
52-
docker-compose exec --user=$(UID) mysql bash
52+
docker compose exec --user=$(UID) mysql bash
5353

5454
migrate:
55-
docker-compose run --user=$(UID) --rm php sh -c 'mkdir -p "tests/tmp/app"'
56-
docker-compose run --user=$(UID) --rm php sh -c 'mkdir -p "tests/tmp/docker_app"'
57-
docker-compose run --user=$(UID) --rm php sh -c 'cd /app/tests && ./yii migrate --interactive=0'
55+
docker compose run --user=$(UID) --rm php sh -c 'mkdir -p "tests/tmp/app"'
56+
docker compose run --user=$(UID) --rm php sh -c 'mkdir -p "tests/tmp/docker_app"'
57+
docker compose run --user=$(UID) --rm php sh -c 'cd /app/tests && ./yii migrate --interactive=0'
5858

5959
installdocker:
60-
docker-compose run --user=$(UID) --rm php composer install && chmod +x tests/yii
60+
docker compose run --user=$(UID) --rm php composer install && chmod +x tests/yii
6161

6262
tests_dir_write_permission:
63-
docker-compose run --user="root" --rm php chmod -R 777 tests/tmp/ # TODO avoid 777 https://github.com/cebe/yii2-openapi/issues/156
63+
docker compose run --user="root" --rm php chmod -R 777 tests/tmp/ # TODO avoid 777 https://github.com/cebe/yii2-openapi/issues/156
6464

6565
testdocker:
66-
docker-compose run --user=$(UID) --rm php sh -c 'vendor/bin/phpunit --repeat 3'
66+
docker compose run --user=$(UID) --rm php sh -c 'vendor/bin/phpunit --repeat 3'
6767

6868
efs: clean_all up migrate # Everything From Scratch
6969

README.md

+219-1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,224 @@ Such values are not allowed:
186186

187187
If `enum` and `x-db-type` both are provided then for database column schema (migrations), only `x-db-type` will be considered ignoring `enum`.
188188

189+
### `x-scenarios`
190+
191+
Automatically generated scenarios from the model 'x-scenarios'.
192+
Each scenario is assigned attributes as in the 'default' scenario.
193+
The advantage is that the scenario can be used immediately.
194+
This can be overridden in the child model if needed.
195+
196+
The 'default' scenario and all scenarios mentioned in the rules() using 'on' and 'except'
197+
are automatically included in the scenarios() function for the model.
198+
199+
There are three ways to define the scenarios `description`:
200+
- use scenario description default settings `public $scenarioDefaultDescription = "Scenario {scenarioName}"`.
201+
- use custom `scenarioDefaultDescription` at `dbModel`.
202+
- use custom `description` for individual scenario.
203+
204+
1. Example with default setting.
205+
```yaml
206+
Invoice:
207+
type: object
208+
x-scenarios:
209+
- name: create
210+
- name: update
211+
```
212+
213+
The following code is generated in the abstract model:
214+
```php
215+
abstract class Invoice extends \yii\db\ActiveRecord
216+
{
217+
/**
218+
* Scenario create
219+
*/
220+
public const SCENARIO_CREATE = 'create';
221+
222+
/**
223+
* Scenario update
224+
*/
225+
public const SCENARIO_UPDATE = 'update';
226+
227+
/**
228+
* Automatically generated scenarios from the model 'x-scenarios'.
229+
* @return array a list of scenarios and the corresponding active attributes.
230+
*/
231+
public function scenarios()
232+
{
233+
$parentScenarios = parent::scenarios();
234+
235+
/**
236+
* Each scenario is assigned attributes as in the 'default' scenario.
237+
* The advantage is that the scenario can be used immediately.
238+
* This can be overridden in the child model if needed.
239+
*/
240+
$default = $parentScenarios[self::SCENARIO_DEFAULT];
241+
242+
return [
243+
self::SCENARIO_CREATE => $default,
244+
self::SCENARIO_UPDATE => $default,
245+
/**
246+
* The 'default' scenario and all scenarios mentioned in the rules() using 'on' and 'except'
247+
* are automatically included in the scenarios() function for the model.
248+
*/
249+
...$parentScenarios,
250+
];
251+
}
252+
}
253+
```
254+
255+
2. Example with custom `description` for individual scenario.
256+
```yaml
257+
Invoice:
258+
type: object
259+
x-scenarios:
260+
- name: create
261+
description: My custom description for scenario create
262+
- name: update
263+
```
264+
265+
The following code is generated in the abstract model:
266+
```php
267+
abstract class Invoice extends \yii\db\ActiveRecord
268+
{
269+
/**
270+
* My custom description for scenario create
271+
*/
272+
public const SCENARIO_CREATE = 'create';
273+
274+
/**
275+
* Scenario update
276+
*/
277+
public const SCENARIO_UPDATE = 'update';
278+
279+
/**
280+
* Automatically generated scenarios from the model 'x-scenarios'.
281+
* @return array a list of scenarios and the corresponding active attributes.
282+
*/
283+
public function scenarios()
284+
{...}
285+
}
286+
```
287+
288+
3. Example with custom `scenarioDefaultDescription`.
289+
290+
Set custom `scenarioDefaultDescription` at `dbModel`.
291+
`scenarioDefaultDescription` Accepted-Placeholder: {scenarioName}, {scenarioConst}, {modelName}.
292+
293+
For example, for the `create` scenario in the `Invoice` model, the placeholders would result in the following:
294+
`{scenarioName}` = `create`
295+
`{scenarioConst}` = `SCENARIO_CREATE`
296+
`{modelName}` = `Invoice`
297+
298+
php-config-settings
299+
```php
300+
$config['modules']['gii']['generators']['api'] = [
301+
'class' => \cebe\yii2openapi\generator\ApiGenerator::class,
302+
'dbModel' => [
303+
/** Accepted-Placeholder: {scenarioName}, {scenarioConst}, {modelName}. @see DbModel::$scenarioDefaultDescription */
304+
'scenarioDefaultDescription' => "This scenario \"{scenarioName}\" at Model \"{modelName}\" has Constant {scenarioConst}.",
305+
],
306+
...
307+
];
308+
```
309+
310+
```yaml
311+
Invoice:
312+
type: object
313+
x-scenarios:
314+
- name: create
315+
description: My custom description for scenario create
316+
- name: update
317+
```
318+
319+
The following code is generated in the abstract model:
320+
```php
321+
abstract class Invoice extends \yii\db\ActiveRecord
322+
{
323+
/**
324+
* My custom description for scenario create
325+
*/
326+
public const SCENARIO_CREATE = 'create';
327+
328+
/**
329+
* This scenario "update" at Model "Invoice" has Constant SCENARIO_UPDATE.
330+
*/
331+
public const SCENARIO_UPDATE = 'update';
332+
333+
/**
334+
* Automatically generated scenarios from the model 'x-scenarios'.
335+
* @return array a list of scenarios and the corresponding active attributes.
336+
*/
337+
public function scenarios()
338+
{...}
339+
}
340+
```
341+
342+
4. Example with custom `scenarioDefaultDescription`
343+
and use-case: both '\cebe\yii2openapi\generator\ApiGenerator::class' and '\common\client_generator\{your_ApiClientGenerator}::class' are used.
344+
345+
Set custom `scenarioDefaultDescription` at `dbModel`.
346+
`scenarioDefaultDescription` Accepted-Placeholder: {scenarioName}, {scenarioConst}, {modelName}.
347+
348+
php-config-settings
349+
```php
350+
$config['modules']['gii']['generators']['api'] = [
351+
'class' => \cebe\yii2openapi\generator\ApiGenerator::class,
352+
'dbModel' => [
353+
/** Accepted-Placeholder: {scenarioName}, {scenarioConst}, {modelName}. @see DbModel::$scenarioDefaultDescription */
354+
'scenarioDefaultDescription' => implode("\n", [
355+
"This Backend-Scenario \"{scenarioName}\" exist in both the frontend model and the backend model.",
356+
"@see \common\client\models\{modelName}::{scenarioConst}",
357+
]),
358+
],
359+
...
360+
];
361+
362+
$config['modules']['gii']['generators']['api-client'] = [
363+
'class' => \common\client_generator\{your_ApiClientGenerator}::class,
364+
'dbModel' => [
365+
/** AcceptedInputs: {scenarioName}, {scenarioConst}, {modelName}. @see DbModel::$scenarioDefaultDescription */
366+
'scenarioDefaultDescription' => implode("\n", [
367+
"This Frontend-Scenario \"{scenarioName}\" exist in both the frontend model and the backend model.",
368+
"@see \common\models\base\{modelName}::{scenarioConst}",
369+
]),
370+
],
371+
...
372+
```
373+
374+
```yaml
375+
Invoice:
376+
type: object
377+
x-scenarios:
378+
- name: create
379+
- name: update
380+
```
381+
382+
The following code is generated in the abstract model:
383+
```php
384+
abstract class Invoice extends \yii\db\ActiveRecord
385+
{
386+
/**
387+
* This Backend-Scenario "create" exist in both the frontend model and the backend model.
388+
* @see \common\client\models\Invoice::SCENARIO_CREATE
389+
*/
390+
public const SCENARIO_CREATE = 'create';
391+
392+
/**
393+
* This Backend-Scenario "update" exist in both the frontend model and the backend model.
394+
* @see \common\client\models\Invoice::SCENARIO_UPDATE
395+
*/
396+
public const SCENARIO_UPDATE = 'update';
397+
398+
/**
399+
* Automatically generated scenarios from the model 'x-scenarios'.
400+
* @return array a list of scenarios and the corresponding active attributes.
401+
*/
402+
public function scenarios()
403+
{...}
404+
}
405+
```
406+
189407
### `x-indexes`
190408

191409
Specify table indexes
@@ -350,7 +568,7 @@ related objects, `x-no-relation` (type: boolean, default: false) is used.
350568

351569
This will not generate 'comments' column in database migrations. But it will generate `getComments()` relation in Yii model file.
352570

353-
In order to make it real database column, extension `x-no-relation` can be used.
571+
In order to make it real database column, OpenAPI extension `x-no-relation` can be used.
354572

355573
```yaml
356574
comments:

docker-compose.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "3.5"
1+
name: yii2-docker
22
services:
33
php:
44
image: yii2-openapi-php:${PHP_VERSION:-8.3}
@@ -73,4 +73,3 @@ networks:
7373
ipam:
7474
config:
7575
- subnet: 172.14.0.0/24
76-

src/generator/ApiGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class ApiGenerator extends Generator
143143
/**
144144
* @var array Map for custom dbModels
145145
*
146-
* @see DbModel::$scenarioDefaultDescription with acceptedInputs: {scenarioName}, {scenarioConst}, {modelName}.
146+
* @see DbModel::$scenarioDefaultDescription with Accepted-Placeholder: {scenarioName}, {scenarioConst}, {modelName}.
147147
* @example
148148
* 'dbModel' => [
149149
* 'scenarioDefaultDescription' => "Scenario {scenarioName}",

src/generator/default/dbmodel.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@ public static function tableName()
8484
*/
8585
public function scenarios()
8686
{
87+
$parentScenarios = parent::scenarios();
88+
8789
/**
8890
* Each scenario is assigned attributes as in the 'default' scenario.
8991
* The advantage is that the scenario can be used immediately.
9092
* This can be overridden in the child model if needed.
9193
*/
92-
$default = parent::scenarios()[self::SCENARIO_DEFAULT];
94+
$default = $parentScenarios[self::SCENARIO_DEFAULT];
9395

9496
return [
9597
<?php foreach ($scenarios as $scenario): ?>
@@ -99,7 +101,7 @@ public function scenarios()
99101
* The 'default' scenario and all scenarios mentioned in the rules() using 'on' and 'except'
100102
* are automatically included in the scenarios() function for the model.
101103
*/
102-
...parent::scenarios(),
104+
...$parentScenarios,
103105
];
104106
}
105107
<?php endif;?>

src/lib/Config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class Config extends BaseObject
112112
/**
113113
* @var array Map for custom dbModels
114114
*
115-
* @see DbModel::$scenarioDefaultDescription with acceptedInputs: {scenarioName}, {scenarioConst}, {modelName}.
115+
* @see DbModel::$scenarioDefaultDescription with Accepted-Placeholder: {scenarioName}, {scenarioConst}, {modelName}.
116116
* @example
117117
* 'dbModel' => [
118118
* 'scenarioDefaultDescription' => "Scenario {scenarioName}",

0 commit comments

Comments
 (0)