PHP code example of marcoazn89 / booboo

1. Go to this page and download the library: Download marcoazn89/booboo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marcoazn89 / booboo example snippets




use Exception\BooBoo;

// This is a simple set up
BooBoo::setUp();

// A more complex set up
BooBoo::setUp(
	$logger,
	true,
	function() { $textMessageService->send('Hey something broke');},
	[E_NOTICE, E_DEPRECATED]
);



use Exception\BooBoo;

BooBoo::setUp();

// This causes a fatal error
$random->error();


class DatabaseException extends \Exception\BooBoo
{
	// It's good practice to define constants so that your exception messages are consistant
	const NOT_FOUND = 'Data requested was not found';
	
	/**
	 * This will be what's shown in the logs
	 * Example: [21-Mar-2016 05:58:27 Europe/Berlin] <TAG>: Something bad happened
	 */
	protected function getTag()
	{
		return 'DbException';
	}
	
	/**
	 * Return an array that defines your error template location or strings.
	 * For any template that is not defined, BooBoo will use its default templates.
	 * There are 4 templates currently supported: html, text, xml, json
	 */
	protected function getTemplates()
	{
		return [
			'html' => "<p>Something went really <h1>WRONG!</h1></p>",
			'json' => __DIR__ . '/json.php'
		];
	}
}


// Let's use json.php that was defined as a template in the previous example
{
    "status":  echo $response->getStatusCode();

// Lets use DatabaseException defined above for this example
// Note that the exceptions you define still work as a regular exception so you can just do a simple throw like:
throw new DatabaseException('Data was not found. The table appears to be empty');

// Or (showing all the options for documentation purposes)
throw (new DatabaseException('Data was not found. The table appears to be empty'))
	//Passing a response object with 400 status code.
	//Please not that BooBoo ignores any status code below 400 and turn it into 500
	->response($response->withStatus(400))
	
	//Use the constant defined for the message displayed to the client
	->displayMessage(DatabaseException::NOT_FOUND)
    	
	//Add context to your logs
	->logContext(['sessionID' => 12345])
    	
	//Pass data to your template
	->templateData(['actions' => 'Contact support'])
    	
	// Turn off stack traces
	->trace(false);
    	
	// Tur off logging
	->noLog();



use BooBoo\BooBoo;

// Add supported types
\HTTP\Support\TypeSupport::addSupport([
	\HTTP\Response\ContentType::HTML,
	\HTTP\Response\ContentType::TEXT
]);

BooBoo::setUp();

// Assume the client sends a text Accept header, the response will
// be in plain text because is the best match. If none match, the
// response will be in HTML because it was the first one added
$random->error();