Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for the data collector exercise #3

Open
wants to merge 1 commit into
base: workshop-advanced-symfony
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Acme/DemoBundle/DataCollector/SomeDataCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Acme\DemoBundle\DataCollector;

use Acme\DemoBundle\Services\SomeService;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;

class SomeDataCollector implements DataCollectorInterface
{
private $service;

public function __construct(SomeService $service)
{
$this->service = $service;
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data = $this->service->getMessages();
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'some_data_collector';
}
}
5 changes: 5 additions & 0 deletions src/Acme/DemoBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@
</service>

<service id="acme.demo.some_service" class="Acme\DemoBundle\Services\SomeService" />

<service id="acme.demo.some_data_collector" class="Acme\DemoBundle\DataCollector\SomeDataCollector">
<tag name="data_collector" template="AcmeDemoBundle:Collector:messages" id="some_data_collector" />
<argument type="service" id="acme.demo.some_service" />
</service>
</services>
</container>
35 changes: 35 additions & 0 deletions src/Acme/DemoBundle/Resources/views/Collector/messages.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{% extends 'WebProfilerBundle:Profiler:layout.html.twig' %}

{% block toolbar %}
{% set icon %}
<img width="20" height="28" alt="Some Service" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAcCAYAAABh2p9gAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQRJREFUeNpi/P//PwM1ARMDlcGogZQDlpMnT7pxc3NbA9nhQKxOpL5rQLwJiPeBsI6Ozl+YBOOOHTv+AOllQNwtLS39F2owKYZ/gRq8G4i3ggxEToggWzvc3d2Pk+1lNL4fFAs6ODi8JzdS7mMRVyDVoAMHDsANdAPiOCC+jCQvQKqBQB/BDbwBxK5AHA3E/kB8nKJkA8TMQBwLxaBIKQbi70AvTADSBiSadwFXpCikpKQU8PDwkGTaly9fHFigkaKIJid4584dkiMFFI6jkTJII0WVmpHCAixZQEXWYhDeuXMnyLsVlEQKI45qFBQZ8eRECi4DBaAlDqle/8A48ip6gAADANdQY88Uc0oGAAAAAElFTkSuQmCC" />
<span class="sf-toolbar-status">{{ collector.data|length }}</span>
{% endset %}
{% set text %}
<div class="sf-toolbar-info-piece">{{ collector.data|length }}</div>
{% endset %}
{% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %}
{% endblock %}

{% block head %}
{{ parent() }}
{% endblock %}

{% block menu %}
<span class="label">
<span class="icon"><img alt="Some Service" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAcCAYAAAB/E6/TAAABLUlEQVR42u3TP0vDQBiA8UK/gDiLzi0IhU4OEunk5OQUAhGSOBUCzqWfIKSzX8DRySF0URCcMjWLIJjFD9Cpk/D6HITecEPUuzhIAz8CIdyTP/f2iqI4qaqqDx8l5Ic2uIeP/bquezCokOAFF+oCN3t4gPzSEjc4NEPaCldQbzjELTYW0RJzHDchwwem+ons6ZBpLSJ7nueJC22h0V+FzmwWV0ee59vQNV67CGVZJmEYbkNjfpY6X6I0Qo4/3RMmTdDDspuQVsJvgkP3IdMbIkIjLPBoadG2646iKJI0Ta2wxm6OdnP0/Tk6DYJgHcfxpw21RtscDTDDnaVZ26474GkkSRIrrPEv5sgMTfHe+cA2O6wPH6vOBpYQNALneHb96XTEDI6dzpEZ0VzO0Rf3pP5LMLI4tAAAAABJRU5ErkJggg==" /></span>
<strong>Some Service</strong>
<span class="count">
<span>{{ collector.data|length }}</span>
</span>
</span>
{% endblock %}

{% block panel %}
<h2>Some Service</h2>
{% for message in collector.data %}
<div>{{ message }}</div>
{% else %}
<p>No messages</p>
{% endfor %}
{% endblock %}