This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
CaptureOutputCapableTrait.php
62 lines (56 loc) · 1.87 KB
/
CaptureOutputCapableTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Dhii\Output;
use Dhii\Exception\InternalExceptionInterface;
use Dhii\Invocation\Exception\InvocationExceptionInterface;
use InvalidArgumentException;
use stdClass;
use Traversable;
use Exception as RootException;
/**
* Functionality for capturing the output of arbitrary code.
*
* @since [*next-version*]
*/
trait CaptureOutputCapableTrait
{
/**
* Invokes the given callable, and returns the output as a string.
*
* @since [*next-version*]
*
* @param callable $callable The callable that may produce output.
* @param array|stdClass|Traversable|null $args The arguments to invoke the callable with. Defaults to empty array.
*
* @throws InvalidArgumentException If the callable or the args list are invalid.
* @throws RootException If a problem occurs.
*
* @return string The output.
*/
protected function _captureOutput(callable $callable, $args = null)
{
// Default
if (is_null($args)) {
$args = [];
}
ob_start();
$this->_invokeCallable($callable, $args);
$output = ob_get_clean();
return $output;
}
/**
* Invokes a callable.
*
* @since [*next-version*]
*
* @param callable $callable The callable to invoke.
* @param array|Traversable|stdClass $args The arguments to invoke the callable with.
*
* @throws InvalidArgumentException If the callable is not callable.
* @throws InvalidArgumentException If the args are not a valid list.
* @throws InvocationExceptionInterface If the callable cannot be invoked.
* @throws InternalExceptionInterface If a problem occurs during invocation.
*
* @return mixed The result of the invocation.
*/
abstract protected function _invokeCallable($callable, $args);
}