Skip to content

Commit

Permalink
Add PHP 8 support (#7)
Browse files Browse the repository at this point in the history
* Add PHP 8 support
  • Loading branch information
daniel-valchev-paysera authored Mar 9, 2023
1 parent 5f2e2e4 commit f7fdd21
Show file tree
Hide file tree
Showing 23 changed files with 236 additions and 77 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ phpunit.xml
composer.lock
bin/
var/
.phpunit.result.cache
9 changes: 0 additions & 9 deletions .php_cs

This file was deleted.

15 changes: 13 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,26 @@ matrix:
- php: 7.3
env: COMPOSER_ARGS=""
- php: 7.4
env: COMPOSER_ARGS="" WITH_CS="true"
env: COMPOSER_ARGS=""
- php: 8.0
env: COMPOSER_ARGS=""
- php: 8.1
env: COMPOSER_ARGS=""
- php: 8.2
env: COMPOSER_ARGS=""

- php: 7.2
env: COMPOSER_ARGS="--prefer-lowest"
- php: 7.3
env: COMPOSER_ARGS="--prefer-lowest"
- php: 7.4
env: COMPOSER_ARGS="--prefer-lowest"
- php: 8.0
env: COMPOSER_ARGS="--prefer-lowest"
- php: 8.1
env: COMPOSER_ARGS="--prefer-lowest"
- php: 8.2
env: COMPOSER_ARGS="--prefer-lowest"

cache:
directories:
Expand All @@ -35,4 +47,3 @@ before_script:

script:
- bin/phpunit
- if [[ "$WITH_CS" == "true" ]]; then bin/paysera-php-cs-fixer fix -v --config=.php_cs --dry-run --stop-on-violation --path-mode=intersection "${COMMIT_SCA_FILES[@]}"; fi
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.0.0] - 2023-03-09
### Added
- PHP 8 support

## [1.0.2] - 2022-08-30
### Fixed
- Fixing deprecation error in symfony versions above 4.2
Expand Down
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ monolog:
channels: ["!event", "!doctrine"]
sentry:
type: service
id: Sentry\Monolog\Handler

id: paysera_logging_extra.sentry_handler
graylog_fingers_crossed:
type: fingers_crossed
action_level: error
Expand All @@ -89,18 +88,15 @@ monolog:
sentry:
dsn: '%env(SENTRY_DSN)%'
register_error_listener: false
monolog:
error_handler:
enabled: true
level: error
tracing:
enabled: false # If using self-hosted Sentry version < v20.6.0
options:
environment: '%kernel.environment%'
release: '%env(VERSION)%' # your app version, optional
send_attempts: 1

paysera_logging_extra:
application_name: app-something # customise this to know which project message was sent from

```
## Usage
Expand Down Expand Up @@ -141,11 +137,6 @@ composer test
Feel free to create issues and give pull requests.
You can fix any code style issues using this command:
```
composer fix-cs
```

### Running dependencies locally
```
Expand Down
75 changes: 75 additions & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Upgrade from 1.x to 2.0
The `sentry/sentry-symfony` package is updated to ^4.0 which requires changes in the configuration if you use the recommended configuration:

- Remove `sentry.monolog` configuration option

Before:
```yaml
sentry:
dsn: '%env(SENTRY_DSN)%'
register_error_listener: false
monolog:
error_handler:
enabled: true
level: error
options:
environment: '%kernel.environment%'
release: '%env(VERSION)%' # your app version, optional
send_attempts: 1
```
After:
```yaml
sentry:
dsn: '%env(SENTRY_DSN)%'
register_error_listener: false
options:
environment: '%kernel.environment%'
release: '%env(VERSION)%' # your app version, optional
send_attempts: 1
```
- Update the sentry monolog handler to `paysera_logging_extra.sentry_handler`

Before:
```yaml
monolog:
handlers:
...
sentry:
type: service
id: Sentry\Monolog\Handler
...
```

