Skip to content

Commit

Permalink
Merge pull request #148 from elliotchance/1.3.1/148-installed-json-do…
Browse files Browse the repository at this point in the history
…es-not-exist

Throws ugly warning if installed.json does not exist
  • Loading branch information
elliotchance committed Sep 3, 2014
2 parents 48693be + fc26bf4 commit 420865f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Concise/Version.php
Expand Up @@ -4,9 +4,26 @@

class Version
{
public function findVendorFolder()
{
$path = __DIR__;
for ($i = 0; $i < 10; ++$i) {
$dir = scandir($path);
if (in_array('vendor', $dir)) {
return "$path/vendor";
}
$path .= "/..";
}
}

public function getVersionForPackage($packageName)
{
$packages = json_decode(file_get_contents(__DIR__ . '/../../vendor/composer/installed.json'));
$vendor = $this->findVendorFolder(__DIR__);
if (!$vendor) {
return '';
}

$packages = json_decode(file_get_contents("$vendor/composer/installed.json"));
foreach ($packages as $package) {
if ($package->name === $packageName) {
return $package->version;
Expand Down
13 changes: 13 additions & 0 deletions tests/Concise/VersionTest.php
Expand Up @@ -24,4 +24,17 @@ public function testWeCanEasilyGetTheConciseVersion()
{
$this->assert($this->version->getConciseVersion(), equals, $this->version->getVersionForPackage('elliotchance/concise'));
}

public function testFindingVendorFolder()
{
$this->assert($this->version->findVendorFolder(), ends_with, '/vendor');
}

public function testReturnEmptyStringIfVendorFolderCannotBeFound()
{
$version = $this->niceMock('Concise\Version')
->stub('findVendorFolder')
->done();
$this->assert($version->getConciseVersion(), is_blank);
}
}

0 comments on commit 420865f

Please sign in to comment.