Skip to content

Commit

Permalink
Prepare to release (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Oct 4, 2021
1 parent 6e11429 commit 8f4bf84
Show file tree
Hide file tree
Showing 57 changed files with 1,394 additions and 1,217 deletions.
28 changes: 11 additions & 17 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '7.4', '8.0' ]
php: [ '7.4', '8.0', '8.1' ]
os: [ ubuntu-latest ]
stability: [ prefer-lowest, prefer-stable ]

Expand Down Expand Up @@ -43,25 +43,19 @@ jobs:
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Restore Composer Cache
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-
- name: Install Dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress
# Upgrade mockery on PHP 8
- name: Install Dependencies
if: ${{ matrix.php == '8.0' }}
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer update mockery/mockery --prefer-dist --no-interaction --no-progress

- name: Install dependencies with composer
if: matrix.php-version != '8.1'
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install dependencies with composer php 8.1
if: matrix.php-version == '8.1'
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi --ignore-platform-reqs

# Execution
- name: Execute Tests
Expand All @@ -71,4 +65,4 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
fail_ci_if_error: false
fail_ci_if_error: false
52 changes: 52 additions & 0 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: static analysis

on:
pull_request:
push:
schedule:
- cron: '0 0 * * *'

jobs:
mutation:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest

php:
- "8.0"

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
tools: composer:v2, cs2pr
coverage: none

- name: Determine composer cache directory
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

- name: Cache dependencies installed with composer
uses: actions/cache@v2
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-
- name: Update composer
run: composer self-update

- name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Static analysis
run: vendor/bin/psalm --shepherd --stats --output-format=checkstyle | cs2pr --graceful-warnings --colorize
7 changes: 5 additions & 2 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
preset: psr12
risky: true

version: 8
version: 7

finder:
not-name:
- "*.stub.txt"

enabled:
- alpha_ordered_traits
Expand All @@ -12,7 +16,6 @@ enabled:
- combine_nested_dirname
- declare_strict_types
- dir_constant
- final_static_access
- fully_qualified_strict_types
- function_to_constant
- hash_to_slash_comment
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 Spiral Scout
Copyright (c) 2021 Spiral Scout

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
92 changes: 35 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# CycleORM Schema renderer
# Cycle ORM Schema renderer

This package may be used to render schema roles in a terminal or generate php representation for CycleORM schema.
This package may be used to render Cycle ORM Schema in a terminal or generate php representation.

## Installation

