123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?php
- /*
- * This file is part of Zippy.
- *
- * (c) Alchemy <info@alchemy.fr>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Alchemy\Zippy\Adapter;
- use Alchemy\Zippy\Adapter\Resource\ResourceInterface;
- use Alchemy\Zippy\Archive\Archive;
- use Alchemy\Zippy\Archive\Member;
- use Alchemy\Zippy\Exception\RuntimeException;
- use Alchemy\Zippy\Exception\InvalidArgumentException;
- use Alchemy\Zippy\Resource\Resource as ZippyResource;
- use Symfony\Component\Process\Exception\ExceptionInterface as ProcessException;
- abstract class AbstractTarAdapter extends AbstractBinaryAdapter
- {
- /**
- * @inheritdoc
- */
- protected function doCreate($path, $files, $recursive)
- {
- return $this->doTarCreate($this->getLocalOptions(), $path, $files, $recursive);
- }
- /**
- * @inheritdoc
- */
- protected function doListMembers(ResourceInterface $resource)
- {
- return $this->doTarListMembers($this->getLocalOptions(), $resource);
- }
- /**
- * @inheritdoc
- */
- protected function doAdd(ResourceInterface $resource, $files, $recursive)
- {
- return $this->doTarAdd($this->getLocalOptions(), $resource, $files, $recursive);
- }
- /**
- * @inheritdoc
- */
- protected function doRemove(ResourceInterface $resource, $files)
- {
- return $this->doTarRemove($this->getLocalOptions(), $resource, $files);
- }
- /**
- * @inheritdoc
- */
- protected function doExtractMembers(ResourceInterface $resource, $members, $to, $overwrite = false)
- {
- return $this->doTarExtractMembers($this->getLocalOptions(), $resource, $members, $to, $overwrite);
- }
- /**
- * @inheritdoc
- */
- protected function doExtract(ResourceInterface $resource, $to)
- {
- return $this->doTarExtract($this->getLocalOptions(), $resource, $to);
- }
- /**
- * @inheritdoc
- */
- protected function doGetInflatorVersion()
- {
- $process = $this
- ->inflator
- ->create()
- ->add('--version')
- ->getProcess();
- $process->run();
- if (!$process->isSuccessful()) {
- throw new RuntimeException(sprintf(
- 'Unable to execute the following command %s {output: %s}',
- $process->getCommandLine(), $process->getErrorOutput()
- ));
- }
- return $this->parser->parseInflatorVersion($process->getOutput() ?: '');
- }
- /**
- * @inheritdoc
- */
- protected function doGetDeflatorVersion()
- {
- return $this->getInflatorVersion();
- }
- protected function doTarCreate($options, $path, $files = null, $recursive = true)
- {
- $files = (array) $files;
- $builder = $this
- ->inflator
- ->create();
- if (!$recursive) {
- $builder->add('--no-recursion');
- }
- $builder->add('-c');
- foreach ((array) $options as $option) {
- $builder->add((string) $option);
- }
- if (0 === count($files)) {
- $nullFile = defined('PHP_WINDOWS_VERSION_BUILD') ? 'NUL' : '/dev/null';
- $builder->add('-f');
- $builder->add($path);
- $builder->add('-T');
- $builder->add($nullFile);
- $process = $builder->getProcess();
- $process->run();
- } else {
- $builder->add(sprintf('--file=%s', $path));
- if (!$recursive) {
- $builder->add('--no-recursion');
- }
- $collection = $this->manager->handle(getcwd(), $files);
- $builder->setWorkingDirectory($collection->getContext());
- $collection->forAll(function($i, ZippyResource $resource) use ($builder) {
- return $builder->add($resource->getTarget());
- });
- $process = $builder->getProcess();
- try {
- $process->run();
- } catch (ProcessException $e) {
- $this->manager->cleanup($collection);
- throw $e;
- }
- $this->manager->cleanup($collection);
- }
- if (!$process->isSuccessful()) {
- throw new RuntimeException(sprintf(
- 'Unable to execute the following command %s {output: %s}',
- $process->getCommandLine(),
- $process->getErrorOutput()
- ));
- }
- return new Archive($this->createResource($path), $this, $this->manager);
- }
- protected function doTarListMembers($options, ResourceInterface $resource)
- {
- $builder = $this
- ->inflator
- ->create();
- foreach ($this->getListMembersOptions() as $option) {
- $builder->add($option);
- }
- $builder
- ->add('--list')
- ->add('-v')
- ->add(sprintf('--file=%s', $resource->getResource()));
- foreach ((array) $options as $option) {
- $builder->add((string) $option);
- }
- $process = $builder->getProcess();
- $process->run();
- if (!$process->isSuccessful()) {
- throw new RuntimeException(sprintf(
- 'Unable to execute the following command %s {output: %s}',
- $process->getCommandLine(),
- $process->getErrorOutput()
- ));
- }
- $members = array();
- foreach ($this->parser->parseFileListing($process->getOutput() ?: '') as $member) {
- $members[] = new Member(
- $resource,
- $this,
- $member['location'],
- $member['size'],
- $member['mtime'],
- $member['is_dir']
- );
- }
- return $members;
- }
- protected function doTarAdd($options, ResourceInterface $resource, $files, $recursive = true)
- {
- $files = (array) $files;
- $builder = $this
- ->inflator
- ->create();
- if (!$recursive) {
- $builder->add('--no-recursion');
- }
- $builder
- ->add('--append')
- ->add(sprintf('--file=%s', $resource->getResource()));
- foreach ((array) $options as $option) {
- $builder->add((string) $option);
- }
- // there will be an issue if the file starts with a dash
- // see --add-file=FILE
- $collection = $this->manager->handle(getcwd(), $files);
- $builder->setWorkingDirectory($collection->getContext());
- $collection->forAll(function($i, ZippyResource $resource) use ($builder) {
- return $builder->add($resource->getTarget());
- });
- $process = $builder->getProcess();
- try {
- $process->run();
- } catch (ProcessException $e) {
- $this->manager->cleanup($collection);
- throw $e;
- }
- $this->manager->cleanup($collection);
- if (!$process->isSuccessful()) {
- throw new RuntimeException(sprintf(
- 'Unable to execute the following command %s {output: %s}',
- $process->getCommandLine(),
- $process->getErrorOutput()
- ));
- }
- return $files;
- }
- protected function doTarRemove($options, ResourceInterface $resource, $files)
- {
- $files = (array) $files;
- $builder = $this
- ->inflator
- ->create();
- $builder
- ->add('--delete')
- ->add(sprintf('--file=%s', $resource->getResource()));
- foreach ((array) $options as $option) {
- $builder->add((string) $option);
- }
- if (!$this->addBuilderFileArgument($files, $builder)) {
- throw new InvalidArgumentException('Invalid files');
- }
- $process = $builder->getProcess();
- $process->run();
- if (!$process->isSuccessful()) {
- throw new RuntimeException(sprintf(
- 'Unable to execute the following command %s {output: %s}',
- $process->getCommandLine(),
- $process->getErrorOutput()
- ));
- }
- return $files;
- }
- protected function doTarExtract($options, ResourceInterface $resource, $to = null)
- {
- if (null !== $to && !is_dir($to)) {
- throw new InvalidArgumentException(sprintf("%s is not a directory", $to));
- }
- $builder = $this
- ->inflator
- ->create();
- $builder
- ->add('--extract')
- ->add(sprintf('--file=%s', $resource->getResource()));
- foreach ($this->getExtractOptions() as $option) {
- $builder
- ->add($option);
- }
- foreach ((array) $options as $option) {
- $builder->add((string) $option);
- }
- if (null !== $to) {
- $builder
- ->add('--directory')
- ->add($to);
- }
- $process = $builder->getProcess();
- $process->run();
- if (!$process->isSuccessful()) {
- throw new RuntimeException(sprintf(
- 'Unable to execute the following command %s {output: %s}',
- $process->getCommandLine(),
- $process->getErrorOutput()
- ));
- }
- return new \SplFileInfo($to ?: $resource->getResource());
- }
- /**
- * @param array $options
- * @param ResourceInterface $resource
- * @param array $members
- * @param string $to
- * @param bool $overwrite
- *
- * @return array
- */
- protected function doTarExtractMembers($options, ResourceInterface $resource, $members, $to = null, $overwrite = false)
- {
- if (null !== $to && !is_dir($to)) {
- throw new InvalidArgumentException(sprintf("%s is not a directory", $to));
- }
- $members = (array) $members;
- $builder = $this
- ->inflator
- ->create();
- if ($overwrite == false) {
- $builder->add('-k');
- }
- $builder
- ->add('--extract')
- ->add(sprintf('--file=%s', $resource->getResource()));
- foreach ($this->getExtractMembersOptions() as $option) {
- $builder
- ->add($option);
- }
- foreach ((array) $options as $option) {
- $builder->add((string) $option);
- }
- if (null !== $to) {
- $builder
- ->add('--directory')
- ->add($to);
- }
- if (!$this->addBuilderFileArgument($members, $builder)) {
- throw new InvalidArgumentException('Invalid files');
- }
- $process = $builder->getProcess();
- $process->run();
- if (!$process->isSuccessful()) {
- throw new RuntimeException(sprintf(
- 'Unable to execute the following command %s {output: %s}',
- $process->getCommandLine(),
- $process->getErrorOutput()
- ));
- }
- return $members;
- }
- /**
- * Returns an array of option for the listMembers command
- *
- * @return array
- */
- abstract protected function getListMembersOptions();
- /**
- * Returns an array of option for the extract command
- *
- * @return array
- */
- abstract protected function getExtractOptions();
- /**
- * Returns an array of option for the extractMembers command
- *
- * @return array
- */
- abstract protected function getExtractMembersOptions();
- /**
- * Gets adapter specific additional options
- *
- * @return array
- */
- abstract protected function getLocalOptions();
- }
|