AbstractTarAdapter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. /*
  3. * This file is part of Zippy.
  4. *
  5. * (c) Alchemy <info@alchemy.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Alchemy\Zippy\Adapter;
  11. use Alchemy\Zippy\Adapter\Resource\ResourceInterface;
  12. use Alchemy\Zippy\Archive\Archive;
  13. use Alchemy\Zippy\Archive\Member;
  14. use Alchemy\Zippy\Exception\RuntimeException;
  15. use Alchemy\Zippy\Exception\InvalidArgumentException;
  16. use Alchemy\Zippy\Resource\Resource as ZippyResource;
  17. use Symfony\Component\Process\Exception\ExceptionInterface as ProcessException;
  18. abstract class AbstractTarAdapter extends AbstractBinaryAdapter
  19. {
  20. /**
  21. * @inheritdoc
  22. */
  23. protected function doCreate($path, $files, $recursive)
  24. {
  25. return $this->doTarCreate($this->getLocalOptions(), $path, $files, $recursive);
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. protected function doListMembers(ResourceInterface $resource)
  31. {
  32. return $this->doTarListMembers($this->getLocalOptions(), $resource);
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. protected function doAdd(ResourceInterface $resource, $files, $recursive)
  38. {
  39. return $this->doTarAdd($this->getLocalOptions(), $resource, $files, $recursive);
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. protected function doRemove(ResourceInterface $resource, $files)
  45. {
  46. return $this->doTarRemove($this->getLocalOptions(), $resource, $files);
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. protected function doExtractMembers(ResourceInterface $resource, $members, $to, $overwrite = false)
  52. {
  53. return $this->doTarExtractMembers($this->getLocalOptions(), $resource, $members, $to, $overwrite);
  54. }
  55. /**
  56. * @inheritdoc
  57. */
  58. protected function doExtract(ResourceInterface $resource, $to)
  59. {
  60. return $this->doTarExtract($this->getLocalOptions(), $resource, $to);
  61. }
  62. /**
  63. * @inheritdoc
  64. */
  65. protected function doGetInflatorVersion()
  66. {
  67. $process = $this
  68. ->inflator
  69. ->create()
  70. ->add('--version')
  71. ->getProcess();
  72. $process->run();
  73. if (!$process->isSuccessful()) {
  74. throw new RuntimeException(sprintf(
  75. 'Unable to execute the following command %s {output: %s}',
  76. $process->getCommandLine(), $process->getErrorOutput()
  77. ));
  78. }
  79. return $this->parser->parseInflatorVersion($process->getOutput() ?: '');
  80. }
  81. /**
  82. * @inheritdoc
  83. */
  84. protected function doGetDeflatorVersion()
  85. {
  86. return $this->getInflatorVersion();
  87. }
  88. protected function doTarCreate($options, $path, $files = null, $recursive = true)
  89. {
  90. $files = (array) $files;
  91. $builder = $this
  92. ->inflator
  93. ->create();
  94. if (!$recursive) {
  95. $builder->add('--no-recursion');
  96. }
  97. $builder->add('-c');
  98. foreach ((array) $options as $option) {
  99. $builder->add((string) $option);
  100. }
  101. if (0 === count($files)) {
  102. $nullFile = defined('PHP_WINDOWS_VERSION_BUILD') ? 'NUL' : '/dev/null';
  103. $builder->add('-f');
  104. $builder->add($path);
  105. $builder->add('-T');
  106. $builder->add($nullFile);
  107. $process = $builder->getProcess();
  108. $process->run();
  109. } else {
  110. $builder->add(sprintf('--file=%s', $path));
  111. if (!$recursive) {
  112. $builder->add('--no-recursion');
  113. }
  114. $collection = $this->manager->handle(getcwd(), $files);
  115. $builder->setWorkingDirectory($collection->getContext());
  116. $collection->forAll(function($i, ZippyResource $resource) use ($builder) {
  117. return $builder->add($resource->getTarget());
  118. });
  119. $process = $builder->getProcess();
  120. try {
  121. $process->run();
  122. } catch (ProcessException $e) {
  123. $this->manager->cleanup($collection);
  124. throw $e;
  125. }
  126. $this->manager->cleanup($collection);
  127. }
  128. if (!$process->isSuccessful()) {
  129. throw new RuntimeException(sprintf(
  130. 'Unable to execute the following command %s {output: %s}',
  131. $process->getCommandLine(),
  132. $process->getErrorOutput()
  133. ));
  134. }
  135. return new Archive($this->createResource($path), $this, $this->manager);
  136. }
  137. protected function doTarListMembers($options, ResourceInterface $resource)
  138. {
  139. $builder = $this
  140. ->inflator
  141. ->create();
  142. foreach ($this->getListMembersOptions() as $option) {
  143. $builder->add($option);
  144. }
  145. $builder
  146. ->add('--list')
  147. ->add('-v')
  148. ->add(sprintf('--file=%s', $resource->getResource()));
  149. foreach ((array) $options as $option) {
  150. $builder->add((string) $option);
  151. }
  152. $process = $builder->getProcess();
  153. $process->run();
  154. if (!$process->isSuccessful()) {
  155. throw new RuntimeException(sprintf(
  156. 'Unable to execute the following command %s {output: %s}',
  157. $process->getCommandLine(),
  158. $process->getErrorOutput()
  159. ));
  160. }
  161. $members = array();
  162. foreach ($this->parser->parseFileListing($process->getOutput() ?: '') as $member) {
  163. $members[] = new Member(
  164. $resource,
  165. $this,
  166. $member['location'],
  167. $member['size'],
  168. $member['mtime'],
  169. $member['is_dir']
  170. );
  171. }
  172. return $members;
  173. }
  174. protected function doTarAdd($options, ResourceInterface $resource, $files, $recursive = true)
  175. {
  176. $files = (array) $files;
  177. $builder = $this
  178. ->inflator
  179. ->create();
  180. if (!$recursive) {
  181. $builder->add('--no-recursion');
  182. }
  183. $builder
  184. ->add('--append')
  185. ->add(sprintf('--file=%s', $resource->getResource()));
  186. foreach ((array) $options as $option) {
  187. $builder->add((string) $option);
  188. }
  189. // there will be an issue if the file starts with a dash
  190. // see --add-file=FILE
  191. $collection = $this->manager->handle(getcwd(), $files);
  192. $builder->setWorkingDirectory($collection->getContext());
  193. $collection->forAll(function($i, ZippyResource $resource) use ($builder) {
  194. return $builder->add($resource->getTarget());
  195. });
  196. $process = $builder->getProcess();
  197. try {
  198. $process->run();
  199. } catch (ProcessException $e) {
  200. $this->manager->cleanup($collection);
  201. throw $e;
  202. }
  203. $this->manager->cleanup($collection);
  204. if (!$process->isSuccessful()) {
  205. throw new RuntimeException(sprintf(
  206. 'Unable to execute the following command %s {output: %s}',
  207. $process->getCommandLine(),
  208. $process->getErrorOutput()
  209. ));
  210. }
  211. return $files;
  212. }
  213. protected function doTarRemove($options, ResourceInterface $resource, $files)
  214. {
  215. $files = (array) $files;
  216. $builder = $this
  217. ->inflator
  218. ->create();
  219. $builder
  220. ->add('--delete')
  221. ->add(sprintf('--file=%s', $resource->getResource()));
  222. foreach ((array) $options as $option) {
  223. $builder->add((string) $option);
  224. }
  225. if (!$this->addBuilderFileArgument($files, $builder)) {
  226. throw new InvalidArgumentException('Invalid files');
  227. }
  228. $process = $builder->getProcess();
  229. $process->run();
  230. if (!$process->isSuccessful()) {
  231. throw new RuntimeException(sprintf(
  232. 'Unable to execute the following command %s {output: %s}',
  233. $process->getCommandLine(),
  234. $process->getErrorOutput()
  235. ));
  236. }
  237. return $files;
  238. }
  239. protected function doTarExtract($options, ResourceInterface $resource, $to = null)
  240. {
  241. if (null !== $to && !is_dir($to)) {
  242. throw new InvalidArgumentException(sprintf("%s is not a directory", $to));
  243. }
  244. $builder = $this
  245. ->inflator
  246. ->create();
  247. $builder
  248. ->add('--extract')
  249. ->add(sprintf('--file=%s', $resource->getResource()));
  250. foreach ($this->getExtractOptions() as $option) {
  251. $builder
  252. ->add($option);
  253. }
  254. foreach ((array) $options as $option) {
  255. $builder->add((string) $option);
  256. }
  257. if (null !== $to) {
  258. $builder
  259. ->add('--directory')
  260. ->add($to);
  261. }
  262. $process = $builder->getProcess();
  263. $process->run();
  264. if (!$process->isSuccessful()) {
  265. throw new RuntimeException(sprintf(
  266. 'Unable to execute the following command %s {output: %s}',
  267. $process->getCommandLine(),
  268. $process->getErrorOutput()
  269. ));
  270. }
  271. return new \SplFileInfo($to ?: $resource->getResource());
  272. }
  273. /**
  274. * @param array $options
  275. * @param ResourceInterface $resource
  276. * @param array $members
  277. * @param string $to
  278. * @param bool $overwrite
  279. *
  280. * @return array
  281. */
  282. protected function doTarExtractMembers($options, ResourceInterface $resource, $members, $to = null, $overwrite = false)
  283. {
  284. if (null !== $to && !is_dir($to)) {
  285. throw new InvalidArgumentException(sprintf("%s is not a directory", $to));
  286. }
  287. $members = (array) $members;
  288. $builder = $this
  289. ->inflator
  290. ->create();
  291. if ($overwrite == false) {
  292. $builder->add('-k');
  293. }
  294. $builder
  295. ->add('--extract')
  296. ->add(sprintf('--file=%s', $resource->getResource()));
  297. foreach ($this->getExtractMembersOptions() as $option) {
  298. $builder
  299. ->add($option);
  300. }
  301. foreach ((array) $options as $option) {
  302. $builder->add((string) $option);
  303. }
  304. if (null !== $to) {
  305. $builder
  306. ->add('--directory')
  307. ->add($to);
  308. }
  309. if (!$this->addBuilderFileArgument($members, $builder)) {
  310. throw new InvalidArgumentException('Invalid files');
  311. }
  312. $process = $builder->getProcess();
  313. $process->run();
  314. if (!$process->isSuccessful()) {
  315. throw new RuntimeException(sprintf(
  316. 'Unable to execute the following command %s {output: %s}',
  317. $process->getCommandLine(),
  318. $process->getErrorOutput()
  319. ));
  320. }
  321. return $members;
  322. }
  323. /**
  324. * Returns an array of option for the listMembers command
  325. *
  326. * @return array
  327. */
  328. abstract protected function getListMembersOptions();
  329. /**
  330. * Returns an array of option for the extract command
  331. *
  332. * @return array
  333. */
  334. abstract protected function getExtractOptions();
  335. /**
  336. * Returns an array of option for the extractMembers command
  337. *
  338. * @return array
  339. */
  340. abstract protected function getExtractMembersOptions();
  341. /**
  342. * Gets adapter specific additional options
  343. *
  344. * @return array
  345. */
  346. abstract protected function getLocalOptions();
  347. }