Skip to content

Commit

Permalink
Merge pull request #5 from zfegg/develop
Browse files Browse the repository at this point in the history
Refactor v2.
  • Loading branch information
Moln committed Nov 26, 2021
2 parents 3a43465 + 90a5369 commit 4eae21c
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 111 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: qa
on:
push:
branches:
- master
pull_request:
jobs:
qa:
name: QA (PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }})
runs-on: ${{ matrix.operating-system }}
strategy:
fail-fast: false
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.4', '8.0', '8.1']
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
with:
php-version: ${{ matrix.php-versions }}
coverage: xdebug #optional
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
# Use composer.json for key, if composer.lock is not committed.
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: CS Check
if: ${{ matrix.php-versions == '8.0' }}
run: ./vendor/bin/phpcs

- name: Run Tests
run: ./vendor/bin/phpunit --coverage-text

- name: Upload coverage to coveralls.io
if: ${{ matrix.php-versions == '8.0' }}
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer require php-coveralls/php-coveralls --dev -q
./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
./vendor/bin/php-coveralls -v
29 changes: 0 additions & 29 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016 Zend Framework Egg
Copyright (c) 2021 ZFEgg

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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Logging for errors in Zend Expressive
Logging for errors in Mezzio
=====================================

[![Build Status](https://travis-ci.org/zfegg/stratigility-logging-error.png)](https://travis-ci.org/zfegg/stratigility-logging-error)
[![GitHub Actions: Run tests](https://github.com/zfegg/stratigility-logging-error/workflows/qa/badge.svg)](https://github.com/zfegg/stratigility-logging-error/actions?query=workflow%3A%22qa%22)
[![Coverage Status](https://coveralls.io/repos/github/zfegg/stratigility-logging-error/badge.svg?branch=master)](https://coveralls.io/github/zfegg/stratigility-logging-error?branch=master)
[![Latest Stable Version](https://poser.pugx.org/zfegg/stratigility-logging-error/v/stable.png)](https://packagist.org/packages/zfegg/stratigility-logging-error)

Expand Down
23 changes: 16 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
{
"name": "zfegg/stratigility-logging-error",
"description": "Logging for errors in Zend Expressive",
"description": "Logging for errors in Mezzio",
"license": "MIT",
"keywords": [
"log",
"error",
"mezzio",
"laminas",
"expressive",
"zend"
],
"require": {
"psr/container": "^1.0",
"psr/log": "^1.0",
"php": ">=7.4",
"psr/container": "^1.0 || ^2.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"psr/http-message": "^1.0",
"laminas/laminas-stratigility": "^2.1 || ^3.0.0rc1"
"laminas/laminas-stratigility": "^2.1 || ^3.0.0"
},
"require-dev": {
"monolog/monolog": "^1.23",
"laminas/laminas-diactoros": "^1.7",
"laminas/laminas-coding-standard": "^1.0",
"phpunit/phpunit": "^6.0"
"laminas/laminas-diactoros": "^2.0",
"slevomat/coding-standard": "^7.0.12",
"phpunit/phpunit": "^9.0"
},
"authors": [
{
"name": "Moln",
"email": "moln.xie@gmail.com"
}
],
"scripts": {
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
},
"autoload": {
"psr-4": {
"Zfegg\\Stratigility\\LoggingError\\": "src/"
Expand Down
57 changes: 53 additions & 4 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,57 @@
<?xml version="1.0"?>
<ruleset name="Zend Framework Coding Standard">
<rule ref="./vendor/laminas/laminas-coding-standard/ruleset.xml"/>

<ruleset name="Coding Standard">
<!-- Paths to check -->
<file>src</file>
<file>test</file>
</ruleset>

<!-- display progress -->
<arg value="p"/>
<arg name="no-colors"/>

<!-- inherit rules from: -->
<rule ref="PSR2"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Generic.Formatting.SpaceAfterNot"/>
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
<properties>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<property name="ignoreBlankLines" value="false"/>
</properties>
</rule>
<rule ref="Squiz.NamingConventions.ValidVariableName">
<exclude name="Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore"/>
</rule>

<rule ref="Squiz.Strings.ConcatenationSpacing" >
<properties>
<property name="spacing" value="1" />
<property name="ignoreNewlines" value="true" />
</properties>
</rule>
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing" />

<config name="installed_paths" value="vendor/slevomat/coding-standard/SlevomatCodingStandard"/>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes" />
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing" />
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint" >
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification" />
<properties>
<property name="enableUnionTypeHint" value="false" />
<property name="enableMixedTypeHint" value="false" />
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.UselessConstantTypeHint" />
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification" />
<properties>
<property name="enableUnionTypeHint" value="false" />
<property name="enableMixedTypeHint" value="false" />
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses" />

</ruleset>
23 changes: 12 additions & 11 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Coding Tests">
<directory>./test</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Tests">
<directory>./test</directory>
</testsuite>
</testsuites>
</phpunit>
8 changes: 6 additions & 2 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types = 1);

namespace Zfegg\Stratigility\LoggingError;

Expand All @@ -8,13 +9,16 @@
class ConfigProvider
{

public function __invoke()
public function __invoke(): array
{
return [
'dependencies' => [
'factories' => [
LoggingErrorListener::class => LoggingErrorListenerFactory::class,
],
'delegators' => [
ErrorHandler::class => [
LoggingErrorListenerFactory::class,
LoggingErrorDelegator::class,
],
],
],
Expand Down
23 changes: 23 additions & 0 deletions src/LoggingErrorDelegator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types = 1);

namespace Zfegg\Stratigility\LoggingError;

use Laminas\Stratigility\Middleware\ErrorHandler;
use Psr\Container\ContainerInterface;

class LoggingErrorDelegator
{
public function __invoke(
ContainerInterface $container,
string $serviceName,
callable $callback
): ErrorHandler {
$listener = $container->get(LoggingErrorListener::class);

$errorHandler = $callback();
$errorHandler->attachListener($listener);
return $errorHandler;
}
}
25 changes: 9 additions & 16 deletions src/LoggingErrorListener.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
<?php

declare(strict_types = 1);

namespace Zfegg\Stratigility\LoggingError;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;

class LoggingErrorListener
final class LoggingErrorListener
{

/**
* Log message string with placeholders
*/
private $message = '%s "%s %s": <<<%s<<<';

private $logger;
private string $message;

/**
* Set log format
*
* @param string $message
*/
public function setMessage($message)
{
$this->message = $message;
}
private LoggerInterface $logger;

public function __construct(LoggerInterface $logger)
public function __construct(LoggerInterface $logger, string $message = '%s "%s %s": <<<%s<<<')
{
$this->logger = $logger;
$this->message = $message;
}

public function __invoke(
$error,
\Throwable $error,
ServerRequestInterface $request,
ResponseInterface $response
) {
): void {
$this->logger->error(
sprintf(
$this->message,
Expand Down
23 changes: 9 additions & 14 deletions src/LoggingErrorListenerFactory.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
<?php

declare(strict_types = 1);

namespace Zfegg\Stratigility\LoggingError;

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Laminas\Stratigility\Middleware\ErrorHandler;

class LoggingErrorListenerFactory
{
public function __invoke(
ContainerInterface $container,
$serviceName,
callable $callback
) : ErrorHandler {
$logger = $container->has(LoggerInterface::class) ?
$container->get(LoggerInterface::class) :
new NullLogger();
public function __invoke(ContainerInterface $container): LoggingErrorListener
{
$config = ($container->has('config') ? $container->get('config') : [])[LoggingErrorListener::class] ?? [];
$logger = isset($config['logger'])
? $container->get($config['logger'])
: ($container->has(LoggerInterface::class) ? $container->get(LoggerInterface::class) : new NullLogger());
$message = $config['message'] ?? '%s "%s %s": <<<%s<<<';

$listener = new LoggingErrorListener($logger);

$errorHandler = $callback();
$errorHandler->attachListener($listener);
return $errorHandler;
return new LoggingErrorListener($logger, $message);
}
}
4 changes: 3 additions & 1 deletion test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

namespace ZfeggTest\Stratigility\LoggingError;

use PHPUnit\Framework\TestCase;
Expand All @@ -8,7 +10,7 @@
class ConfigProviderTest extends TestCase
{

public function testInvoke()
public function testInvoke(): void
{
$config = new ConfigProvider();
$this->assertArrayHasKey('dependencies', $config());
Expand Down

0 comments on commit 4eae21c

Please sign in to comment.