Skip to content

Commit 6802c2c

Browse files
mikeSimonsonchr-hertel
authored andcommitted
Adding a data collector for the debug toolbar
The change to dev-master for doctrine/migrations in the composer.json is temporary. As soon as the 1.3 version of the migrations is out it will target that.
1 parent 33004b4 commit 6802c2c

File tree

3 files changed

+344
-0
lines changed

3 files changed

+344
-0
lines changed

Collector/MigrationsCollector.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: mike
5+
* Date: 9/01/16
6+
* Time: 23:02
7+
*/
8+
9+
namespace Doctrine\Bundle\MigrationsBundle\Collector;
10+
11+
12+
use Doctrine\DBAL\Connection;
13+
use Doctrine\DBAL\Migrations\Configuration\Configuration;
14+
use Doctrine\DBAL\Migrations\Tools\Console\Helper\MigrationStatusInfosHelper;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
18+
19+
class MigrationsCollector implements DataCollectorInterface
20+
{
21+
/** @var string */
22+
private $migrationTablename;
23+
24+
/** @var string */
25+
private $migrationName;
26+
27+
/** @var Connection */
28+
private $connection;
29+
30+
/** @var string */
31+
private $migrationNamespace;
32+
33+
/** @var string */
34+
private $migrationDirectory;
35+
36+
public function __construct(Connection $connection, $migrationNamespace, $migrationDirectory, $migrationTablename, $migrationName)
37+
{
38+
$this->connection = $connection;
39+
$this->migrationNamespace = $migrationNamespace;
40+
$this->migrationDirectory = $migrationDirectory;
41+
$this->migrationTablename = $migrationTablename;
42+
$this->migrationName = $migrationName;
43+
}
44+
45+
/**
46+
* Collects data for the given Request and Response.
47+
*
48+
* @param Request $request A Request instance
49+
* @param Response $response A Response instance
50+
* @param \Exception $exception An Exception instance
51+
*/
52+
public function collect(Request $request, Response $response, \Exception $exception = null)
53+
{
54+
$configuration = new Configuration($this->connection);
55+
$configuration->setMigrationsNamespace($this->migrationNamespace);
56+
$configuration->setMigrationsDirectory($this->migrationDirectory);
57+
$configuration->setMigrationsTableName($this->migrationTablename);
58+
$configuration->setName($this->migrationName);
59+
$configuration->registerMigrationsFromDirectory($configuration->getMigrationsDirectory());
60+
$migrationsStatusInfos = new MigrationStatusInfosHelper($configuration);
61+
$this->data = $migrationsStatusInfos->getMigrationsInfos();
62+
$newMigrationsList = $configuration->getMigrationsToExecute('up', $configuration->getLatestVersion());
63+
$this->data['newMigrationsList'] = array_map(function($migration) {
64+
return $migration->getVersion();
65+
}, $newMigrationsList);
66+
$this->data['executedUnavailableMigrationsList'] = array_map(function($migration) {
67+
return $migration->getVersion();
68+
}, $migrationsStatusInfos->getExecutedUnavailableMigrations());
69+
70+
$this->connection = null;
71+
$this->migrationDirectory = null;
72+
$this->migrationNamespace = null;
73+
$this->migrationName = null;
74+
$this->migrationTablename = null;
75+
}
76+
77+
/**
78+
* Returns the name of the collector.
79+
*
80+
* @return string The collector name
81+
*/
82+
public function getName()
83+
{
84+
return 'doctrine.migrations_collector';
85+
}
86+
87+
public function getPreviousMigration()
88+
{
89+
return $this->data['Previous Version'];
90+
}
91+
92+
public function getCurrentMigration()
93+
{
94+
return $this->data['Current Version'];
95+
}
96+
97+
public function getNextMigration()
98+
{
99+
return $this->data['Next Version'];
100+
}
101+
102+
public function getLatestMigration()
103+
{
104+
return $this->data['Latest Version'];
105+
}
106+
107+
public function getExecutedMigrations()
108+
{
109+
return $this->data['Executed Migrations'];
110+
}
111+
112+
public function getExecutedUnavailableMigrations()
113+
{
114+
return $this->data['Executed Unavailable Migrations'];
115+
}
116+
117+
public function getAvailableMigrations()
118+
{
119+
return $this->data['Available Migrations'];
120+
}
121+
122+
public function getNewMigrations()
123+
{
124+
return $this->data['New Migrations'];
125+
}
126+
127+
public function getNewMigrationsList()
128+
{
129+
return $this->data['newMigrationsList'];
130+
}
131+
132+
public function getExecutedUnavailableMigrationsList()
133+
{
134+
return $this->data['executedUnavailableMigrationsList'];
135+
}
136+
137+
public function getDatabaseDriver()
138+
{
139+
return $this->data['Database Driver'];
140+
}
141+
public function getDatabaseName()
142+
{
143+
return $this->data['Database Name'];
144+
}
145+
public function getConfigurationSource()
146+
{
147+
return $this->data['Configuration Source'];
148+
}
149+
public function getVersionTableName()
150+
{
151+
return $this->data['Version Table Name'];
152+
}
153+
public function getVersionColumnName()
154+
{
155+
return $this->data['Version Column Name'];
156+
}
157+
public function getMigrationNamespace()
158+
{
159+
return $this->data['Migrations Namespace'];
160+
}
161+
public function getMigrationDirectory()
162+
{
163+
return $this->data['Migrations Directory'];
164+
}
165+
}

