44
55use CodedMonkey \Dirigent \Doctrine \Repository \PackageRepository ;
66use CodedMonkey \Dirigent \Message \SchedulePackageUpdate ;
7+ use CodedMonkey \Dirigent \Message \UpdatePackage ;
78use Symfony \Component \Console \Attribute \AsCommand ;
89use Symfony \Component \Console \Command \Command ;
910use Symfony \Component \Console \Input \InputArgument ;
@@ -29,48 +30,92 @@ public function __construct(
2930 protected function configure (): void
3031 {
3132 $ this
32- ->addArgument ('package ' , InputArgument::OPTIONAL , 'Package to update ' )
33- ->addOption ('force ' , null , InputOption::VALUE_NONE , 'Forces a re-crawl of all packages ' );
33+ ->addArgument ('package ' , InputArgument::IS_ARRAY , 'Package to update ' )
34+ ->addOption ('all ' , null , InputOption::VALUE_NONE , 'Update all packages ' )
35+ ->addOption ('sync ' , null , InputOption::VALUE_NONE , 'Update packages synchronously ' )
36+ ->setHelp (<<<'TXT'
37+ The <info>%command.name%</info> command schedules packages in the registry for update:
38+
39+ <info>%command.full_name%</info>
40+
41+ By default, only packages that have passed the periodic update interval will be scheduled for update.
42+
43+ Use the <comment>--all</comment> option to schedule all packages for update instead:
44+
45+ <info>%command.full_name% --all</info>
46+
47+ It's possible to update specific packages by passing their name as arguments:
48+
49+ <info>%command.full_name% psr/cache psr/log</info>
50+
51+ Use the <comment>--sync</comment> option to update packages synchronously:
52+
53+ <info>%command.full_name% psr/cache psr/log --sync</info>
54+ TXT);
3455 }
3556
3657 protected function execute (InputInterface $ input , OutputInterface $ output ): int
3758 {
3859 $ io = new SymfonyStyle ($ input , $ output );
3960
40- $ force = $ input ->getOption ('force ' );
41- $ packageName = $ input ->getArgument ('package ' );
61+ $ all = $ input ->getOption ('all ' );
62+ $ packageNames = $ input ->getArgument ('package ' );
63+ $ sync = $ input ->getOption ('sync ' );
64+
65+ if ($ sync && !count ($ packageNames )) {
66+ $ io ->error ('Specify a package to update when using the --sync option. ' );
4267
68+ return Command::FAILURE ;
69+ }
70+
71+ // Force refresh updates even if alread
72+ $ forceRefresh = false ;
73+ // Randomize time of updates
4374 $ randomTimes = true ;
75+ // Schedule update even if already scheduled
4476 $ reschedule = false ;
4577
46- if ($ packageName ) {
47- if (null === $ package = $ this ->packageRepository ->findOneByName ($ packageName )) {
48- $ io ->error ("Package $ packageName not found " );
78+ if (count ($ packageNames )) {
79+ $ packageIds = [];
80+ foreach ($ packageNames as $ packageName ) {
81+ if (null === $ package = $ this ->packageRepository ->findOneByName ($ packageName )) {
82+ $ io ->error ("Package $ packageName not found " );
4983
50- return Command::FAILURE ;
51- }
84+ return Command::FAILURE ;
85+ }
5286
53- $ io ->writeln ("Scheduling package $ packageName for update... " );
54-
55- $ packages = [[ ' id ' => $ package -> getId ()]];
87+ $ io ->writeln ("Scheduling package $ packageName for update... " );
88+ $ packageIds [] = $ package -> getId ();
89+ }
5690
91+ $ forceRefresh = true ;
5792 $ randomTimes = false ;
5893 $ reschedule = true ;
59- } elseif ($ force ) {
94+ } elseif ($ all ) {
6095 $ io ->writeln ('Scheduling all packages for update... ' );
61- $ packages = $ this ->packageRepository ->getAllPackageIds ();
96+ $ packageIds = $ this ->packageRepository ->getAllPackageIds ();
6297
98+ $ forceRefresh = true ;
6399 $ reschedule = true ;
64100 } else {
65101 $ io ->writeln ('Scheduling stale packages for update... ' );
66- $ packages = $ this ->packageRepository ->getStalePackageIds ();
102+ $ packageIds = $ this ->packageRepository ->getStalePackageIds ();
103+ }
104+
105+ if ($ sync ) {
106+ foreach ($ packageIds as $ packageId ) {
107+ $ this ->messenger ->dispatch (new UpdatePackage ($ packageId , forceRefresh: $ forceRefresh ));
108+ }
109+
110+ $ packageCount = count ($ packageIds );
111+ $ io ->success ("Updated $ packageCount package(s). " );
67112 }
68113
69- foreach ($ packages as $ package ) {
70- $ this ->messenger ->dispatch (new SchedulePackageUpdate ($ package [ ' id ' ] , randomTime: $ randomTimes , reschedule: $ reschedule , forceRefresh: $ force ));
114+ foreach ($ packageIds as $ packageId ) {
115+ $ this ->messenger ->dispatch (new SchedulePackageUpdate ($ packageId , randomTime: $ randomTimes , reschedule: $ reschedule , forceRefresh: $ forceRefresh ));
71116 }
72117
73- $ packageCount = count ($ packages );
118+ $ packageCount = count ($ packageIds );
74119 $ io ->success ("Scheduled $ packageCount package(s) for update. " );
75120
76121 return Command::SUCCESS ;
0 commit comments