Skip to content
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

Expose root package name #56

Merged
merged 1 commit into from
Feb 5, 2018
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
4 changes: 3 additions & 1 deletion src/PackageVersions/FallbackVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
final class FallbackVersions
{
const ROOT_PACKAGE_NAME = 'unknown/root-package@UNKNOWN';

private function __construct()
{
}
Expand Down Expand Up @@ -73,6 +75,6 @@ private static function getVersions(string $composerLockFile) : \Generator
);
}

yield 'unknown/root-package@UNKNOWN';
yield self::ROOT_PACKAGE_NAME;
}
}
21 changes: 13 additions & 8 deletions src/PackageVersions/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class Installer implements PluginInterface, EventSubscriberInterface
*/
%s
{
const ROOT_PACKAGE_NAME = '%s';
const VERSIONS = %s;

private function __construct()
Expand All @@ -40,13 +41,13 @@ private function __construct()
*/
public static function getVersion(string $packageName) : string
{
if (! isset(self::VERSIONS[$packageName])) {
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
if (isset(self::VERSIONS[$packageName])) {
return self::VERSIONS[$packageName];
}

return self::VERSIONS[$packageName];
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
}
}

Expand Down Expand Up @@ -81,22 +82,26 @@ public static function getSubscribedEvents()
public static function dumpVersionsClass(Event $composerEvent)
{
$composer = $composerEvent->getComposer();
$versions = iterator_to_array(self::getVersions($composer->getLocker(), $composer->getPackage()));
$rootPackage = $composer->getPackage();
$versions = iterator_to_array(self::getVersions($composer->getLocker(), $rootPackage));

if (!array_key_exists('ocramius/package-versions', $versions)) {
//plugin must be globally installed - we only want to generate versions for projects which specifically
//require ocramius/package-versions
return;
}

self::writeVersionClassToFile(self::generateVersionsClass($versions), $composer, $composerEvent->getIO());
$versionClass = self::generateVersionsClass($rootPackage->getName(), $versions);

self::writeVersionClassToFile($versionClass, $composer, $composerEvent->getIO());
}

private static function generateVersionsClass(array $versions) : string
private static function generateVersionsClass(string $rootPackageName, array $versions) : string
{
return sprintf(
self::$generatedClassTemplate,
'fin' . 'al ' . 'cla' . 'ss ' . 'Versions', // note: workaround for regex-based code parsers :-(
$rootPackageName,
var_export($versions, true)
);
}
Expand Down
1 change: 1 addition & 0 deletions src/PackageVersions/Versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
final class Versions
{
const ROOT_PACKAGE_NAME = FallbackVersions::ROOT_PACKAGE_NAME;
const VERSIONS = [];

private function __construct()
Expand Down
47 changes: 26 additions & 21 deletions test/PackageVersionsTest/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Composer\Installer\InstallationManager;
use Composer\IO\IOInterface;
use Composer\Package\Locker;
use Composer\Package\Package;
use Composer\Package\RootAliasPackage;
use Composer\Package\RootPackage;
use Composer\Package\RootPackageInterface;
Expand Down Expand Up @@ -157,6 +156,7 @@ public function testDumpVersionsClass()
*/
final class Versions
{
const ROOT_PACKAGE_NAME = 'root/package';
const VERSIONS = array (
'ocramius/package-versions' => '1.0.0@',
'foo/bar' => '1.2.3@abc123',
Expand All @@ -174,13 +174,13 @@ private function __construct()
*/
public static function getVersion(string $packageName) : string
{
if (! isset(self::VERSIONS[$packageName])) {
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
if (isset(self::VERSIONS[$packageName])) {
return self::VERSIONS[$packageName];
}

return self::VERSIONS[$packageName];
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
}
}

Expand Down Expand Up @@ -261,6 +261,7 @@ public function testDumpVersionsClassNoDev()
*/
final class Versions
{
const ROOT_PACKAGE_NAME = 'root/package';
const VERSIONS = array (
'ocramius/package-versions' => '1.0.0@',
'foo/bar' => '1.2.3@abc123',
Expand All @@ -277,13 +278,13 @@ private function __construct()
*/
public static function getVersion(string $packageName) : string
{
if (! isset(self::VERSIONS[$packageName])) {
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
if (isset(self::VERSIONS[$packageName])) {
return self::VERSIONS[$packageName];
}

return self::VERSIONS[$packageName];
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
}
}

Expand Down Expand Up @@ -366,6 +367,7 @@ public function testDumpVersionsWithoutPackageSourceDetails()
*/
final class Versions
{
const ROOT_PACKAGE_NAME = 'root/package';
const VERSIONS = array (
'ocramius/package-versions' => '1.0.0@',
'foo/bar' => '1.2.3@abc123',
Expand All @@ -382,13 +384,13 @@ private function __construct()
*/
public static function getVersion(string $packageName) : string
{
if (! isset(self::VERSIONS[$packageName])) {
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
if (isset(self::VERSIONS[$packageName])) {
return self::VERSIONS[$packageName];
}

return self::VERSIONS[$packageName];
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
}
}

Expand Down Expand Up @@ -594,6 +596,8 @@ public function testVersionsAreNotDumpedIfPackageIsScheduledForRemoval()
],
]);

$package->expects(self::any())->method('getName')->willReturn('root/package');

$this->composer->expects(self::any())->method('getConfig')->willReturn($config);
$this->composer->expects(self::any())->method('getLocker')->willReturn($locker);
$this->composer->expects(self::any())->method('getPackage')->willReturn($package);
Expand Down Expand Up @@ -768,6 +772,7 @@ public function testGetVersionsIsNotNormalizedForRootPackage()
*/
final class Versions
{
const ROOT_PACKAGE_NAME = 'root/package';
const VERSIONS = array (
'ocramius/package-versions' => '1.0.0@',
'root/package' => '1.3.5@aaabbbcccddd',
Expand All @@ -782,13 +787,13 @@ private function __construct()
*/
public static function getVersion(string $packageName) : string
{
if (! isset(self::VERSIONS[$packageName])) {
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
if (isset(self::VERSIONS[$packageName])) {
return self::VERSIONS[$packageName];
}

return self::VERSIONS[$packageName];
throw new \OutOfBoundsException(
'Required package "' . $packageName . '" is not installed: cannot detect its version'
);
}
}

Expand Down