Skip to content

Commit

Permalink
Allow setting a profiler bag verbosity in bundle configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
MortalFlesh committed Apr 5, 2022
1 parent d5f2267 commit c7a1a44
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- There should always be "Unreleased" section at the beginning. -->

## Unreleased
- Allow setting a profiler bag verbosity in bundle configuration

## 1.2.0 - 2021-08-10
- Allow an `$initiator` in `ResponseDecoders` `supports` method
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ composer require lmc/cqrs-bundle

```yaml
lmc_cqrs:
profiler: false # Whether to enable profiler and allow to profile queries and commands [default false]
profiler: false # Whether to enable profiler and allow profiling queries and commands [default false]
debug: false # Whether to enable debug the CQRS by a console command [default false]

cache:
Expand All @@ -41,6 +41,14 @@ lmc_cqrs:
solr: false # Whether should solr extension be active (requires a lmc/cqrs-solr dependency) [default false]
```

### Profiler extended configuration
```yaml
lmc_cqrs:
profiler:
enabled: false # Whether to enable profiler and allow profiling queries and commands [default false]
verbosity: '' # Verbosity level (verbose or debug) for a profiler bag - empty string is a default for normal
```

**TIPs**:
- it is advised to set `profiler: '%kernel.debug%'` so it profiles (and registers all services for profiling) only when it is really used
- you can define `profiler` and `debug` in your `dev/lmc_cqrs.yaml` to only allow it in `dev` Symfony environment
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"php": "^7.4",
"ext-json": "*",
"ext-mbstring": "*",
"lmc/cqrs-handler": "^1.1",
"lmc/cqrs-types": "^2.0",
"lmc/cqrs-handler": "^1.3",
"lmc/cqrs-types": "^2.3",
"symfony/config": "^4.4 || ^5.1",
"symfony/console": "^4.4 || ^5.1",
"symfony/dependency-injection": "^4.4 || ^5.1",
Expand Down
17 changes: 15 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lmc\Cqrs\Bundle\DependencyInjection;

use Lmc\Cqrs\Handler\ProfilerBag;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -12,8 +13,20 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder('lmc_cqrs');
$treeBuilder->getRootNode()
->children()
->booleanNode('profiler')
->defaultFalse()
->arrayNode('profiler')
->beforeNormalization()
->ifTrue(fn ($v) => is_bool($v))
->then(fn (bool $v) => ['enabled' => $v])
->end()
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->defaultFalse()
->end()
->scalarNode('verbosity')
->defaultValue(ProfilerBag::VERBOSITY_NORMAL)
->end()
->end()
->end()
->booleanNode('debug')
->defaultFalse()
Expand Down
9 changes: 8 additions & 1 deletion src/DependencyInjection/LmcCqrsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lmc\Cqrs\Bundle\DependencyInjection;

use Lmc\Cqrs\Handler\ProfilerBag;
use Lmc\Cqrs\Solr\QueryBuilder\Applicator\ApplicatorInterface;
use Lmc\Cqrs\Types\Decoder\ResponseDecoderInterface;
use Lmc\Cqrs\Types\Formatter\ProfilerFormatterInterface;
Expand Down Expand Up @@ -82,8 +83,14 @@ private function setUpCache(array $config, ContainerBuilder $container): void

private function tryRegisterProfiler(array $config, ContainerBuilder $container, YamlFileLoader $loader): void
{
if ($config['profiler']) {
if ($config['profiler']['enabled']) {
$loader->load('services-profiler.yaml');

if ($container->has('lmc_cqrs.profiler_bag')) {
/** @var ProfilerBag $profilerBag */
$profilerBag = $container->get('lmc_cqrs.profiler_bag');
$profilerBag->setVerbosity($config['profiler']['verbosity']);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public function configurationDefinition(): void

$reference = <<<CONFIG
lmc_cqrs:
profiler:%w false
profiler:
enabled:%w false
verbosity:%w ''
debug:%w false
cache:
enabled:%w null
Expand Down
46 changes: 40 additions & 6 deletions tests/DependencyInjection/LmcCqrsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,11 @@ public function shouldSetUpAllServices(): void

/**
* @test
* @dataProvider provideProfilerConfigs
*/
public function shouldSetUpServicesForProfiler(): void
public function shouldSetUpServicesForProfiler(array $config): void
{
$configs = [
[
'profiler' => true,
],
];
$configs = [$config];

$this->extension->load($configs, $this->containerBuilder);

Expand All @@ -274,6 +271,15 @@ public function shouldSetUpServicesForProfiler(): void
$this->assertNoSolrExtensionSettings($this->containerBuilder);
}

public function provideProfilerConfigs(): array
{
return [
// config
'short' => [['profiler' => true]],
'full' => [['profiler' => ['enabled' => true]]],
];
}

private function assertProfilerSettings(ContainerBuilder $containerBuilder): void
{
$this->assertTrue($containerBuilder->has(CacheController::class));
Expand Down Expand Up @@ -306,6 +312,34 @@ private function assertProfilerSettings(ContainerBuilder $containerBuilder): voi
);
}

/**
* @test
*/
public function shouldSetUpServicesForProfilerWithVerbosity(): void
{
$configs = [
[
'profiler' => [
'enabled' => true,
'verbosity' => ProfilerBag::VERBOSITY_DEBUG,
],
],
];

$this->extension->load($configs, $this->containerBuilder);

$this->assertDefaultSettings($this->containerBuilder);
$this->assertNoCacheSettings($this->containerBuilder);
$this->assertProfilerSettings($this->containerBuilder);
$this->assertNoDebugSettings($this->containerBuilder);
$this->assertNoHttpExtensionSettings($this->containerBuilder);
$this->assertNoSolrExtensionSettings($this->containerBuilder);

$profilerBag = $this->containerBuilder->get('lmc_cqrs.profiler_bag');
$this->assertInstanceOf(ProfilerBag::class, $profilerBag);
$this->assertSame(ProfilerBag::VERBOSITY_DEBUG, $profilerBag->getVerbosity());
}

/**
* @test
*/
Expand Down

0 comments on commit c7a1a44

Please sign in to comment.