Resources/config/services.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@
140140

141141
<tag name="console.command" command="doctrine:migrations:version" />
142142
</service>
143+
144+
<service id="doctrine_migrations.migrations_collector" class="Doctrine\Bundle\MigrationsBundle\Collector\MigrationsCollector">
145+
<argument type="service" id="doctrine.dbal.default_connection"/>
146+
<argument>%doctrine_migrations.namespace%</argument>
147+
<argument>%doctrine_migrations.dir_name%</argument>
148+
<argument>%doctrine_migrations.table_name%</argument>
149+
<argument>%doctrine_migrations.name%</argument>
150+
<tag name="data_collector" template="DoctrineMigrationsBundle::migrationsCollector.html.twig" id="doctrine_bundle.migrations_collector" priority="249"/>
151+
</service>
143152
</services>
144153

145154
</container>
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{% extends app.request.isXmlHttpRequest ? '@WebProfiler/Profiler/ajax_layout.html.twig' : '@WebProfiler/Profiler/layout.html.twig' %}
2+
3+
{% block toolbar %}
4+
{% set profiler_markup_version = profiler_markup_version|default(1) %}
5+
6+
{% set color = collector.availableMigrations == collector.executedMigrations ? 'green' : '' %}
7+
{% set color = collector.executedUnavailableMigrations > 0 ? 'red' : '' %}
8+
{% if color is empty %}
9+
{% set color = 'yellow' %}
10+
{% endif %}
11+
12+
{# this is the content displayed as a panel in the toolbar #}
13+
{% set icon %}
14+
{% if profiler_markup_version == 1 %}
15+
{% set color = collector.availableMigrations == collector.executedMigrations ? 'green' : '' %}
16+
{% set color = collector.executedUnavailableMigrations > 0 ? 'red' : '' %}
17+
{% if color is empty %}
18+
{% set color = 'yellow' %}
19+
{% endif %}
20+
21+
<img width="20" height="28" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAcCAYAAAB/E6/TAAABLUlEQVR42u3TP0vDQBiA8UK/gDiLzi0IhU4OEunk5OQUAhGSOBUCzqWfIKSzX8DRySF0URCcMjWLIJjFD9Cpk/D6HITecEPUuzhIAz8CIdyTP/f2iqI4qaqqDx8l5Ic2uIeP/bquezCokOAFF+oCN3t4gPzSEjc4NEPaCldQbzjELTYW0RJzHDchwwem+ons6ZBpLSJ7nueJC22h0V+FzmwWV0ee59vQNV67CGVZJmEYbkNjfpY6X6I0Qo4/3RMmTdDDspuQVsJvgkP3IdMbIkIjLPBoadG2646iKJI0Ta2wxm6OdnP0/Tk6DYJgHcfxpw21RtscDTDDnaVZ26474GkkSRIrrPEv5sgMTfHe+cA2O6wPH6vOBpYQNALneHb96XTEDI6dzpEZ0VzO0Rf3pP5LMLI4tAAAAABJRU5ErkJggg==" alt="Doctrine Migrations">
22+
<span class="sf-toolbar-info-piece-additional-detail">Migrations</span>
23+
<span class="sf-toolbar-value sf-toolbar-status sf-toolbar-status-{{ color }}">{{ collector.executedMigrations }} / {{ collector.availableMigrations }}</span>
24+
{% else %}
25+
{% endif %}
26+
{% endset %}
27+
28+
{% set text %}
29+
{# this is the content displayed when hovering the mouse over
30+
the toolbar panel #}
31+
{% set properties = ['previousMigration', 'currentMigration', 'nextMigration', 'latestMigration', 'executedMigrations', 'executedUnavailableMigrations', 'availableMigrations', 'newMigrations'] %}
32+
{% for property in properties%}
33+
{% set result = attribute(collector, 'get' ~ (property|capitalize) ) %}
34+
<div class="sf-toolbar-info-piece">
35+
<b>{{ property|replace({'Migrations': '', 'Migration': ''})|humanize }}</b>
36+
<span>{{ result|replace({'<comment>': '', '</comment>': '', '(': '', ')': ''}) }}</span>
37+
</div>
38+
{% endfor %}
39+
{% endset %}
40+
41+
{# the 'link' value set to 'false' means that this panel doesn't
42+
show a section in the web profiler (default is 'true'). #}
43+
{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { link: true }) }}
44+
{% endblock %}
45+
46+
47+
{% block menu %}
48+
{# This left-hand menu appears when using the full-screen profiler. #}
49+
50+
<span class="label">
51+
<span class="icon"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAcCAYAAAB/E6/TAAABLUlEQVR42u3TP0vDQBiA8UK/gDiLzi0IhU4OEunk5OQUAhGSOBUCzqWfIKSzX8DRySF0URCcMjWLIJjFD9Cpk/D6HITecEPUuzhIAz8CIdyTP/f2iqI4qaqqDx8l5Ic2uIeP/bquezCokOAFF+oCN3t4gPzSEjc4NEPaCldQbzjELTYW0RJzHDchwwem+ons6ZBpLSJ7nueJC22h0V+FzmwWV0ee59vQNV67CGVZJmEYbkNjfpY6X6I0Qo4/3RMmTdDDspuQVsJvgkP3IdMbIkIjLPBoadG2646iKJI0Ta2wxm6OdnP0/Tk6DYJgHcfxpw21RtscDTDDnaVZ26474GkkSRIrrPEv5sgMTfHe+cA2O6wPH6vOBpYQNALneHb96XTEDI6dzpEZ0VzO0Rf3pP5LMLI4tAAAAABJRU5ErkJggg==" alt=""></span>
52+
<strong>Migrations</strong>
53+
<span class="count">
54+
<span>
55+
{{ collector.executedMigrations }} / {{ collector.availableMigrations }}
56+
</span>
57+
</span>
58+
</span>
59+
{% endblock %}
60+
61+
{% block panel %}
62+
{# Optional, for showing the most details. #}
63+
<h2>Doctrine Migrations</h2>
64+
<table>
65+
<tr>
66+
<th>Name</th>
67+
<th>Status</th>
68+
</tr>
69+
70+
<tr>
71+
<td>Database Driver</td>
72+
<td>{{ collector.getDatabaseDriver }}</td>
73+
</tr>
74+
75+
<tr>
76+
<td>Database Name</td>
77+
<td>{{ collector.getDatabaseName }}</td>
78+
</tr>
79+
80+
<tr>
81+
<td>Configuration Source</td>
82+
<td>{{ collector.getConfigurationSource }}</td>
83+
</tr>
84+
85+
<tr>
86+
<td>Version Table Name</td>
87+
<td>{{ collector.getVersionTableName }}</td>
88+
</tr>
89+
90+
<tr>
91+
<td>Version Column Name</td>
92+
<td>{{ collector.getVersionColumnName }}</td>
93+
</tr>
94+
95+
<tr>
96+
<td>Migrations Namespace</td>
97+
<td>{{ collector.getMigrationNamespace }}</td>
98+
</tr>
99+
100+
<tr>
101+
<td>Migrations Directory</td>
102+
<td>{{ collector.getMigrationDirectory }}</td>
103+
</tr>
104+
105+
<tr>
106+
<td>Previous Version</td>
107+
<td>{{ collector.getPreviousMigration|replace({'<comment>': '', '</comment>': '', '(': '', ')': ''})}}</td>
108+
</tr>
109+
<tr>
110+
<td>Current Version</td>
111+
<td>{{ collector.getCurrentMigration|replace({'<comment>': '', '</comment>': '', '(': '', ')': ''}) }}</td>
112+
</tr>
113+
<tr>
114+
<td>Next Version</td>
115+
<td>{{ collector.getNextMigration|replace({'<comment>': '', '</comment>': '', '(': '', ')': ''}) }}</td>
116+
</tr>
117+
<tr>
118+
<td>Latest Version</td>
119+
<td>{{ collector.getLatestMigration|replace({'<comment>': '', '</comment>': '', '(': '', ')': ''}) }}</td>
120+
</tr>
121+
122+
<tr>
123+
<td>Executed Migrations</td>
124+
<td>{{ collector.getExecutedMigrations }}</td>
125+
</tr>
126+
<tr>
127+
<td>Executed Unavailable Migrations</td>
128+
<td>{{ collector.getExecutedUnavailableMigrations }}</td>
129+
</tr>
130+
<tr>
131+
<td>Available Migrations</td>
132+
<td>{{ collector.getExecutedUnavailableMigrations }}</td>
133+
</tr>
134+
<tr>
135+
<td>New Migrations</td>
136+
<td>{{ collector.getNewMigrations }}</td>
137+
</tr>
138+
139+
</table>
140+
141+
{% if collector.getExecutedUnavailableMigrations > 0 %}
142+
<h2>Executed Unavailable Migrations</h2>
143+
<table>
144+
<tr>
145+
<th>Name</th>
146+
</tr>
147+
148+
{% for name in collector.getExecutedUnavailableMigrationsList %}
149+
<tr>
150+
<td>{{ name }}</td>
151+
</tr>
152+
{% endfor %}
153+
</table>
154+
{% endif %}
155+
156+
{% if collector.getNewMigrations > 0 %}
157+
<h2>New Migrations Available</h2>
158+
<table>
159+
<tr>
160+
<th>Name</th>
161+
</tr>
162+
163+
{% for name in collector.getNewMigrationsList %}
164+
<tr>
165+
<td>{{ name }}</td>
166+
</tr>
167+
{% endfor %}
168+
</table>
169+
{% endif %}
170+
{% endblock %}

0 commit comments

Comments
 (0)