Skip to content

Commit

Permalink
feat: symfony 7 support from ekino#289
Browse files Browse the repository at this point in the history
  • Loading branch information
spacebluebox committed Nov 12, 2024
1 parent 13a4508 commit 3abd922
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 165 deletions.
35 changes: 15 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,48 @@ jobs:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
- uses: actions/checkout@v4
- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga

build:
name: Build
runs-on: Ubuntu-20.04
runs-on: Ubuntu-22.04
strategy:
fail-fast: false
matrix:
php: ['7.1', '7.2', '7.3', '7.4', '8.0']
php: ['8.1', '8.2', '8.3']
composer: ['']
phpunit: ['']
deprecation: ['']
symfony: ['']
stability: ['']
include:
# Minimum supported dependencies with the latest and oldest PHP version
- php: 8.0
composer: --prefer-stable --prefer-lowest
deprecation: max[direct]=0
- php: 7.1
- php: 8.1
composer: --prefer-stable --prefer-lowest
deprecation: max[direct]=0

# symfony version
- php: 8.0
symfony: '^3.0'
- php: 8.0
symfony: '^4.0'
- php: 8.0
symfony: '^5.0'
- php: 8.3
symfony: '^6.4'
- php: 8.3
symfony: '^7.0'

# dev
- php: 8.0
- php: 8.3
stability: 'dev'

steps:
- name: Set up PHP
uses: shivammathur/setup-php@2.7.0
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
tools: flex

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup stability
if: matrix.stability != ''
Expand All @@ -79,10 +74,10 @@ jobs:
- name: Validate
run: |
composer validate --strict --no-check-lock
composer validate --strict --no-check-lock
- name: Run tests
env:
SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT: 1
run: |
${{ matrix.phpunit }} ./vendor/bin/simple-phpunit
${{ matrix.phpunit }} ./vendor/bin/simple-phpunit
6 changes: 2 additions & 4 deletions Command/NotifyDeploymentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ class NotifyDeploymentCommand extends Command
public const EXIT_UNAUTHORIZED = 2;
public const EXIT_HTTP_ERROR = 3;

protected static $defaultName = 'newrelic:notify-deployment';

private $newrelic;

public function __construct(Config $newrelic)
{
$this->newrelic = $newrelic;

parent::__construct();
parent::__construct('newrelic:notify-deployment');
}

protected function configure(): void
Expand Down Expand Up @@ -171,4 +169,4 @@ private function createPayload(string $appName, InputInterface $input): string

return http_build_query($content_array);
}
}
}
8 changes: 4 additions & 4 deletions EkinoNewRelicBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

class EkinoNewRelicBundle extends Bundle
{
public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
parent::build($container);

$container->addCompilerPass(new MonologHandlerPass());
}

public function boot()
public function boot(): void
{
parent::boot();

Expand All @@ -36,12 +36,12 @@ public function boot()
}
}

public function shutdown()
public function shutdown(): void
{
if ($this->container->has(DeprecationListener::class)) {
$this->container->get(DeprecationListener::class)->unregister();
}

parent::shutdown();
}
}
}
11 changes: 3 additions & 8 deletions Listener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelEvents;

Expand Down Expand Up @@ -44,17 +43,13 @@ public static function getSubscribedEvents(): array
*/
public function onKernelException(KernelExceptionEvent $event): void
{
$exception = method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->getException();
$exception = $event->getThrowable();
if (!$exception instanceof HttpExceptionInterface) {
$this->interactor->noticeThrowable($exception);
}
}
}

if (!class_exists(KernelExceptionEvent::class)) {
if (class_exists(ExceptionEvent::class)) {
class_alias(ExceptionEvent::class, KernelExceptionEvent::class);
} else {
class_alias(GetResponseForExceptionEvent::class, KernelExceptionEvent::class);
}
}
class_alias(ExceptionEvent::class, KernelExceptionEvent::class);
}
17 changes: 6 additions & 11 deletions Listener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
use Ekino\NewRelicBundle\TransactionNamingStrategy\TransactionNamingStrategyInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
Expand Down Expand Up @@ -51,9 +50,9 @@ public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => [
['setApplicationName', 255],
['setIgnoreTransaction', 31],
['setTransactionName', -10],
['setApplicationName', 255],
['setIgnoreTransaction', 31],
['setTransactionName', -10],
],
];
}
Expand Down Expand Up @@ -112,14 +111,10 @@ public function setIgnoreTransaction(KernelRequestEvent $event): void
*/
private function isEventValid(KernelRequestEvent $event): bool
{
return HttpKernelInterface::MASTER_REQUEST === $event->getRequestType();
return HttpKernelInterface::MAIN_REQUEST === $event->getRequestType();
}
}

