Skip to content

Commit cb4b5a2

Browse files
committed
initial commit
0 parents  commit cb4b5a2

Some content is hidden

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

60 files changed

+3206
-0
lines changed

.codeclimate.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version: "2"
2+
exclude_patterns:
3+
- "src/Parser/TokenParser.php"

.github/workflows/ci.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: CI
2+
3+
on: [push]
4+
5+
jobs:
6+
coverage:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v1
10+
- name: Download cc-test-reporter
11+
run: |
12+
mkdir -p ${GITHUB_WORKSPACE}/tmp/
13+
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./tmp/cc-test-reporter
14+
chmod +x ./tmp/cc-test-reporter
15+
- name: Start docker containers
16+
env:
17+
USER_ID: 1001
18+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose up -d
19+
- name: Install dependencies
20+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose exec -T php make update-dependencies
21+
- name: Run coverage
22+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose exec -T php make coverage
23+
- name: Upload coverage results to Code Climate
24+
env:
25+
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
26+
run: |
27+
${GITHUB_WORKSPACE}/tmp/cc-test-reporter after-build -p /var/www/html --coverage-input-type clover;
28+
infection-testing:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v1
32+
- name: Start docker containers
33+
env:
34+
USER_ID: 1001
35+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose up -d
36+
- name: Install dependencies
37+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose exec -T php make update-dependencies
38+
- name: Run infection testing
39+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose exec -T php make infection-testing
40+
static-analysis:
41+
runs-on: ubuntu-latest
42+
steps:
43+
- uses: actions/checkout@v1
44+
- name: Start docker containers
45+
env:
46+
USER_ID: 1001
47+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose up -d
48+
- name: Install dependencies
49+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose exec -T php make update-dependencies
50+
- name: Run static analysis
51+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose exec -T php make static-analysis
52+
code-style:
53+
runs-on: ubuntu-latest
54+
steps:
55+
- uses: actions/checkout@v1
56+
- name: Start docker containers
57+
env:
58+
USER_ID: 1001
59+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose up -d
60+
- name: Install dependencies
61+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose exec -T php make update-dependencies
62+
- name: Check code style
63+
run: cd ${GITHUB_WORKSPACE}/docker && docker-compose exec -T php make code-style

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.idea/
2+
vendor/
3+
example/schema/
4+
composer.lock
5+
.phpunit.result.cache
6+
build/
7+
.php_cs.cache
8+
clover.xml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Nick Chiu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.PHONY: clean code-style coverage help static-analysis update-dependencies install-dependencies infection-testing
2+
.DEFAULT_GOAL := coverage
3+
4+
PHPUNIT = ./vendor/bin/phpunit -c ./phpunit.xml
5+
PHPSTAN = ./vendor/bin/phpstan
6+
PHPCS = ./vendor/bin/phpcs --extensions=php
7+
PHPCBF = ./vendor/bin/
8+
INFECTION = ./vendor/bin/infection
9+
CONSOLE = ./bin/console
10+
COVCHECK = ./vendor/bin/coverage-check
11+
12+
clean:
13+
rm -rf ./build ./vendor
14+
15+
code-style:
16+
${PHPCS} --report-full --report-gitblame --standard=PSR12 ./src
17+
18+
coverage:
19+
${PHPUNIT} && ${COVCHECK} ./build/logs/phpunit/clover.xml 100
20+
cp ./build/logs/phpunit/clover.xml ./
21+
22+
fix-code-style:
23+
${PHPCBF} src/ --standard=PSR12
24+
25+
infection-testing:
26+
make coverage
27+
${INFECTION} --coverage=build/logs/phpunit --min-msi=68 --threads=`nproc`
28+
29+
static-analysis:
30+
${PHPSTAN} analyse --no-progress
31+
32+
update-dependencies:
33+
composer update
34+
35+
install-dependencies:
36+
composer install
37+
38+
help:
39+
# Usage:
40+
# make <target> [OPTION=value]
41+
#
42+
# Targets:
43+
# clean Cleans the coverage and the vendor directory
44+
# code-style Check codestyle using phpcs
45+
# coverage (default) Generate code coverage (html, clover)
46+
# fix-code-style Fix code style
47+
# help You're looking at it!
48+
# infection-testing Run infection/mutation testing
49+
# static-analysis Run static analysis using phpstan
50+
# update-dependencies Run composer update
51+
# install-dependencies Run composer install

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Avro schema generator for PHP
2+
[![Actions Status](https://github.com/nick-zh/php-avro-schema-generator/workflows/CI/badge.svg)](https://github.com/nick-zh/php-avro-schema-generator/workflows/CI/badge.svg)
3+
[![Maintainability](https://api.codeclimate.com/v1/badges/937e14c63beb08885c70/maintainability)](https://codeclimate.com/github/nick-zh/php-avro-schema-generator/maintainability)
4+
[![Test Coverage](https://api.codeclimate.com/v1/badges/937e14c63beb08885c70/test_coverage)](https://codeclimate.com/github/nick-zh/php-avro-schema-generator/test_coverage)
5+
[![Latest Stable Version](https://poser.pugx.org/nick-zh/php-avro-schema-generator/v/stable)](https://packagist.org/packages/nick-zh/php-avro-schema-generator)
6+
[![Latest Unstable Version](https://poser.pugx.org/nick-zh/php-avro-schema-generator/v/unstable)](https://packagist.org/packages/nick-zh/php-avro-schema-generator)
7+
8+
## Installation
9+
```
10+
composer require nick-zh/php-avro-schema-generator "^0.1.0"
11+
```
12+
13+
## Description
14+
Since avro does not support external subschemas, this is just a small
15+
helper to unify your schemas and to create basic schemas from php classes (experimental!).
16+
17+
### Merging subschemas / schemas
18+
Schema template directories: directories containing avsc template files (with subschema)
19+
Output directory: output directory for the unified schema files
20+
21+
#### Merge subschemas (code)
22+
```php
23+
<?php
24+
25+
use NickZh\PhpAvroSchemaGenerator\Registry\SchemaRegistry;
26+
use NickZh\PhpAvroSchemaGenerator\Merger\SchemaMerger;
27+
28+
$registry = (new SchemaRegistry())
29+
->addSchemaTemplateDirectory('./schemaTemplates')
30+
->load();
31+
32+
$merger = new SchemaMerger($registry, './schema');
33+
34+
$merger->merge();
35+
36+
```
37+
38+
#### Merge subschemas (command)
39+
```bash
40+
./vendor/bin/avro-cli avro:subschema:merge ./example/schemaTemplates ./example/schema
41+
```
42+
43+
### Generating schemas from classes
44+
Please note, that this feature is highly experimental.
45+
You probably still need to adjust the generated templates, but it gives you a basic tempalte to work with.
46+
Class direcotries: Directories containing the classes you want to generate schemas from
47+
Output directory: output directory for your generated schema templates
48+
49+
#### Generate schemas (code)
50+
```php
51+
<?php
52+
53+
use NickZh\PhpAvroSchemaGenerator\Registry\ClassRegistry;
54+
use NickZh\PhpAvroSchemaGenerator\Generator\SchemaGenerator;
55+
56+
$registry = (new ClassRegistry())
57+
->addClassDirectory('./example/classes')
58+
->load();
59+
60+
$generator = new SchemaGenerator($registry, './example/schemaTemplates');
61+
62+
$schemas = $generator->generate();
63+
64+
$generator->exportSchemas($schemas);
65+
66+
```
67+
68+
#### Merge subschemas (command)
69+
```bash
70+
./vendor/bin/avro-cli avro:schema:generate ./example/classes ./example/schemaTemplates
71+
```

bin/avro-cli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../src/console

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "nick-zh/php-avro-schema-generator",
3+
"description": "PHP avro schema generator for subschema",
4+
"keywords": [
5+
"avro",
6+
"avro-schema",
7+
"subschema"
8+
],
9+
"license": "MIT",
10+
"require": {
11+
"ext-json": "*",
12+
"symfony/console": "^4.3",
13+
"flix-tech/avro-php": "^3.0|^4.0"
14+
},
15+
"require-dev": {
16+
"phpunit/phpunit": "^9.3",
17+
"phpstan/phpstan": "^0.12",
18+
"squizlabs/php_codesniffer": "^3.4.2",
19+
"friendsofphp/php-cs-fixer": "^2.15",
20+
"infection/infection": "^0.17",
21+
"rregeer/phpunit-coverage-check": "^0.3"
22+
},
23+
"bin": [
24+
"bin/avro-cli"
25+
],
26+
"autoload": {
27+
"psr-4": {
28+
"NickZh\\PhpAvroSchemaGenerator\\": "src/",
29+
"NickZh\\PhpAvroSchemaGenerator\\Example\\": "example/classes/",
30+
"NickZh\\PhpAvroSchemaGenerator\\Tests\\": "tests/"
31+
}
32+
},
33+
"extra": {
34+
"branch-alias": {
35+
"dev-master": "1.0-dev"
36+
}
37+
}
38+
}

docker/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COMPOSE_PROJECT_NAME=php-avro-schema-generator

docker/dev/php/Dockerfile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM php:7.3-cli-alpine3.10
2+
3+
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
4+
ENV COMPOSER_ALLOW_SUPERUSER 1
5+
ARG HOST_USER_ID
6+
ARG HOST_USER
7+
8+
COPY dev/php/files/bin/ /usr/local/bin/
9+
10+
# SYS: Install required packages
11+
RUN apk --no-cache upgrade && \
12+
apk --no-cache add bash git sudo make autoconf gcc g++
13+
14+
RUN if [ ! -z "$HOST_USER_ID" ]; then \
15+
adduser -u $HOST_USER_ID -D -H $HOST_USER && \
16+
echo "ALL ALL=NOPASSWD: ALL" >> /etc/sudoers; \
17+
fi
18+
19+
# PHP: Install php extensions
20+
RUN mkdir /phpIni && \
21+
pecl channel-update pecl.php.net && \
22+
pecl install pcov && \
23+
php-ext-enable pcov
24+
25+
# COMPOSER: install binary
26+
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
27+
28+
# COMPOSER: install dependencies
29+
RUN composer global require hirak/prestissimo
30+
31+
USER $HOST_USER
32+
33+
WORKDIR /var/www/html
34+
35+
CMD tail -f /dev/null
36+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
if [ $# -eq 0 ]
3+
then
4+
echo "Please pass a php module name"
5+
exit
6+
fi
7+
8+
for phpmod in "$@"
9+
do
10+
rm -f /usr/local/etc/php/conf.d/*$phpmod*.ini
11+
done
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
if [[ ${#} -eq 0 ]] ; then
4+
echo -e "\\nPHP module name required!\\n"
5+
exit 1
6+
fi
7+
8+
for phpmod in "${@}" ; do
9+
10+
files=($(find /phpIni -type f -iname "*${phpmod}*.ini" -exec ls -1 '{}' +))
11+
12+
for i in "${files[@]}" ; do
13+
ln -s "${i}" /usr/local/etc/php/conf.d
14+
done
15+
16+
if [[ ${#files[@]} -eq 0 ]] ; then
17+
docker-php-ext-enable "${phpmod}"
18+
fi
19+
20+
done

docker/docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3.2'
2+
services:
3+
php:
4+
build:
5+
context: ./
6+
dockerfile: dev/php/Dockerfile
7+
args:
8+
HOST_USER: ${USER}
9+
HOST_USER_ID: ${USER_ID}
10+
volumes:
11+
- ../:/var/www/html

example/classes/SomeBaseClass.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpKafka\PhpAvroSchemaGenerator\Example;
6+
7+
use PhpKafka\PhpAvroSchemaGenerator\Example\Wonderland\Wonderland;
8+
9+
abstract class SomeBaseClass
10+
{
11+
12+
/**
13+
* @var Wonderland
14+
*/
15+
protected $unicorn;
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpKafka\PhpAvroSchemaGenerator\Example;
6+
7+
class SomeOtherTestClass
8+
{
9+
10+
}

0 commit comments

Comments
 (0)