DebugTrait uses trait:short
Provides debugging and mock-related functionality for classes.
This trait allows a class to handle:
- A debug mode, indicating if debug features should be enabled.
- A mock mode, used for testing or simulating data, which is only active if debug mode is active.
Both modes can be initialized via arrays using the constants DEBUG and MOCK.
Any non-boolean value provided will fall back to a default value.
Example usage:
class MyService
{
use DebugTrait;
}
$service = (new MyService())
->initializeDebug([DebugTrait::DEBUG => true])
->initializeMock([DebugTrait::MOCK => true]);
var_dump($service->isDebug()); // true
var_dump($service->isMock()); // true
// Non-boolean values will use the default
$service->initializeDebug([DebugTrait::DEBUG => 'yes'], false);
var_dump($service->isDebug()); // false
Table of Contents
Constants
- DEBUG = 'debug'
- The 'debug' parameter constant.
- LOGGABLE = 'loggable'
- The 'loggable' parameter constant.
- LOGGER = 'logger'
- The 'logger' parameter constant.
- MOCK = 'mock'
- The 'mock' parameter constant.
Properties
- $debug : bool
- Indicates if use the debug mode.
- $loggable : bool
- The loggable flag.
- $mock : bool
- The mock flag to test the model.
Methods
- alert() : void
- Action must be taken immediately.
- critical() : void
- Critical conditions.
- debug() : void
- Detailed debug information.
- emergency() : void
- System is unusable.
- error() : void
- Runtime errors that do not require immediate action but should typically be logged and monitored.
- getLogger() : LoggerInterface|null
- Returns the logger reference.
- info() : void
- Interesting events.
- initializeDebug() : static
- Initialize the debug flag.
- initializeLoggable() : static
- Initialize the loggable flag.
- initializeLogger() : static
- Initializes the logger reference for the current instance.
- initializeMock() : static
- Initialize the mock flag.
- isDebug() : bool
- Check if debug mode is active.
- isMock() : bool
- Check if mock mode is active.
- log() : void
- Logs with an arbitrary level.
- notice() : void
- Normal but significant events.
- warning() : void
- Exceptional occurrences that are not errors.
Constants
DEBUG
The 'debug' parameter constant.
public
mixed
DEBUG
= 'debug'
LOGGABLE
The 'loggable' parameter constant.
public
mixed
LOGGABLE
= 'loggable'
LOGGER
The 'logger' parameter constant.
public
mixed
LOGGER
= 'logger'
MOCK
The 'mock' parameter constant.
public
mixed
MOCK
= 'mock'
Properties
$debug
Indicates if use the debug mode.
public
bool
$debug
= false
$loggable
The loggable flag.
public
bool
$loggable
= false
$mock
The mock flag to test the model.
public
bool
$mock
= false
Methods
alert()
Action must be taken immediately.
public
alert(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
critical()
Critical conditions.
public
critical(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: Application component unavailable, unexpected exception.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
debug()
Detailed debug information.
public
debug(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
emergency()
System is unusable.
public
emergency(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
error()
Runtime errors that do not require immediate action but should typically be logged and monitored.
public
error(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
getLogger()
Returns the logger reference.
public
getLogger() : LoggerInterface|null
Return values
LoggerInterface|nullinfo()
Interesting events.
public
info(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: User logs in, SQL logs.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
initializeDebug()
Initialize the debug flag.
public
initializeDebug([array<string|int, mixed> $init = [] ][, bool $defaultValue = false ]) : static
This method sets the $debug property using the value provided in the $init array,
or falls back to the current $debug value. If the value in $init is not a boolean,
the provided $defaultValue is used instead.
Parameters
- $init : array<string|int, mixed> = []
-
Optional initialization array.
- $defaultValue : bool = false
-
Default value to use if the init value is not a boolean.
Return values
static —Returns the current instance for chaining.
initializeLoggable()
Initialize the loggable flag.
public
initializeLoggable([bool|array<string|int, mixed>|null $init = null ][, ContainerInterface|null $container = null ][, bool|array<string|int, mixed>|null $defaultValue = false ]) : static
Parameters
- $init : bool|array<string|int, mixed>|null = null
-
The definition to initialize the loggable property.
- $container : ContainerInterface|null = null
- $defaultValue : bool|array<string|int, mixed>|null = false
-
The default value if the $init argument is not defined.
Tags
Return values
staticinitializeLogger()
Initializes the logger reference for the current instance.
public
initializeLogger([array<string|int, mixed>|LoggerInterface|string|null $init = null ][, ContainerInterface|null $container = null ][, bool $useDefault = true ]) : static
This method accepts either:
- A LoggerInterface instance
- An associative array containing a logger reference under the static::LOGGER key
- A string representing a service ID or class name resolvable by the container
nullor an empty value, which will default to LoggerInterface::class depending on the$useDefaultparameter.
If a dependency injection container is provided, the method will attempt to
resolve the logger service from it. If no valid logger can be resolved, the
$this->logger property will be set to null.
Parameters
- $init : array<string|int, mixed>|LoggerInterface|string|null = null
-
Logger initialization data. May be an instance, an array with a logger entry, a string service ID/class name, or
null. - $container : ContainerInterface|null = null
-
Optional dependency injection container used to resolve the logger service.
- $useDefault : bool = true
-
Whether to use LoggerInterface::class as a fallback if
$initdoes not provide a valid logger string. Defaults totrue.
Tags
Return values
static —Returns the current instance for method chaining.
initializeMock()
Initialize the mock flag.
public
initializeMock([array<string|int, mixed> $init = [] ][, bool $defaultValue = false ]) : static
This method sets the $mock property using the value provided in the $init array,
or falls back to the current $mock value. If the value in $init is not a boolean,
the provided $defaultValue is used instead.
Parameters
- $init : array<string|int, mixed> = []
-
Optional initialization array.
- $defaultValue : bool = false
-
Default value to use if the init value is not a boolean.
Return values
static —Returns the current instance for chaining.
isDebug()
Check if debug mode is active.
public
isDebug([array<string|int, mixed> $init = [] ]) : bool
This method returns the boolean value of the debug flag. If a value is provided
in the $init array, it is used; otherwise the current $debug property is used.
Non-boolean values in $init are treated as false.
Parameters
- $init : array<string|int, mixed> = []
-
Optional array containing a debug value.
Return values
bool —True if debug mode is active, false otherwise.
isMock()
Check if mock mode is active.
public
isMock([array<string|int, mixed> $init = [] ]) : bool
Mock mode is only active if debug mode is active as well.
If a value is provided in the $init array, it is used; otherwise the current $mock property is used.
Non-boolean values in $init are treated as false.
Parameters
- $init : array<string|int, mixed> = []
-
Optional array containing a mock value.
Return values
bool —True if mock mode is active, false otherwise.
log()
Logs with an arbitrary level.
public
log(mixed $level, string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $level : mixed
- $message : string|Stringable
- $context : array<string|int, mixed> = []
notice()
Normal but significant events.
public
notice(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
warning()
Exceptional occurrences that are not errors.
public
warning(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []