Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
on:
- push

name: Run PHPStan checks

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

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

strategy:
matrix:
os:
- ubuntu-latest

php:
- "8.1"
- "8.2"
- "8.3"

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

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
coverage: pcov
ini-values: assert.exception=1, zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On
tools: composer:v2, cs2pr

- 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@v4
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-

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

- name: Run static analysis with PHPStan
run: vendor/bin/phpstan analyse
124 changes: 73 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# dot-flashmessenger

Flash messenger library for session messages between redirects.
A flash message, or session message is a piece of text data that survives one requests(available only in the next request).
This library accepts session data as well, not just string messages, with the same behaviour.
The flash messenger is a convenient way to add data to the session and get it back on the next request without bothering with setting and clearing the data manually.

## Documentation

Documentation is available at: https://docs.dotkernel.org/dot-flashmessenger/.

## Badges

![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-flashmessenger)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-flashmessenger/3.4.2)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-flashmessenger/3.6.0)

[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/issues)
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/network)
Expand All @@ -10,29 +21,28 @@

[![Build Static](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/continuous-integration.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/continuous-integration.yml)
[![codecov](https://codecov.io/gh/dotkernel/dot-flashmessenger/graph/badge.svg?token=B4WAT3RYKJ)](https://codecov.io/gh/dotkernel/dot-flashmessenger)

[![SymfonyInsight](https://insight.symfony.com/projects/94ace687-5124-446f-a324-0ecca1b47f88/big.svg)](https://insight.symfony.com/projects/94ace687-5124-446f-a324-0ecca1b47f88)

Flash messenger library for session messages between redirects. A flash message, or session message is a piece of text data that survives one requests(available only in the next request).
This library accepts session data as well, not just string messages, with the same behaviour.
The flash messenger is a convenient way to add data to the session and get it back on the next request without bothering with setting and clearing the data manually.
[![PHPStan](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/static-analysis.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/static-analysis.yml)

## Installation

Run the following command in your project folder

composer require dotkernel/dot-flashmessenger
```shell
composer require dotkernel/dot-flashmessenger
```

This will also install `laminas/laminas-session` as session handling is based on this library.
Next, merge the `ConfigProvider` to your application's configuration

## Configuration

return [
'dot_flashmessenger' => [
'namespace' => 'flash messages session namespace name'
],
];
```php
return [
'dot_flashmessenger' => [
'namespace' => 'flash messages session namespace name'
],
];
```

Sets the session namespace to use for all flash messages and data

Expand All @@ -43,56 +53,64 @@ Just inject this service in you classes, wherever you need flash messages.

### Getting the service in a factory

$container->get(FlashMessengerInterface::class);
```php
$container->get(FlashMessengerInterface::class);
```

### Using the flash messenger service

To add and retrieve text messages

$this->flashMessenger->addMessage('error', 'This is a error flash message');

//on the next request you can get all messages from a namespace, or all messages from all namespaces if namespace is omitted
$this->flashMessenger->getMessages('error');
```php
$this->flashMessenger->addMessage('error', 'This is a error flash message');
//on the next request you can get all messages from a namespace, or all messages from all namespaces if namespace is omitted
$this->flashMessenger->getMessages('error');
```

Adding general data, not just messages, has a different method for that, accepting data as key/value pairs

$this->flashMessenger->addData('myData', $someData);

// next request
$this->flashMessenger->getData('myData');
```php
$this->flashMessenger->addData('myData', $someData);
// next request
$this->flashMessenger->getData('myData');
```

There are also some predefined namespaces, along with shortcuts to add a message in the predefined namespaces

FlashMessengerInterface::ERROR_NAMESPACE
FlashMessengerInterface::WARNING_NAMESPACE
FlashMessengerInterface::INFO_NAMESPACE
FlashMessengerInterface::SUCCESS_NAMESPACE
```php
FlashMessengerInterface::ERROR_NAMESPACE
FlashMessengerInterface::WARNING_NAMESPACE
FlashMessengerInterface::INFO_NAMESPACE
FlashMessengerInterface::SUCCESS_NAMESPACE
```

using the methods:

/**
* @param string $error
* @return void
*/
public function addError($error);

/**
* @param string $info
* @return void
*/
public function addInfo($info);

/**
* @param string $warning
* @return void
*/
public function addWarning($warning);

/**
* @param string $success
* @return void
*/
public function addSuccess($success);
```php
/**
* @param string $error
* @return void
*/
public function addError($error);

/**
* @param string $info
* @return void
*/
public function addInfo($info);

/**
* @param string $warning
* @return void
*/
public function addWarning($warning);

/**
* @param string $success
* @return void
*/
public function addSuccess($success);
```

## FlashMessengerRenderer

Expand All @@ -102,10 +120,14 @@ There are also a twig extension provided in [dot-twigrenderer](https://github.co

## Registered services

Dot\FlashMessenger\FlashMessengerInterface::class
```php
Dot\FlashMessenger\FlashMessengerInterface::class
```

The flash messenger service

Dot\FlashMessenger\View\RendererInterface::class
```php
Dot\FlashMessenger\View\RendererInterface::class
```

The registered renderer class
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "dotkernel/dot-flashmessenger",
"type": "library",
"description": "DotKernel flash messenger component for session messages between redirects",
"description": "Dotkernel flash messenger component for session messages between redirects",
"license": "MIT",
"homepage": "https://github.com/dotkernel/dot-flashmessenger",
"authors": [
{
"name": "DotKernel Team",
"name": "Dotkernel Team",
"email": "team@dotkernel.com"
}
],
Expand All @@ -29,9 +29,10 @@
"mezzio/mezzio-template": "^2.9.0"
},
"require-dev": {
"laminas/laminas-coding-standard": "^2.5.0",
"phpunit/phpunit": "^10.4.2",
"vimeo/psalm": "^5.16.0"
"laminas/laminas-coding-standard": "^3.0.0",
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^10.4.2"
},
"autoload": {
"psr-4": {
Expand All @@ -46,12 +47,12 @@
"scripts": {
"check": [
"@cs-check",
"@test"
"@test",
"@static-analysis"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"static-analysis": "psalm --shepherd --stats"
"static-analysis": "phpstan analyse --memory-limit 1G"
}
}
1 change: 0 additions & 1 deletion docs/book/index.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/book/index.md
4 changes: 3 additions & 1 deletion docs/book/v3/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Run the following command in your project folder

composer require dotkernel/dot-flashmessenger
``` shell
composer require dotkernel/dot-flashmessenger
```

This will also install `laminas/laminas-session` as session handling is based on this library.
14 changes: 14 additions & 0 deletions docs/book/v3/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ A flash message, or session message is a piece of text data that survives one re
This library accepts session data as well, not just string messages, with the same behaviour.

The flash messenger is a convenient way to add data to the session and get it back on the next request without bothering with setting and clearing the data manually.

## Badges

![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-flashmessenger)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-flashmessenger/3.6.0)

[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/issues)
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/network)
[![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/stargazers)
[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-flashmessenger)](https://github.com/dotkernel/dot-flashmessenger/blob/3.0/LICENSE.md)

[![Build Static](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/continuous-integration.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/continuous-integration.yml)
[![codecov](https://codecov.io/gh/dotkernel/dot-flashmessenger/graph/badge.svg?token=B4WAT3RYKJ)](https://codecov.io/gh/dotkernel/dot-flashmessenger)
[![PHPStan](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/static-analysis.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-flashmessenger/actions/workflows/static-analysis.yml)
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ nav:
- Usage: v3/usage.md
- Renderer: v3/renderer.md
site_name: dot-flashmessenger
site_description: "DotKernel's one time session message"
site_description: "Dotkernel's one time session message"
repo_url: "https://github.com/dotkernel/dot-flashmessenger"
plugins:
- search
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
parameters:
level: 5
paths:
- src
- test
treatPhpDocTypesAsCertain: false
17 changes: 0 additions & 17 deletions psalm.xml

This file was deleted.

2 changes: 2 additions & 0 deletions src/FlashMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function addData(
): void {
$container = $this->getSessionContainer();
if (! isset($container->data)) {
/** @phpstan-ignore-next-line */
$container->data = [];
}

Expand Down Expand Up @@ -127,6 +128,7 @@ public function addMessage(
): void {
$container = $this->getSessionContainer();
if (! isset($container->messages)) {
/** @phpstan-ignore-next-line */
$container->messages = [];
}

Expand Down
8 changes: 4 additions & 4 deletions test/Factory/FlashMessengerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testCreateFlashMessengerWithFoundSessionManager(): void

$result = (new FlashMessengerFactory())($this->container, FlashMessenger::class);

$this->assertInstanceOf(FlashMessenger::class, $result);
$this->assertSame(FlashMessenger::class, $result::class);
$this->assertSame('dot_messenger', $result->getNamespace());
$this->assertSame($managerInterface, $result->getSessionManager());
}
Expand All @@ -84,7 +84,7 @@ public function testCreateFlashMessengerWithSessionManagerClass(): void

$result = (new FlashMessengerFactory())($this->container, FlashMessenger::class);

$this->assertInstanceOf(FlashMessenger::class, $result);
$this->assertSame(FlashMessenger::class, $result::class);
$this->assertSame('dot_messenger', $result->getNamespace());
$this->assertInstanceOf(
$this->config['dot_flashmessenger']['options']['session_manager'],
Expand Down Expand Up @@ -117,9 +117,9 @@ public function testCreateFlashMessengerWithDefaultSessionManager(): void

$result = (new FlashMessengerFactory())($this->container, FlashMessenger::class);

$this->assertInstanceOf(FlashMessenger::class, $result);
$this->assertSame(FlashMessenger::class, $result::class);
$this->assertSame('test_namespace', $result->getNamespace());
$this->assertInstanceOf(ManagerInterface::class, $result->getSessionManager());
$this->assertContainsOnlyInstancesOf(ManagerInterface::class, [$result->getSessionManager()]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/Factory/FlashMessengerOptionsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function testCreateFlashMessengerOptionsWithDefaultConfig(): void
->willReturn($this->config);

$result = (new FlashMessengerOptionsFactory())($container);
$this->assertInstanceOf(FlashMessengerOptions::class, $result);
$this->assertSame(FlashMessengerOptions::class, $result::class);
}
}
Loading