Skip to content

Commit

Permalink
Functions: echo() can also be overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
vasa-c committed Jun 14, 2016
1 parent 4281000 commit f88306b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @method bool date_default_timezone_set(string $timezone_identifier)
* @method mixed set_error_handler(callable $error_handler, int $error_types = 32767)
* @method callable set_exception_handler(callable $exception_handler)
* @method echo(string $arg1)
*/
class Env
{
Expand Down Expand Up @@ -108,10 +109,13 @@ public function __call($name, $arguments)
}
return call_user_func_array($callback, $arguments);
}
if (!function_exists($name)) {
throw new FieldNotExist($name, 'global', null, $this);
if (function_exists($name)) {
return call_user_func_array($name, $arguments);
} elseif ($name === 'echo') {
echo implode(' ', $arguments);
return;
}
return call_user_func_array($name, $arguments);
throw new FieldNotExist($name, 'global', null, $this);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ $env->header('Content-Type: text/plain'); // The header will not be sent

Checking the existence of functions `$env->isFunctionExists(string $name):bool`.

`echo()` can also be overridden.

##### Example

Function `getallheaders()` not exist in all environments.
Expand Down
22 changes: 22 additions & 0 deletions tests/EnvFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,26 @@ public function testCallMagic()
];
$this->assertSame($expected, $headers);
}

public function testEcho()
{
$echoed = null;
$config = [
'functions' => [
'echo' => function ($e) use (&$echoed) {
$echoed = $e;
}
],
];
$envNormal = new Env([]);
$envEcho = new Env($config);
ob_start();
$envNormal->echo('one', 'two');
$envEcho->echo('three', 'four');
$out = ob_get_clean();
$this->assertSame('one two', $out);
$this->assertSame('three', $echoed);
$this->assertFalse($envNormal->isFunctionExists('echo'));
$this->assertTrue($envEcho->isFunctionExists('echo'));
}
}

0 comments on commit f88306b

Please sign in to comment.