if (!class_exists(KernelRequestEvent::class)) {
if (class_exists(RequestEvent::class)) {
class_alias(RequestEvent::class, KernelRequestEvent::class);
} else {
class_alias(GetResponseEvent::class, KernelRequestEvent::class);
}
}
class_alias(RequestEvent::class, KernelRequestEvent::class);
}
32 changes: 8 additions & 24 deletions Listener/ResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,18 @@
use Ekino\NewRelicBundle\Twig\NewRelicExtension;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class ResponseListener implements EventSubscriberInterface
{
private $newRelic;
private $interactor;
private $instrument;
private $symfonyCache;
private $newRelicTwigExtension;

public function __construct(
Config $newRelic,
NewRelicInteractorInterface $interactor,
bool $instrument = false,
bool $symfonyCache = false,
NewRelicExtension $newRelicTwigExtension = null
private Config $newRelic,
private NewRelicInteractorInterface $interactor,
private bool $instrument = false,
private bool $symfonyCache = false,
private ?NewRelicExtension $newRelicTwigExtension = null,
) {
$this->newRelic = $newRelic;
$this->interactor = $interactor;
$this->instrument = $instrument;
$this->symfonyCache = $symfonyCache;
$this->newRelicTwigExtension = $newRelicTwigExtension;
}

public static function getSubscribedEvents(): array
Expand All @@ -55,7 +43,7 @@ public static function getSubscribedEvents(): array

public function onKernelResponse(KernelResponseEvent $event): void
{
$isMainRequest = method_exists($event, 'isMainRequest') ? $event->isMainRequest() : $event->isMasterRequest();
$isMainRequest = $event->isMainRequest();

if (!$isMainRequest) {
return;
Expand Down Expand Up @@ -113,9 +101,5 @@ public function onKernelResponse(KernelResponseEvent $event): void
}

if (!class_exists(KernelResponseEvent::class)) {
if (class_exists(ResponseEvent::class)) {
class_alias(ResponseEvent::class, KernelResponseEvent::class);
} else {
class_alias(FilterResponseEvent::class, KernelResponseEvent::class);
}
}
class_alias(ResponseEvent::class, KernelResponseEvent::class);
}
32 changes: 15 additions & 17 deletions Tests/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function registerBundles(): iterable
* (From MicroKernelTrait)
* {@inheritdoc}
*/
public function registerContainerConfiguration(LoaderInterface $loader)
public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', [
Expand All @@ -100,21 +100,19 @@ public function registerContainerConfiguration(LoaderInterface $loader)
],
]);

// Not setting the router to utf8 is deprecated in symfony 5.1
if (Kernel::VERSION_ID >= 50100) {
// Fix deprecations in symfony 6
if (Kernel::VERSION_ID >= 60100) {
$container->loadFromExtension('framework', [
'router' => ['utf8' => true],
]);
}

// Not setting the "framework.session.storage_factory_id" configuration option is deprecated in symfony 5.3
if (Kernel::VERSION_ID >= 50300) {
$container->loadFromExtension('framework', [
'session' => ['storage_factory_id' => 'session.storage.factory.mock_file'],
]);
} else {
$container->loadFromExtension('framework', [
'session' => ['storage_id' => 'session.storage.mock_file'],
'http_method_override' => false,
'handle_all_throwables' => true,
'session' => [
'cookie_secure' => 'auto',
'cookie_samesite' => 'lax',
'handler_id' => null,
],
'php_errors' => [
'log' => true,
],
]);
}

Expand All @@ -140,7 +138,7 @@ protected function buildContainer(): ContainerBuilder
$container = parent::buildContainer();

$container->addCompilerPass(new class() implements CompilerPassInterface {
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
foreach ($container->getDefinitions() as $id => $definition) {
if (preg_match('|Ekino.*|i', $id)) {
Expand All @@ -158,4 +156,4 @@ public function process(ContainerBuilder $container)

return $container;
}
}
}
4 changes: 2 additions & 2 deletions Tests/BundleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
*/
class BundleInitializationTest extends TestCase
{
protected function getBundleClass()
protected function getBundleClass(): string
{
return EkinoNewRelicBundle::class;
}

public function testInitBundle()
public function testInitBundle(): void
{
$kernel = new AppKernel(uniqid('cache'));
$kernel->boot();
Expand Down
8 changes: 4 additions & 4 deletions Tests/Listener/CommandListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class CommandListenerTest extends TestCase
{
public function testCommandMarkedAsBackgroundJob()
public function testCommandMarkedAsBackgroundJob(): void
{
if (!class_exists('Symfony\Component\Console\Event\ConsoleCommandEvent')) {
$this->markTestSkipped('Console Events is only available from Symfony 2.3');
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testCommandMarkedAsBackgroundJob()
$listener->onConsoleCommand($event);
}

public function testIgnoreBackgroundJob()
public function testIgnoreBackgroundJob(): void
{
$interactor = $this->getMockBuilder(NewRelicInteractorInterface::class)->getMock();
$interactor->expects($this->never())->method('startTransaction');
Expand All @@ -84,7 +84,7 @@ public function testIgnoreBackgroundJob()
$listener->onConsoleCommand($event);
}

public function testConsoleError()
public function testConsoleError(): void
{
$exception = new \Exception('', 1);

Expand All @@ -103,7 +103,7 @@ public function testConsoleError()
$listener->onConsoleError($event);
}

public function testConsoleErrorsWithThrowable()
public function testConsoleErrorsWithThrowable(): void
{
$exception = new \Error();

Expand Down
Loading

0 comments on commit 3abd922

Please sign in to comment.