Skip to content

Commit

Permalink
init Laminas-whoops
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickael TONNELIER committed Jan 14, 2020
0 parents commit 3276d73
Show file tree
Hide file tree
Showing 20 changed files with 928 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### JetBrains
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
*.iml

## Directory-based project format:
.idea/

### Composer
vendor/
composer.lock
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#MIT License

Copyright (c) 2020 Tonnelier Mickael

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:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Laminas-Whoops, integrated [whoops](https://github.com/filp/whoops) in Laminas

-----

![Whoops!](http://i.imgur.com/0VQpe96.png)

**whoops** is an error handler base/framework for PHP. Out-of-the-box, it provides a pretty
error interface that helps you debug your web projects, but at heart it's a simple yet
powerful stacked error handling system.

# Table of Contents

* [Module installation](#module-installation)
* [Features](#features)
* [Render View Manager - Twig Support](#render-view-manager---twig-support)
* [Module Visibility Manager](#module-visibility-manager)
* [License](#license)



# Module installation
1. `cd my/project/directory`
2. create a `composer.json` file with following contents:

```json
{
"require": {
"ppito/laminas-whoops": "^2.0"
}
}
```
3. install composer via `curl -s http://getcomposer.org/installer | php` (on windows, download
http://getcomposer.org/installer and execute it with PHP)
4. run `php composer.phar install`
5. open `my/project/directory/configs/modules.config.php` and add the following key :

```php
'WhoopsErrorHandler', // must be added as the first module
```
6. optional : copy `config/module.config.php` in `my/project/directory/config/autoload/laminas-whoops.local.php`
7. optional : edit `my/project/directory/config/autoload/laminas-whoops.local.php`

# Features

### Render View Manager - Twig Support

By default this module use the simple php render, but you can now specify your favorite render.

##### Usage :
`Twig render` has been supported, you just need to change the `template_render` configuration:
```php
'template_render' => 'laminas_whoops/twig_error',
```

### Module Visibility Manager

It is now possible to manage the module loading by implement your own rules.
For example, the module can be loaded only for the admin users or only for dev&preprod environments.

##### Usage :
* Create your own class by implement the interface [VisibilityServiceInterface](src/Service/VisibilityServiceInterface.php) (or the abstract [VisibilityServiceAbstract](src/Service/VisibilityServiceAbstract.php))
* @see example [VisibilityService.visibility-example.php](src/Service/VisibilityService.visibility-example.php).
* Change the `visibility_service_name` configuration to specify the name of your class.
* @see example [module.config.visibility-example.php](config/module.config.visibility-example.php).
```php
'visibility_service_name' => Application\Service\VisibilityService::class,
```


# License

**ppito/laminas-whoops** is licensed under the MIT License - See the [LICENSE](LICENSE.md) file for details.

31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "ppito/laminas-whoops",
"description": "Laminas-Whoops, integrated whoops in Laminas Framework",
"type": "module",
"license": "MIT",
"homepage": "https://github.com/ppito/laminas-whoops",
"keywords": [
"Laminas",
"Whoops",
"PHP Error"
],
"authors": [
{
"name": "Mickael TONNELIER",
"role": "Developer"
}
],
"require": {
"php": "^7.2",
"laminas/laminas-eventmanager": "^3.2",
"laminas/laminas-servicemanager": "^3.4",
"laminas/laminas-mvc": "^3.1",
"laminas/laminas-console": "^2.8",
"filp/whoops": "^2.7"
},
"autoload": {
"psr-4": {
"WhoopsErrorHandler\\": "src/"
}
}
}
31 changes: 31 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace WhoopsErrorHandler;

return [
'whoops' => [
'editor' => 'phpstorm',
'show_trace' => [
'ajax_display' => true,
'cli_display' => true,
],
'template_render' => 'laminas_whoops/simple_error',
'visibility_service_name' => null,
],

'service_manager' => [
'factories' => [
Service\WhoopsService::class => Factory\Factory::class,
Handler\PageHandler::class => Factory\Factory::class,
Handler\ConsoleHandler::class => Factory\Factory::class,
Handler\AjaxHandler::class => Factory\Factory::class,
],
],

'view_manager' => [
'template_map' => [
'laminas_whoops/simple_error' => __DIR__ . '/../view/render.phtml',
'laminas_whoops/twig_error' => __DIR__ . '/../view/twig/render.html.twig',
],
],
];
36 changes: 36 additions & 0 deletions config/module.config.visibility-example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace WhoopsErrorHandler;

use Application\Service\VisibilityService;

return [
'whoops' => [
'editor' => 'phpstorm',
'show_trace' => [
'ajax_display' => true,
'cli_display' => true,
],
'template_render' => 'laminas_whoops/simple_error',
// Specify the class name
'visibility_service_name' => VisibilityService::class,
],

'service_manager' => [
'factories' => [
Service\WhoopsService::class => Factory\Factory::class,
Handler\PageHandler::class => Factory\Factory::class,
Handler\ConsoleHandler::class => Factory\Factory::class,
Handler\AjaxHandler::class => Factory\Factory::class,
// register visibility class
VisibilityService::class => Factory\Factory::class,
],
],

'view_manager' => [
'template_map' => [
'laminas_whoops/simple_error' => __DIR__ . '/../view/render.phtml',
'laminas_whoops/twig_error' => __DIR__ . '/../view/twig/render.html.twig',
],
],
];
35 changes: 35 additions & 0 deletions src/Factory/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Created by PhpStorm.
* User: Ppito
* Date: 13/01/2020
* Time: 8:45 PM
*
* @link https://github.com/Ppito/laminas-whoops for the canonical source repository
* @copyright Copyright (c) 2020 Mickael TONNELIER.
* @license https://github.com/Ppito/laminas-whoops/blob/master/LICENSE.md The MIT License
*/

namespace WhoopsErrorHandler\Factory;

use Interop\Container\ContainerInterface;
use WhoopsErrorHandler\Handler\HandlerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;

class Factory implements FactoryInterface {

/**
* Invoke Handler
*
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
* @return HandlerInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
$config = $container->has('config') ? $container->get('config') : [];
$config = isset($config['whoops']) ? $config['whoops'] : [];

return new $requestedName($container, $config);
}
}
59 changes: 59 additions & 0 deletions src/Handler/AjaxHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Created by PhpStorm.
* User: Ppito
* Date: 13/01/2020
* Time: 8:45 PM
*
* @link https://github.com/Ppito/laminas-whoops for the canonical source repository
* @copyright Copyright (c) 2020 Mickael TONNELIER.
* @license https://github.com/Ppito/laminas-whoops/blob/master/LICENSE.md The MIT License
*/

namespace WhoopsErrorHandler\Handler;

use Interop\Container\ContainerInterface;
use Whoops\Handler\JsonResponseHandler as WhoopsAjaxHandler;

class AjaxHandler extends HandlerAbstract implements HandlerInterface {

/**
* AjaxHandler constructor.
*
* @param ContainerInterface $container
* @param array $options
* @return self
*/
public function __construct(ContainerInterface $container, $options = []) {
parent::__construct($container, $options);
$this->handler = new WhoopsAjaxHandler();
$this->configure();
return $this;
}

/**
* Inject an editor into the whoops configuration.
*
* @return void
* @throws \InvalidArgumentException for an invalid show trace option.
*/
public function configure(): void {
/** @var WhoopsAjaxHandler $handler */
$handler = $this->getHandler();
$handler->setJsonApi(true);

if (!isset($this->options['show_trace']) || !isset($this->options['show_trace']['ajax_display'])) {
return;
}

$show_trace = $this->options['show_trace']['ajax_display'];

if (! is_bool($show_trace)) {
throw new \InvalidArgumentException(sprintf(
'Whoops show trace option must be a boolean; received "%s"',
(is_object($show_trace) ? get_class($show_trace) : gettype($show_trace))
));
}
$handler->addTraceToOutput($show_trace);
}
}
58 changes: 58 additions & 0 deletions src/Handler/ConsoleHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Created by PhpStorm.
* User: Ppito
* Date: 13/01/2020
* Time: 8:45 PM
*
* @link https://github.com/Ppito/laminas-whoops for the canonical source repository
* @copyright Copyright (c) 2020 Mickael TONNELIER.
* @license https://github.com/Ppito/laminas-whoops/blob/master/LICENSE.md The MIT License
*/

namespace WhoopsErrorHandler\Handler;

use Interop\Container\ContainerInterface;
use Whoops\Handler\PlainTextHandler as WhoopsConsoleHandler;

class ConsoleHandler extends HandlerAbstract implements HandlerInterface {

/**
* ConsoleHandler constructor.
*
* @param ContainerInterface $container
* @param array $options
* @return self
*/
public function __construct(ContainerInterface $container, $options = []) {
parent::__construct($container, $options);
$this->handler = new WhoopsConsoleHandler();
$this->configure();
return $this;
}

/**
* Inject an editor into the whoops configuration.
*
* @return void
* @throws \InvalidArgumentException for an invalid show trace option.
*/
public function configure(): void {
/** @var WhoopsConsoleHandler $handler */
$handler = $this->getHandler();

if (!isset($this->options['show_trace']) || !isset($this->options['show_trace']['cli_display'])) {
return;
}

$show_trace = $this->options['show_trace']['cli_display'];

if (!is_bool($show_trace)) {
throw new \InvalidArgumentException(sprintf(
'Whoops show trace option must be a boolean; received "%s"',
(is_object($show_trace) ? get_class($show_trace) : gettype($show_trace))
));
}
$handler->addTraceToOutput($show_trace);
}
}
29 changes: 29 additions & 0 deletions src/Handler/HandlerAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Created by PhpStorm.
* User: Ppito
* Date: 13/01/2020
* Time: 8:45 PM
*
* @link https://github.com/Ppito/laminas-whoops for the canonical source repository
* @copyright Copyright (c) 2020 Mickael TONNELIER.
* @license https://github.com/Ppito/laminas-whoops/blob/master/LICENSE.md The MIT License
*/

namespace WhoopsErrorHandler\Handler;

use WhoopsErrorHandler\Service\ServiceAbstract;
use Whoops\Handler\HandlerInterface as WhoopsHandlerInterface;

abstract class HandlerAbstract extends ServiceAbstract implements HandlerInterface {

/** @var WhoopsHandlerInterface */
protected $handler;

/**
* @return WhoopsHandlerInterface
*/
public function getHandler() {
return $this->handler;
}
}
Loading

0 comments on commit 3276d73

Please sign in to comment.