Skip to content

Commit

Permalink
Removed PlainTextHandler->(onlyForCommandLine|outputOnlyIfCommandLine…
Browse files Browse the repository at this point in the history
…). Added Whoops\isCommandLine() as a more flexible successor.
  • Loading branch information
staabm committed Nov 29, 2015
1 parent 9425bc2 commit fa4206e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 150 deletions.
26 changes: 25 additions & 1 deletion docs/API Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

### Core Functions:
- [`Whoops\isAjaxRequest()`](#fn-ajax) - Determines whether the current request was triggered by XMLHttpRequest
- [`Whoops\isCommandLine()`](#fn-cli) - Determines whether the current request was triggered via php commandline interface (CLI)


# Core Classes:
Expand Down Expand Up @@ -408,10 +409,33 @@ PrettyPageHandler::handle()

## <a name="fn-ajax"></a> `Whoops\isAjaxRequest()`
#=> boolean

```php
// Use a certain handler only in AJAX triggered requests
if (Whoops\isAjaxRequest()){
$run->addHandler($myHandler)
}
```

## <a name="fn-cli"></a> `Whoops\isCommandLine()`
#=> boolean

```php
// Use a certain handler only in php cli
if (Whoops\isCommandLine()){
$run->addHandler($myHandler)
}
```

```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)
```
63 changes: 1 addition & 62 deletions src/Whoops/Handler/PlainTextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ class PlainTextHandler extends Handler
*/
private $traceFunctionArgsOutputLimit = 1024;

/**
* @var bool
*/
private $onlyForCommandLine = false;

/**
* @var bool
*/
private $outputOnlyIfCommandLine = true;

/**
* @var bool
*/
Expand Down Expand Up @@ -149,34 +139,6 @@ public function getTraceFunctionArgsOutputLimit()
return $this->traceFunctionArgsOutputLimit;
}

/**
* Restrict error handling to command line calls.
* @param bool|null $onlyForCommandLine
* @return null|bool
*/
public function onlyForCommandLine($onlyForCommandLine = null)
{
if (func_num_args() == 0) {
return $this->onlyForCommandLine;
}
$this->onlyForCommandLine = (bool) $onlyForCommandLine;
}

/**
* Output the error message only if using command line.
* else, output to logger if available.
* Allow to safely add this handler to web pages.
* @param bool|null $outputOnlyIfCommandLine
* @return null|bool
*/
public function outputOnlyIfCommandLine($outputOnlyIfCommandLine = null)
{
if (func_num_args() == 0) {
return $this->outputOnlyIfCommandLine;
}
$this->outputOnlyIfCommandLine = (bool) $outputOnlyIfCommandLine;
}

/**
* Only output to logger.
* @param bool|null $loggerOnly
Expand All @@ -191,32 +153,13 @@ public function loggerOnly($loggerOnly = null)
$this->loggerOnly = (bool) $loggerOnly;
}

/**
* Check, if possible, that this execution was triggered by a command line.
* @return bool
*/
private function isCommandLine()
{
return PHP_SAPI == 'cli';
}

/**
* Test if handler can process the exception..
* @return bool
*/
private function canProcess()
{
return $this->isCommandLine() || !$this->onlyForCommandLine();
}

/**
* Test if handler can output to stdout.
* @return bool
*/
private function canOutput()
{
return ($this->isCommandLine() || ! $this->outputOnlyIfCommandLine())
&& ! $this->loggerOnly();
return !$this->loggerOnly();
}

/**
Expand Down Expand Up @@ -297,10 +240,6 @@ private function getTraceOutput()
*/
public function handle()
{
if (! $this->canProcess()) {
return Handler::DONE;
}

$exception = $this->getException();

$response = sprintf("%s: %s in file %s on line %d%s\n",
Expand Down
9 changes: 9 additions & 0 deletions src/Whoops/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ function isAjaxRequest()
return (
!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
}

/**
* Check, if possible, that this execution was triggered by a command line.
* @return bool
*/
function isCommandLine()
{
return PHP_SAPI == 'cli';
}
96 changes: 9 additions & 87 deletions tests/Whoops/Handler/PlainTextHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,19 @@ public function getException($message = 'test message')
* @param bool $withTrace
* @param bool $withTraceArgs
* @param bool $loggerOnly
* @param bool $onlyForCommandLine
* @param bool $outputOnlyIfCommandLine
* @return array
*/
private function getPlainTextFromHandler(
$withTrace = false,
$withTraceArgs = false,
$traceFunctionArgsOutputLimit = 1024,
$loggerOnly = false,
$onlyForCommandLine = false,
$outputOnlyIfCommandLine = true
$loggerOnly = false
) {
$handler = $this->getHandler();
$handler->addTraceToOutput($withTrace);
$handler->addTraceFunctionArgsToOutput($withTraceArgs);
$handler->setTraceFunctionArgsOutputLimit($traceFunctionArgsOutputLimit);
$handler->loggerOnly($loggerOnly);
$handler->onlyForCommandLine($onlyForCommandLine);
$handler->outputOnlyIfCommandLine($outputOnlyIfCommandLine);

$run = $this->getRunInstance();
$run->pushHandler($handler);
Expand Down Expand Up @@ -164,64 +158,6 @@ public function testGetSetTraceFunctionArgsOutputLimit()
$this->assertEquals(0, $handler->getTraceFunctionArgsOutputLimit());
}

/**
* @covers Whoops\Handler\PlainTextHandler::onlyForCommandLine
*/
public function testOnlyForCommandLine()
{
$handler = $this->getHandler();

$handler->onlyForCommandLine(true);
$this->assertEquals(true, $handler->onlyForCommandLine());

$handler->onlyForCommandLine(false);
$this->assertEquals(false, $handler->onlyForCommandLine());

$handler->onlyForCommandLine(null);
$this->assertEquals(null, $handler->onlyForCommandLine());

$handler->onlyForCommandLine(1);
$this->assertEquals(true, $handler->onlyForCommandLine());

$handler->onlyForCommandLine(0);
$this->assertEquals(false, $handler->onlyForCommandLine());

$handler->onlyForCommandLine('');
$this->assertEquals(false, $handler->onlyForCommandLine());

$handler->onlyForCommandLine('false');
$this->assertEquals(true, $handler->onlyForCommandLine());
}

/**
* @covers Whoops\Handler\PlainTextHandler::outputOnlyIfCommandLine
*/
public function testOutputOnlyIfCommandLine()
{
$handler = $this->getHandler();

$handler->outputOnlyIfCommandLine(true);
$this->assertEquals(true, $handler->outputOnlyIfCommandLine());

$handler->outputOnlyIfCommandLine(false);
$this->assertEquals(false, $handler->outputOnlyIfCommandLine());

$handler->outputOnlyIfCommandLine(null);
$this->assertEquals(null, $handler->outputOnlyIfCommandLine());

$handler->outputOnlyIfCommandLine(1);
$this->assertEquals(true, $handler->outputOnlyIfCommandLine());

$handler->outputOnlyIfCommandLine(0);
$this->assertEquals(false, $handler->outputOnlyIfCommandLine());

$handler->outputOnlyIfCommandLine('');
$this->assertEquals(false, $handler->outputOnlyIfCommandLine());

$handler->outputOnlyIfCommandLine('false');
$this->assertEquals(true, $handler->outputOnlyIfCommandLine());
}

/**
* @covers Whoops\Handler\PlainTextHandler::loggerOnly
*/
Expand Down Expand Up @@ -261,9 +197,7 @@ public function testReturnsWithoutFramesOutput()
$withTrace = false,
$withTraceArgs = true,
$traceFunctionArgsOutputLimit = 1024,
$loggerOnly = false,
$onlyForCommandLine = false,
$outputOnlyIfCommandLine = true
$loggerOnly = false
);

// Check that the response has the correct value:
Expand All @@ -283,7 +217,6 @@ public function testReturnsWithoutFramesOutput()
/**
* @covers Whoops\Handler\PlainTextHandler::addTraceToOutput
* @covers Whoops\Handler\PlainTextHandler::getTraceOutput
* @covers Whoops\Handler\PlainTextHandler::canProcess
* @covers Whoops\Handler\PlainTextHandler::canOutput
* @covers Whoops\Handler\PlainTextHandler::handle
*/
Expand All @@ -293,9 +226,7 @@ public function testReturnsWithFramesOutput()
$withTrace = true,
$withTraceArgs = false,
$traceFunctionArgsOutputLimit = 1024,
$loggerOnly = false,
$onlyForCommandLine = false,
$outputOnlyIfCommandLine = true
$loggerOnly = false
);

$lines = explode("\n", $text);
Expand All @@ -311,7 +242,7 @@ public function testReturnsWithFramesOutput()
'Whoops\Handler\PlainTextHandlerTest',
'getException',
__FILE__,
61
55
),
$lines[3]
);
Expand All @@ -322,7 +253,6 @@ public function testReturnsWithFramesOutput()
* @covers Whoops\Handler\PlainTextHandler::addTraceFunctionArgsToOutput
* @covers Whoops\Handler\PlainTextHandler::getTraceOutput
* @covers Whoops\Handler\PlainTextHandler::getFrameArgsOutput
* @covers Whoops\Handler\PlainTextHandler::canProcess
* @covers Whoops\Handler\PlainTextHandler::canOutput
* @covers Whoops\Handler\PlainTextHandler::handle
*/
Expand All @@ -332,9 +262,7 @@ public function testReturnsWithFramesAndArgsOutput()
$withTrace = true,
$withTraceArgs = true,
$traceFunctionArgsOutputLimit = 2048,
$loggerOnly = false,
$onlyForCommandLine = false,
$outputOnlyIfCommandLine = true
$loggerOnly = false
);

