Skip to content

Commit

Permalink
Merge pull request #1 from codex-team/dev-master
Browse files Browse the repository at this point in the history
First version release: Hawk Symfony integration library
  • Loading branch information
pavelzotikov authored Sep 18, 2024
2 parents d733752 + 8a446d3 commit c68f878
Show file tree
Hide file tree
Showing 13 changed files with 565 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@

# Embedded web-server pid file
/.web-server-pid

# PhpStorm
/.idea

###> friendsofphp/php-cs-fixer ###
/.php-cs-fixer.cache
###< friendsofphp/php-cs-fixer ###
63 changes: 63 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

return (new PhpCsFixer\Config())
->setUsingCache(true)
->setCacheFile(__DIR__ . '/.php-cs-fixer.cache')
->setRules([
'@PSR2' => true,
'align_multiline_comment' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => true,
'blank_line_before_statement' => ['statements' => ['return']],
'cast_spaces' => true,
'concat_space' => ['spacing' => 'one'],
'constant_case' => true,
'declare_equal_normalize' => true,
'class_attributes_separation' => ['elements' => ['method' => 'one']],
'lowercase_cast' => true,
'method_argument_space' => true,
'no_empty_statement' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_multiline_whitespace_around_double_arrow' => true,
'no_unused_imports' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'multiline_whitespace_before_semicolons' => false,
'ordered_imports' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_align' => true,
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_order' => true,
'phpdoc_return_self_reference' => true,
'phpdoc_scalar' => true,
'phpdoc_separation' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_trim' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_types_order' => true,
'phpdoc_var_annotation_correct_order' => true,
'phpdoc_var_without_name' => true,
'single_quote' => true,
'short_scalar_cast' => true,
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
'ternary_to_null_coalescing' => true,
'trim_array_spaces' => true,
'visibility_required' => ['elements' => ['property', 'method', 'const']],
'yoda_style' => false,
])
->setFinder(
PhpCsFixer\Finder::create()
->in([__DIR__ . '/src'])
->append([__FILE__])
->exclude([
'tests',
'vendor',
'var'
])
);
111 changes: 109 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,109 @@
# hawk.symfony
Symfony errors Catcher module for Hawk.so
# Hawk Symfony

