-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DX] Introduce set providers, to enable package + version based set r…
…egistration (#5976) * kick of set provider * should not happen must have string message * add TwigSetProvider * register twig set providers on load * polyfill is internal set * add InstalledPackageResolver * add simple test for SetCollector * add simple InstalledPackageResolverTest * use SetManager name as more features * add cache
- Loading branch information
1 parent
f366a7e
commit 2dda748
Showing
15 changed files
with
396 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Composer; | ||
|
||
use Nette\Utils\FileSystem; | ||
use Nette\Utils\Json; | ||
use Rector\Composer\ValueObject\InstalledPackage; | ||
use Rector\Exception\ShouldNotHappenException; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see \Rector\Tests\Composer\InstalledPackageResolverTest | ||
*/ | ||
final class InstalledPackageResolver | ||
{ | ||
/** | ||
* @var array<string, InstalledPackage[]> | ||
*/ | ||
private array $resolvedInstalledPackages = []; | ||
|
||
|
||
/** | ||
* @return InstalledPackage[] | ||
*/ | ||
public function resolve(string $projectDirectory): array | ||
{ | ||
// cache | ||
if (isset($this->resolvedInstalledPackages[$projectDirectory])) { | ||
return $this->resolvedInstalledPackages[$projectDirectory]; | ||
} | ||
|
||
Assert::directory($projectDirectory); | ||
|
||
$installedPackagesFilePath = $projectDirectory . '/vendor/composer/installed.json'; | ||
if (! file_exists($installedPackagesFilePath)) { | ||
throw new ShouldNotHappenException( | ||
'The installed package json not found. Make sure you run `composer update` and "vendor/composer/installed.json" file exists' | ||
); | ||
} | ||
|
||
$installedPackageFileContents = FileSystem::read($installedPackagesFilePath); | ||
$installedPackagesFilePath = Json::decode($installedPackageFileContents, true); | ||
|
||
$installedPackages = []; | ||
|
||
foreach ($installedPackagesFilePath['packages'] as $installedPackage) { | ||
$installedPackages[] = new InstalledPackage( | ||
$installedPackage['name'], | ||
$installedPackage['version_normalized'] | ||
); | ||
} | ||
|
||
$this->resolvedInstalledPackages[$projectDirectory] = $installedPackages; | ||
|
||
return $installedPackages; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Composer\ValueObject; | ||
|
||
final readonly class InstalledPackage | ||
{ | ||
public function __construct( | ||
private string $name, | ||
private string $version, | ||
) { | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getVersion(): string | ||
{ | ||
return $this->version; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Set\Contract; | ||
|
||
interface SetInterface | ||
{ | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Set\Contract; | ||
|
||
interface SetProviderInterface | ||
{ | ||
/** | ||
* @return SetInterface[] | ||
*/ | ||
public function provide(): array; | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Set\Enum; | ||
|
||
final class SetGroup | ||
{ | ||
public const TWIG = 'twig'; | ||
} |
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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Set; | ||
|
||
use Rector\Composer\InstalledPackageResolver; | ||
use Rector\Set\Contract\SetProviderInterface; | ||
use Rector\Set\ValueObject\ComposerTriggeredSet; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see \Rector\Tests\Set\SetCollector\SetCollectorTest | ||
*/ | ||
final readonly class SetManager | ||
{ | ||
/** | ||
* @param SetProviderInterface[] $setProviders | ||
*/ | ||
public function __construct( | ||
private array $setProviders | ||
) { | ||
Assert::allIsInstanceOf($setProviders, SetProviderInterface::class); | ||
} | ||
|
||
/** | ||
* @return ComposerTriggeredSet[] | ||
*/ | ||
public function matchComposerTriggered(string $groupName): array | ||
{ | ||
$matchedSets = []; | ||
|
||
foreach ($this->setProviders as $setProvider) { | ||
foreach ($setProvider->provide() as $set) { | ||
if (! $set instanceof ComposerTriggeredSet) { | ||
continue; | ||
} | ||
|
||
if ($set->getGroupName() === $groupName) { | ||
$matchedSets[] = $set; | ||
} | ||
} | ||
} | ||
|
||
return $matchedSets; | ||
} | ||
|
||
/** | ||
* @param string[] $setGroups | ||
* @return string[] | ||
*/ | ||
public function matchBySetGroups(array $setGroups): array | ||
{ | ||
$installedPackageResolver = new InstalledPackageResolver(); | ||
$installedComposerPackages = $installedPackageResolver->resolve(getcwd()); | ||
|
||
$groupLoadedSets = []; | ||
|
||
foreach ($setGroups as $setGroup) { | ||
$composerTriggeredSets = $this->matchComposerTriggered($setGroup); | ||
|
||
foreach ($composerTriggeredSets as $composerTriggeredSet) { | ||
if ($composerTriggeredSet->matchInstalledPackages($installedComposerPackages)) { | ||
// @todo add debug note somewhere | ||
// echo sprintf('Loaded "%s" set as it meets the conditions', $composerTriggeredSet->getSetFilePath()); | ||
|
||
// it matched composer package + version requirements → load set | ||
$groupLoadedSets[] = $composerTriggeredSet->getSetFilePath(); | ||
} | ||
} | ||
} | ||
|
||
return $groupLoadedSets; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Set\SetProvider; | ||
|
||
use Rector\Set\Contract\SetInterface; | ||
use Rector\Set\Contract\SetProviderInterface; | ||
use Rector\Set\Enum\SetGroup; | ||
use Rector\Set\ValueObject\ComposerTriggeredSet; | ||
use Rector\Symfony\Set\TwigSetList; | ||
|
||
/** | ||
* Temporary location, move to rector-symfony package once this is merged | ||
* @experimental 2024-06 | ||
*/ | ||
final class TwigSetProvider implements SetProviderInterface | ||
{ | ||
/** | ||
* @return SetInterface[] | ||
*/ | ||
public function provide(): array | ||
{ | ||
// @todo temporary name to test, these will be located in rector-symfony, rector-doctrine, rector-phpunit packages | ||
return [new ComposerTriggeredSet(SetGroup::TWIG, 'twig/twig', '1.12', TwigSetList::TWIG_112)]; | ||
} | ||
} |
Oops, something went wrong.