Skip to content

Commit

Permalink
Initialize sulu messenger components (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored Mar 10, 2022
1 parent 37ec78e commit 73da6f6
Show file tree
Hide file tree
Showing 30 changed files with 1,110 additions and 1 deletion.
95 changes: 95 additions & 0 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Test application

on:
pull_request:
push:
branches:
- '[0-9]+.x'
- '[0-9]+.[0-9]+'

jobs:
test:
name: 'PHP ${{ matrix.php-version }} (${{ matrix.dependency-versions }}, Coverage ${{ matrix.coverage }})'
runs-on: ubuntu-latest

env:
DATABASE_URL: mysql://root:root@127.0.0.1:3306/sulu_messenger_test?serverVersion=5.7&charset=utf8mb4

strategy:
fail-fast: false
matrix:
include:
- php-version: '8.0'
coverage: false
dependency-versions: 'lowest'
env:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.1'
coverage: true
dependency-versions: 'highest'
env:
SYMFONY_DEPRECATIONS_HELPER: weak

services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5

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

- name: Install and configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: ctype, iconv, mysql
coverage: pcov

- name: Install composer dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependency-versions }}
composer-options: ${{ matrix.composer-options }}

- name: Bootstrap test environment
run: composer bootstrap-test-environment

- name: Execute test cases
if: matrix.coverage == false
run: composer test

- name: Execute test cases with coverage
if: ${{ matrix.coverage }}
run: composer test-with-coverage

- name: Check code coverage
if: ${{ matrix.coverage }}
run: composer check-coverage

lint:
name: "PHP Lint"
runs-on: ubuntu-latest

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

- name: Install and configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
extensions: ctype, iconv, mysql

- name: Install composer dependencies
uses: ramsey/composer-install@v1
with:
dependency-versions: highest

- name: Lint Code
run: composer lint
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.idea
*.iml

/vendor/
composer.lock

.php-cs-fixer.cache
.phpunit.result.cache
.deptrac.cache

/tests/Application/var
/tests/Application/secrets
43 changes: 43 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
->exclude(['tests/Application/var'])
->notName('bundles.php');

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'ordered_imports' => true,
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
'phpdoc_align' => ['align' => 'left'],
'class_definition' => [
'multi_line_extends_each_single_line' => true,
],
'linebreak_after_opening_tag' => true,
'declare_strict_types' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'native_constant_invocation' => true,
'native_function_casing' => true,
'native_function_invocation' => ['include' => ['@internal']],
'no_php4_constructor' => true,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'remove_inheritdoc' => true],
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_strict' => true,
'phpdoc_order' => true,
'semicolon_after_instruction' => true,
'strict_comparison' => true,
'strict_param' => true,
'array_indentation' => true,
'multiline_whitespace_before_semicolons' => true,
'single_line_throw' => false,
'visibility_required' => ['elements' => ['property', 'method', 'const']],
])
->setFinder($finder);
74 changes: 73 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
# messenger
<h1 align="center">Sulu Messenger</h1>

<p align="center">
<a href="https://sulu.io/" target="_blank">
<img width="30%" src="https://sulu.io/uploads/media/800x/00/230-Official%20Bundle%20Seal.svg?v=2-6&inline=1" alt="Official Sulu Bundle Badge">
</a>
</p>

<p align="center">
<a href="LICENSE" target="_blank">
<img src="https://img.shields.io/github/license/sulu/messenger.svg" alt="GitHub license">
</a>
<a href="https://github.com/sulu/messenger/releases" target="_blank">
<img src="https://img.shields.io/github/tag/sulu/messenger.svg" alt="GitHub tag (latest SemVer)">
</a>
<a href="https://github.com/sulu/messenger/actions" target="_blank">
<img src="https://img.shields.io/github/workflow/status/sulu/messenger/Test%20application.svg?label=test-workflow" alt="Test workflow status">
</a>
<a href="https://github.com/sulu/sulu/releases" target="_blank">
<img src="https://img.shields.io/badge/sulu%20compatibility-%3E=2.0-52b6ca.svg" alt="Sulu compatibility">
</a>
</p>
<br/>

This library provides the stamps and middlewares which configures the sulu message bus.
It can be used independently in any symfony installation.

## Installation