After:
```yaml
monolog:
handlers:
...
sentry:
type: service
id: paysera_logging_extra.sentry_handler
...
```

- Due to a bug in all versions below 6.0 of the SensioFrameworkExtraBundle bundle, you will likely receive an error during the building of symfony container related to the missing `Nyholm\Psr7\Factory\Psr17Factory` class. To workaround the issue, if you are not using the PSR-7 bridge, please change the configuration of that bundle as follows:

```yaml
sensio_framework_extra:
psr_message:
enabled: false
```

For more details about the issue see https://github.com/sensiolabs/SensioFrameworkExtraBundle/pull/710.

- The version of `sentry/sentry` was upgraded to ^3.0. If you're using self-hosted Sentry version < v20.6.0 then you should disable the tracing as it uses the envelope endpoint which requires Sentry version >= v20.6.0 to work.

```yaml
sentry:
tracing:
enabled: false
```

Check the [UPGRADE-3.0.md](https://github.com/getsentry/sentry-php/blob/master/UPGRADE-3.0.md) file of `sentry/sentry` for other notable updates.
Check the [UPGRADE-4.0.md](https://github.com/getsentry/sentry-symfony/blob/4.6.0/UPGRADE-4.0.md) file of `sentry/sentry-symfony` for other notable updates.
25 changes: 13 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,32 @@
}
},
"require": {
"php": ">=7.2,<7.5",
"php": "^7.2 || ^8.0",
"ext-json": "*",
"graylog2/gelf-php": "^1.4.2",
"monolog/monolog": "^1.24 || ^2.0",
"sentry/sentry-symfony": "^3.0",
"sentry/sdk": "^3.1",
"sentry/sentry": "^3.1",
"sentry/sentry-symfony": "^4.0",
"symfony/config": "^3.4|^4.0",
"symfony/dependency-injection": "^3.4|^4.0",
"symfony/framework-bundle": "^3.4.26|^4.2.7",
"symfony/http-kernel": "^3.4|^4.0",
"symfony/monolog-bundle": "^3.4"
},
"require-dev": {
"doctrine/doctrine-bundle": "^1.4",
"doctrine/orm": "^2.5.14",
"doctrine/persistence": "^1.3",
"mockery/mockery": "^1.2",
"paysera/lib-php-cs-fixer-config": "^2.3",
"phpunit/phpunit": "^6.5",
"symfony/yaml": "^3.4.34|^4.3"
"doctrine/annotations": "^1.14",
"doctrine/doctrine-bundle": "~2.6.0",
"doctrine/orm": "^2.10",
"phpunit/phpunit": "^8.5",
"symfony/yaml": "^4.3"
},
"config": {
"bin-dir": "bin",
"sort-packages": true
},
"scripts": {
"phpunit": "phpunit",
"fix-cs": "paysera-php-cs-fixer fix",
"test-cs": "paysera-php-cs-fixer fix --dry-run -v",
"test": ["@phpunit", "@test-cs"]
"test": ["@phpunit"]
}
}
7 changes: 1 addition & 6 deletions example/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ monolog:
channels: ["!event", "!doctrine"]
sentry:
type: service
id: Sentry\Monolog\Handler

id: paysera_logging_extra.sentry_handler
graylog_fingers_crossed:
type: fingers_crossed
action_level: error
Expand All @@ -42,10 +41,6 @@ monolog:
sentry:
dsn: '%env(SENTRY_DSN)%'
register_error_listener: false
monolog:
error_handler:
enabled: true
level: error
options:
environment: '%kernel.environment%'
release: 'v123'
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "vendor/autoload.php" >

<testsuites>
Expand Down
3 changes: 1 addition & 2 deletions src/Listener/IterationEndListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Paysera\LoggingExtraBundle\Service\CorrelationIdProvider;
use Sentry\ClientInterface;
use Sentry\FlushableClientInterface;

