55use CodedMonkey \Dirigent \Composer \ComposerClient ;
66use CodedMonkey \Dirigent \Composer \ConfigFactory ;
77use CodedMonkey \Dirigent \Doctrine \Entity \Version ;
8+ use CodedMonkey \Dirigent \Message \ResolveDistribution ;
89use Composer \IO \NullIO ;
910use Composer \Pcre \Preg ;
1011use Composer \Util \Filesystem as ComposerFilesystem ;
1314use Composer \Util \Url ;
1415use Symfony \Component \DependencyInjection \Attribute \Autowire ;
1516use Symfony \Component \Filesystem \Filesystem ;
17+ use Symfony \Component \Messenger \Envelope ;
18+ use Symfony \Component \Messenger \MessageBusInterface ;
19+ use Symfony \Component \Messenger \Stamp \TransportNamesStamp ;
1620
1721readonly class PackageDistributionResolver
1822{
1923 private Filesystem $ filesystem ;
2024 private string $ distributionStoragePath ;
2125
2226 public function __construct (
27+ private MessageBusInterface $ messenger ,
2328 private ComposerClient $ composer ,
24- #[Autowire(param: 'dirigent.dist_builder.enabled ' )]
29+ #[Autowire(param: 'dirigent.distributions.build ' )]
2530 private bool $ buildDistributions ,
26- #[Autowire(param: 'dirigent.dist_builder.dev_packages ' )]
27- private bool $ buildDevDistributions ,
31+ #[Autowire(param: 'dirigent.distributions.mirror ' )]
32+ private bool $ mirrorDistributions ,
33+ #[Autowire(param: 'dirigent.distributions.dev_versions ' )]
34+ private bool $ includeDevVersions ,
2835 #[Autowire(param: 'dirigent.storage.path ' )]
2936 string $ storagePath ,
3037 ) {
@@ -42,7 +49,7 @@ public function path(string $packageName, string $versionName, string $reference
4249 return "$ this ->distributionStoragePath / $ packageName/ $ versionName- $ reference. $ type " ;
4350 }
4451
45- public function resolve (Version $ version , string $ reference , string $ type ): bool
52+ public function resolve (Version $ version , string $ reference , string $ type, bool $ async ): bool
4653 {
4754 $ package = $ version ->getPackage ();
4855 $ packageName = $ package ->getName ();
@@ -52,17 +59,27 @@ public function resolve(Version $version, string $reference, string $type): bool
5259 return true ;
5360 }
5461
55- if (
56- null === $ version ->getDist ()
57- && $ this ->buildDistributions
58- && (!$ version ->isDevelopment () || $ this ->buildDevDistributions )
59- ) {
60- return $ this ->build ($ version , $ reference , $ type );
61- } elseif (null !== $ version ->getDist ()) {
62- return $ this ->mirror ($ version , $ reference , $ type );
62+ if ($ version ->isDevelopment () && !$ this ->includeDevVersions ) {
63+ return false ;
64+ }
65+
66+ if ($ async ) {
67+ // Resolve the distribution asynchronously so it's available in the future now that we know it was requested
68+ $ message = Envelope::wrap (new ResolveDistribution ($ version ->getId (), $ reference , $ type ))
69+ ->with (new TransportNamesStamp ('async ' ));
70+ $ this ->messenger ->dispatch ($ message );
71+
72+ // Still return false so the service resolving the distribution doesn't try to fetch it anyway
73+ return false ;
6374 }
6475
65- return false ;
76+ $ hasDistribution = null !== $ version ->getDist ();
77+
78+ return match (true ) {
79+ $ this ->buildDistributions && !$ hasDistribution => $ this ->build ($ version , $ reference , $ type ),
80+ $ this ->mirrorDistributions && $ hasDistribution => $ this ->mirror ($ version , $ reference , $ type ),
81+ default => false ,
82+ };
6683 }
6784
6885 private function build (Version $ version , string $ reference , string $ type ): bool
0 commit comments