Make sure Composer is installed globally, as explained in the
[installation chapter](https://getcomposer.org/doc/00-intro.md)
of the Composer documentation.

Open a command console, enter your project directory and execute:

```bash
composer require sulu/messenger
```

Then, enable the bundle by adding it to the list of registered bundles
in the `config/bundles.php` file of your project:

```php
// config/bundles.php

return [
// ...
Sulu\Messenger\Infrastructure\Symfony\HttpKernel\SuluMessengerBundle::class => ['all' => true],
];
```

## Middlewares

### UnpackExceptionMiddleware

The `UnpackExceptionMiddleware` will unpack the `HandlerFailedException` which
is created by the Symfony [`HandleMessageMiddleware`](https://github.com/symfony/symfony/blob/c7dbcc954366f92f66360f3960a10dc1ef5f2584/src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php#L129).
This way we make sure that the real exception is thrown out by this message
bus, and a controller can catch or convert it to a specific http status code.
This middleware is always activated in the sulu message bus.

### DoctrineFlushMiddleware

The `DoctrineFlushMiddleware` is a Middleware which let us flush the Doctrine
EntityManager by an opt-in flag via the `EnableFlushStamp`. It can be used this way:

```php
use Sulu\Messenger\Infrastructure\Symfony\Messenger\FlushMiddleware\EnableFlushStamp;

$this->handle(new Envelope(new YourMessage(), [new EnableFlushStamp()]));
```

This middleware is always activated in the sulu message bus.
105 changes: 105 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"name": "sulu/messenger",
"description": "This library provides the stamps, middlewares and the sulu message bus.",
"license": "proprietary",
"type": "sulu-bundle",
"require": {
"php": "8.0.* || 8.1.*",
"doctrine/dbal": "^3.3",
"doctrine/doctrine-bundle": "^2.5",
"doctrine/orm": "^2.11",
"symfony/config": "^5.4 || ^6.0",
"symfony/dependency-injection": "^5.4 || ^6.0",
"symfony/doctrine-bridge": "^5.4 || ^6.0",
"symfony/framework-bundle": "^5.4 || ^6.0",
"symfony/http-kernel": "^5.4 || ^6.0",
"symfony/messenger": "^5.4 || ^6.0"
},
"require-dev": {
"coduo/php-matcher": "^6.0",
"friendsofphp/php-cs-fixer": "^3.6",
"handcraftedinthealps/code-coverage-checker": "^0.2.5",
"jangregor/phpstan-prophecy": "^1.0",
"nunomaduro/collision": "^5.0 || ^6.0",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^1.4",
"phpstan/phpstan-doctrine": "^1.2",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-symfony": "^1.1",
"phpstan/phpstan-webmozart-assert": "^1.0",
"phpunit/phpunit": "^9.5",
"qossmic/deptrac-shim": "^0.19.3",
"rector/rector": "^0.12.16",
"schranz/test-generator": "^0.3",
"symfony/browser-kit": "^5.4 || ^6.0",
"symfony/css-selector": "^5.4 || ^6.0",
"symfony/debug-bundle": "^5.4 || ^6.0",
"symfony/error-handler": "^5.4 || ^6.0",
"symfony/phpunit-bridge": "^5.4 || ^6.0",
"thecodingmachine/phpstan-strict-rules": "^1.0",
"symfony/dotenv": "^5.4 || ^6.0",
"symfony/yaml": "^5.4 || ^6.0"
},
"autoload": {
"psr-4": {
"Sulu\\Messenger\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Sulu\\Messenger\\Tests\\": "tests/"
}
},
"scripts": {
"bootstrap-test-environment": [
"@php tests/Application/bin/console cache:clear --env test",
"@php tests/Application/bin/console doctrine:database:drop --force --if-exists --env test",
"@php tests/Application/bin/console doctrine:database:create --env test",
"@php tests/Application/bin/console doctrine:schema:create --env test"
],
"test": "@phpunit",
"test-with-coverage": "@phpunit --coverage-php Tests/Application/var/reports/coverage.php --coverage-cobertura=Tests/Application/var/cobertura-coverage.xml --coverage-html Tests/Application/var/reports/html --log-junit Tests/Application/var/reports/junit.xml",
"check-coverage": [
"@php vendor/bin/code-coverage-checker \"Tests/Application/var/reports/coverage.php\" \"line\" \"100.00\" \"src\""
],
"lint": [
"@php-cs",
"@phpstan",
"@lint-rector",
"@lint-twig",
"@lint-yaml",
"@lint-container",
"@lint-composer",
"@lint-doctrine",
"@lint-deptrac"
],
"phpunit": "@php vendor/bin/phpunit",
"phpstan": [
"@php tests/Application/bin/console cache:warmup --env=dev",
"@php vendor/bin/phpstan analyze"
],
"lint-rector": [
"@php tests/Application/bin/console cache:warmup --env=dev",
"@php vendor/bin/rector process --dry-run"
],
"rector": [
"@php vendor/bin/rector process"
],
"php-cs": "@php vendor/bin/php-cs-fixer fix --verbose --diff --dry-run",
"php-cs-fix": "@php vendor/bin/php-cs-fixer fix",
"lint-composer": "@composer validate --no-check-publish --strict",
"lint-yaml": "@php tests/Application/bin/console lint:yaml tests/Application/config --parse-tags",
"lint-deptrac": "@php vendor/bin/deptrac",
"lint-container": [
"@php tests/Application/bin/console lint:container --env dev",
"@php tests/Application/bin/console lint:container --env test",
"@php tests/Application/bin/console lint:container --env prod"
],
"lint-doctrine": [
"@php tests/Application/bin/console doctrine:schema:validate --skip-sync --env prod"
]
},
"config": {
"sort-packages": true
}
}
19 changes: 19 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Sulu\Messenger\Infrastructure\Symfony\Messenger\FlushMiddleware\DoctrineFlushMiddleware;
use Sulu\Messenger\Infrastructure\Symfony\Messenger\UnpackExceptionMiddleware\UnpackExceptionMiddleware;

return function (ContainerConfigurator $configurator) {
$services = $configurator->services();

$services->set('sulu_messenger.doctrine_flush_middleware')
->class(DoctrineFlushMiddleware::class)
->args([service('doctrine.orm.entity_manager')]);

$services->set('sulu_messenger.unpack_exception_middleware')
->class(UnpackExceptionMiddleware::class);
};
Loading

0 comments on commit 73da6f6

Please sign in to comment.