Skip to content

Commit

Permalink
Merge pull request #687 from gwillz/master
Browse files Browse the repository at this point in the history
Builder-style handlers
  • Loading branch information
denis-sokolov committed Mar 16, 2021
2 parents 4722454 + 3c975c4 commit bd09320
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 45 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"role": "Developer"
}
],
"scripts": {
"test": "phpunit --testdox tests"
},
"require": {
"php": "^5.5.9 || ^7.0 || ^8.0",
"psr/log": "^1.0.1"
Expand Down
4 changes: 2 additions & 2 deletions src/Whoops/Handler/JsonResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class JsonResponseHandler extends Handler
/**
* Returns errors[[]] instead of error[] to be in compliance with the json:api spec
* @param bool $jsonApi Default is false
* @return $this
* @return static
*/
public function setJsonApi($jsonApi = false)
{
Expand All @@ -38,7 +38,7 @@ public function setJsonApi($jsonApi = false)

/**
* @param bool|null $returnFrames
* @return bool|$this
* @return bool|static
*/
public function addTraceToOutput($returnFrames = null)
{
Expand Down
15 changes: 10 additions & 5 deletions src/Whoops/Handler/PlainTextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,18 @@ public function getLogger()
* Set var dumper callback function.
*
* @param callable $dumper
* @return void
* @return static
*/
public function setDumper(callable $dumper)
{
$this->dumper = $dumper;
return $this;
}

/**
* Add error trace to output.
* @param bool|null $addTraceToOutput
* @return bool|$this
* @return bool|static
*/
public function addTraceToOutput($addTraceToOutput = null)
{
Expand All @@ -122,7 +123,7 @@ public function addTraceToOutput($addTraceToOutput = null)
/**
* Add previous exceptions to output.
* @param bool|null $addPreviousToOutput
* @return bool|$this
* @return bool|static
*/
public function addPreviousToOutput($addPreviousToOutput = null)
{
Expand All @@ -138,7 +139,7 @@ public function addPreviousToOutput($addPreviousToOutput = null)
* Add error trace function arguments to output.
* Set to True for all frame args, or integer for the n first frame args.
* @param bool|integer|null $addTraceFunctionArgsToOutput
* @return null|bool|integer
* @return static|bool|integer
*/
public function addTraceFunctionArgsToOutput($addTraceFunctionArgsToOutput = null)
{
Expand All @@ -151,17 +152,20 @@ public function addTraceFunctionArgsToOutput($addTraceFunctionArgsToOutput = nul
} else {
$this->addTraceFunctionArgsToOutput = $addTraceFunctionArgsToOutput;
}
return $this;
}

/**
* Set the size limit in bytes of frame arguments var_dump output.
* If the limit is reached, the var_dump output is discarded.
* Prevent memory limit errors.
* @var integer
* @return static
*/
public function setTraceFunctionArgsOutputLimit($traceFunctionArgsOutputLimit)
{
$this->traceFunctionArgsOutputLimit = (integer) $traceFunctionArgsOutputLimit;
return $this;
}

/**
Expand Down Expand Up @@ -199,7 +203,7 @@ public function getTraceFunctionArgsOutputLimit()
/**
* Only output to logger.
* @param bool|null $loggerOnly
* @return null|bool
* @return static|bool
*/
public function loggerOnly($loggerOnly = null)
{
Expand All @@ -208,6 +212,7 @@ public function loggerOnly($loggerOnly = null)
}

$this->loggerOnly = (bool) $loggerOnly;
return $this;
}

/**
Expand Down
36 changes: 24 additions & 12 deletions src/Whoops/Handler/PrettyPageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,12 @@ public function contentType()
* @param string $label
* @param array $data
*
* @return void
* @return static
*/
public function addDataTable($label, array $data)
{
$this->extraTables[$label] = $data;
return $this;
}

/**
Expand All @@ -368,7 +369,7 @@ public function addDataTable($label, array $data)
*
* @throws InvalidArgumentException If $callback is not callable
*
* @return void
* @return static
*/
public function addDataTableCallback($label, /* callable */ $callback)
{
Expand All @@ -387,6 +388,8 @@ public function addDataTableCallback($label, /* callable */ $callback)
return [];
}
};

return $this;
}

/**
Expand Down Expand Up @@ -418,7 +421,7 @@ public function getDataTables($label = null)
*
* @param bool|null $value
*
* @return bool|null
* @return bool|static
*/
public function handleUnconditionally($value = null)
{
Expand All @@ -427,6 +430,7 @@ public function handleUnconditionally($value = null)
}

$this->handleUnconditionally = (bool) $value;
return $this;
}

/**
Expand All @@ -447,11 +451,12 @@ public function handleUnconditionally($value = null)
* @param string $identifier
* @param string|callable $resolver
*
* @return void
* @return static
*/
public function addEditor($identifier, $resolver)
{
$this->editors[$identifier] = $resolver;
return $this;
}

/**
Expand All @@ -469,7 +474,7 @@ public function addEditor($identifier, $resolver)
*
* @throws InvalidArgumentException If invalid argument identifier provided
*
* @return void
* @return static
*/
public function setEditor($editor)
{
Expand All @@ -481,6 +486,7 @@ public function setEditor($editor)
}

$this->editor = $editor;
return $this;
}

