Permalink
Browse files

Initial import

  • Loading branch information...
victorstanciu committed Sep 26, 2018
0 parents commit 2d61928910c5c26da614397e9279060e753475bd
Showing with 8,039 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +112 −0 README.md
  3. +32 −0 composer.json
  4. +33 −0 phpunit.xml.dist
  5. +80 −0 src/Context/Breadcrumbs.php
  6. +53 −0 src/Context/Breadcrumbs/Breadcrumb.php
  7. +131 −0 src/Context/EventContext.php
  8. +68 −0 src/Context/User.php
  9. +101 −0 src/DebugRenderer/DebugCliRenderer.php
  10. +10 −0 src/DebugRenderer/DebugRenderer.php
  11. +251 −0 src/DebugRenderer/DebugTextRenderer.php
  12. +71 −0 src/DebugRenderer/DebugWebRenderer.php
  13. +100 −0 src/ErrorHandler.php
  14. +62 −0 src/Event.php
  15. +175 −0 src/EventHandler/DebugHandler.php
  16. +44 −0 src/EventHandler/EventHandler.php
  17. +10 −0 src/EventHandler/EventHandlerException.php
  18. +53 −0 src/Exception/ExceptionData.php
  19. +53 −0 src/Exception/ExceptionInspector.php
  20. +72 −0 src/Formatter/StackFrameCallFormatter.php
  21. +50 −0 src/Formatter/StackFrameCallHtmlFormatter.php
  22. +43 −0 src/Formatter/StackFrameCallTextFormatter.php
  23. +36 −0 src/Formatter/StackTraceTagFormatter.php
  24. +18 −0 src/Formatter/TextVarDumper.php
  25. +82 −0 src/Formatter/VarDumper.php
  26. +141 −0 src/Http/Request.php
  27. +60 −0 src/Level.php
  28. +195 −0 src/Resources/assets/scripts/main.js
  29. +46 −0 src/Resources/assets/scripts/prettify.js
  30. +163 −0 src/Resources/assets/stylesheets/bootstrap.css
  31. +39 −0 src/Resources/assets/stylesheets/fontello.css
  32. +444 −0 src/Resources/assets/stylesheets/main.css
  33. +277 −0 src/Resources/assets/stylesheets/themes/default.css
  34. +28 −0 src/Resources/views/index.php
  35. +28 −0 src/Resources/views/layout.php
  36. +111 −0 src/Resources/views/partials/context.php
  37. +19 −0 src/Resources/views/partials/context/breadcrumbs.php
  38. +21 −0 src/Resources/views/partials/context/table.php
  39. +21 −0 src/Resources/views/partials/context/user.php
  40. +29 −0 src/Resources/views/partials/frame.php
  41. +26 −0 src/Resources/views/partials/frame/arguments.php
  42. +53 −0 src/Resources/views/partials/stacktrace.php
  43. +60 −0 src/Serializer/ArgumentSerializer.php
  44. +58 −0 src/Serializer/HumanSerializer.php
  45. +102 −0 src/Serializer/Serializer.php
  46. +116 −0 src/SlashTrace.php
  47. +113 −0 src/StackTrace/StackFrame.php
  48. +56 −0 src/StackTrace/StackFrameContextExtractor.php
  49. +129 −0 src/StackTrace/StackTraceInspector.php
  50. +21 −0 src/System/CLImateOutput.php
  51. +22 −0 src/System/HasSystemProvider.php
  52. +8 −0 src/System/OutputReceiver.php
  53. +190 −0 src/System/System.php
  54. +64 −0 src/System/SystemProvider.php
  55. +45 −0 src/Template/PlatesTemplateEngine.php
  56. +48 −0 src/Template/ResourceLoader.php
  57. +13 −0 src/Template/TemplateEngine.php
  58. +112 −0 src/Template/TemplateHelper.php
  59. +86 −0 tests/Context/BreadcrumbsTest.php
  60. +81 −0 tests/Context/EventContextTest.php
  61. +28 −0 tests/Context/UserTest.php
  62. +100 −0 tests/DebugRenderer/DebugCliRendererTest.php
  63. +230 −0 tests/DebugRenderer/DebugTextRendererTest.php
  64. +64 −0 tests/DebugRenderer/DebugTextRendererTestCase.php
  65. +88 −0 tests/DebugRenderer/DebugWebRendererTest.php
  66. +93 −0 tests/Doubles/Context/MockRequest.php
  67. +8 −0 tests/Doubles/Locator/AbstractTestDummy.php
  68. +8 −0 tests/Doubles/Locator/TestDummy.php
  69. +8 −0 tests/Doubles/Locator/TestDummyInterface.php
  70. +23 −0 tests/Doubles/StackTrace/MaxDepthStackTraceInspector.php
  71. +34 −0 tests/Doubles/StackTrace/MockStackFrameContextExtractor.php
  72. +22 −0 tests/Doubles/StackTrace/MockStackTraceInspector.php
  73. +190 −0 tests/Doubles/System/MockSystemProvider.php
  74. +24 −0 tests/Doubles/System/OutputReceiverSpy.php
  75. +31 −0 tests/Doubles/Template/MockTemplateEngine.php
  76. +302 −0 tests/ErrorHandlerTest.php
  77. +228 −0 tests/EventHandler/DebugHandlerTest.php
  78. +62 −0 tests/Exception/ExceptionInspectorTest.php
  79. +30 −0 tests/Fixtures/BreadcrumbsFixture.php
  80. +50 −0 tests/Fixtures/Code/TestClass.php
  81. +7 −0 tests/Fixtures/Code/test_file.php
  82. +34 −0 tests/Fixtures/Code/test_functions.php
  83. +188 −0 tests/Fixtures/EventProvider.php
  84. +10 −0 tests/Fixtures/resources/lines.txt
  85. +36 −0 tests/Formatter/StackFrameCallFormatterTestCase.php
  86. +123 −0 tests/Formatter/StackFrameCallHtmlFormatterTest.php
  87. +86 −0 tests/Formatter/StackFrameCallTextFormatterTest.php
  88. +40 −0 tests/Formatter/StackTraceTagFormatterTest.php
  89. +36 −0 tests/Http/RequestTest.php
  90. +79 −0 tests/Serializer/ArgumentSerializerTest.php
  91. +85 −0 tests/Serializer/HumanSerializerTest.php
  92. +165 −0 tests/Serializer/SerializerTest.php
  93. +245 −0 tests/SlashTraceTest.php
  94. +84 −0 tests/StackTrace/StackFrameContextExtractorTest.php
  95. +48 −0 tests/StackTrace/StackFrameTest.php
  96. +213 −0 tests/StackTrace/StackTraceInspectorTest.php
  97. +136 −0 tests/Template/TemplateHelperTest.php
  98. +7 −0 tests/TestCase.php
  99. +13 −0 tests/bootstrap.php
  100. +13 −0 tests/render.php
