Skip to content

Commit

Permalink
feat: Web profiler bundle integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Feb 6, 2024
1 parent 0162a59 commit 35bf75e
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 54 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 @@
## 0.6.2

* feat: Data collector.
* feat: Web profiler bundle integration.

## 0.6.1

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"dave-liddament/php-language-extensions": "^0.6.0",
"dave-liddament/phpstan-php-language-extensions": "^0.5.0",
"brick/money": "^0.9.0",
"tomasvotruba/unused-public": "^0.3.5"
"tomasvotruba/unused-public": "^0.3.5",
"twig/twig": "^2.12|^3.0"
},
"autoload": {
"psr-4": {
Expand Down
36 changes: 8 additions & 28 deletions src/Debug/MapperDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@

namespace Rekalogika\Mapper\Debug;

use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface;
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\PropertyInfo\Type;

final class MapperDataCollector extends DataCollector
final class MapperDataCollector extends AbstractDataCollector
{
public function getName()
public function getName(): string
{
return 'rekalogika_mapper';
}
Expand All @@ -33,29 +31,6 @@ public function collect(
) {
}

/**
* @param class-string<TransformerInterface> $transformerClass
*/
public function createTraceData(
?string $path,
mixed $source,
mixed $target,
?Type $sourceType,
?Type $targetType,
string $transformerClass
): TraceData {
$traceData = new TraceData(
$path,
$this->cloneVar($source),
$this->cloneVar($target),
$sourceType,
$targetType,
$transformerClass
);

return $traceData;
}

public function collectTraceData(TraceData $traceData): void
{
/** @psalm-suppress MixedArrayAssignment */
Expand All @@ -70,4 +45,9 @@ public function getMappings(): array
/** @var array<int,TraceData> */
return $this->data['mappings'];
}

public static function getTemplate(): string
{
return "@RekalogikaMapper/data_collector.html.twig";
}
}
61 changes: 40 additions & 21 deletions src/Debug/TraceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@

use Rekalogika\Mapper\Exception\LogicException;
use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface;
use Rekalogika\Mapper\Util\TypeUtil;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\VarDumper\Cloner\Data;

final class TraceData
{
private string $sourceType;
private string $existingTargetType;
private string $targetType;
private ?string $resultType = null;
private ?float $time = null;

/** @var array<int,self> */
Expand All @@ -30,25 +35,22 @@ final class TraceData
*/
public function __construct(
private ?string $path,
private Data $source,
private Data $target,
private ?Type $sourceType,
private ?Type $targetType,
mixed $source,
mixed $existingTargetValue,
?Type $targetType,
private string $transformerClass,
) {
}
$this->sourceType = \get_debug_type($source);
$this->existingTargetType = \get_debug_type($existingTargetValue);

public function getSourceType(): ?Type
{
return $this->sourceType;
}

public function getTargetType(): ?Type
{
return $this->targetType;
if ($targetType !== null) {
$this->targetType = TypeUtil::getTypeString($targetType);
} else {
$this->targetType = 'mixed';
}
}

public function finalizeTime(float $time): self
public function finalizeTime(float $time): void
{
if (count($this->nestedTraceData) === 0) {
// If this is the last trace data (no nested trace data)
Expand All @@ -59,8 +61,11 @@ public function finalizeTime(float $time): self
// nested trace data
$this->time = array_sum(array_map(fn (self $traceData) => $traceData->getTime(), $this->nestedTraceData));
}
}

return $this;
public function finalizeResult(mixed $result): void
{
$this->resultType = \get_debug_type($result);
}

public function getTime(): float
Expand Down Expand Up @@ -93,18 +98,32 @@ public function addNestedTraceData(self $traceData): void
$this->nestedTraceData[] = $traceData;
}

public function getSource(): Data
public function getPath(): ?string
{
return $this->source;
return $this->path;
}

public function getTarget(): Data
public function getSourceType(): string
{
return $this->target;
return $this->sourceType;
}

public function getPath(): ?string
public function getExistingTargetType(): string
{
return $this->path;
return $this->existingTargetType;
}

public function getTargetType(): string
{
return $this->targetType;
}

public function getResultType(): string
{
if ($this->resultType === null) {
throw new LogicException('Result type is not set');
}

return $this->resultType;
}
}
5 changes: 2 additions & 3 deletions src/Debug/TraceableTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Rekalogika\Mapper\Transformer\Contracts\MainTransformerAwareTrait;
use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\VarDumper\Cloner\Data;

final class TraceableTransformer implements
TransformerInterface,
Expand Down Expand Up @@ -70,11 +69,10 @@ public function transform(
$path = null;
}

$traceData = $this->dataCollector->createTraceData(
$traceData = new TraceData(
$path,
$source,
$target,
$sourceType,
$targetType,
$this->decorated::class
);
Expand All @@ -96,6 +94,7 @@ public function transform(
$time = microtime(true) - $start;

$traceData->finalizeTime($time);
$traceData->finalizeResult($result);

return $result;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Util/TypeUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use DaveLiddament\PhpLanguageExtensions\Friend;
use Rekalogika\Mapper\Attribute\MapperAttributeInterface;
use Rekalogika\Mapper\Debug\TraceData;
use Rekalogika\Mapper\Exception\InvalidArgumentException;
use Rekalogika\Mapper\MainTransformer\Exception\TransformerReturnsUnexpectedValueException;
use Rekalogika\Mapper\Tests\UnitTest\Util\TypeUtil2Test;
Expand Down Expand Up @@ -260,7 +261,8 @@ public static function getDebugType(null|Type|MixedType|array $type): string
#[Friend(
TypeResolver::class,
TransformerReturnsUnexpectedValueException::class,
TypeUtilTest::class
TypeUtilTest::class,
TraceData::class
)]
public static function getTypeString(Type|MixedType $type): string
{
Expand Down
49 changes: 49 additions & 0 deletions templates/data_collector.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% block menu %}
<span class="label {{ not collector.mappings|length ? 'disabled' }}">
<span class="icon">{{ source('@RekalogikaMapper/map.svg') }}</span>
<strong>Mapper</strong>
</span>
{% endblock %}

{% block panel %}
<h2>Mapper</h2>
<div class="sf-rekalogika_mapper sf-reset">
<table>
<tr>
<th>Path</th>
<th>Source</th>
<th>Existing Target</th>
<th>Wanted Target</th>
<th>Result</th>
<th>Transformer</th>
<th>Duration</th>
</tr>

{% for tracedata in collector.mappings %}
{{ _self.render_row(tracedata, 0) }}
{% endfor %}
</table>
</div>
{% endblock %}

{% macro render_row(tracedata, depth) %}
<tr>
<td>
{% for i in 1..depth %}&nbsp;&nbsp;{% endfor %}{{ tracedata.path ?? '(root)' }}
</td>
<td>{{ tracedata.sourceType }}</td>
<td>{{ tracedata.existingTargetType }}</td>
<td>{{ tracedata.targetType }}</td>
<td>{{ tracedata.resultType }}</td>
<td>{{ tracedata.transformerClass|abbr_class }}</td>
<td>
<span class="nowrap">{{ '%.2f'|format(tracedata.time * 1000) }} ms</span>
</td>
</tr>

{% for child in tracedata.nestedTraceData %}
{{ _self.render_row(child, depth + 1) }}
{% endfor %}
{% endmacro %}
1 change: 1 addition & 0 deletions templates/map.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/Common/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function registerBundles(): iterable
public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(function (ContainerBuilder $container) {
$container->setParameter('kernel.secret', 'test');
$container->loadFromExtension('framework', [
'http_method_override' => false,
'handle_all_throwables' => true,
Expand Down

0 comments on commit 35bf75e

Please sign in to comment.