Permalink
Browse files

Add more code, and screenshots of the profiler

  • Loading branch information...
1 parent 8319260 commit 971352427be1d95b0258555cc9bc7b7fbb433fb3 @rande rande committed Dec 3, 2013
@@ -102,7 +102,7 @@ public function getCacheKeys(BlockInterface $block)
{
return array(
'block_id' => $block->getId(),
- 'updated_at' => $block->getUpdatedAt()->format('U'),
+ 'updated_at' => $block->getUpdatedAt() ? $block->getUpdatedAt()->format('U') : strtotime('now'),
);
}
View
@@ -51,4 +51,13 @@ public function getBlocks()
{
return $this->blocks;
}
+
+ /**
+ * @param string $name
+ * @param mixed $default
+ */
+ public function getSetting($name, $default = null)
+ {
+ return isset($this->settings[$name]) ? $this->settings[$name] : $default;
+ }
}
@@ -0,0 +1,53 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\BlockBundle\Event;
+
+use Sonata\AdminBundle\Admin\AdminInterface;
+use Sonata\BlockBundle\Model\Block;
+use Symfony\Component\EventDispatcher\Event;
+
+/**
+ * This class is just a demo of how to define a Listener to generate a valid block instance used
+ * to render a block from an event call using the sonata_block_render_event template helper.
+ *
+ * For instance, you can add an element on the top to each admin form with the following code:
+ *
+ * text.listener:
+ * class: Sonata\BlockBundle\Event\TextBlockListener
+ * tags:
+ * - { name: kernel.event_listener, event: 'sonata.block.event.sonata.admin.form.edit.top', method: onBlock}
+ *
+ */
+class TextBlockListener
+{
+ /**
+ * @param BlockEvent $event
+ */
+ public function onBlock(BlockEvent $event)
+ {
+ $content = 'This block is coming from inline event from the template';
+ if ($event->getSetting('admin') instanceof AdminInterface && $event->getSetting('action') == 'edit') {
+ $admin = $event->getSetting('admin');
+
+ $content = sprintf("<p class='well'>The admin subject is <strong>%s</strong></p>", $admin->toString($admin->getSubject()));
+ }
+
+ $block = new Block();
+ $block->setId(uniqid());
+ $block->setSettings(array(
+ 'content' => $event->getSetting('content', $content)
+ ));
+ $block->setType('sonata.block.service.text');
+
+ $event->addBlock($block);
+ }
+}
@@ -43,8 +43,16 @@ class BlockDataCollector implements DataCollectorInterface, \Serializable
*/
protected $realBlocks = array();
+ /**
+ * @var array
+ */
protected $containerTypes = array();
+ /**
+ * @var array
+ */
+ protected $events = array();
+
/**
* Constructor
*
@@ -69,18 +77,47 @@ public function collect(Request $request, Response $response, \Exception $except
$this->blocks = $this->blocksHelper->getTraces();
// split into containers & real blocks
- foreach ($this->blocks as $block) {
+ foreach ($this->blocks as $id => $block) {
if (!is_array($block)) {
return; // something went wrong while collecting information
}
+
+ if ($id == '_events') {
+ foreach ($block as $uniqid => $event) {
+ $this->events[$uniqid] = $event;
+ }
+
+ continue;
+ }
+
if (in_array($block['type'], $this->containerTypes)) {
- $this->containers[] = $block;
+ $this->containers[$id] = $block;
} else {
- $this->realBlocks[] = $block;
+ $this->realBlocks[$id] = $block;
}
}
}
+ /**
+ * Returns the number of block used
+ *
+ * @return int
+ */
+ public function getTotalBlock()
+ {
+ return count($this->realBlocks) + count($this->containers);
+ }
+
+ /**
+ * Return the events used on the current page
+ *
+ * @return array
+ */
+ public function getEvents()
+ {
+ return $this->events;
+ }
+
/**
* Returns the block rendering history
*
@@ -122,6 +159,7 @@ public function serialize()
'blocks' => $this->blocks,
'containers' => $this->containers,
'realBlocks' => $this->realBlocks,
+ 'events' => $this->events,
);
return serialize($data);
@@ -141,6 +179,7 @@ public function unserialize($data)
$this->blocks = $merged['blocks'];
$this->containers = $merged['containers'];
$this->realBlocks = $merged['realBlocks'];
+ $this->events = $merged['events'];
}
/**
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -5,7 +5,8 @@
<div class="sf-toolbar-icon">
<a href="{{ path('_profiler', { 'token': token, 'panel': name }) }}">
{# fake image span #}<span style="width:0px; height: 28px; vertical-align: middle;"></span>
- <span class="sf-toolbar-status">{{ collector.blocks|length }}</span> blocks
+ <span class="sf-toolbar-status">{{ collector.getTotalBlock() }}</span> blocks
+ {% if collector.events|length > 0 %}<strong>*</strong>{% endif %}
</a>
</div>
<div class="sf-toolbar-info">
@@ -19,7 +20,12 @@
</div>
<div class="sf-toolbar-info-piece">
<b>Total Blocks</b>
- <span>{{ collector.blocks|length }}</span>
+ <span>{{ collector.getTotalBlock() }}</span>
+ </div>
+ <div class="sf-toolbar-info-piece">
+ <hr />
+ <b>Events</b>
+ <span>{{ collector.events|length }}</span>
</div>
</div>
</div>
@@ -28,14 +34,45 @@
{% block menu %}
<span class="label">
<span class="icon"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAEJGlDQ1BJQ0MgUHJvZmlsZQAAOBGFVd9v21QUPolvUqQWPyBYR4eKxa9VU1u5GxqtxgZJk6XtShal6dgqJOQ6N4mpGwfb6baqT3uBNwb8AUDZAw9IPCENBmJ72fbAtElThyqqSUh76MQPISbtBVXhu3ZiJ1PEXPX6yznfOec7517bRD1fabWaGVWIlquunc8klZOnFpSeTYrSs9RLA9Sr6U4tkcvNEi7BFffO6+EdigjL7ZHu/k72I796i9zRiSJPwG4VHX0Z+AxRzNRrtksUvwf7+Gm3BtzzHPDTNgQCqwKXfZwSeNHHJz1OIT8JjtAq6xWtCLwGPLzYZi+3YV8DGMiT4VVuG7oiZpGzrZJhcs/hL49xtzH/Dy6bdfTsXYNY+5yluWO4D4neK/ZUvok/17X0HPBLsF+vuUlhfwX4j/rSfAJ4H1H0qZJ9dN7nR19frRTeBt4Fe9FwpwtN+2p1MXscGLHR9SXrmMgjONd1ZxKzpBeA71b4tNhj6JGoyFNp4GHgwUp9qplfmnFW5oTdy7NamcwCI49kv6fN5IAHgD+0rbyoBc3SOjczohbyS1drbq6pQdqumllRC/0ymTtej8gpbbuVwpQfyw66dqEZyxZKxtHpJn+tZnpnEdrYBbue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+zmpr8KLz7Onte6q6vrb85NGNkWb6CDMsOVoYSaPg+diTuK5q0ib2Njo0MDpXdwrrgk/YI/kElnLuMNUzEYcZCqaeOhchMvRD1bRnUR7QhlqbzgI+gQXe8BlH8lAxj6LOsd0Mn6KlA0NPCGSNjrnLlEScObampqaoa2Q4wMI6C4LP+73QkRpnIM6XJfc+E65j+1F5Dx5/UoJR++sTRzKBsi63/TnlsrT5SkT9pS9JFXsQzhvfUggUEVoI6nB8iLlNrDVpgxgmz/jEfo43TK36WUeRTFKjsZ/Z4MgOai9sgq90KO9QFobxnAWlabjijFKHiacBzjlssSKkBRiBI9QuSVCdafgvt5cHCwjvWHli5X5vbySMyA7STkwAxuoYbkk4QZPsdTCH0dI0b4J/MMOPWGfkBfTJVagBHj0HfPzMxMkpBqSuqC+oIWBVb4VZIMAD8BKjvz6GBGRqKAkDSzOuFFXH0WuIZncijN05jyGLTGmY9xF1B+H3z6f6Fnm+SWs0B49RAPWaPygBqGNMIXuoxWaYFK+MJR40foD0f4Ki6Dm/2XwXyn+HD/JOdKZH1LzFNd+D2GM0wVmPbI5IMknXl6g1CMRBADBMszJc/wxhfc5IZ4oIvx7ZRj97qkeUeoipJLS0tzxPk0RuhANa8YxgC8PGSe6cx6tAjWhCcfiEL6utYMJV+Md+cemjy6VK5n0X3F/SXTt/8e4tqAS3XT/TSQENdnMH5RSuiW7fDKU3vxiifFl3YdXb9A6/0PvyPQa+d+1RoAAAAASUVORK5CYII=" alt="" /></span>
- <strong>Blocks</strong>
+ <strong>Blocks{% if collector.events|length > 0 %}<strong>*</strong>{% endif %}</strong>
<span class="count">
- <span>{{ collector.blocks|length }}</span>
+ <span>{{ collector.getTotalBlock() }}</span>
</span>
</span>
{% endblock %}
{% block panel %}
+ <h2>Events Blocks</h2>
+ <table>
+ <tr>
+ <th>code name</th>
+ <th>listener tag</th>
+ <th>Block types</th>
+ <th>Listeners</th>
+ </tr>
+
+ {% for event in collector.events %}
+ <tr>
+ <td>{{ event['template_code'] }}</td>
+ <td>{{ event['event_name'] }}</td>
+ <td>
+ {% for type in event['blocks'] %}
+ {{ type.1 }} (id:{{ type.0 }})
+ {% else %}
+ no block returned
+ {% endfor %}
+ </td>
+ <td>
+ {% for listener in event['listeners'] %}
+ {{ listener }}
+ {% else %}
+ no listener registered
+ {% endfor %}
+ </td>
+ </tr>
+ {% endfor %}
+ </table>
+
<h2>Real Blocks</h2>
{% set blocks = collector.realBlocks %}
{{ block('table') }}
@@ -67,7 +67,7 @@ class BlockHelper extends Helper
*/
public function __construct(BlockServiceManagerInterface $blockServiceManager, array $cacheBlocks, BlockRendererInterface $blockRenderer,
BlockContextManagerInterface $blockContextManager, EventDispatcherInterface $eventDispatcher,
- CacheManagerInterface $cacheManager = null, HttpCacheHandlerInterface $cacheHandler = null, Stopwatch $stopwatch= null)
+ CacheManagerInterface $cacheManager = null, HttpCacheHandlerInterface $cacheHandler = null, Stopwatch $stopwatch = null)
{
$this->blockServiceManager = $blockServiceManager;
$this->cacheBlocks = $cacheBlocks;
@@ -83,7 +83,9 @@ public function __construct(BlockServiceManagerInterface $blockServiceManager, a
'css' => array()
);
- $this->traces = array();
+ $this->traces = array(
+ '_events' => array()
+ );
}
/**
@@ -195,7 +197,7 @@ protected function startTracing(BlockInterface $block)
'assets' => array(
'js' => array(),
'css' => array(),
- ),
+ )
);
}
@@ -224,17 +226,69 @@ protected function stopTracing(BlockInterface $block, array $stats)
*/
public function renderEvent($name, array $options = array())
{
- $event = $this->eventDispatcher->dispatch($name, new BlockEvent($options));
+ $eventName = sprintf('sonata.block.event.%s', $name);
+
+ $event = $this->eventDispatcher->dispatch($eventName, new BlockEvent($options));
$content = "";
foreach ($event->getBlocks() as $block) {
$content .= $this->render($block);
}
+ if ($this->stopwatch) {
+ $this->traces['_events'][uniqid()] = array(
+ 'template_code' => $name,
+ 'event_name' => $eventName,
+ 'blocks' => $this->getEventBlocks($event),
+ 'listeners' => $this->getEventListeners($event),
+ );
+ }
+
return $content;
}
+ /**
+ * @param BlockEvent $event
+ *
+ * @return array
+ */
+ protected function getEventBlocks(BlockEvent $event)
+ {
+ $results = array();
+
+ foreach ($event->getBlocks() as $block) {
+ $results[] = array($block->getId(), $block->getType());
+ }
+
+ return $results;
+ }
+
+ /**
+ * @param BlockEvent $event
+ *
+ * @return array
+ */
+ protected function getEventListeners(BlockEvent $event)
+ {
+ $results = array();
+
+ foreach ($this->eventDispatcher->getListeners($event->getName()) as $listener) {
+ if (is_object($listener[0])) {
+ $results[] = get_class($listener[0]);
+ } else if (is_string($listener[0])) {
+ $results[] = $listener[0];
+ } else if ($listener instanceof \Closure) {
+ $results[] = '{closure}()';
+ } else {
+ $results[] = 'Unkown type!';
+ }
+ }
+
+ return $results;
+
+ }
+
/**
* @param mixed $block
* @param array $options
@@ -25,14 +25,17 @@ public function provideFunction()
{
return array(
array('sonata_block_render', array(
- 'foobar', array('bar' => 'foo')
+ 'foobar', array('bar' => 'foo') // arguments
), 'render'),
array('sonata_block_include_javascripts', array(
- 'screen',
+ 'screen', // arguments
), 'includeJavascripts'),
array('sonata_block_include_stylesheets', array(
- 'foo',
+ 'foo', // arguments
), 'includeStylesheets'),
+ array('sonata_block_render_event', array(
+ 'event.name', array(), // arguments
+ ), 'renderEvent')
);
}

0 comments on commit 9713524

Please sign in to comment.