@@ -0,0 +1,3 @@
.idea
vendor/
composer.lock
112 README.md
@@ -0,0 +1,112 @@
# SlashTrace - Awesome error handler

[![Screenshot](https://i.imgur.com/pt4jlYX.png)](https://slashtrace.com/demo.php)

---

SlashTrace is, at its core, an error and exception handler. You hook it into your error handling routine (or let it set itself up to catch all errors and exceptions), and it captures and displays a lot of nice information about your errors. It does this for normal browser requests, but also for [AJAX](https://i.imgur.com/BnvCp4N.png) and the [CLI](https://i.imgur.com/GA7tS0T.png).

When you're done with local debugging, you can configure SlashTrace to send errors to dedicated error reporting services, like [Sentry](https://sentry.io/), [Raygun](https://raygun.com/), and [Bugsnag](https://www.bugsnag.com/).

## Usage

1. Install using Composer:

```
composer require slashtrace/slashtrace
```

2. Capture errors:

```PHP
use SlashTrace\SlashTrace;
use SlashTrace\EventHandler\DebugHandler;
$slashtrace = new SlashTrace();
$slashtrace->addHandler(new DebugHandler());
$slashtrace->register();
```

Alternatively, you can explicitly handle exceptions:

```PHP
try {
// Your code
} catch (Exception $exception) {
$slashtrace->handleException($exception);
}
```

## Handlers

SlashTrace comes bundled with the DebugHandler used in the example above, but you will usually want to set it up to send errors to an error tracking service when running in production. Currently, there are handlers implemented for the following providers, and more are on the way. Click each link to view the usage documentation:

- [Sentry](https://github.com/slashtrace/slashtrace-sentry)
- [Raygun](https://github.com/slashtrace/slashtrace-raygun)
- [Bugsnag](https://github.com/slashtrace/slashtrace-bugsnag)

## Capturing additional data

Besides the complex error information that SlashTrace captures out of the box, you can attach other types of data to each report. This is especially useful when using one of the external handlers above.

This way, SlashTrace acts like an abstraction layer between you and these providers, and normalizes the data that you provide into a single format. This helps you to avoid vendor lock-in and lets you switch error reporting providers simply by switching the handler.

### Capturing user data

If you want to attach information about the affected user, you can do so like this:

```PHP
use SlashTrace\Context\User;
$user = new User();
$user->setId(12345);
$user->setEmail('pfry@planetexpress.com');
$user->setName('Philip J. Fry');
$slashtrace->addUser($user);
```

Note that a user needs at least an ID or an email. The name is completely optional.

This feature corresponds to the respective implementations in each error tracker:

- [Sentry - Capturing the User](https://docs.sentry.io/learn/context/?platform=javascript#capturing-the-user)
- [Raygun - User Tracking](https://raygun.com/docs/workflow/user-tracking)
- [Bugsnag - Identifying users](https://docs.bugsnag.com/platforms/php/other/#identifying-users)


### Recording breadcrumbs

Sometimes a stack trace isn't enough to figure out what steps lead to an error. To this end, SlashTrace let's you record breadcrumbs during execution:

```PHP
$slashtrace->recordBreadcrumb("Router loaded");
$slashtrace->recordBreadcrumb("Matched route", [
"controller" => "orders",
"action" => "confirm",
]);
```

Relevant tracker docs:

- [Sentry - Breadcrumbs in PHP](https://blog.sentry.io/2016/05/27/php-breadcrumbs.html)
- Raygun - The current PHP SDK doesn't support breadcrumbs
- [Bugsnag - Logging breadcrumbs](https://docs.bugsnag.com/platforms/php/other/#logging-breadcrumbs)

### Tracking releases

Often, it's useful to know which release introduced a particular bug, or which release triggered a regression. Tagging events with a particular release or version number is very easy:

```PHP
$slashtrace->setRelease("1.0.0"); // <- Your version number, commit hash, etc.
```
Tracker docs:
- [Sentry - Releases](https://docs.sentry.io/learn/releases/?platform=javascript)
- [Raygun - Version numbers](https://raygun.com/docs/languages/php#php-version-number)
- Bugsnag - The release version cannot be explicitly set per event. Read the [Bugsnag docs](https://docs.bugsnag.com/platforms/php/other/#tracking-releases) for more details.
@@ -0,0 +1,32 @@
{
"name": "slashtrace/slashtrace",
"description": "SlashTrace - Awesome error handler",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Victor Stanciu",
"email": "vic.stanciu@gmail.com"
}
],
"require": {
"php": ">= 5.6",
"league/plates": "^3.3",
"symfony/var-dumper": "^3.2",
"league/climate": "^3.2"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-4": {
"SlashTrace\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"SlashTrace\\Tests\\": "tests/",
"SlashTrace\\Tests\\Fixtures\\Code\\": "tests/Fixtures/code/"
}
}
}
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php"
colors="true"
verbose="true"
processIsolation="false"
convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false">
<testsuites>
<testsuite>
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
<exclude>
<directory>./src/Resources</directory>

<file>./src/Context/User.php</file>
<file>./src/Context/Breadcrumbs/Breadcrumb.php</file>
<file>./src/Formatter/TextVarDumper.php</file>
<file>./src/Formatter/VarDumper.php</file>
<file>./src/Http/Request.php</file>
<file>./src/Level.php</file>
<file>./src/System/CLImateOutput.php</file>
<file>./src/System/System.php</file>
<file>./src/Template/PlatesTemplateEngine.php</file>
<file>./src/Template/ResourceLoader.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
@@ -0,0 +1,80 @@
<?php
namespace SlashTrace\Context;
use SlashTrace\Context\Breadcrumbs\Breadcrumb;
use SlashTrace\System\SystemProvider;
use DateTime;
class Breadcrumbs
{
const MAX_SIZE = 25;
/** @var SystemProvider */
private $system;
/** @var Breadcrumb[] */
private $crumbs = [];
public function __construct(SystemProvider $system)
{
$this->system = $system;
}
/**
* @param string $title
* @param array $data
*/
public function record($title, array $data = [])
{
$breadcrumb = new Breadcrumb($title, $data, $this->getDateTime());
if ($this->getSize() == self::MAX_SIZE) {
array_shift($this->crumbs);
}
$this->crumbs[] = $breadcrumb;
}
/**
* @return Breadcrumb[]
*/
public function getCrumbs()
{
return $this->crumbs;
}
/**
* @return DateTime
*/
protected function getDateTime()
{
return $this->system->getDateTime();
}
/**
* @return int
*/
public function getMaxSize()
{
return self::MAX_SIZE;
}
/**
* @return int
*/
private function getSize()
{
return count($this->crumbs);
}
public function isEmpty()
{
return $this->getSize() == 0;
}
public function clear()
{
$this->crumbs = [];
}
}
@@ -0,0 +1,53 @@
<?php
namespace SlashTrace\Context\Breadcrumbs;
use DateTime;
class Breadcrumb
{
/** @var string */
private $title;
/** @var array */
private $data = [];
/** @var DateTime */
private $dateTime;
/**
* @param string $title
* @param array $data
* @param DateTime $dateTime
*/
public function __construct($title, array $data, DateTime $dateTime)
{
$this->title = $title;
$this->dateTime = $dateTime;
$this->data = $data;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* @return DateTime
*/
public function getDateTime()
{
return $this->dateTime;
}
}
Oops, something went wrong.

0 comments on commit 2d61928

Please sign in to comment.