Commit e2b2a3e0 authored by Dries's avatar Dries

- Patch #1473600 by amateescu, Berdir: Convert archiver.inc to PSR-0.

parent 50a8bb85
This diff is collapsed.
<?php
/**
* @file
* Definition of Drupal\Component\Archiver\ArchiverInterface.
*/
namespace Drupal\Component\Archiver;
/**
* Defines the common interface for all Archiver classes.
*/
interface ArchiverInterface {
/**
* Constructs a new archiver instance.
*
* @param string $file_path
* The full system path of the archive to manipulate. Only local files
* are supported. If the file does not yet exist, it will be created if
* appropriate.
*/
public function __construct($file_path);
/**
* Adds the specified file or directory to the archive.
*
* @param string $file_path
* The full system path of the file or directory to add. Only local files
* and directories are supported.
*
* @return Drupal\Component\Archiver\ArchiverInterface
* The called object.
*/
public function add($file_path);
/**
* Removes the specified file from the archive.
*
* @param string $path
* The file name relative to the root of the archive to remove.
*
* @return Drupal\Component\Archiver\ArchiverInterface
* The called object.
*/
public function remove($path);
/**
* Extracts multiple files in the archive to the specified path.
*
* @param string $path
* A full system path of the directory to which to extract files.
* @param array $files
* Optionally specify a list of files to be extracted. Files are
* relative to the root of the archive. If not specified, all files
* in the archive will be extracted.
*
* @return Drupal\Component\Archiver\ArchiverInterface
* The called object.
*/
public function extract($path, array $files = array());
/**
* Lists all files in the archive.
*
* @return array
* An array of file names relative to the root of the archive.
*/
public function listContents();
}
<?php
/**
* @file
* Definition of Drupal\Component\Archiver\Tar.
*/
namespace Drupal\Component\Archiver;
/**
* Defines a archiver implementation for .tar files.
*/
class Tar implements ArchiverInterface {
/**
* The underlying ArchiveTar instance that does the heavy lifting.
*
* @var Drupal\Component\Archiver\ArchiveTar
*/
protected $tar;
/**
* Constructs a Tar object.
*/
public function __construct($file_path) {
$this->tar = new ArchiveTar($file_path);
}
/**
* Implements Drupal\Component\Archiver\ArchiveInterface::add().
*/
public function add($file_path) {
$this->tar->add($file_path);
return $this;
}
/**
* Implements Drupal\Component\Archiver\ArchiveInterface::remove().
*/
public function remove($file_path) {
// @todo Archive_Tar doesn't have a remove operation
// so we'll have to simulate it somehow, probably by
// creating a new archive with everything but the removed
// file.
return $this;
}
/**
* Implements Drupal\Component\Archiver\ArchiveInterface::extract().
*/
public function extract($path, array $files = array()) {
if ($files) {
$this->tar->extractList($files, $path);
}
else {
$this->tar->extract($path);
}
return $this;
}
/**
* Implements Drupal\Component\Archiver\ArchiveInterface::listContents().
*/
public function listContents() {
$files = array();
foreach ($this->tar->listContent() as $file_data) {
$files[] = $file_data['filename'];
}
return $files;
}
/**
* Retrieves the tar engine itself.
*
* In some cases it may be necessary to directly access the underlying
* Archive_Tar object for implementation-specific logic. This is for advanced
* use only as it is not shared by other implementations of ArchiveInterface.
*
* @return Archive_Tar
* The Archive_Tar object used by this object.
*/
public function getArchive() {
return $this->tar;
}
}
<?php
/**
* @file
* Definition of Drupal\Component\Archiver\Zip.
*/
namespace Drupal\Component\Archiver;
use ZipArchive;
/**
* Defines a archiver implementation for .zip files.
*
* @link http://php.net/zip
*/
class Zip implements ArchiverInterface {
/**
* The underlying ZipArchive instance that does the heavy lifting.
*
* @var ZipArchive
*/
protected $zip;
/**
* Constructs a Tar object.
*
* @param string $file_path
* The full system path of the archive to manipulate.
*/
public function __construct($file_path) {
$this->zip = new ZipArchive();
if ($this->zip->open($file_path) !== TRUE) {
// @todo: This should be an interface-specific exception some day.
throw new Exception(t('Cannot open %file_path', array('%file_path' => $file_path)));
}
}
/**
* Implements Drupal\Component\Archiver\ArchiveInterface::add().
*/
public function add($file_path) {
$this->zip->addFile($file_path);
return $this;
}
/**
* Implements Drupal\Component\Archiver\ArchiveInterface::remove().
*/
public function remove($file_path) {
$this->zip->deleteName($file_path);
return $this;
}
/**
* Implements Drupal\Component\Archiver\ArchiveInterface::extract().
*/
public function extract($path, Array $files = array()) {
if ($files) {
$this->zip->extractTo($path, $files);
}
else {
$this->zip->extractTo($path);
}
return $this;
}
/**
* Implements Drupal\Component\Archiver\ArchiveInterface::listContents().
*/
public function listContents() {
$files = array();
for ($i=0; $i < $this->zip->numFiles; $i++) {
$files[] = $this->zip->getNameIndex($i);
}
return $files;
}
/**
* Retrieves the zip engine itself.
*
* In some cases it may be necessary to directly access the underlying
* ZipArchive object for implementation-specific logic. This is for advanced
* use only as it is not shared by other implementations of ArchiveInterface.
*
* @return ZipArchive
* The ZipArchive object used by this object.
*/
public function getArchive() {
return $this->zip;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment