Skip to content

Commit

Permalink
Show required versions
Browse files Browse the repository at this point in the history
  • Loading branch information
milo committed Jan 18, 2016
1 parent 97f179d commit bf3680d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Vendor Versions
Vendor Versions is a bar panel for [Tracy](https://tracy.nette.org/) debugger. It loads `composer.lock` file and shows you versions of all currently installed libraries.
Vendor Versions is a bar panel for [Tracy](https://tracy.nette.org/) debugger. It loads `composer.json` and `composer.lock` files and shows you versions of all currently installed libraries.

![Tracy panel screenshot](https://github.com/milo/vendor-versions/raw/master/screenshot.png)


# Installation
Expand All @@ -10,7 +12,7 @@ If you are using Nette DI container, register panel in `config.neon`:
extensions:
vendorVersions: Milo\VendorVersions\Bridges\Nette\DI\Extension(%debugMode%)

# Optionally set path do directory with composer.lock file
# Optionally set path do directory with composer.json and composer.lock files
vendorVersions:
dir: 'some/path'
```
Expand All @@ -22,12 +24,8 @@ Tracy\Debugger::getBar()->addPanel(
);


# Optionally with a path to directory with composer.lock file
# Optionally with a path to directory with composer.json and composer.lock files
Tracy\Debugger::getBar()->addPanel(
new Milo\VendorVersions\Panel(__DIR__ . '/some/dir')
);
```


# Screenshot
![Tracy panel screenshot](https://github.com/milo/vendor-versions/raw/master/screenshot.png)
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 39 additions & 17 deletions src/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,17 @@ public function getPanel()
{
ob_start();

if ($this->error === NULL) {
$file = $this->dir . DIRECTORY_SEPARATOR . 'composer.lock';
$json = @file_get_contents($file);
if ($json === FALSE) {
$this->error = error_get_last()['message'];
return NULL;
}

$decoded = @json_decode($json, TRUE);
if (!is_array($decoded)) {
$this->error = error_get_last()['message'];
return NULL;
}
$jsonFile = $this->dir . DIRECTORY_SEPARATOR . 'composer.json';
$lockFile = $this->dir . DIRECTORY_SEPARATOR . 'composer.lock';

$required = $this->decode($jsonFile);
$installed = $this->decode($lockFile);

if ($this->error === NULL) {
$required += ['require' => [], 'require-dev' => []];
$data = [
'Packages' => self::format($decoded['packages']),
'Dev Packages' => self::format($decoded['packages-dev']),
'Packages' => self::format($installed['packages'], $required['require']),
'Dev Packages' => self::format($installed['packages-dev'], $required['require-dev']),
];
}

Expand All @@ -82,17 +76,23 @@ public function getPanel()

/**
* @param array $packages
* @param array $required
* @return array
*/
private static function format(array $packages)
private static function format(array $packages, array $required)
{
$data = [];
foreach ($packages as $p) {
$data[$p['name']] = (object) [
'version' => $p['version'] . ($p['version'] === 'dev-master'
'installed' => $p['version'] . ($p['version'] === 'dev-master'
? (' #' . substr($p['source']['reference'], 0, 7))
: ''
),

'required' => isset($required[$p['name']])
? $required[$p['name']]
: NULL,

'url' => isset($p['source']['url'])
? preg_replace('/\.git$/', '', $p['source']['url'])
: NULL,
Expand All @@ -103,4 +103,26 @@ private static function format(array $packages)
return $data;
}


/**
* @param string $file
* @return array|NULL
*/
private function decode($file)
{
$json = @file_get_contents($file);
if ($json === FALSE) {
$this->error = error_get_last()['message'];
return NULL;
}

$decoded = @json_decode($json, TRUE);
if (!is_array($decoded)) {
$this->error = error_get_last()['message'];
return NULL;
}

return $decoded;
}

}
18 changes: 13 additions & 5 deletions src/templates/Panel.panel.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function h($str) {
}

/**
* @var string|NULL $file
* @var string|NULL $lockFile
* @var string|NULL $jsonFile
* @var string|NULL $error
* @var array $data
*/
Expand All @@ -24,13 +25,18 @@ function h($str) {
}

.milo-VendorVersionsPanel .tracy-inner {
width: 350px;
width: 500px;
}

.milo-VendorVersionsPanel table {
white-space: nowrap;
font: 9pt/1.5 Consolas,monospace !important;
}

.milo-VendorVersionsPanel td.version {
/*color: green !important;*/
font-weight: bold !important;
}
</style>

<div class="milo-VendorVersionsPanel">
Expand All @@ -45,20 +51,22 @@ function h($str) {
<thead>
<tr>
<th>Name</th>
<th>Version</th>
<th>Installed</th>
<th>Required</th>
</tr>
</thead>
<tbody>
<?php foreach ($packages as $name => $p): ?>
<tr>
<td><?= $p->url ? ("<a href='" . h($p->url) . "' target='_blank' rel='noreferrer'>" . h($name) . "</a>") : h($name) ?></td>
<td><?= h($p->version) ?></td>
<td class="version"><?= h($p->installed) ?></td>
<td><?= h($p->required) ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php endforeach ?>
<p><small>Source: <?= Helpers::editorLink($file) ?></small></p>
<p><small>Source: <?= Helpers::editorLink($jsonFile) . h(' & ') . Helpers::editorLink($lockFile) ?></small></p>
<?php endif ?>
</div>
</div>

0 comments on commit bf3680d

Please sign in to comment.