diff --git a/src/Concise/Version.php b/src/Concise/Version.php index 44811774..e25a2ee5 100644 --- a/src/Concise/Version.php +++ b/src/Concise/Version.php @@ -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; diff --git a/tests/Concise/VersionTest.php b/tests/Concise/VersionTest.php index 982a25a6..3fd6efae 100644 --- a/tests/Concise/VersionTest.php +++ b/tests/Concise/VersionTest.php @@ -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); + } }