### Install Whoops using Composer Source: https://github.com/filp/whoops/blob/master/README.md This command installs the Whoops library into your PHP project using Composer, the dependency manager for PHP. Ensure you have Composer installed and configured in your project. ```bash composer require filp/whoops ``` -------------------------------- ### Adding Whoops Integration to Project Composer.json Source: https://github.com/filp/whoops/blob/master/docs/Framework Integration.md This JSON snippet demonstrates how a user would add a framework-specific Whoops integration to their project's `composer.json` file, ensuring both Whoops and the integration are installed. ```json "require": { "username/whoops-someframework": "*" } ``` -------------------------------- ### WhoopsExceptionInspector Methods Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Provides methods to inspect an exception instance and its stack trace. Includes getting the exception, its name, message, and iterating through frames. ```APIDOC Inspector::__construct(Exception $exception) #=> null // Returns the Exception instance being inspected Inspector::getException() #=> Exception // Returns the string name of the Exception being inspected // A faster way of doing get_class($inspector->getException()) Inspector::getExceptionName() #=> string // Returns the string message for the Exception being inspected // A faster way of doing $inspector->getException()->getMessage() Inspector::getExceptionMessage() #=> string // Returns an iterator instance for all the frames in the stack // trace for the Exception being inspected. Inspector::getFrames() #=> Whoops\Exception\FrameIterator ``` -------------------------------- ### Set Default Editor Source: https://github.com/filp/whoops/blob/master/docs/Open Files In An Editor.md Configures the PrettyPageHandler to use a predefined editor for opening files. This is a common setup for integrating Whoops with your preferred IDE. ```php setEditor('sublime'); ``` -------------------------------- ### PrettyPageHandler Page Title and Resource Management Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Methods for setting and getting the page title for the error page, and managing resource paths for templates and CSS files. ```APIDOC PrettyPageHandler::setPageTitle(string $title) - Sets the title for the error page. PrettyPageHandler::getPageTitle() - Returns the current title for the error page. PrettyPageHandler::getResourcesPaths() - Returns a list of string paths where resources (template and CSS files) are searched for. PrettyPageHandler::addResourcePath(string $resourcesPath) - Adds a string path to the location of resources for the handler. ``` -------------------------------- ### Conditional Logger Output for PlainTextHandler Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md This example shows how to configure a PlainTextHandler to output errors only to a logger when not running in a command-line environment. This allows the handler to be safely added to web pages. ```php /* Output the error message only if using command line. else, output to logger if available. Allow to safely add this handler to web pages. */ $plainTextHandler = new PlainTextHandler(); if (!Whoops\isCommandLine()){ $plainTextHandler->loggerOnly(true); } $run->addHandler($myHandler); ``` -------------------------------- ### Check Production Settings Method Source: https://github.com/filp/whoops/wiki/Possible-features-to-add This snippet describes a proposed method `checkProductionSettings` for checking server configurations like `display_errors` to prevent usage in production environments. ```php /** * Checks production settings to prevent usage in production. * Verifies configurations such as display_errors. */ public function checkProductionSettings(): bool { // Implementation details to check server settings // For example: return !ini_get('display_errors'); return true; // Placeholder } ``` -------------------------------- ### API Documentation Reference Source: https://github.com/filp/whoops/wiki/API-Documentation This section links to the comprehensive API documentation for the Whoops project, detailing available endpoints, methods, parameters, and return values. It serves as the primary reference for interacting with the Whoops API. ```APIDOC Refer to the official documentation for detailed API specifications: https://github.com/filp/whoops/blob/master/docs/API%20Documentation.md ``` -------------------------------- ### IntelliJ Platform Editor Integration Source: https://github.com/filp/whoops/blob/master/docs/Open Files In An Editor.md Provides a specific handler for IntelliJ Platform-based IDEs (like PhpStorm), including logic for mapping remote files to local paths and making an AJAX request to the IDE's API. ```php $handler->setEditor( function ($file, $line) { // if your development server is not local it's good to map remote files to local $translations = array('^' . __DIR__ => '~/Development/PhpStormOpener'); // change to your path foreach ($translations as $from => $to) { $file = preg_replace('#' . $from . '#', $to, $file, 1); } // IntelliJ platform requires that you send an Ajax request, else the browser will quit the page return array( 'url' => "http://localhost:63342/api/file/?file=$file&line=$line", 'ajax' => true ); } ); ``` -------------------------------- ### Composer.json for Whoops Integration Source: https://github.com/filp/whoops/blob/master/docs/Framework Integration.md This JSON configuration file is used to define a framework integration package for Whoops. It specifies the package name, a description, and the dependency on the core Whoops library. ```json { "name": "username/whoops-someframework", "description": "Integrates the Whoops library into SomeFramework", "require": { "filp/whoops": "1.*" } } ``` -------------------------------- ### PrettyPageHandler Method Signatures Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md API documentation for PrettyPageHandler methods, covering data table management, page titles, resource paths, editor configuration, and general handling. ```APIDOC PrettyPageHandler::addDataTable(string $label, array $data) - Adds a key=>value table of arbitrary data. - Returns: null PrettyPageHandler::addDataTableCallback(string $label, callable $callback) - Adds data via a callable that is executed only when rendering an exception. - Returns: null PrettyPageHandler::getDataTables(string $label = null) - Returns all registered data tables. Optionally accepts a string label. - Returns: array | array[] PrettyPageHandler::setPageTitle(string $title) - Sets the title for the error page. - Returns: null PrettyPageHandler::getPageTitle() - Returns the title for the error page. - Returns: string PrettyPageHandler::getResourcesPaths() - Returns a list of string paths where resources are searched for. - Returns: array PrettyPageHandler::addResourcePath(string $resourcesPath) - Adds a string path to the location of resources for the handler. - Returns: null PrettyPageHandler::setEditor(string $editor | callable) - Sets an editor to use for opening referenced files. - Returns: null PrettyPageHandler::addEditor(string $editor, $resolver) - Adds a named custom editor. - Returns: null PrettyPageHandler::handle() - Handles the exception. - Returns: int | null ``` -------------------------------- ### PrettyPageHandler Editor Configuration Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Allows configuration of how to open files in an editor from the error page. Supports string identifiers, callables, and AJAX requests. ```APIDOC PrettyPageHandler::setEditor(string $editor) - Sets an editor using a string identifier. PrettyPageHandler::setEditor(function ($file, $line) { return string }) - Sets an editor using a callable that returns a URL string. PrettyPageHandler::setEditor(function ($file, $line) { return array( 'url' => "http://localhost:63342/api/file/?file=$file&line=$line", 'ajax' => true ); } ) - Sets an editor with AJAX support. PrettyPageHandler::addEditor(string $editor, $resolver) - Adds a named custom editor, where $resolver can be a callable or a string with placeholders (%file, %line). ``` -------------------------------- ### PrettyPageHandler Features Source: https://github.com/filp/whoops/wiki/Possible-features-to-add This section details various features and improvements for the PrettyPageHandler component of the Whoops library, including Xdebug integration, a REPL, mobile layout, and JS API. ```php // Integration with Xdebug tools // A REPL // Add information on common problems // Simple stack trace in comments // Mobile layout support // Share/export feature // Whoops JS API // Nested blacklisting // Detect frame source library // Split PrettyPageHandler off // Limit the number of frames shown ``` -------------------------------- ### WhoopsRun Class Methods Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Provides methods for managing the handler stack, registering Whoops with PHP, and controlling execution flow. ```php Run::prependHandler(Whoops\HandlerInterface $handler) #=> Whoops\Run Run::appendHandler(Whoops\HandlerInterface $handler) #=> Whoops\Run Run::removeFirstHandler() #=> null Run::removeLastHandler() #=> null // Returns all handlers in the stack Run::getHandlers() #=> Whoops\HandlerInterface[] // Returns a Whoops\Inspector instance for a given Exception Run::getInspector(Exception $exception) #=> Whoops\Exception\Inspector // Registers this Whoops\Run instance as an error/exception/shutdown // handler with PHP Run::register() #=> Whoops\Run // I'll let you guess this one Run::unregister() #=> Whoops\Run // Send a custom exit code in CLI context (default: 1) Run::sendExitCode($code = null) #=> int // If true, allows Whoops to terminate script execution (default: true) Run::allowQuit($allowQuit = null) #=> bool // Silence errors for paths matching regular expressions and PHP error constants. // Can be called multiple times. Run::silenceErrorsInPaths($patterns, $levels = E_STRICT | E_DEPRECATED) #=> Whoops\Run // If true, allows Whoops to send output produced by handlers directly // to the client. You'll want to set this to false if you want to // package the handlers' response into your HTTP response abstraction // or something (default: true) Run::writeToOutput($send = null) #=> bool // ** HANDLERS ** // These are semi-internal methods that receive input from // PHP directly. If you know what you're doing, you can // also call them directly // Handles an exception with the current stack. Returns the // output produced by handlers. Run::handleException(Exception $exception) #=> string // Handles an error with the current stack. Errors are // converted into SPL ErrorException instances Run::handleError(int $level, string $message, string $file = null, int $line = null) #=> null // Hooked as a shutdown handler, captures fatal errors and handles them // through the current stack: Run::handleShutdown() #=> null // adds a new frame filter callback to the frame filters stack Run::addFrameFilter() #=> Whoops\Run ``` -------------------------------- ### JsonResponseHandler Method Signatures Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md API documentation for JsonResponseHandler methods, including controlling trace output and handling exceptions. ```APIDOC JsonResponseHandler::addTraceToOutput(bool $yes = null) - Controls whether detailed stack trace output is added to the JSON payload body. - Returns: bool JsonResponseHandler::handle() - Handles the exception. - Returns: int | null ``` -------------------------------- ### Replay Production Errors in Whoops Source: https://github.com/filp/whoops/blob/master/docs/Replay Errors.md This snippet demonstrates how to serialize an exception, register Whoops, and then handle the unserialized exception to replay an error that occurred in production. It requires the Whoops library. ```php $serialized_exception = serialize(new Exception('Something has happened!')); $whoops = new \Whoops\Run; $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler); $whoops->register(); $whoops->handleException(unserialize($serialized_exception)); ``` -------------------------------- ### Register Pretty Page Handler Source: https://github.com/filp/whoops/blob/master/README.md This PHP code snippet demonstrates how to instantiate the WhoopsRun class and register the PrettyPageHandler to display errors in a user-friendly format. This is typically done early in your application's bootstrap process. ```php $whoops = new \Whoops\Run; $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler); $whoops->register(); ``` -------------------------------- ### WhoopsExceptionFrame Methods Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Models a single frame in an exception's stack trace. Allows retrieval of file path, line number, class, function, arguments, file contents, and adding/retrieving comments. ```APIDOC // Returns the file path for the file where this frame occurred. // The optional $shortened argument allows you to retrieve a // shorter, human-readable file path for display. Frame::getFile(bool $shortened = false) #=> string | null (Some frames do not have a file path) // Returns the line number for this frame Frame::getLine() #=> int | null // Returns the class name for this frame, if it occurred // within a class/instance. Frame::getClass() #=> string | null // Returns the function name for this frame, if it occurred // within a function/method Frame::getFunction() #=> string | null // Returns an array of arguments for this frame. Empty if no // arguments were provided. Frame::getArgs() #=> array // Returns the full file contents for the file where this frame // occurred. Frame::getFileContents() #=> string | null // Returns an array of lines for a file, optionally scoped to a // given range of line numbers. i.e: Frame::getFileLines(0, 3) // returns the first 3 lines after line 0 (1) Frame::getFileLines(int $start = 0, int $length = null) #=> array | null // Adds a comment to this Frame instance. Comments are shared // with everything that can access the frame instance, obviously, // so they can be used for a variety of inter-operability purposes. // The context option can be used to improve comment filtering. // Additionally, if frames contain URIs, the PrettyPageHandler // will automagically convert them to clickable anchor elements. Frame::addComment(string $comment, string $context = 'global') #=> null // Returns all comments for this instance optionally filtered by // a string context identifier. Frame::getComments(string $filter = null) #=> array ``` -------------------------------- ### WhoopsUtilMisc Utility Functions Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Provides utility methods for checking the current execution environment, such as detecting AJAX requests or command-line execution. ```php Whoops\Util\Misc::isAjaxRequest() #=> bool Whoops\Util\Misc::isCommandLine() #=> bool ``` -------------------------------- ### Add Custom Editor Handler Source: https://github.com/filp/whoops/blob/master/docs/Open Files In An Editor.md Allows defining a custom handler for opening files, providing flexibility for unsupported editors or custom URL schemes. This is useful for unique editor integrations. ```php $handler->setEditor(function($file, $line) { return "whatever://open?file=$file&line=$line"; }); ``` -------------------------------- ### Capture and Process HTML Output Source: https://github.com/filp/whoops/blob/master/README.md This PHP code shows how to configure Whoops to capture the generated HTML output instead of displaying it directly. This allows for custom processing or logging of the error page content. The `allowQuit(false)` and `writeToOutput(false)` methods are used to prevent default behavior. ```php $whoops = new \Whoops\Run; $whoops->allowQuit(false); $whoops->writeToOutput(false); $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler); $html = $whoops->handleException($e); ``` -------------------------------- ### WhoopsHandlerCallbackHandler Methods Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Constructs a CallbackHandler with a callable and provides a handle method for processing exceptions. ```APIDOC // Accepts any valid callable // For example, a closure, a string function name, an array // in the format array($class, $method) CallbackHandler::__construct($callable) #=> null CallbackHandler::handle() #=> int | null ``` -------------------------------- ### WhoopsRun Class Constants Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Defines constants used by the `WhoopsRun` class for specifying handler methods. ```php string Run::EXCEPTION_HANDLER // (name for exception handler method) string Run::ERROR_HANDLER // (name for error handler method) string Run::SHUTDOWN_HANDLER // (name for shutdown handler method) ``` -------------------------------- ### Whoops Built-in Handlers Source: https://github.com/filp/whoops/blob/master/README.md Details the available built-in error handlers in the Whoops library, each designed for specific output formats or use cases. ```php PrettyPageHandler: Displays a user-friendly error page. PlainTextHandler: Outputs errors as plain text, suitable for CLI applications. CallbackHandler: Wraps closures or callables as handlers, automatically used when pushing callables to Run::pushHandler. JsonResponseHandler: Formats exception data as a JSON string, useful for AJAX requests. XmlResponseHandler: Formats exception data as an XML string, also useful for AJAX requests. ``` -------------------------------- ### PrettyPageHandler Data Table Management Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Methods for adding and retrieving custom data tables to the PrettyPageHandler output. Data can be static or dynamically generated via callbacks. ```APIDOC PrettyPageHandler::addDataTable(string $label, array $data) - Adds a key=>value table of arbitrary data, labeled by $label, to the output. PrettyPageHandler::addDataTableCallback(string $label, callable $callback) - Adds data via a callable that is executed only when rendering an exception. PrettyPageHandler::getDataTables(string $label = null) - Returns all registered data tables. If $label is provided, returns data only for that label. ``` -------------------------------- ### WhoopsHandlerCallbackHandler Usage Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Wraps regular PHP closures as valid handlers for exception processing. Useful for quick prototypes or simple custom handlers. ```php pushHandler(function($exception, $inspector, $run) { var_dump($exception->getMessage()); return Handler::DONE; }); $run->popHandler() // #=> Whoops\Handler\CallbackHandler ``` -------------------------------- ### WhoopsExceptionFrameCollection Methods Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Exposes a fluent interface to manipulate and examine a collection of Frame instances. Supports counting, filtering, and mapping frames. ```APIDOC // Returns the number of frames in the collection // May also be called as count($frameCollection) FrameCollection::count() #=> int // Filter the Frames in the collection with a callable. // The callable must accept a Frame object, and return // true to keep it in the collection, or false not to. FrameCollection::filter(callable $callable) #=> FrameCollection // See: array_map // The callable must accept a Frame object, and return // a Frame object, doesn't matter if it's the same or not // - will throw an UnexpectedValueException if something // else is returned. FrameCollection::map(callable $callable) #=> FrameCollection ``` -------------------------------- ### Check if running in CLI Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md This snippet demonstrates how to use Whoops\Util\Misc::isCommandLine() to conditionally add a handler only when the script is executed from the command line. ```php if (Whoops\Util\Misc::isCommandLine()){ $run->addHandler($myHandler); } ``` -------------------------------- ### WhoopsHandlerHandler & WhoopsHandlerHandlerInterface Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Defines the base methods and constants for custom error handlers in Whoops. Handlers can extend this abstract class or implement the interface directly. ```APIDOC Whoops\Handler\Handler & Whoops\Handler\HandlerInterface This abstract class contains the base methods for concrete handler implementations. Custom handlers can extend it, or implement the `Whoops\Handler\HandlerInterface` interface. ### Constants ```php int Handler::DONE // If returned from HandlerInterface::handle, does absolutely nothing. int Handler::LAST_HANDLER // ...tells whoops to not execute any more handlers after this one. int Handler::QUIT // ...tells whoops to quit script execution immediately. ``` ### Methods ```php // Custom handlers should expose this method, which will be called once an // exception needs to be handled. The Handler::* constants can be used to // signal the underlying logic as to what to do next. HandlerInterface::handle() #=> null | int // Sets the Run instance for this handler HandlerInterface::setRun(Whoops\Run $run) #=> null // Sets the Inspector instance for this handler HandlerInterface::setInspector(Whoops\Exception\Inspector $inspector) #=> null // Sets the Exception for this handler to handle HandlerInterface::setException(Exception $exception) #=> null ``` ``` -------------------------------- ### JsonResponseHandler JSON Payload Structure Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md Defines the structure of the JSON payload generated by JsonResponseHandler for error reporting. It includes error type, message, file, line, and optionally a stack trace. ```json { "error": { "type": "RuntimeException", "message": "Something broke!", "file": "/var/project/foo/bar.php", "line": 22, # if JsonResponseHandler::addTraceToOutput(true): "trace": [ { "file": "/var/project/foo/index.php", "line": 157, "function": "handleStuffs", "class": "MyApplication\DoerOfThings", "args": [ true, 10, "yay method arguments" ] }, # ... more frames here ... ] } } ``` -------------------------------- ### AJAX Request Detection Source: https://github.com/filp/whoops/blob/master/docs/API Documentation.md A utility function to determine if the current request was triggered via AJAX. ```php // Use a certain handler only in AJAX triggered requests if (Whoops\Util\Misc::isAjaxRequest()){ $run->addHandler($myHandler); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.