Skip to content

Commit

Permalink
Update SF
Browse files Browse the repository at this point in the history
  • Loading branch information
GDXbsv committed Jan 16, 2025
1 parent e1f5bc4 commit b77ca25
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<projectFiles>
<directory name="src"/>
<ignoreFiles>
<directory name="src/Symfony/DependencyInjection"/>
</ignoreFiles>
</projectFiles>

Expand Down
27 changes: 21 additions & 6 deletions src/Symfony/DependencyInjection/Compiler/ExceptionContextPass.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Gotphoto\Logging\Symfony\DependencyInjection\Compiler;

use Gotphoto\Logging\Formatter;
use Gotphoto\Logging\LogstashFormatter;
use Gotphoto\Logging\OtelFormatter;
use ReflectionClass;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand All @@ -19,21 +20,35 @@ public function process(ContainerBuilder $container)
foreach ($container->findTaggedServiceIds('gotphoto_logging.exception_context') as $id => $_tags) {
$definition = $container->getDefinition($id);
$className = $definition->getClass();
/** @psalm-suppress ArgumentTypeCoercion, PossiblyNullArgument */
$reflectionClass = new ReflectionClass($className);
if (!$reflectionClass->hasMethod('__invoke')) {
/** @psalm-suppress PossiblyNullOperand */
throw new \Exception($definition->getClass() . ' has to have __invoke method.');
}
$reflectionMethod = $reflectionClass->getMethod('__invoke');
$typehintClassName = $reflectionMethod->getParameters()[0]->getClass()->getName();

/** @psalm-suppress UndefinedMethod, PossiblyNullReference, PossiblyUndefinedIntArrayOffset */
$typehintClassName = $reflectionMethod->getParameters()[0]->getType()->getName();
/** @psalm-suppress MixedArgument */
if (!is_subclass_of($typehintClassName, Throwable::class)) {
throw new \Exception($definition->getClass() . ' has to have __invoke method with argument "is_subclass_of Throwable".');
/** @psalm-suppress PossiblyNullOperand */
throw new \Exception(
$definition->getClass() . ' has to have __invoke method with argument "is_subclass_of Throwable".',
);
}

$exceptionContextMap[$typehintClassName][] = new Reference($id);
}

$container->getDefinition(Formatter::class)->setArgument('$exceptionContextProviderMap', $exceptionContextMap);
$container->getDefinition(LogstashFormatter::class)->setArgument(
'$exceptionContextProviderMap',
$exceptionContextMap,
);

$container->getDefinition(OtelFormatter::class)->setArgument('$exceptionContextProviderMap', $exceptionContextMap);
$container->getDefinition(OtelFormatter::class)->setArgument(
'$exceptionContextProviderMap',
$exceptionContextMap,
);
}
}
3 changes: 2 additions & 1 deletion src/Symfony/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

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

$rootNode = $treeBuilder->getRootNode();
/** @psalm-suppress MixedMethodCall, UndefinedMethod */
$rootNode
->children()
->scalarNode('app_name')
Expand Down
11 changes: 7 additions & 4 deletions src/Symfony/DependencyInjection/SymfonyLoggingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Gotphoto\Logging\Symfony\DependencyInjection;

use Gotphoto\Logging\ExceptionContext\ExceptionContext;
use Gotphoto\Logging\Formatter;
use Gotphoto\Logging\LogstashFormatter;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -21,18 +21,21 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
{
$loader = new PhpFileLoader(
$container,
new FileLocator(__DIR__.'/../config')
new FileLocator(__DIR__ . '/../config'),
);
$loader->load('services.php');
$env = $container->getParameter('kernel.environment');
try {
/** @psalm-suppress PossiblyInvalidCast */
$loader->load("services_{$env}.php");
} catch (FileLocatorFileNotFoundException $e) {
//ignore if no file for env
}

$container->getDefinition(Formatter::class)
->setArgument('$applicationName', $configs['app_name'])
/** @psalm-suppress PossiblyUndefinedStringArrayOffset */
$container
->getDefinition(LogstashFormatter::class)
->setArgument('$applicationName', $mergedConfig['app_name'])
->setArgument('$environment', $env);

$container
Expand Down

0 comments on commit b77ca25

Please sign in to comment.