2929use function filter_var ;
3030use function getcwd ;
3131use function implode ;
32+ use function in_array ;
3233use function is_array ;
3334use function is_string ;
3435use function json_decode ;
@@ -53,9 +54,8 @@ class CreateEcosystemDatabase extends Command
5354
5455 private CurlHandle $ curl ;
5556 private CurlHandle $ githubCurl ;
56- private ?string $ ghToken = null ;
57- private bool $ forceRebuild = false ;
58- private array $ validPackages = [];
57+ private ?string $ ghToken = null ;
58+ private bool $ forceRebuild = false ;
5959 public PdoMapper $ mapper ;
6060
6161 /** @var string[] */
@@ -218,8 +218,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
218218 $ pdo = $ this ->createDatabase ($ dbFile );
219219 $ this ->initCurl ();
220220
221+ $ validPackages = [];
221222 /** @var array{packagistUrl: string, keywords: array<string>, homepage: string, category: string} $userData */
222223 foreach ($ userDataArray as $ userData ) {
224+ $ urlComponents = [];
225+ preg_match ('/packagist.org\/packages\/((?>\w-?)+\/(?>\w-?)+)/i ' , $ userData ['packagistUrl ' ], $ urlComponents );
226+
227+ if (in_array ($ urlComponents [1 ], $ validPackages )) {
228+ continue ;
229+ }
230+
231+ $ validPackages [] = $ urlComponents [1 ];
232+
233+ if (! $ this ->forceRebuild ) {
234+ $ existingPackage = $ this ->mapper ->searchPackage ($ urlComponents [1 ]);
235+ if ($ existingPackage !== null && $ existingPackage !== []) {
236+ continue ;
237+ }
238+ }
239+
240+ $ userData ['packagistUrl ' ] = $ urlComponents [1 ];
241+
223242 $ curlResult = $ this ->getPackageData ($ userData );
224243 if ($ curlResult === null ) {
225244 continue ;
@@ -235,7 +254,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
235254
236255 if (! $ this ->forceRebuild ) {
237256 $ currentPackages = $ this ->mapper ->getPackagesTitles ();
238- $ removedPackages = array_diff ($ currentPackages , $ this -> validPackages );
257+ $ removedPackages = array_diff ($ currentPackages , $ validPackages );
239258 /** @var string $package */
240259 foreach ($ removedPackages as $ package ) {
241260 $ this ->mapper ->deletePackageByName ($ package );
@@ -285,20 +304,9 @@ public function createDatabase(string $path): PDO
285304 */
286305 private function getPackageData (array $ userData ): ?array
287306 {
288- $ urlComponents = [];
289- preg_match ('/packagist.org\/packages\/((?>\w-?)+\/(?>\w-?)+)/i ' , $ userData ['packagistUrl ' ], $ urlComponents );
290-
291- if (! $ this ->forceRebuild ) {
292- $ this ->validPackages [] = $ urlComponents [1 ];
293- $ existingPackage = $ this ->mapper ->searchPackage ($ urlComponents [1 ]);
294- if ($ existingPackage !== null && $ existingPackage !== []) {
295- return null ;
296- }
297- }
298-
299307 $ packagistUrl = sprintf (
300308 'https://packagist.org/packages/%s.json ' ,
301- $ urlComponents [ 1 ]
309+ $ userData [ ' packagistUrl ' ]
302310 );
303311 curl_setopt ($ this ->curl , CURLOPT_URL , $ packagistUrl );
304312 $ rawResult = curl_exec ($ this ->curl );
0 commit comments