The preferred way to install this package is through [Composer](https://getcomposer.org/download/):

```bash
composer require cycle/schema-renderer
```

## Example

### Convert schema to array

Expand All @@ -11,45 +21,41 @@ $converter = new \Cycle\Schema\Renderer\SchemaToArrayConverter();
$schemaArray = $converter->convert($schema);
```

By default, SchemaToArrayConverter converts only common properties from `Cycle\ORM\SchemaInterface`.
If passed `SchemaInterface` doesn't contain `toArray()` method then the `SchemaToArrayConverter` will convert
only common properties from `Cycle\ORM\SchemaInterface`. Null values will be skipped also.

In this case Iif you want to use custom properties you can pass them to the constructor

But if you want to use custom properties you can pass them to the constructor
```php
$converter = new \Cycle\Schema\Renderer\SchemaToArrayConverter();

$schemaArray = $converter->convert($schema, [
'my_custom_property',
SchemaInterface::SOURCE,
42,
CustomClass::CUSTOM_PROPERTY,
...
]);
```

### Render schema to a terminal

```php
use Cycle\Schema\Renderer\ConsoleRenderer\DefaultSchemaOutputRenderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Formatters\StyledFormatter;
use Cycle\Schema\Renderer\ConsoleRenderer\Formatters\PlainFormatter;
use Cycle\Schema\Renderer\OutputSchemaRenderer;

$output = new \Symfony\Component\Console\Output\ConsoleOutput();

$formatter = new StyledFormatter(); // Colorized output
// or
$formatter = new PlainFormatter(); // Plain output without colors
$renderer = new OutputSchemaRenderer(colorize: true);

$renderer = new DefaultSchemaOutputRenderer($schemaArray, $formatter);

foreach ($renderer as $role => $rows) {
$output->writeln($rows);
}
$output->write($renderer->render($schemaArray));
```

By default, DefaultSchemaOutputRenderer renders only common properties.
If you want to extend default CycleORM schema you can create custom renderers and add them to the Output renderer.
By default, `DefaultSchemaOutputRenderer` renders in template only common properties and relations.
Custom properties will be rendered as is in separated block.
If you want to extend default rendering template you can create custom renderers and add them to the Output renderer.

```php
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
use Cycle\Schema\Renderer\OutputSchemaRenderer;

class CustomPropertyRenderer implements Renderer {

Expand All @@ -65,60 +71,32 @@ class CustomPropertyRenderer implements Renderer {
}
}

$renderer = new DefaultSchemaOutputRenderer($schemaArray, $formatter);
$renderer = new OutputSchemaRenderer();

$renderer->addRenderer(
new CustomPropertyRenderer(),
new PropertyRenderer('my_custom_property', 'My super property')
);

foreach ($renderer as $role => $rows) {
$output->writeln($rows);
}
$output->write($renderer->render($schemaArray))
```

### Store schema in a PHP file

```php
use Cycle\Schema\Renderer\SchemaToPhpFileRenderer;
use Cycle\Schema\Renderer\PhpFileRenderer\DefaultSchemaGenerator;
use Cycle\Schema\Renderer\PhpSchemaRenderer;

$path = __DIR__. '/schema.php'

$generator = new DefaultSchemaGenerator();

$renderer = new SchemaToPhpFileRenderer(
$orm->getSchema(), $generator
);
$renderer = new PhpSchemaRenderer();

file_put_contents($path, $renderer->render());
file_put_contents($path, $renderer->render($schemaArray));
```

By default, DefaultSchemaGenerator generates only common properties.
If you want to extend default CycleORM schema you can create custom generators and add them to the Output php file renderer.
The Renderer generates valid PHP code, in which constants from Cycle ORM classes are substituted
for better readability.

## License:

```php
use Cycle\Schema\Renderer\SchemaToPhpFileRenderer;
use Cycle\Schema\Renderer\PhpFileRenderer\DefaultSchemaGenerator;
use Cycle\Schema\Renderer\PhpFileRenderer\Generator;
use Cycle\Schema\Renderer\PhpFileRenderer\VarExporter;

class CustomPropertyGenerator implements Generator {

public function generate(array $schema, string $role): VarExporter
{
$key = 'my_custom_property';

return new VarExporter($key, $schema[$key] ?? null);
}
}

$generator = new DefaultSchemaGenerator([
'my_custom_property' => new CustomPropertyGenerator()
]);

$renderer = new SchemaToPhpFileRenderer(
$orm->getSchema(), $generator
);
```
The MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information.
Maintained by [Spiral Scout](https://spiralscout.com).
58 changes: 35 additions & 23 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
{
"name": "cycle/schema-renderer",
"type": "library",
"license": "MIT",
"description": "Utils for Cycle Schema rendering",
"require": {
"php": ">=7.4",
"cycle/orm": "^1.5|2.0.x-dev"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"spiral/code-style": "^1.0",
"vimeo/psalm": "^4.9"
},
"autoload": {
"psr-4": {
"Cycle\\Schema\\Renderer\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Cycle\\Schema\\Renderer\\Tests\\": "tests/Schema/Renderer/"
}
}
"name": "cycle/schema-renderer",
"type": "library",
"license": "MIT",
"description": "Utils for Cycle ORM Schema rendering",
"require": {
"php": ">=7.4",
"cycle/orm": "1.2 - 2"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"spiral/code-style": "^1.0",
"vimeo/psalm": "^4.10"
},
"autoload": {
"psr-4": {
"Cycle\\Schema\\Renderer\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Cycle\\Schema\\Renderer\\Tests\\": "tests/Schema/Renderer/"
}
},
"scripts": {
"test": [
"phpcs --standard=phpcs.xml",
"psalm --no-cache",
"phpunit"
]
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
45 changes: 45 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Spiral Framework Code Style Ruleset">
<!-- config -->
<rule ref="vendor/spiral/code-style/config/ruleset.xml" />

<rule ref="Generic.PHP.ForbiddenFunctions">
<properties>
<property name="forbiddenFunctions" type="array" extend="true">
<!-- deprecated/aliased functions -->
<element key="join" value="implode" />
<element key="chop" value="rtrim" />
<element key="strchr" value="strstr" />
<element key="show_source" value="highlight_file" />
<element key="ini_alter" value="ini_set" />
<element key="dns_check_record" value="checkdnsrr" />
<element key="dns_get_mx" value="getmxrr" />
<element key="doubleval" value="floatval" />
<element key="is_long" value="is_int" />
<element key="is_integer" value="is_int" />
<element key="is_double" value="is_float" />
<element key="is_real" value="is_float" />
<element key="fputs" value="fwrite" />
<element key="set_file_buffer" value="stream_set_write_buffer" />
<element key="set_socket_blocking" value="stream_set_blocking" />
<element key="socket_set_blocking" value="stream_set_blocking" />
<element key="stream_register_wrapper" value="stream_wrapper_register" />
<element key="socket_set_timeout" value="stream_set_timeout" />
<element key="socket_get_status" value="stream_get_meta_data" />
<element key="is_writeable" value="is_writable" />
<element key="pos" value="current" />
<element key="sizeof" value="count" />

<!-- deprecated/aliased operators -->
<element key="delete" value="unset" />
<element key="print" value="echo" />
<element key="create_function" value="null" />
</property>
</properties>
</rule>

<arg name="colors"/>

<!-- includes -->
<file>./src</file>
</ruleset>
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
Loading

0 comments on commit 8f4bf84

Please sign in to comment.