Skip to content

Commit

Permalink
Added feedback about the current symfony version
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Apr 3, 2015
1 parent ac389b6 commit a4551f9
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
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
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;
}
}
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

5 comments on commit a4551f9

@tgabi333
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better with a full javascript solution?

@wouterj
Copy link
Member Author

@wouterj wouterj commented on a4551f9 Apr 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tgabi333 I don't think so. As:

(1) We can't cache it
(2) The AJAX requests will be catched by the AJAX data collector
(3) Afaik, there is no real advantage of JS requests, compared to PHP requests

@tgabi333
Copy link
Contributor

@tgabi333 tgabi333 commented on a4551f9 Apr 5, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@King2500
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this feature configurable?

@tgabi333
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the release model is time based, you can easily calculate release, eom and eol dates. What would be the disadvantages of it?

For the current implementation:

  • stricter type check is needed, any malformed but valid json would break symfony instances all around the world. (Undefined index would throw an exception)

Please sign in to comment.