/**
* Intended for cases where the same process is reused for separate job or request processing, like in PHPPM.
Expand All @@ -24,7 +23,7 @@ public function __construct(
ClientInterface $sentryClient = null
) {
$this->correlationIdProvider = $correlationIdProvider;
$this->sentryClient = $sentryClient instanceof FlushableClientInterface ? $sentryClient : null;
$this->sentryClient = $sentryClient instanceof ClientInterface ? $sentryClient : null;
}

public function afterIteration()
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
class="Paysera\LoggingExtraBundle\Service\CorrelationIdProvider">
<argument>%paysera_logging_extra.application_name%</argument>
</service>

<service id="paysera_logging_extra.sentry_handler" alias="paysera_logging_extra.handler.sentry"/>
</services>
</container>
18 changes: 18 additions & 0 deletions src/Resources/config/services/handlers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="paysera_logging_extra.handler.sentry"
class="Paysera\LoggingExtraBundle\Service\Handler\SentryExtraInformationHandler">
<argument type="service">
<service class="Sentry\Monolog\Handler">
<argument type="service" id="Sentry\State\HubInterface"/>
<argument type="constant">Monolog\Logger::ERROR</argument>
</service>
</argument>
</service>
</services>
</container>
45 changes: 45 additions & 0 deletions src/Service/Handler/SentryExtraInformationHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Paysera\LoggingExtraBundle\Service\Handler;

use Monolog\Handler\HandlerWrapper;
use Monolog\Handler\ProcessableHandlerTrait;
use Sentry\State\Scope;
use function Sentry\withScope;

final class SentryExtraInformationHandler extends HandlerWrapper
{
use ProcessableHandlerTrait;

public function handle(array $record): bool
{
if (!$this->isHandling($record)) {
return false;
}

$result = false;
$record = $this->processRecord($record);

$record['formatted'] = $this->getFormatter()->format($record);

withScope(function (Scope $scope) use ($record, &$result): void {
if (isset($record['context']['extra']) && \is_array($record['context']['extra'])) {
foreach ($record['context']['extra'] as $key => $value) {
$scope->setExtra((string) $key, $value);
}
}

if (isset($record['context']['tags']) && \is_array($record['context']['tags'])) {
foreach ($record['context']['tags'] as $key => $value) {
$scope->setTag($key, $value);
}
}

$result = $this->handler->handle($record);
});

return $result;
}
}
3 changes: 2 additions & 1 deletion src/Service/Processor/GroupExceptionsProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Monolog\Processor\ProcessorInterface;
use Sentry\SentryBundle\SentryBundle;
use Sentry\SentrySdk;
use Sentry\State\Scope;

/**
Expand All @@ -30,7 +31,7 @@ public function __invoke(array $record)
$exceptionClass = get_class($exception);

if (isset($this->exceptionsClassesToGroup[$exceptionClass])) {
SentryBundle::getCurrentHub()
SentrySdk::getCurrentHub()
->configureScope(function (Scope $scope) use ($exceptionClass) {
$scope->setFingerprint([$exceptionClass]);
})
Expand Down
6 changes: 3 additions & 3 deletions tests/Functional/Fixtures/Handler/TestSentryTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Sentry\Transport\ClosableTransportInterface;
use Sentry\Transport\TransportInterface;

class TestSentryTransport implements TransportInterface, ClosableTransportInterface
class TestSentryTransport implements TransportInterface
{
private $pendingEvents;
private $events;
Expand All @@ -23,10 +23,10 @@ public function __construct()
$this->id = 0;
}

public function send(Event $event): ?string
public function send(Event $event): PromiseInterface
{
$this->pendingEvents[] = $event;
return (string)$this->id++;
return new FulfilledPromise($this->id++);
}

public function close(?int $timeout = null): PromiseInterface
Expand Down
Loading

0 comments on commit f7fdd21

Please sign in to comment.