brenno-duarte/modern-php-exception

PHP errors in a modern way

3.2.0 2024-05-01 12:36 UTC

README

Display PHP errors and exceptions in a modern and intuitive way!

68747470733a2f2f7265732e636c6f7564696e6172792e636f6d2f62646c736c74666d6b2f696d6167652f75706c6f61642f76313730343733313038382f696e6465785f796b6e6879652e706e67

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!

68747470733a2f2f7265732e636c6f7564696e6172792e636f6d2f62646c736c74666d6b2f696d6167652f75706c6f61642f76313635313431323138302f70726f64756374696f6e2d6d6f64655f7a616a6577672e706e67

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

68747470733a2f2f7265732e636c6f7564696e6172792e636f6d2f62646c736c74666d6b2f696d6167652f75706c6f61642f76313730343733303837302f6f6363757272656e6365735f6e76646d62652e706e67

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 exception

setDescription: Detailed description of exception solution

setDocs: If a documentation exists, this method will display a button for a documentation. By default, the name of the button will be Read 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 Hides inherited class properties.
@dumpignore-inherited-class Hides the class name from inherited properties.
@dumpignore-private Show all properties except the private ones.
@dumpignore-protected Show all properties except the protected ones.
@dumpignore-public Show all properties except the public ones.
@dumpignore Hide the property the Doc comment belongs to.
/**
* @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