Skip to content

Commit

Permalink
v2.0.0 (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
designcise authored May 11, 2020
2 parents 3c76436 + c5bf8ce commit 4a2b8df
Show file tree
Hide file tree
Showing 32 changed files with 1,841 additions and 638 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# Auto detect text files and perform LF normalization
* text=auto

/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml export-ignore
/phpstan.neon export-ignore
/test export-ignore
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor/
/.idea
.DS_Store
*.cache
composer.lock
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cache:
apt: true
directories:
- $HOME/.composer/cache/files

language: php

php:
- 7.4.0

before_script:
- travis_retry composer install --no-interaction --no-suggest --prefer-source

script:
- vendor/bin/phpunit --configuration phpunit.xml --coverage-clover=coverage.xml

after_success:
- bash <(curl -s https://codecov.io/bash)
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Portions of the "BitFrame Whoops Middleware" incorporates the work by (as provid

All other copyright for the "BitFrame Whoops Middleware" are held by:

* Copyright (c) 2017-2018 Daniyal Hamid (https://designcise.com)
* Copyright (c) 2017-2020 Daniyal Hamid (https://designcise.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
117 changes: 96 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,121 @@
# BitFrame\ErrorHandler\Whoops
# BitFrame\Whoops

[![codecov](https://codecov.io/gh/designcise/bitframe-whoops/branch/2.x/graph/badge.svg)](https://codecov.io/gh/designcise/bitframe-whoops)
[![Build Status](https://travis-ci.org/designcise/bitframe-whoops.svg?branch=2.x)](https://travis-ci.org/designcise/bitframe-whoops)

Whoops error handler middleware to handle application or middleware specific errors.

### Installation
## Installation

```
$ composer require "designcise/bitframe-whoops"
```

Please note that this package requires PHP 7.4.0 or newer.

## Quickstart

### Instantiating

The constructor has the following signature:

```php
new ErrorHandler(
\Psr\Http\Message\ResponseFactoryInterface,
\BitFrame\Whoops\Provider\HandlerProviderNegotiator::class
[options]
);
```

1. The first argument to the constructor must be an instance of `Psr\Http\Message\ResponseFactoryInterface`;
1. The second argument to the constructor must be the name of the handler provider class (which extends `\BitFrame\Whoops\Provider\AbstractProvider`)
1. The third argument to the constructor is an optional array of options to specify the following:
1. `catchGlobalErrors`: When set to `true` errors will be handled outside of current batch of middleware set.
1. Other options are simply method names in `Whoops\Handler\*Handler.php` and `BitFrame\Whoops\Handler\*Handler.php`. For example, to set `Whoops\Handler\JsonResponseHandler::setJsonApi()` you would pass in: `['setJsonApi' => false]`, etc.

As a shortcut, you can also use the static method `ErrorHandler::fromNegotiator($factory, $options)`. This would use the `\BitFrame\Whoops\Provider\HandlerProviderNegotiator` by default.

See [installation docs](https://www.bitframephp.com/middleware/error-handler/whoops) for instructions on installing and using this middleware.
### How to Run the Middleware

### Usage Example
To run the middleware, simply pass in a `BitFrame\Whoops\ErrorHandler` instance to your middleware runner / dispatcher.

For example, to handle middleware-specific errors with `BitFrame\App` (or other PSR-15 dispatchers) it would look something like this:

```php
use BitFrame\App;
use BitFrame\Emitter\SapiEmitter;
use BitFrame\Whoops\ErrorHandler;
use \BitFrame\Whoops\Provider\HandlerProviderNegotiator;
use BitFrame\Factory\HttpFactory;

$app = new App();

$middleware = function () {
throw new \Exception('hello world!');
};

$app->use([
SapiEmitter::class,
new ErrorHandler(HttpFactory::getFactory(), HandlerProviderNegotiator::class, [
'addTraceToOutput' => true,
'setJsonApi' => false,
]),
$middleware,
]);

$app->run();
```
use \BitFrame\ErrorHandler\WhoopsErrorHandler;

require 'vendor/autoload.php';
To handle global errors with `BitFrame\App` (or other PSR-15 dispatchers) it would look something like this:

$app = new \BitFrame\Application;
```php
use BitFrame\App;
use BitFrame\Whoops\ErrorHandler;
use BitFrame\Factory\HttpFactory;

$format = 'auto';
$handler = new WhoopsErrorHandler($format);
$app = new App();

$app->run([
/* In order to output response from the whoops error handler,
* make sure you include a response emitter middleware, for example:
* \BitFrame\Message\DiactorosResponseEmitter::class, */
$handler
ErrorHandler::fromNegotiator(HttpFactory::getFactory(), [
'catchGlobalErrors' => true,
'addTraceToOutput' => true,
'setJsonApi' => false,
]),
]);

throw new \Exception('hello world!');
```

### Tests
### How Does It Work?

The error handler middleware automatically determines the error handler to use based on the `Accept` header. The following error handler provders are included:

1. `BitFrame\Whoops\Provider\HtmlHandlerProvider` for `Whoops\Handler\PrettyPageHandler`;
1. `BitFrame\Whoops\Provider\JsonHandlerProvider` for `Whoops\Handler\JsonResponseHandler`;
1. `BitFrame\Whoops\Provider\JsonpHandlerProvider` for `BitFrame\Whoops\Handler\JsonpResponseHandler`;
1. `BitFrame\Whoops\Provider\TextHandlerProvider` for `Whoops\Handler\PlainTextHandler`;
1. `BitFrame\Whoops\Provider\XmlHandlerProvider` for `Whoops\Handler\XmlResponseHandler`;

To execute the test suite, you will need [PHPUnit](https://phpunit.de/).
## Tests

### Contributing
To run the tests you can use the following commands:

| Command | Type |
| ---------------- |:---------------:|
| `composer test` | PHPUnit tests |
| `composer style` | CodeSniffer |
| `composer md` | MessDetector |
| `composer check` | PHPStan |

## Contributing

* File issues at https://github.com/designcise/bitframe-whoops/issues
* Issue patches to https://github.com/designcise/bitframe-whoops/pulls

### Documentation

Documentation is available at:
## Documentation

* https://www.bitframephp.com/middleware/error-handler/whoops/
Complete documentation for v2.0 will be available soon.

### License
## License

Please see [License File](LICENSE.md) for licensing information.
30 changes: 19 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "designcise/bitframe-whoops",
"version": "1.0.1",
"version": "2.0.0",
"type": "library",
"description": "Whoops error handler middleware for BitFrame microframework",
"description": "Whoops error handler middleware",
"license": "MIT",
"authors": [
{
Expand All @@ -11,25 +11,33 @@
}
],
"require": {
"php": ">=7.1.0",
"designcise/bitframe": "^1.0.0",
"filp/whoops": "~2.1",
"php": ">=7.4.0",
"filp/whoops": "~2.7",
"psr/http-factory": "~1.0",
"psr/http-server-handler": "~1.0",
"psr/http-server-middleware": "~1.0",
"symfony/var-dumper": "^2.6 || ^3.0",
"zendframework/zend-diactoros": "^1.1.0"
"psr/http-server-middleware": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "^6.4"
"phpunit/phpunit": "^8.5",
"phpspec/prophecy": "~1.0",
"squizlabs/php_codesniffer": "3.*",
"phpmd/phpmd": "@stable",
"phpstan/phpstan": "*"
},
"scripts": {
"style": "vendor/bin/phpcs --standard=PSR12 src",
"check": "vendor/bin/phpstan analyse src --level=5 -c phpstan.neon",
"md": "vendor/bin/phpmd src text cleancode,unusedcode,codesize,design,naming",
"test": "vendor/bin/phpunit --configuration phpunit.xml --testsuite bitframe_whoops"
},
"autoload": {
"psr-4": {
"BitFrame\\ErrorHandler\\": "src/"
"BitFrame\\Whoops\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"BitFrame\\Test\\": "test/"
"BitFrame\\Whoops\\Test\\": "test/"
}
}
}
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
treatPhpDocTypesAsCertain: false
reportStaticMethodSignatures: false
13 changes: 7 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
syntaxCheck="false"
>
>
<testsuites>
<testsuite name="BitFrame Tests">
<directory>./test/</directory>
<testsuite name="bitframe_whoops">
<directory suffix="Test.php">./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
<exclude>
<file>./src/Provider/AbstractProvider.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit 4a2b8df

Please sign in to comment.