This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
239 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Narrowspark\Automatic; | ||
|
||
use Composer\IO\IOInterface; | ||
use Composer\Semver\Constraint\Constraint; | ||
use Composer\Semver\VersionParser; | ||
|
||
final class TagsManager | ||
{ | ||
/** | ||
* The composer io implementation. | ||
* | ||
* @var \Composer\IO\IOInterface | ||
*/ | ||
private $io; | ||
|
||
/** | ||
* A version parser instance. | ||
* | ||
* @var \Composer\Semver\VersionParser | ||
*/ | ||
private $versionParser; | ||
|
||
/** | ||
* @var \Composer\Semver\Constraint\Constraint[] | ||
*/ | ||
private $legacyTags = []; | ||
|
||
/** | ||
* TagsManager constructor. | ||
* | ||
* @param IOInterface $io | ||
*/ | ||
public function __construct(IOInterface $io) | ||
{ | ||
$this->io = $io; | ||
$this->versionParser = new VersionParser(); | ||
|
||
$this->addConstraint('symfony/symfony', '>=3.4'); | ||
} | ||
|
||
/** | ||
* Add a legacy package constraint. | ||
* | ||
* @param string $name | ||
* @param string $require | ||
* | ||
* @return void | ||
*/ | ||
public function addConstraint(string $name, string $require): void | ||
{ | ||
$this->legacyTags[$name] = $this->versionParser->parseConstraints($require); | ||
} | ||
|
||
/** | ||
* Check if the provider is supported. | ||
* | ||
* @param string $file the composer provider file name | ||
* | ||
* @return bool | ||
*/ | ||
public function hasProvider(string $file): bool | ||
{ | ||
foreach ($this->legacyTags as $name => $constraint) { | ||
[$namespace, $packageName] = \explode('/', $name, 2); | ||
|
||
if (\mb_strpos($file, \sprintf('provider-%s$', $namespace)) !== false) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* | ||
* @return array | ||
*/ | ||
public function removeLegacyTags(array $data): array | ||
{ | ||
if (\count($data) === 0) { | ||
return $data; | ||
} | ||
|
||
foreach ($data['packages'] as $name => $versions) { | ||
if (! isset($this->legacyTags[$name])) { | ||
continue; | ||
} | ||
|
||
foreach ($versions as $version => $package) { | ||
foreach ($this->legacyTags[$name] as $legacyName => $legacyVersion) { | ||
if (isset($data['packages'][$legacyName]) && $data['packages'][$legacyName][\preg_replace('/^(\d++\.\d++)\..*/', '$1.x-dev', $version)]['replace'][$name] !== 'self.version') { | ||
continue; | ||
} | ||
} | ||
|
||
$normalizedVersion = $package['extra']['branch-alias'][$version] ?? null; | ||
$normalizedVersion = $normalizedVersion ? $this->versionParser->normalize($normalizedVersion) : $package['version_normalized']; | ||
|
||
if (! $this->legacyTags[$name]->matches(new Constraint('==', $normalizedVersion))) { | ||
$this->io->writeError(\sprintf('<info>Restricting packages listed in [%s] to [%s]</info>', $name, (string) $this->legacyTags[$name])); | ||
|
||
unset($data['packages'][$name][$version]); | ||
} | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
tests/Automatic/Fixture/Packagist/provider-codeigniter$framework.json
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/Automatic/Fixture/Packagist/provider-symfony$security-guard.json
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/Automatic/Fixture/Packagist/provider-symfony$symfony.json
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/Automatic/Fixture/Packagist/provider-zendframework$zend-diactoros.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Narrowspark\Automatic\Test; | ||
|
||
use Composer\IO\IOInterface; | ||
use Narrowspark\Automatic\TagsManager; | ||
use Narrowspark\TestingHelper\Phpunit\MockeryTestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TagsManagerTest extends MockeryTestCase | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $downloadFileList; | ||
|
||
/** | ||
* @var \Composer\IO\IOInterface|\Mockery\MockInterface | ||
*/ | ||
private $ioMock; | ||
|
||
/** | ||
* @var \Narrowspark\Automatic\TagsManager | ||
*/ | ||
private $tagsManger; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$pPath = __DIR__ . \DIRECTORY_SEPARATOR . 'Fixture' . \DIRECTORY_SEPARATOR . 'Packagist'; | ||
|
||
$this->downloadFileList = [ | ||
$pPath . \DIRECTORY_SEPARATOR . 'provider-codeigniter$framework.json', | ||
$pPath . \DIRECTORY_SEPARATOR . 'provider-symfony$security-guard.json', | ||
$pPath . \DIRECTORY_SEPARATOR . 'provider-symfony$symfony.json', | ||
$pPath . \DIRECTORY_SEPARATOR . 'provider-zendframework$zend-diactoros.json', | ||
]; | ||
|
||
$this->ioMock = $this->mock(IOInterface::class); | ||
$this->tagsManger = new TagsManager($this->ioMock); | ||
} | ||
|
||
public function testHasProvider(): void | ||
{ | ||
$count = 0; | ||
|
||
$this->tagsManger->addConstraint('symfony/security-guard', '>=4.1'); | ||
|
||
foreach ($this->downloadFileList as $file) { | ||
if ($this->tagsManger->hasProvider($file)) { | ||
$count++; | ||
} | ||
} | ||
|
||
static::assertSame(2, $count); | ||
} | ||
|
||
public function testRemoveLegacyTagsWithSymfony(): void | ||
{ | ||
$originalData = \json_decode(\file_get_contents($this->downloadFileList[3]), true); | ||
|
||
$data = $this->tagsManger->removeLegacyTags($originalData); | ||
|
||
static::assertNotSame($originalData['packages'], $data['packages']); | ||
} | ||
} |