Skip to content

Commit

Permalink
Merge branch 'feature/profiler' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
lhapaipai committed Aug 4, 2024
2 parents 6d2a115 + d4e4525 commit e7f5c24
Show file tree
Hide file tree
Showing 16 changed files with 765 additions and 12 deletions.
5 changes: 5 additions & 0 deletions install/config/routes/pentatrion_vite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ when@dev:
_pentatrion_vite:
prefix: /build
resource: "@PentatrionViteBundle/Resources/config/routing.yaml"

_profiler_vite:
path: /_profiler/vite
defaults:
_controller: Pentatrion\ViteBundle\Controller\ProfilerController::info
32 changes: 32 additions & 0 deletions src/Controller/ProfilerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Pentatrion\ViteBundle\Controller;

use Pentatrion\ViteBundle\Service\Debug;
use Pentatrion\ViteBundle\Twig\TypeExtension;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

class ProfilerController
{
public function __construct(
private Debug $debug,
private Environment $twig
) {
}

public function info(): Response
{
$viteConfigs = $this->debug->getViteCompleteConfigs();

$this->twig->addExtension(new TypeExtension());

$response = new Response(
$this->twig->render('@PentatrionVite/Profiler/info.html.twig', [
'viteConfigs' => $viteConfigs,
])
);

return $response;
}
}
48 changes: 48 additions & 0 deletions src/DataCollector/ViteCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Pentatrion\ViteBundle\DataCollector;

use Pentatrion\ViteBundle\Model\Tag;
use Pentatrion\ViteBundle\Service\EntrypointRenderer;
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ViteCollector extends AbstractDataCollector
{
public function __construct(
private EntrypointRenderer $entrypointRenderer
) {
}

public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
{
$this->data = $this->entrypointRenderer->getRenderedFiles();
}

/**
* @return array<string, Tag>
*/
public function getRenderedStyles(): array
{
return $this->data['styles'];
}

/**
* @return array<string, Tag>
*/
public function getRenderedScripts(): array
{
return $this->data['scripts'];
}

public function getName(): string
{
return 'pentatrion_vite.vite_collector';
}

public static function getTemplate(): ?string
{
return '@PentatrionVite/Collector/vite_collector.html.twig';
}
}
31 changes: 26 additions & 5 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ services:
arguments:
- "@pentatrion_vite.entrypoint_renderer"

Pentatrion\ViteBundle\DataCollector\ViteCollector:
tags:
- { name: "data_collector", id: pentatrion_vite.vite_collector }
arguments:
- "@pentatrion_vite.entrypoint_renderer"

Pentatrion\ViteBundle\Service\Debug:
alias: pentatrion_vite.debug

pentatrion_vite.debug:
class: Pentatrion\ViteBundle\Service\Debug
arguments:
- "%pentatrion_vite.configs%"
- "@http_client"
- "@pentatrion_vite.entrypoints_lookup_collection"

Pentatrion\ViteBundle\Controller\ViteController:
tags: ["controller.service_arguments"]
arguments:
Expand All @@ -40,6 +56,12 @@ services:
- "@pentatrion_vite.entrypoints_lookup_collection"
- "%pentatrion_vite.proxy_origin%"

Pentatrion\ViteBundle\Controller\ProfilerController:
tags: ["controller.service_arguments"]
arguments:
- "@pentatrion_vite.debug"
- "@twig"

Pentatrion\ViteBundle\Asset\ViteAssetVersionStrategy:
arguments:
- "@pentatrion_vite.file_accessor"
Expand All @@ -49,7 +71,6 @@ services:
- "@?request_stack"
- "%pentatrion_vite.throw_on_missing_asset%"


pentatrion_vite.preload_assets_event_listener:
class: Pentatrion\ViteBundle\EventListener\PreloadAssetsEventListener
tags: ["kernel.event_subscriber"]
Expand All @@ -68,16 +89,16 @@ services:
class: Symfony\Component\Cache\Adapter\PhpArrayAdapter
factory: [Symfony\Component\Cache\Adapter\PhpArrayAdapter, create]
arguments:
- '%kernel.cache_dir%/pentatrion_vite.cache.php'
- '@cache.pentatrion_vite_fallback'
- "%kernel.cache_dir%/pentatrion_vite.cache.php"
- "@cache.pentatrion_vite_fallback"

pentatrion_vite.cache_warmer:
class: Pentatrion\ViteBundle\CacheWarmer\EntrypointsCacheWarmer
class: Pentatrion\ViteBundle\CacheWarmer\EntrypointsCacheWarmer
tags: ["kernel.cache_warmer"]
arguments:
- "%kernel.project_dir%%pentatrion_vite.public_directory%"
- "%pentatrion_vite.configs%"
- '%kernel.cache_dir%/pentatrion_vite.cache.php'
- "%kernel.cache_dir%/pentatrion_vite.cache.php"

cache.pentatrion_vite_fallback:
tags: ["cache.pool"]
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/views/Collector/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 80 additions & 0 deletions src/Resources/views/Collector/vite_collector.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{% extends "@WebProfiler/Profiler/layout.html.twig" %}

{% block toolbar %}
{% set icon %}
{{ include('@PentatrionVite/Collector/icon.svg') }}
<span class="sf-toolbar-value">Vite</span>
{% endset %}
{% set text %}
<div class="sf-toolbar-info-piece">
<b>Vite dev Server</b>
<span>
<a href="{{ path('_profiler_vite') }}">Config</a>
</span>
</div>
<div class="sf-toolbar-info-piece">
<b>Rendered &lt;script /&gt;</b>
<span class="sf-toolbar-status">{{ collector.renderedScripts | length }}</span>
</div>
<div class="sf-toolbar-info-piece">
<b>Rendered &lt;link /&gt;</b>
<span class="sf-toolbar-status">{{ collector.renderedStyles | length }}</span>
</div>
{% endset %}
{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { 'link': true }) }}
{% endblock %}

{% block menu %}
<span class="label">
<span class="icon">
{{ include('@PentatrionVite/Collector/icon.svg') }}
</span>
<strong>Vite</strong>
</span>
{% endblock %}

{% block panel %}
<h2>Rendered &lt;script /&gt;</h2>
{% for _, tag in collector.renderedScripts %}
<table>
<thead>
<tr>
<th class="key">Attribute</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for key, value in tag.attributes %}
<tr>
<th>{{ key }}</th>
<td>
<pre class="sf-dump">{{ value }}</pre>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}

<h2>Rendered &lt;link /&gt;</h2>
{% for _, tag in collector.renderedStyles %}
<table>
<thead>
<tr>
<th class="key">Attribute</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for key, value in tag.attributes %}
<tr>
<th>{{ key }}</th>
<td>
<pre class="sf-dump">{{ value }}</pre>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
{% endblock %}
Loading

0 comments on commit e7f5c24

Please sign in to comment.