AbstractArchive.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Class to work with archives
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Archive;
  12. class AbstractArchive
  13. {
  14. /**
  15. * Write data to file. If file can't be opened - throw exception
  16. *
  17. * @param string $destination
  18. * @param string $data
  19. * @return true
  20. * @throws \Exception
  21. */
  22. protected function _writeFile($destination, $data)
  23. {
  24. $destination = trim($destination);
  25. if (false === file_put_contents($destination, $data)) {
  26. throw new \Exception("Can't write to file: " . $destination);
  27. }
  28. return true;
  29. }
  30. /**
  31. * Read data from file. If file can't be opened, throw to exception.
  32. *
  33. * @param string $source
  34. * @return string
  35. * @throws \Magento\Framework\Exception\LocalizedException
  36. */
  37. protected function _readFile($source)
  38. {
  39. $data = '';
  40. if (is_file($source) && is_readable($source)) {
  41. $data = @file_get_contents($source);
  42. if ($data === false) {
  43. throw new \Magento\Framework\Exception\LocalizedException(
  44. new \Magento\Framework\Phrase("Can't get contents from: %1", [$source])
  45. );
  46. }
  47. }
  48. return $data;
  49. }
  50. /**
  51. * Get file name from source (URI) without last extension.
  52. *
  53. * @param string $source
  54. * @param bool $withExtension
  55. * @return string
  56. */
  57. public function getFilename($source, $withExtension = false)
  58. {
  59. $file = str_replace(dirname($source) . '/', '', $source);
  60. if (!$withExtension) {
  61. $file = substr($file, 0, strrpos($file, '.'));
  62. }
  63. return $file;
  64. }
  65. }