Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 13, 2020
1 parent cd1d629 commit 291b7ae
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 113 deletions.
Expand Up @@ -20,8 +20,7 @@ interface ResolvableInterface
public function __construct(BuilderInterface $builder);

/**
*
* @return Chevere\Interfaces\App\BuilderInterface A resolvable BuilderInterface
* @return BuilderInterface A resolvable BuilderInterface
*/
public function builder(): BuilderInterface;
}
Expand Up @@ -39,7 +39,7 @@ public function testWithBadAddedCallable(): void
{
$benchmark = new Benchmark(1, 2, 3);
$this->expectException(ArgumentCountException::class);
$benchmark = $benchmark->withAddedCallable(
$benchmark->withAddedCallable(
function (int $one) {
return $one;
},
Expand Down
Expand Up @@ -18,7 +18,12 @@
interface SetErrorHandlerInterface extends SetInterface
{
/**
* @return mixed The handler value as returned by set_error_handler()
* Uhh no!
*/
public function __construct(string $value);

/**
* @return mixed The handler value as returned by `set_error_handler`
*/
public function handler();
}
Expand Up @@ -18,7 +18,7 @@
interface SetExceptionHandlerInterface extends SetInterface
{
/**
* @return mixed The handler value as returned by set_exception_handler()
* @return mixed The handler value as returned by `set_exception_handler`
*/
public function handler();
}
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevereto.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Components\Runtime\Sets;

use TypeError;
use InvalidArgumentException;
use Chevere\Components\Message\Message;
use Chevere\Components\Runtime\Traits\SetTrait;
use Chevere\Components\Runtime\Interfaces\SetInterface;

/**
* Sets the abstract handler using `getSetHandler`
*/
abstract class SetAbstractHandler implements SetInterface
{
use SetTrait;

protected $handler;

/**
* Sets the handler callable.
* @param string $value A callable name.
*
* @throws InvalidArgumentException If $value is not callable
*/
final public function __construct(string $value)
{
$this->value = $value;
if ('' == $this->value) {
$this->restoreHandler();

return;
}
$this->assertArgument();
$this->handler = $this->value;
$this->getSetHandler()($this->value);
}

abstract public function getSetHandler(): callable;

abstract public function getRestoreHandler(): callable;

public function handler()
{
return $this->handler;
}

private function assertArgument(): void
{
if (!is_callable($this->value)) {
throw new InvalidArgumentException(
(new Message('Runtime value must be a valid callable for %subject%'))
->code('%subject%', (string) $this->getSetHandler())
->toString()
);
}
}

private function restoreHandler(): void
{
$restoreFn = $this->getRestoreHandler();
$setFn = $this->getSetHandler();
$restoreFn();
$this->handler = $setFn(fn () => '');
try {
$this->value = $this->handler ?? '';
} catch (TypeError $e) {
$this->value = '@';
}
$restoreFn();
}
}
Expand Up @@ -18,7 +18,7 @@
use Chevere\Components\Runtime\Traits\SetTrait;
use InvalidArgumentException;

class SetDebug implements SetInterface
final class SetDebug implements SetInterface
{
use SetTrait;

Expand Down
Expand Up @@ -22,7 +22,7 @@
/**
* Sets the `default_charset` ini propery
*/
class SetDefaultCharset implements SetInterface
final class SetDefaultCharset implements SetInterface
{
use SetTrait;

Expand Down
Expand Up @@ -13,65 +13,20 @@

namespace Chevere\Components\Runtime\Sets;

use TypeError;
use Chevere\Components\Message\Message;
use Chevere\Components\Runtime\Traits\SetTrait;
use Chevere\Components\Runtime\Interfaces\Sets\SetErrorHandlerInterface;
use InvalidArgumentException;
use Chevere\Components\Runtime\Traits\HandlerTrait;

/**
* Sets the exception handler using `set_error_handler`
* Sets and restores the error handler using `set_error_handler` and `restore_error_handler`
*/
class SetErrorHandler implements SetErrorHandlerInterface
final class SetErrorHandler extends SetAbstractHandler implements SetErrorHandlerInterface
{
use SetTrait;
use HandlerTrait;

/** @var mixed Value returned from PHP */
private $handler;

/**
* Sets the error handler function
*
* @param string $value A full-qualified callable name or empty string for restore handler.
* @throws InvalidArgumentException If the value passed isn't acceptable.
*/
public function __construct(string $value)
{
$this->value = $value;
if ('' == $this->value) {
$this->restoreErrorHandler();

return;
}
$this->assertArgument();
set_error_handler($this->value);
}

private function assertArgument(): void
public function getSetHandler(): callable
{
if (!is_callable($this->value)) {
throw new InvalidArgumentException(
(new Message('Value must be a valid %type% for %subject%, value %value% passed'))
->code('%type%', 'callable')
->code('%subject%', 'set_error_handler()')
->code('%value%', $this->value)
->toString()
);
}
return 'set_error_handler';
}

private function restoreErrorHandler(): void
public function getRestoreHandler(): callable
{
restore_error_handler();
$this->handler = set_error_handler(function () {
});
try {
$this->value = $this->handler;
} catch (TypeError $e) {
$this->value = '@';
}
restore_error_handler();
return 'restore_error_handler';
}
}
Expand Up @@ -13,60 +13,23 @@

namespace Chevere\Components\Runtime\Sets;

use TypeError;
use InvalidArgumentException;
use Chevere\Components\Message\Message;
use Chevere\Components\Runtime\Interfaces\Sets\SetExceptionHandlerInterface;
use Chevere\Components\Runtime\Traits\SetTrait;
use Chevere\Components\Runtime\Interfaces\SetInterface;
use Chevere\Components\Runtime\Traits\HandlerTrait;

/**
* Sets the exception handler using `set_exception_handler`
* Sets and restores the exception handler using `set_exception_handler` and `restore_exception_handler`
*/
class SetExceptionHandler implements SetInterface
final class SetExceptionHandler extends SetAbstractHandler implements SetExceptionHandlerInterface
{
use SetTrait;
use HandlerTrait;

/**
* Sets the exception handler function
*
* @param string $value A full-qualified callable name or empty string for restore handler.
* @throws InvalidArgumentException If the value passed isn't acceptable.
*/
public function __construct(string $value)
public function getSetHandler(): callable
{
$this->value = $value;
if ('' == $this->value) {
$this->restoreExceptionHandler();

return;
}
$this->assertArgument();
set_exception_handler($this->value);
}

private function assertArgument(): void
{
if (!is_callable($this->value)) {
throw new InvalidArgumentException(
(new Message('Runtime value must be a valid callable for %subject%'))
->code('%subject%', 'set_exception_handler')
->toString()
);
}
return 'set_exception_handler';
}

private function restoreExceptionHandler(): void
public function getRestoreHandler(): callable
{
restore_exception_handler();
$this->handler = set_exception_handler(function () {
});
try {
$this->value = $this->handler ?? '';
} catch (TypeError $e) {
$this->value = '@';
}
restore_exception_handler();
return 'restore_exception_handler';
}
}
Expand Up @@ -21,7 +21,7 @@
/**
* Sets the locale using `setlocale`
*/
class SetLocale implements SetInterface
final class SetLocale implements SetInterface
{
use SetTrait;

Expand Down
Expand Up @@ -22,7 +22,7 @@
/**
* Sets the `precision` ini property
*/
class SetPrecision implements SetInterface
final class SetPrecision implements SetInterface
{
use SetTrait;

Expand Down
Expand Up @@ -22,7 +22,7 @@
/**
* Sets the default timezone
*/
class SetTimeZone implements SetInterface
final class SetTimeZone implements SetInterface
{
use SetTrait;

Expand Down
Expand Up @@ -21,7 +21,7 @@
/**
* Set the HTTP scheme
*/
class SetUriScheme implements SetInterface
final class SetUriScheme implements SetInterface
{
use SetTrait;

Expand Down
Expand Up @@ -18,8 +18,4 @@ trait HandlerTrait
/**
* {@inheritdoc}
*/
public function handler()
{
return $this->handler;
}
}
Expand Up @@ -13,12 +13,11 @@

namespace Chevere\Components\Runtime\Traits;

use Chevere\Components\Data\Interfaces\DataInterface;
use function ChevereFn\stringReplaceFirst;

trait SetTrait
{
private string $value;
protected string $value;

/**
* {@inheritdoc}
Expand All @@ -33,7 +32,7 @@ public function value(): string
*/
public function name(): string
{
$explode = explode('\\', __CLASS__);
$explode = explode('\\', get_class($this));
$name = stringReplaceFirst('Set', '', end($explode));

return lcfirst($name);
Expand Down

0 comments on commit 291b7ae

Please sign in to comment.