Browse Source

Support multiple package updates from one same repo url

Jordi Boggiano 11 năm trước cách đây
mục cha
commit
7025a13db1
1 tập tin đã thay đổi với 27 bổ sung24 xóa
  1. 27 24
      src/Packagist/WebBundle/Controller/ApiController.php

+ 27 - 24
src/Packagist/WebBundle/Controller/ApiController.php

@@ -225,9 +225,9 @@ class ApiController extends Controller
         }
 
         // try to find the user package
-        $package = $this->findPackageByUrl($user, $url, $urlRegex);
+        $packages = $this->findPackagesByUrl($user, $url, $urlRegex);
 
-        if (!$package) {
+        if (!$packages) {
             return new Response(json_encode(array('status' => 'error', 'message' => 'Could not find a package that matches this request (does user maintain the package?)')), 404);
         }
 
@@ -240,23 +240,25 @@ class ApiController extends Controller
         $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE);
 
         try {
-            $em->transactional(function($em) use ($package, $updater, $io) {
-                // prepare dependencies
-                $config = Factory::createConfig();
-                $io->loadConfiguration($config);
-                $loader = new ValidatingArrayLoader(new ArrayLoader());
-
-                // prepare repository
-                $repository = new VcsRepository(array('url' => $package->getRepository()), $io, $config);
-                $repository->setLoader($loader);
-
-                // perform the actual update (fetch and re-scan the repository's source)
-                $updater->update($package, $repository);
-
-                // update the package entity
-                $package->setAutoUpdated(true);
-                $em->flush();
-            });
+            foreach ($packages as $package) {
+                $em->transactional(function($em) use ($package, $updater, $io) {
+                    // prepare dependencies
+                    $config = Factory::createConfig();
+                    $io->loadConfiguration($config);
+                    $loader = new ValidatingArrayLoader(new ArrayLoader());
+
+                    // prepare repository
+                    $repository = new VcsRepository(array('url' => $package->getRepository()), $io, $config);
+                    $repository->setLoader($loader);
+
+                    // perform the actual update (fetch and re-scan the repository's source)
+                    $updater->update($package, $repository);
+
+                    // update the package entity
+                    $package->setAutoUpdated(true);
+                    $em->flush();
+                });
+            }
         } catch (\Exception $e) {
             if ($e instanceof InvalidRepositoryException) {
                 $this->get('packagist.package_manager')->notifyUpdateFailure($package, $e, $io->getOutput());
@@ -300,23 +302,24 @@ class ApiController extends Controller
      * @param User $user
      * @param string $url
      * @param string $urlRegex
-     * @return Package|null the found package or null otherwise
+     * @return array the packages found
      */
-    protected function findPackageByUrl(User $user, $url, $urlRegex)
+    protected function findPackagesByUrl(User $user, $url, $urlRegex)
     {
         if (!preg_match($urlRegex, $url, $matched)) {
-            return null;
+            return array();
         }
 
+        $packages = array();
         foreach ($user->getPackages() as $package) {
             if (preg_match($urlRegex, $package->getRepository(), $candidate)
                 && $candidate['host'] === $matched['host']
                 && $candidate['path'] === $matched['path']
             ) {
-                return $package;
+                $packages[] = $package;
             }
         }
 
-        return null;
+        return $packages;
     }
 }