Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preFileDownload event on packages.json fetch #2434

Merged
merged 1 commit into from
Nov 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/Composer/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,15 @@ public function createComposer(IOInterface $io, $localConfig = null, $disablePlu
// 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);
Expand All @@ -234,16 +241,11 @@ public function createComposer(IOInterface $io, $localConfig = null, $disablePlu
// 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);

Expand Down Expand Up @@ -285,9 +287,9 @@ public function createComposer(IOInterface $io, $localConfig = null, $disablePlu
* @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');
Expand Down
13 changes: 11 additions & 2 deletions src/Composer/Repository/ComposerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
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>
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a use case for making it nullable ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really, just following the existing ;)

{
if (!preg_match('{^[\w.]+\??://}', $repoConfig['url'])) {
// assume http as the default protocol
Expand Down Expand Up @@ -82,6 +86,7 @@ public function __construct(array $repoConfig, IOInterface $io, Config $config)
$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)
Expand Down Expand Up @@ -538,7 +543,11 @@ protected function fetchFile($filename, $cacheKey = null, $sha256 = null)
$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);
Expand Down
7 changes: 5 additions & 2 deletions src/Composer/Repository/RepositoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Composer\IO\IOInterface;
use Composer\Config;
use Composer\EventDispatcher\EventDispatcher;

/**
* Repositories manager.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -98,7 +101,7 @@ public function createRepository($type, $config)

$class = $this->repositoryClasses[$type];

return new $class($config, $this->io, $this->config);
return new $class($config, $this->io, $this->config, $this->eventDispatcher);
}

/**
Expand Down