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

Dist directory fetcher #1266

Closed
wants to merge 2 commits into from
Closed
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
115 changes: 115 additions & 0 deletions src/Composer/Downloader/LocalDirectoryDownloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Composer\Downloader;

use Composer\Config;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Util\Filesystem;
use Composer\Util\GitHub;
use Composer\Util\RemoteFilesystem;

/**
* Base downloader for directories
*
* @author Daniel Fahlke aka Flyingmana <danielm@digitalmanufaktur.com>
*/
class LocalDirectoryDownloader implements DownloaderInterface
{

/**
* Constructor.
*
* @param IOInterface $io The IO instance
* @param Config $config The config
* @param RemoteFilesystem $rfs The remote filesystem
* @param Filesystem $filesystem The filesystem
*/
public function __construct(IOInterface $io, Config $config, Filesystem $filesystem = null)
{
$this->io = $io;
$this->config = $config;
$this->filesystem = $filesystem ?: new Filesystem();
}


/**
* {@inheritDoc}
*/
public function getInstallationSource()
{
return 'dist';
}

/**
* {@inheritDoc}
*/
public function download(PackageInterface $package, $path)
{
$url = $package->getDistUrl();
if (!$url) {
throw new \InvalidArgumentException('The given package is missing url information');
}

$this->filesystem->ensureDirectoryExists($path);

$dirName = $path;

var_dump($path);
$this->io->write(" - Installing <info>" . $package->getName() . "</info> (<comment>" . VersionParser::formatVersion($package) . "</comment>)");


try {
try {
$this->filesystem->copyDirectory($url,$dirName);// ->copy(parse_url($processUrl, PHP_URL_HOST), $processUrl, $fileName);
} catch (TransportException $e) {
throw $e;
}

if (!is_dir($dirName)) {
throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the'
.' directory is writable and you have internet connectivity');
}

$checksum = $package->getDistSha1Checksum();
if ($checksum && hash_file('sha1', $fileName) !== $checksum) {
throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url.')');
}
} catch (\Exception $e) {
// clean up
$this->filesystem->removeDirectory($path);
throw $e;
}
}

/**
* {@inheritDoc}
*/
public function update(PackageInterface $initial, PackageInterface $target, $path)
{
$this->remove($initial, $path);
$this->download($target, $path);
}

/**
* {@inheritDoc}
*/
public function remove(PackageInterface $package, $path)
{
$this->io->write(" - Removing <info>" . $package->getName() . "</info> (<comment>" . VersionParser::formatVersion($package) . "</comment>)");
if (!$this->filesystem->removeDirectory($path)) {
throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
}
}
}
1 change: 1 addition & 0 deletions src/Composer/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ public function createDownloadManager(IOInterface $io, Config $config)
$dm->setDownloader('tar', new Downloader\TarDownloader($io, $config));
$dm->setDownloader('phar', new Downloader\PharDownloader($io, $config));
$dm->setDownloader('file', new Downloader\FileDownloader($io, $config));
$dm->setDownloader('local-directory', new Downloader\LocalDirectoryDownloader($io, $config));

return $dm;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Composer/Util/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ public function removeDirectory($directory)
return $result && !is_dir($directory);
}

public function copyDirectory($sourceDirectory, $destinationDirectory)
{

$sourceDirectory.= '/.';
$destinationDirectory.= '';
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$cmd = sprintf('xcopy %s %s /s /e /i /h', escapeshellarg($sourceDirectory), escapeshellarg($destinationDirectory));
} else {
$cmd = sprintf('cp -r -f %s %s ', escapeshellarg($sourceDirectory), escapeshellarg($destinationDirectory));
}

$result = $this->getProcess()->execute($cmd) === 0;

// clear stat cache because external processes aren't tracked by the php stat cache
clearstatcache();

return $result && is_dir($destinationDirectory);

}

public function ensureDirectoryExists($directory)
{
if (!is_dir($directory)) {
Expand Down