Skip to content

Commit

Permalink
Merge pull request #56 from rybakit/root_package_name
Browse files Browse the repository at this point in the history
Expose root package name
  • Loading branch information
Ocramius committed Feb 5, 2018
2 parents 30d4856 + f477ae8 commit 633b8db
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/PackageVersions/FallbackVersions.php
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
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
Expand Up @@ -12,6 +12,7 @@
*/
final class Versions
{
const ROOT_PACKAGE_NAME = FallbackVersions::ROOT_PACKAGE_NAME;
const VERSIONS = [];

private function __construct()
Expand Down
46 changes: 26 additions & 20 deletions test/PackageVersionsTest/InstallerTest.php
Expand Up @@ -157,6 +157,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 @@ -175,13 +176,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 @@ -262,6 +263,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 @@ -279,13 +281,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 @@ -368,6 +370,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 @@ -385,13 +388,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 @@ -599,6 +602,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 @@ -774,6 +779,7 @@ public function testGetVersionsIsNotNormalizedForRootPackage()
*/
final class Versions
{
const ROOT_PACKAGE_NAME = 'root/package';
const VERSIONS = array (
'ocramius/package-versions' => '1.0.0@',
'some-replaced/package' => 'self.version@aaabbbcccddd',
Expand All @@ -789,13 +795,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

0 comments on commit 633b8db

Please sign in to comment.