$lines = explode("\n", $text);
Expand All @@ -353,7 +281,7 @@ public function testReturnsWithFramesAndArgsOutput()
'Whoops\Handler\PlainTextHandlerTest',
'getException',
__FILE__,
61
55
),
$lines[8]
);
Expand All @@ -372,7 +300,6 @@ public function testReturnsWithFramesAndArgsOutput()
* @covers Whoops\Handler\PlainTextHandler::addTraceFunctionArgsToOutput
* @covers Whoops\Handler\PlainTextHandler::getTraceOutput
* @covers Whoops\Handler\PlainTextHandler::getFrameArgsOutput
* @covers Whoops\Handler\PlainTextHandler::canProcess
* @covers Whoops\Handler\PlainTextHandler::canOutput
* @covers Whoops\Handler\PlainTextHandler::handle
*/
Expand All @@ -382,9 +309,7 @@ public function testReturnsWithFramesAndLimitedArgsOutput()
$withTrace = true,
$withTraceArgs = 3,
$traceFunctionArgsOutputLimit = 1024,
$loggerOnly = false,
$onlyForCommandLine = false,
$outputOnlyIfCommandLine = true
$loggerOnly = false
);

$lines = explode("\n", $text);
Expand All @@ -400,7 +325,7 @@ public function testReturnsWithFramesAndLimitedArgsOutput()
'Whoops\Handler\PlainTextHandlerTest',
'getException',
__FILE__,
61
55
),
$lines[8]
);
Expand All @@ -417,7 +342,6 @@ public function testReturnsWithFramesAndLimitedArgsOutput()

/**
* @covers Whoops\Handler\PlainTextHandler::loggerOnly
* @covers Whoops\Handler\PlainTextHandler::canProcess
* @covers Whoops\Handler\PlainTextHandler::handle
*/
public function testReturnsWithLoggerOnlyOutput()
Expand All @@ -426,9 +350,7 @@ public function testReturnsWithLoggerOnlyOutput()
$withTrace = true,
$withTraceArgs = true,
$traceFunctionArgsOutputLimit = 1024,
$loggerOnly = true,
$onlyForCommandLine = false,
$outputOnlyIfCommandLine = true
$loggerOnly = true
);
// Check that the response has the correct value:
$this->assertEquals('', $text);
Expand Down

0 comments on commit fa4206e

Please sign in to comment.