Skip to content

Commit

Permalink
Merge branch 'release/v0.7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
betterthanclay committed Sep 1, 2019
2 parents 48fec0f + b488712 commit 4cdf87e
Show file tree
Hide file tree
Showing 31 changed files with 1,231 additions and 329 deletions.
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# Glitch
### Better exceptions for PHP.

Glitch is a standalone PHP package designed to improve end-to-end error handling when developing your applications.

The initial release provides a radically enhanced Exception framework that decouples the _meaning_ of an Exception from the underlying _implementation_ functionality.

Future releases will aim to also include a full suite of dump tools and exception handler interfaces to be used to inspect your code at run time during development.
Glitch is a standalone PHP package designed to improve end-to-end error handling and inspection when developing your applications.

The initial release provides a radically enhanced Exception framework that decouples the _meaning_ of an Exception from the underlying _implementation_ functionality, alongside deep data inspection tools.

## Rationale
PHP (and, as it happens, most modern languages) rely on a fairly rudimentary concept of Exceptions to handle errors at runtime. The principle is generally sound, however the implementation suffers from a handful of key flaws.
Expand Down Expand Up @@ -52,7 +49,7 @@ try {

However interfaces alone cannot immediately infer where the problem originated as you still require a class to be defined for each context from which the Exception may be thrown.

Also, this requires writing and loading **lot** of unneeded code to represent what are ultimately simple, static messages.
Also, this requires writing and loading **lots** of boilerplate code to represent what are ultimately simple, static messages.


### Solution
Expand Down Expand Up @@ -164,6 +161,30 @@ try {
```


### Traits

Custom functionality can be mixed in to the generated Glitch by defining traits at the same level as any of the E* interfaces being generated.

```php
namespace MyLibrary;

trait EBadThingTrait {

public function getCustomData(): ?string {
return $this->params['customData'] ?? null;
}
}

class Thing {
public function doAThing() {
throw \Glitch::EBadThing('A bad thing happened', [
'customData' => 'My custom info'
]);
}
}
```



## Licensing
Glitch is licensed under the MIT License. See [LICENSE](https://github.com/decodelabs/glitch/blob/master/LICENSE) for the full license text.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
},
"autoload": {
"psr-4": {
"": "src/"
"DecodeLabs\\Glitch\\": "src/Glitch"
},
"files": [
"src/EGlitch.php",
"src/Glitch.php",
"src/helpers.php"
]
}
Expand Down
11 changes: 0 additions & 11 deletions src/DecodeLabs/Glitch/Dumper/Inspect/Spl.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/EGlitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This file is part of the Glitch package
* @license http://opensource.org/licenses/MIT
*/

use DecodeLabs\Glitch\Stack\Frame;
use DecodeLabs\Glitch\Stack\Trace;

Expand Down
39 changes: 19 additions & 20 deletions src/DecodeLabs/Glitch/Context.php → src/Glitch/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use DecodeLabs\Glitch\Dumper\Inspector;
use DecodeLabs\Glitch\Dumper\Dump;

use DecodeLabs\Glitch\Dumper\Renderer;
use DecodeLabs\Glitch\Renderer;
use DecodeLabs\Glitch\Transport;

use Composer\Autoload\ClassLoader;
Expand Down Expand Up @@ -120,14 +120,12 @@ public function isProduction(): bool
}




/**
* Send variables to dump, carry on execution
*/
public function dump(array $values, int $rewind=null): void
public function dump(array $values, int $rewind=0): void
{
$trace = Trace::create($rewind + 1);
$trace = Trace::create($rewind - 1);
$inspector = new Inspector($this);
$dump = new Dump($trace);

Expand All @@ -136,21 +134,20 @@ public function dump(array $values, int $rewind=null): void
}

foreach ($values as $value) {
$dump->addEntity($inspector($value));
$dump->addEntity($inspector->inspectValue($value));
}

$dump->setTraceEntity($inspector($trace, function ($entity) {
$entity->setOpen(false);
}));
$inspector->reset();
unset($inspector);

$packet = $this->getDumpRenderer()->render($dump, true);
$packet = $this->getRenderer()->renderDump($dump, true);
$this->getTransport()->sendDump($packet);
}

/**
* Send variables to dump, exit and render
*/
public function dumpDie(array $values, int $rewind=null): void
public function dumpDie(array $values, int $rewind=0): void
{
$this->dump($values, $rewind + 1);
exit(1);
Expand All @@ -161,7 +158,7 @@ public function dumpDie(array $values, int $rewind=null): void
/**
* Quit a stubbed method
*/
public function incomplete($data=null, int $rewind=null): void
public function incomplete($data=null, int $rewind=0): void
{
$frame = Frame::create($rewind + 1);

Expand Down Expand Up @@ -246,13 +243,15 @@ public function getPathAliases(): array
/**
* Lookup and replace path prefix
*/
public function normalizePath(string $path): string
public function normalizePath(?string $path): ?string
{
$path = str_replace('\\', '/', $path);

foreach ($this->pathAliases as $name => $test) {
if (0 === strpos($path, $test)) {
return $name.'://'.ltrim(substr($path, strlen($test)), '/');
$len = strlen($test);

if (substr($path, 0, $len) == $test) {
return $name.'://'.ltrim(substr($path, $len), '/');
}
}

Expand Down Expand Up @@ -334,15 +333,15 @@ public function gatherDefaultStats(Dump $dump, Context $context): void
// Location
(new Stat('location', 'Dump location', $frame))
->setRenderer('text', function ($frame) {
return $this->normalizePath($frame->getFile()).' : '.$frame->getLine();
return $this->normalizePath($frame->getCallingFile()).' : '.$frame->getCallingLine();
})
);
}

/**
* TODO: move these to a shared location
*/
private static function formatFilesize($bytes)
public static function formatFilesize($bytes)
{
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];

Expand All @@ -353,7 +352,7 @@ private static function formatFilesize($bytes)
return round($bytes, 2).' '.$units[$i];
}

private static function formatMicrotime($time)
public static function formatMicrotime($time)
{
return number_format($time * 1000, 2).' ms';
}
Expand Down Expand Up @@ -417,7 +416,7 @@ public function getVendorPath(): string
/**
* Set dump renderer
*/
public function setDumpRenderer(Renderer $renderer): Context
public function setRenderer(Renderer $renderer): Context
{
$this->dumpRenderer = $renderer;
return $this;
Expand All @@ -426,7 +425,7 @@ public function setDumpRenderer(Renderer $renderer): Context
/**
* Get dump renderer
*/
public function getDumpRenderer(): Renderer
public function getRenderer(): Renderer
{
if (!$this->dumpRenderer) {
$this->dumpRenderer = new Renderer\Html($this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Dump implements \IteratorAggregate
protected $entities = [];

protected $trace;
protected $traceEntity;

/**
* Construct with stack trace of invoking call
Expand Down Expand Up @@ -102,24 +101,6 @@ public function getEntities(): array
}


/**
* Set trace entity
*/
public function setTraceEntity(Entity $entity): Dump
{
$this->traceEntity = $entity;
return $this;
}

/**
* Get trace entity
*/
public function getTraceEntity(): ?Entity
{
return $this->traceEntity;
}


/**
* Loop all entities
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,27 @@ public function getAllMeta(): ?array
return $this->meta;
}

/**
* Has meta value
*/
public function hasMeta(string $key): bool
{
if ($this->meta === null) {
return false;
}

return array_key_exists($key, $this->meta);
}

/**
* Remove meta value
*/
public function removeMeta(string $key): Entity
{
unset($this->meta[$key]);
return $this;
}



/**
Expand Down Expand Up @@ -466,6 +487,15 @@ public function hasProperty(string $key): bool
return array_key_exists($key, $this->properties);
}

/**
* Remove property
*/
public function removeProperty(string $key): Entity
{
unset($this->properties[$key]);
return $this;
}



/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,32 @@

use DecodeLabs\Glitch\Dumper\Entity;
use DecodeLabs\Glitch\Dumper\Inspector;
use DecodeLabs\Glitch\Stack\Trace;

class Core
{
/**
* Inspect generic exception
*/
public static function inspectException(\Throwable $exception, Entity $entity, Inspector $inspector): void
{
$entity
->setText($exception->getMessage())
->setProperty('*code', $inspector($exception->getCode()))
->setProperty('!previous', $inspector($exception->getPrevious(), function ($entity) {
$entity->setOpen(false);
}))
->setFile($exception->getFile())
->setStartLine($exception->getLine())
->setStackTrace(Trace::fromException($exception));

$reflection = new \ReflectionObject($exception);
$inspector->inspectClassMembers($exception, $reflection, $entity, [
'code', 'previous', 'message', 'file', 'line', 'trace'
]);
}


/**
* Inspect Closure
*/
Expand Down Expand Up @@ -53,6 +76,6 @@ public static function inspectIncompleteClass(\__PHP_Incomplete_Class $class, En
$vars = (array)$class;
$entity->setDefinition($vars['__PHP_Incomplete_Class_Name']);
unset($vars['__PHP_Incomplete_Class_Name']);
$entity->setValues($inspector->inspectValues($vars));
$entity->setValues($inspector->inspectList($vars));
}
}
File renamed without changes.
Loading

0 comments on commit 4cdf87e

Please sign in to comment.