Skip to content

[WebProfilerBundle] Added feedback about the current symfony version #13626

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

Merged
merged 1 commit into from
Apr 3, 2015
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@
{% if collector.applicationname %}
{{ collector.applicationname }} {{ collector.applicationversion }}
{% else %}
{{ collector.symfonyversion }}
{% if 'unknown' == collector.symfonyState -%}
<span class="sf-toolbar-status sf-toolbar-info-piece-additional" title="Unable to retrieve information about the Symfony version.">
{%- elseif 'eol' == collector.symfonyState -%}
<span class="sf-toolbar-status sf-toolbar-status-red" title="This Symfony version will no longer receive security fixes.">
{%- elseif 'eom' == collector.symfonyState -%}
<span class="sf-toolbar-status sf-toolbar-status-yellow" title="This Symfony version will only receive security fixes.">
{%- elseif 'dev' == collector.symfonyState -%}
<span class="sf-toolbar-status sf-toolbar-status-yellow" title="This Symfony version is still in the development phase.">
{%- else -%}
<span class="sf-toolbar-status sf-toolbar-status-green">
{%- endif -%}
{{ collector.symfonyversion }}</span>
{% endif %}
</span>
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
*/
class ConfigDataCollector extends DataCollector
{
/**
* @var KernelInterface
*/
private $kernel;
private $name;
private $version;
private $cacheVersionInfo = true;

/**
* Constructor.
Expand Down Expand Up @@ -59,6 +63,7 @@ public function collect(Request $request, Response $response, \Exception $except
'app_version' => $this->version,
'token' => $response->headers->get('X-Debug-Token'),
'symfony_version' => Kernel::VERSION,
'symfony_state' => 'unknown',
'name' => isset($this->kernel) ? $this->kernel->getName() : 'n/a',
'env' => isset($this->kernel) ? $this->kernel->getEnvironment() : 'n/a',
'debug' => isset($this->kernel) ? $this->kernel->isDebug() : 'n/a',
Expand All @@ -77,6 +82,8 @@ public function collect(Request $request, Response $response, \Exception $except
foreach ($this->kernel->getBundles() as $name => $bundle) {
$this->data['bundles'][$name] = $bundle->getPath();
}

$this->data['symfony_state'] = $this->requestSymfonyState();
}
}

Expand Down Expand Up @@ -110,6 +117,21 @@ public function getSymfonyVersion()
return $this->data['symfony_version'];
}

/**
* Returns the state of the current Symfony release.
*
* @return string One of: unknown, dev, stable, eom, eol
*/
public function getSymfonyState()
{
return $this->data['symfony_state'];
}

public function setCacheVersionInfo($cacheVersionInfo)
{
$this->cacheVersionInfo = $cacheVersionInfo;
}

/**
* Gets the PHP version.
*
Expand Down Expand Up @@ -242,4 +264,68 @@ public function getName()
{
return 'config';
}

/**
* Tries to retrieve information about the current Symfony version.
*
* @return string One of: unknown, dev, stable, eom, eol
*/
private function requestSymfonyState()
{
$versionInfo = null;

// get version information from cache or the roadmap
$versionCachePath = $this->kernel->getCacheDir().'/version_info.json';
if (file_exists($versionCachePath)) {
$versionInfo = json_decode(file_get_contents($versionCachePath), true);
} else {
$versionResponse = @file_get_contents('http://symfony.com/roadmap.json?version='.preg_replace('/^(\d+\.\d+).*/', '\\1', $this->data['symfony_version']));

if (false !== $versionResponse) {
$versionInfo = json_decode($versionResponse, true);

if (isset($versionInfo['error_message'])) {
// wrong version
$versionInfo = null;
}
}
}

// get the version state
$versionState = 'unknown';
if (null !== $versionInfo) {
$now = new \DateTime();
$eom = \DateTime::createFromFormat('m/Y', $versionInfo['eom'])->modify('last day of this month');
$eol = \DateTime::createFromFormat('m/Y', $versionInfo['eol'])->modify('last day of this month');

if ($now > $eom) {
$versionState = 'eom';
} elseif ($now > $eol) {
$versionState = 'eol';
} elseif ('DEV' === Kernel::EXTRA_VERSION) {
$versionState = 'dev';
} else {
$versionState = 'stable';
}
}

// invalidate or create cache
if (null === $versionInfo) {
// nothing to cache
} elseif (isset($versionInfo['previous_state'])) {
if ($versionInfo['previous_state'] !== $versionState) {
// state changed => invalidate the cache
unlink($versionCachePath);
}
} elseif (substr(Kernel::VERSION, 0, 3) !== $versionInfo['version']) {
// version changed => invalidate the cache
unlink($versionCachePath);
} elseif ($this->cacheVersionInfo) {
// no cache yet
$versionInfo['previous_state'] = $versionState;
file_put_contents($versionCachePath, json_encode($versionInfo));
}

return $versionState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function testCollect()
{
$kernel = new KernelForTest('test', true);
$c = new ConfigDataCollector();
$c->setCacheVersionInfo(false);
$c->setKernel($kernel);
$c->collect(new Request(), new Response());

Expand Down