2011-06-07 22:14:50 +00:00
< ? 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\Package\PackageInterface ;
/**
* @ author Benjamin Eberlei < kontakt @ beberlei . de >
2011-06-28 18:42:02 +00:00
* @ author Jordi Boggiano < j . boggiano @ seld . be >
2011-06-07 22:14:50 +00:00
*/
2011-09-15 19:58:00 +00:00
class PearDownloader implements DownloaderInterface
2011-06-07 22:14:50 +00:00
{
2011-07-06 19:06:52 +00:00
public function download ( PackageInterface $package , $path , $url , $checksum = null )
2011-06-07 22:14:50 +00:00
{
2011-06-28 18:42:02 +00:00
$targetPath = $path . " / " . $package -> getName ();
if ( ! is_dir ( $targetPath )) {
if ( file_exists ( $targetPath )) {
throw new \UnexpectedValueException ( $targetPath . ' exists and is not a directory.' );
2011-06-07 22:14:50 +00:00
}
2011-06-28 18:42:02 +00:00
if ( ! mkdir ( $targetPath , 0777 , true )) {
throw new \UnexpectedValueException ( $targetPath . ' does not exist and could not be created.' );
2011-06-07 22:14:50 +00:00
}
}
2011-06-08 20:06:57 +00:00
2011-06-28 18:42:02 +00:00
$cwd = getcwd ();
chdir ( $targetPath );
2011-07-06 19:06:52 +00:00
$tarName = basename ( $url );
2011-06-07 22:14:50 +00:00
2011-07-06 19:06:52 +00:00
echo 'Downloading ' . $url . ' to ' . $targetPath . '/' . $tarName . PHP_EOL ;
copy ( $url , './' . $tarName );
2011-06-28 18:42:02 +00:00
if ( ! file_exists ( $tarName )) {
throw new \UnexpectedValueException ( $package -> getName () . ' could not be saved into ' . $tarName . ', make sure the'
2011-06-07 22:14:50 +00:00
. ' directory is writable and you have internet connectivity.' );
}
2011-06-28 00:19:52 +00:00
2011-07-06 19:06:52 +00:00
if ( $checksum && hash_file ( 'sha1' , './' . $tarName ) !== $checksum ) {
throw new \UnexpectedValueException ( 'The checksum verification failed for the ' . $package -> getName () . ' archive (downloaded from ' . $url . '). Installation aborted.' );
}
2011-06-28 18:42:02 +00:00
echo 'Unpacking archive' . PHP_EOL ;
exec ( 'tar -xzf "' . escapeshellarg ( $tarName ) . '"' );
echo 'Cleaning up' . PHP_EOL ;
unlink ( './' . $tarName );
@ unlink ( './package.sig' );
@ unlink ( './package.xml' );
if ( list ( $dir ) = glob ( './' . $package -> getName () . '-*' , GLOB_ONLYDIR )) {
foreach ( array_merge ( glob ( $dir . '/.*' ), glob ( $dir . '/*' )) as $file ) {
if ( trim ( basename ( $file ), '.' )) {
rename ( $file , './' . basename ( $file ));
}
}
rmdir ( $dir );
}
2011-06-07 22:14:50 +00:00
chdir ( $cwd );
}
}