brenno-duarte / modern-php-exception
Display PHP errors and exceptions in a modern and intuitive way
Requires
- php: ^8.3
- ext-mbstring: *
- ext-pdo: *
- symfony/yaml: ^7.0
README
Display PHP errors and exceptions in a modern and intuitive way!
Requirements
- PHP >= 8.3
- ext-mbstring
- ext-pdo
Installing via Composer
Use the command below:
composer require brenno-duarte/modern-php-exception
How to use
You only need to call a single method as shown below.
use ModernPHPException\ModernPHPException; $exc = new ModernPHPException(); $exc->start();
From there, all errors and exceptions that are triggered will be displayed through the ModernPHPException component.
You can change the return, title and theme settings in the class constructor as shown in the items below.
YAML configuration
You can customize the exception title, enable dark mode and also enable production mode. Use the example file config.example.yaml
or create a new one.
$config = __DIR__ . '/config.example.yaml'; $exc = new ModernPHPException($config); $exc->start();
Changing the page title
title: My title
Enabling dark mode
# Default: false dark_mode: true
Enabling production mode
# Default: false production_mode: true
To change the message, change the error_message
variable:
production_mode: true error_message: Something wrong!
Load CSS files if there is no internet connection
# Use `false` only if you have no internet connection enable_cdn_assets: false
Enabling Log file
enable_logs: false # Default: sys_get_temp_dir() . "/ModernPHPExceptionLogs/ModernPHPExceptionLogs.log" dir_logs: C:\wamp64\www\modern-php-exception\
Enable occurrences
If you want to have a history of all exceptions and errors that your application displays, you can enable the occurrences using the enableOccurrences
method:
$config = __DIR__ . '/config.example.yaml'; $exc = new ModernPHPException($config); $exc->enableOccurrences(); // <- Before `start` method $exc->start();
Don't forget to configure the database in the config.example.yaml
file.
# Database for Occurrences db_drive: mysql db_host: localhost db_name: database_name db_user: root db_pass: pass
Creating a solution for an exception
If you are creating a custom exception class, you can add a solution to resolve this exception.
For that, use the static getSolution
method implementing the SolutionInterface
interface:
<?php namespace Test; use ModernPHPException\Solution; use ModernPHPException\Interface\SolutionInterface; class CustomException extends \Exception implements SolutionInterface { public function getSolution(): Solution { return Solution::createSolution('My Solution') ->setDescription('description') ->setDocs('https://google.com'); } #...
createSolution:
Name of solution to fix exceptionsetDescription:
Detailed description of exception solutionsetDocs:
If a documentation exists, this method will display a button for a documentation. By default, the name of the button will beRead More
, but you can change the name by changing the second parameter of the method
You can test using a new class:
public static function staticCall() { throw new CustomException("Error Processing Request"); }
Functions
Modern PHP Exceptions has some functions to help you debug your code. The available functions are:
- An easy function to pull all details of the debug backtrace.
get_debug_backtrace()
- Function to returns the value of
var_dump()
instead of outputting it.
echo var_dump_buffer()
- PHP function to replace var_dump(), print_r() based on the XDebug style.
var_dump_debug()
In terminal, you can simple hide or show some object attribute using a Doc block flag:
/** * @dumpignore-inheritance * @dumpignore-inherited-class * @dumpignore-private * @dumpignore-public * @dumpignore-public */ Class Foo extends Bar { /** @dumpignore */ private ?BigObject $foo = null; }
- Dump PHP value and die script. This function use
var_dump_debug
.
dump_die()
- View a PHP Closure's Source
closure_dump()
Logger
If you want to record a log to a file, you can use the Debug
class. To record a log, use the log
method.
use ModernPHPException\Debug; Debug::log($message, $log_file);
You can register the file and line on which this method is being called.
use ModernPHPException\Debug; Debug::log($message, $log_file, __FILE, __LINE__);
And to retrieve the logs that were recorded in a file, use the get
method.
use ModernPHPException\Debug; Debug::get($log_file);
Test
If you want to test the component, use the code below in your index.php
.
<?php require 'vendor/autoload.php'; use ModernPHPException\ModernPHPException; $exc = new ModernPHPException(); $exc->start(); throw new Exception("Error Test", 1); $a = new FakeClass();
License
MIT