/**
Expand Down Expand Up @@ -591,11 +597,12 @@ protected function getEditor($filePath, $line)
*
* @param string $title
*
* @return void
* @return static
*/
public function setPageTitle($title)
{
$this->pageTitle = (string) $title;
return $this;
}

/**
Expand All @@ -615,7 +622,7 @@ public function getPageTitle()
*
* @throws InvalidArgumentException If $path is not a valid directory
*
* @return void
* @return static
*/
public function addResourcePath($path)
{
Expand All @@ -626,30 +633,33 @@ public function addResourcePath($path)
}

array_unshift($this->searchPaths, $path);
return $this;
}

/**
* Adds a custom css file to be loaded.
*
* @param string|null $name
*
* @return void
* @return static
*/
public function addCustomCss($name)
{
$this->customCss = $name;
return $this;
}

/**
* Adds a custom js file to be loaded.
*
* @param string|null $name
*
* @return void
* @return static
*/
public function addCustomJs($name)
{
$this->customJs = $name;
return $this;
}

/**
Expand Down Expand Up @@ -718,11 +728,12 @@ public function getResourcesPath()
*
* @param string $resourcesPath
*
* @return void
* @return static
*/
public function setResourcesPath($resourcesPath)
{
$this->addResourcePath($resourcesPath);
return $this;
}

/**
Expand Down Expand Up @@ -767,19 +778,20 @@ public function setApplicationRootPath($applicationRootPath)
* @param string $key The key within the superglobal
* @see hideSuperglobalKey
*
* @return void
* @return static
*/
public function blacklist($superGlobalName, $key)
{
$this->blacklist[$superGlobalName][] = $key;
return $this;
}

/**
* Hide a sensitive value within one of the superglobal arrays.
*
* @param string $superGlobalName The name of the superglobal array, e.g. '_GET'
* @param string $key The key within the superglobal
* @return void
* @return static
*/
public function hideSuperglobalKey($superGlobalName, $key)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Whoops/Handler/XmlResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class XmlResponseHandler extends Handler

/**
* @param bool|null $returnFrames
* @return bool|$this
* @return bool|static
*/
public function addTraceToOutput($returnFrames = null)
{
Expand Down
8 changes: 4 additions & 4 deletions tests/Whoops/Handler/PlainTextHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private function getPlainTextFromHandler(
$run->register();

$exception = $exception ?: $this->getException();

try {
ob_start();
$run->handleException($exception);
Expand Down Expand Up @@ -100,7 +100,7 @@ public function testAddTraceToOutput()
{
$handler = $this->getHandler();

$handler->addTraceToOutput(true);
$this->assertEquals($handler, $handler->addTraceToOutput(true));
$this->assertTrue($handler->addTraceToOutput());

$handler->addTraceToOutput(false);
Expand Down Expand Up @@ -129,7 +129,7 @@ public function testAddTraceFunctionArgsToOutput()
{
$handler = $this->getHandler();

$handler->addTraceFunctionArgsToOutput(true);
$this->assertEquals($handler, $handler->addTraceFunctionArgsToOutput(true));
$this->assertTrue($handler->addTraceFunctionArgsToOutput());

$handler->addTraceFunctionArgsToOutput(false);
Expand Down Expand Up @@ -178,7 +178,7 @@ public function testLoggerOnly()
{
$handler = $this->getHandler();

$handler->loggerOnly(true);
$this->assertEquals($handler, $handler->loggerOnly(true));
$this->assertTrue($handler->loggerOnly());

$handler->loggerOnly(false);
Expand Down
Loading

0 comments on commit bd09320

Please sign in to comment.