Browse Source

Add preFileDownload event on package.json fetch

Jérémy JOURDIN 11 years ago
parent
commit
fbadc19bf6

+ 11 - 9
src/Composer/Factory.php

@@ -220,8 +220,15 @@ class Factory
         // setup process timeout
         ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
 
+        // initialize composer
+        $composer = new Composer();
+        $composer->setConfig($config);
+
+        // initialize event dispatcher
+        $dispatcher = new EventDispatcher($composer, $io);
+
         // initialize repository manager
-        $rm = $this->createRepositoryManager($io, $config);
+        $rm = $this->createRepositoryManager($io, $config, $dispatcher);
 
         // load local repository
         $this->addLocalRepository($rm, $vendorDir);
@@ -234,16 +241,11 @@ class Factory
         // initialize installation manager
         $im = $this->createInstallationManager();
 
-        // initialize composer
-        $composer = new Composer();
-        $composer->setConfig($config);
+        // Composer composition
         $composer->setPackage($package);
         $composer->setRepositoryManager($rm);
         $composer->setInstallationManager($im);
 
-        // initialize event dispatcher
-        $dispatcher = new EventDispatcher($composer, $io);
-
         // initialize download manager
         $dm = $this->createDownloadManager($io, $config, $dispatcher);
 
@@ -285,9 +287,9 @@ class Factory
      * @param  Config                       $config
      * @return Repository\RepositoryManager
      */
-    protected function createRepositoryManager(IOInterface $io, Config $config)
+    protected function createRepositoryManager(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null)
     {
-        $rm = new RepositoryManager($io, $config);
+        $rm = new RepositoryManager($io, $config, $eventDispatcher);
         $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
         $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
         $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');

+ 11 - 2
src/Composer/Repository/ComposerRepository.php

@@ -22,6 +22,9 @@ use Composer\Cache;
 use Composer\Config;
 use Composer\IO\IOInterface;
 use Composer\Util\RemoteFilesystem;
+use Composer\Plugin\PluginEvents;
+use Composer\Plugin\PreFileDownloadEvent;
+use Composer\EventDispatcher\EventDispatcher;
 
 /**
  * @author Jordi Boggiano <j.boggiano@seld.be>
@@ -45,12 +48,13 @@ class ComposerRepository extends ArrayRepository implements StreamableRepository
     protected $loader;
     protected $rootAliases;
     protected $allowSslDowngrade = false;
+    protected $eventDispatcher;
     private $rawData;
     private $minimalPackages;
     private $degradedMode = false;
     private $rootData;
 
-    public function __construct(array $repoConfig, IOInterface $io, Config $config)
+    public function __construct(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null)
     {
         if (!preg_match('{^[\w.]+\??://}', $repoConfig['url'])) {
             // assume http as the default protocol
@@ -82,6 +86,7 @@ class ComposerRepository extends ArrayRepository implements StreamableRepository
         $this->cache = new Cache($io, $config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url), 'a-z0-9.$');
         $this->loader = new ArrayLoader();
         $this->rfs = new RemoteFilesystem($this->io, $this->options);
+        $this->eventDispatcher = $eventDispatcher;
     }
 
     public function setRootAliases(array $rootAliases)
@@ -538,7 +543,11 @@ class ComposerRepository extends ArrayRepository implements StreamableRepository
         $retries = 3;
         while ($retries--) {
             try {
-                $json = $this->rfs->getContents($filename, $filename, false);
+                $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->rfs, $filename);
+                if ($this->eventDispatcher) {
+                    $this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent);
+                }
+                $json = $preFileDownloadEvent->getRemoteFilesystem()->getContents($filename, $filename, false);
                 if ($sha256 && $sha256 !== hash('sha256', $json)) {
                     if ($retries) {
                         usleep(100000);

+ 5 - 2
src/Composer/Repository/RepositoryManager.php

@@ -14,6 +14,7 @@ namespace Composer\Repository;
 
 use Composer\IO\IOInterface;
 use Composer\Config;
+use Composer\EventDispatcher\EventDispatcher;
 
 /**
  * Repositories manager.
@@ -29,11 +30,13 @@ class RepositoryManager
     private $repositoryClasses = array();
     private $io;
     private $config;
+    private $eventDispatcher;
 
-    public function __construct(IOInterface $io, Config $config)
+    public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null)
     {
         $this->io = $io;
         $this->config = $config;
+        $this->eventDispatcher = $eventDispatcher;
     }
 
     /**
@@ -98,7 +101,7 @@ class RepositoryManager
 
         $class = $this->repositoryClasses[$type];
 
-        return new $class($config, $this->io, $this->config);
+        return new $class($config, $this->io, $this->config, $this->eventDispatcher);
     }
 
     /**