Symfony errors Catcher for [Hawk.so](https://hawk.so).

## Setup

1. [Register](https://garage.hawk.so/sign-up) an account, create a Project and get an Integration Token.

2. Install SDK via [composer](https://getcomposer.org) to install the Catcher

Catcher provides support for PHP 7.2 or later

```bash
$ composer require codex-team/hawk.symfony
```

### Configuration

Add the following authorization information to your `.env` file:

```env
HAWK_TOKEN=<your_token_from_the_control_panel>
```

Create a configuration file at `config/packages/hawk.yaml` with the following content:

```php
HawkBundle\HawkBundle::class => ['all' => true]
```

In the `config/packages/monolog.yaml` file, specify the handler settings under the appropriate section (`dev` or `prod`):

```yaml
hawk:
type: service
id: HawkBundle\Monolog\Handler
level: error
```
### Adding User Information to Error Reports:
```php
$this->catcher->setUser([
'name' => 'user name',
'photo' => 'user photo',
]);

$this->catcher->setContext([
// Additional context information
]);
```

### Sending Exceptions Manually:
To manually send exceptions, initialize `__construct(\HawkBundle\Catcher $catcher)` class via dependency injection (DI), and use the following method:

```php
$this->catcher->sendException($exception);
```

### Sending Custom Messages:

You can also send custom messages using the `->sendMessage(...)` method:

```php
$this->catcher->sendMessage(
'your message',
[
// Additional context information
]
);
```

### Example: Sending Manually

```php
private $catcher;

public function __construct(\HawkBundle\Catcher $catcher)
{
$this->catcher = $catcher;
}

public function test()
{
try {
// The code where you need to catch the error
} catch (\Exception $exception) {
$this->catcher->sendException($exception);
}
}
```

## Issues and improvements

Feel free to ask questions or improve the project.

## Links

Repository: https://github.com/codex-team/hawk.symfony

Report a bug: https://github.com/codex-team/hawk.symfony/issues

Composer Package: https://packagist.org/packages/codex-team/hawk.symfony

CodeX Team: https://codex.so

## License

MIT
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "codex-team/hawk.symfony",
"description": "Symfony errors Catcher module for Hawk.so",
"keywords": ["hawk", "php", "error", "catcher", "monolog", "symfony"],
"type": "library",
"version": "0.0.1",
"license": "MIT",
"require": {
"php": "^7.2 || ^8.0",
"monolog/monolog": "^2.2 || ^3.0",
"guzzlehttp/guzzle": "^6.0 || ^7.0",
"codex-team/hawk.php": "^2.2.2",
"jean85/pretty-package-versions": "^1.5 || ^2.0",
"symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0",
"symfony/dependency-injection": "^4.4.20||^5.0.11||^6.0||^7.0",
"symfony/http-kernel": "^4.4.20||^5.0.11||^6.0||^7.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.19||^3.40"
},
"autoload": {
"psr-4": {
"HawkBundle\\": "src/"
}
},
"extra": {
"symfony": {
"autoload": true
}
},
"scripts": {
"csfix": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --using-cache=no --verbose"
}
}
5 changes: 5 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<psalm>
<projectFiles>
<directory name="src" />
</projectFiles>
</psalm>
110 changes: 110 additions & 0 deletions src/Catcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace HawkBundle;

use Hawk\Addons\Headers;
use Hawk\EventPayloadBuilder;
use Hawk\Handler;
use Hawk\Options;
use Hawk\Serializer;
use Hawk\StacktraceFrameBuilder;
use Hawk\Transport\TransportInterface;

/**
* Hawk PHP Catcher SDK
*
* @copyright CodeX
*
* @see https://hawk.so/docs#add-server-handler
*/
final class Catcher
{
/**
* SDK handler: contains methods that catchs errors and exceptions
*
* @var Handler
*/
private $handler;

/**
* @param array $options
* @param TransportInterface $transport
*/
public function __construct(array $options, TransportInterface $transport)
{
$options = new Options($options);

/**
* Init stacktrace frames builder and inject serializer
*/
$serializer = new Serializer();
$stacktraceBuilder = new StacktraceFrameBuilder($serializer);

/**
* Prepare Event payload builder
*/
$builder = new EventPayloadBuilder($stacktraceBuilder);
$builder->registerAddon(new Headers());

$this->handler = new Handler($options, $transport, $builder);

$this->handler->registerErrorHandler();
$this->handler->registerExceptionHandler();
$this->handler->registerFatalHandler();
}

/**
* @param array $user
*
* @return $this
*/
public function setUser(array $user): self
{
$this->handler->setUser($user);

return $this;
}

/**
* @param array $context
*
* @return $this
*/
public function setContext(array $context): self
{
$this->handler->setContext($context);

return $this;
}

/**
* @param string $message
* @param array $context
*/
public function sendMessage(string $message, array $context = []): void
{
$this->handler->sendEvent([
'title' => $message,
'context' => $context
]);
}

/**
* @param \Throwable $throwable
* @param array $context
*/
public function sendException(\Throwable $throwable, array $context = [])
{
$this->handler->handleException($throwable, $context);
}

/**
* @param array $payload
*/
public function sendEvent(array $payload): void
{
$this->handler->sendEvent($payload);
}
}
29 changes: 29 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace HawkBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('hawk');

$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
->scalarNode('integration_token')
->isRequired()
->cannotBeEmpty()
->end()
->end()
;

return $treeBuilder;
}
}
Loading

0 comments on commit c68f878

Please sign in to comment.