Skip to content

Commit 0dd0bd5

Browse files
committed
Merge branch 'wip'
* wip: (162 commits) fix faker version update gii and faker revert upd for yii2-fractal changes update yii2-fractal version Update README.md fix condition for add USING add json/jsonb casting for alert columns fix #69 #46 fix pk constraint name for non-integer keys fix support size properties for non-integer primary keys fix andtest support for string primary key fix access to property format add typehint to findModel fix action template for findModel if resource param is not 'id' fix controller ident fix #59 - fix transformer namespace for model with relation to self property - support self-relation by custom property, not by pk fix action templates separate make and save methods; add argument for pass custom uniqueGenerator ...
2 parents a4c4e3e + c9d19f0 commit 0dd0bd5

File tree

308 files changed

+20599
-1463
lines changed

Some content is hidden

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

308 files changed

+20599
-1463
lines changed

.dockerignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/.idea
2+
/.git
3+
/tests/tmp/*
4+
/vendor
5+
/.dockerignore
6+
/.editorconfig
7+
/.env
8+
/.env.dist
9+
/.gitattributes
10+
/.gitignore
11+
/.php_cs.cache
12+
/composer.lock
13+
/Makefile

.github/workflows/test.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: yii2-openapi
2+
on:
3+
push:
4+
branches: [ master, wip]
5+
pull_request:
6+
branches: [ master, wip ]
7+
paths-ignore:
8+
- 'docs/**'
9+
- '*.md'
10+
11+
jobs:
12+
test:
13+
if: "!contains(github.event.head_commit.message, 'skip ci') && !contains(github.event.head_commit.message, 'ci skip')"
14+
name: yii2-openapi (PHP ${{ matrix.php-versions }})
15+
runs-on: ubuntu-latest
16+
env:
17+
DB_USERNAME: dbuser
18+
DB_PASSWORD: dbpass
19+
DB_CHARSET: utf8
20+
strategy:
21+
fail-fast: true
22+
matrix:
23+
php-versions: ['7.1', '7.2', '7.3', '7.4']
24+
steps:
25+
- uses: actions/checkout@v2
26+
27+
- name: Setup PHP, with composer and extensions
28+
uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
29+
with:
30+
php-version: ${{ matrix.php-versions }}
31+
extensions: mbstring, intl, gd, zip, dom, pgsql
32+
tools: php-cs-fixer
33+
34+
- name: Get composer cache directory
35+
id: composercache
36+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
37+
38+
- name: Cache Composer packages
39+
id: composer-cache
40+
uses: actions/cache@v2
41+
with:
42+
path: ${{ steps.composercache.outputs.dir }}
43+
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.json') }}
44+
restore-keys: |
45+
${{ runner.os }}-php-${{ matrix.php-versions }}
46+
47+
- name: Install deps
48+
run: composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader
49+
50+
- name: Prepare permissions
51+
run: mkdir -p tests/tmp && chmod -R 0777 tests/tmp
52+
53+
- name: Unit tests
54+
run: php vendor/bin/phpunit
55+
56+
- name: Check style
57+
run: make check-style
58+

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/vendor
22
/composer.lock
3-
4-
3+
/phpunit.xml
54
/.php_cs.cache

.php_cs.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
2-
2+
$finder = PhpCsFixer\Finder::create()
3+
->in(['src'])
4+
->exclude(['default']);
35
return PhpCsFixer\Config::create()
6+
->setFinder($finder)
47
->setRules([
58
'@PSR2' => true,
69
'array_syntax' => ['syntax' => 'short'],

.travis.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

Makefile

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
PHPARGS=-dmemory_limit=64M
2+
#PHPARGS=-dmemory_limit=64M -dzend_extension=xdebug.so -dxdebug.remote_enable=1 -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_autostart=1
3+
#PHPARGS=-dmemory_limit=64M -dxdebug.remote_enable=1
14

25
all:
36

47
check-style:
5-
vendor/bin/php-cs-fixer fix src/ --diff --dry-run
8+
vendor/bin/php-cs-fixer fix --diff --dry-run
69

710
fix-style:
811
vendor/bin/indent --tabs composer.json
@@ -13,7 +16,32 @@ install:
1316
composer install --prefer-dist --no-interaction
1417

1518
test:
16-
vendor/bin/phpunit
19+
php $(PHPARGS) vendor/bin/phpunit
1720

18-
.PHONY: all check-style fix-style install test
21+
clean_all:
22+
docker-compose down
23+
sudo rm -rf tests/tmp/*
24+
25+
clean:
26+
sudo rm -rf tests/tmp/app/*
27+
sudo rm -rf tests/tmp/docker_app/*
28+
29+
up:
30+
docker-compose up -d
31+
32+
cli:
33+
docker-compose exec php bash
34+
35+
migrate:
36+
mkdir -p "tests/tmp/app"
37+
mkdir -p "tests/tmp/docker_app"
38+
docker-compose run --rm php sh -c 'cd /app/tests && ./yii migrate --interactive=0'
39+
40+
installdocker:
41+
docker-compose run --rm php composer install && chmod +x tests/yii
42+
43+
testdocker:
44+
docker-compose run --rm php sh -c 'vendor/bin/phpunit tests/unit'
45+
46+
.PHONY: all check-style fix-style install test clean clean_all up cli installdocker migrate testdocker
1947

README.md

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Base on [Gii, the Yii Framework Code Generator](https://www.yiiframework.com/ext
77
[![Latest Stable Version](https://poser.pugx.org/cebe/yii2-openapi/v/stable)](https://packagist.org/packages/cebe/yii2-openapi)
88
[![Total Downloads](https://poser.pugx.org/cebe/yii2-openapi/downloads)](https://packagist.org/packages/cebe/yii2-openapi)
99
[![License](https://poser.pugx.org/cebe/yii2-openapi/license)](https://packagist.org/packages/cebe/yii2-openapi)
10-
[![Build Status](https://travis-ci.org/cebe/yii2-openapi.svg?branch=master)](https://travis-ci.org/cebe/yii2-openapi)
10+
![yii2-openapi](https://github.com/cebe/yii2-openapi/workflows/yii2-openapi/badge.svg?branch=wip)
1111

1212
## what should this do?
1313

@@ -23,7 +23,6 @@ This library is currently work in progress, current features are checked here wh
2323
- [x] generate Models
2424
- [x] generate Database migration
2525
- [x] provide Dummy API via Faker
26-
2726
- [x] update Database and models when API schema changes
2827

2928
## Requirements
@@ -92,6 +91,160 @@ You may specify custom PHP code for generating fake data for a property:
9291
x-faker: "$faker->randomElements(['one', 'two', 'three', 'four'])"
9392
```
9493
94+
### `x-table`
95+
96+
Specify the table name for a Schema that defines a model which is stored in the database.
97+
98+
### `x-pk`
99+
100+
Explicitly specify primary key name for table, if it is different from "id"
101+
102+
```yaml
103+
Post:
104+
x-table: posts
105+
x-pk: uid
106+
properties:
107+
uid:
108+
type: integer
109+
title:
110+
type: string
111+
```
112+
113+
### `x-db-type`
114+
115+
Explicitly specify the database type for a column. (MUST contains only db type! (json, jsonb, uuid, varchar etc))
116+
If x-db-type sets as false, property will be processed as virtual;
117+
It will be added in model as public property, but skipped for migrations generation
118+
119+
### `x-indexes`
120+
Specify table indexes
121+
122+
```yaml
123+
Post:
124+
x-table: posts
125+
x-indexes:
126+
- 'visible,publish_date'
127+
- 'unique:title' #for unique attributes also unique validation check will be added
128+
- 'gist:metadata' #for postgres will generate index using GIST index type
129+
properties:
130+
id:
131+
type: integer
132+
x-db-type: INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT
133+
title:
134+
type: string
135+
visible:
136+
type: boolean
137+
publish_date:
138+
type: string
139+
format: date
140+
metadata:
141+
type: object
142+
x-db-type: JSON NOT NULL DEFAULT '{}'
143+
```
144+
145+
### Many-to-Many relation definition
146+
147+
There are two ways for define many-to-many relations:
148+
149+
#### Simple many-to-many without junction model
150+
151+
- property name for many-to-many relation should be equal lower-cased, pluralized related schema name
152+
153+
- referenced schema should contains mirrored reference to current schema
154+
155+
- migration for junction table can be generated automatically - table name should be [pluralized, lower-cased
156+
schema_name1]2[pluralized, lower-cased schema name2], in alphabetical order;
157+
For example, for schemas Post and Tag - table should be posts2tags, for schemas Post and Attachement - table should
158+
be attachments2posts
159+
160+
```
161+
Post:
162+
properties:
163+
...
164+
tags:
165+
type: array
166+
items:
167+
$ref: '#/components/schemas/Tag'
168+
169+
Tag:
170+
properties:
171+
...
172+
posts:
173+
type: array
174+
items:
175+
$ref: '#/components/schemas/Post'
176+
```
177+
178+
#### Many-to-many with junction model
179+
180+
This way allowed creating multiple many-to-many relations between to models
181+
182+
- define junction schema with all necessary attributes. There are only one important requirement - the junction
183+
schema name
184+
must be started with prefix 'junction_' (This prefix will be used internally only and
185+
will be trimmed before table and model generation)
186+
187+
```
188+
# Model TeamMembers with table team_members will be generated with columns team_id, user_id and role
189+
junction_TeamMembers:
190+
team:
191+
$ref: '#/components/schemas/Team'
192+
user:
193+
$ref: '#/components/schemas/User'
194+
role:
195+
type: string
196+
```
197+
- Both many-to-many related schemas must have properties with reference to "junction_*" schema. These properties will be
198+
used as relation names
199+
200+
```
201+
Team:
202+
properties:
203+
...
204+
team_members:
205+
type: array
206+
items:
207+
$ref: '#/components/schemas/junction_TeamMembers'
208+
209+
User:
210+
properties:
211+
...
212+
memberships: #You absolutely free with naming for relationship attributes
213+
type: array
214+
items:
215+
$ref: '#/components/schemas/junction_TeamMembers'
216+
```
217+
218+
- see both examples here [tests/specs/many2many.yaml](tests/specs/many2many.yaml)
219+
220+
221+
## Things to keep in mind
222+
223+
### Adding columns to existing tables
224+
225+
When adding new fields in the API models, new migrations will be generated to add these fields to the table.
226+
For a project that is already in production, it should be considered to adjust the generated migration to add default
227+
values for existing data records.
228+
229+
One case where this is important is the addition of a new column with `NOT NULL` contraint, which does not provide a default value.
230+
Such a migration will fail when the table is not empty:
231+
232+
```php
233+
$this->addColumn('{{%company}}', 'name', $this->string(128)->notNull());
234+
```
235+
236+
Fail on a PostgreSQL database with
237+
238+
> add column name string(128) NOT NULL to table {{%company}} ...Exception: SQLSTATE[23502]: Not null violation: 7 ERROR: column "name" contains null values
239+
240+
The solution would be to create the column, allowing NULL, set the value to a default and add the null constraint later.
241+
242+
```php
243+
$this->addColumn('{{%company}}', 'name', $this->string(128)->null());
244+
$this->update('{{%company}}', ['name' => 'No name']);
245+
$this->alterColumn('{{%company}}', 'name', $this->string(128)->notNull());
246+
```
247+
95248

96249
## Screenshots
97250

composer.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,29 @@
1919
},
2020
"require": {
2121
"php": ">=7.1.0",
22-
"cebe/php-openapi": "^1.1",
22+
"cebe/php-openapi": "^1.5.0",
2323
"yiisoft/yii2": "~2.0.15",
24-
"yiisoft/yii2-gii": "~2.0.0 | ~2.1.0",
25-
"fzaninotto/faker": "^1.8"
24+
"yiisoft/yii2-gii": "~2.0.0 | ~2.1.0 | ~2.2.0| ~2.3.0",
25+
"laminas/laminas-code": "^3.4",
26+
"insolita/yii2-fractal": "^1.0.0",
27+
"fakerphp/faker": "^1.9"
2628
},
2729
"require-dev": {
2830
"cebe/indent": "*",
29-
"friendsofphp/php-cs-fixer": "~2.13.1",
30-
"phpunit/phpunit": "^6.5",
31+
"friendsofphp/php-cs-fixer": "~2.16",
32+
"phpunit/phpunit": "^6.5|^8.0|^9.0",
3133
"yiisoft/yii2-gii": ">=2.1.0"
3234
},
3335
"autoload": {
3436
"psr-4": {
3537
"cebe\\yii2openapi\\": "src/"
3638
}
3739
},
40+
"autoload-dev": {
41+
"psr-4": {
42+
"tests\\": "tests/"
43+
}
44+
},
3845
"config": {
3946
"platform": {
4047
"php": "7.1.3"

0 commit comments

Comments
 (0)