1
0

FileAccessHelper.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\IntegrityCheck\Helpers;
  25. /**
  26. * Class FileAccessHelper provides a helper around file_get_contents and
  27. * file_put_contents
  28. *
  29. * @package OC\IntegrityCheck\Helpers
  30. */
  31. class FileAccessHelper {
  32. /**
  33. * Wrapper around file_get_contents($filename, $data)
  34. *
  35. * @param string $filename
  36. * @return string|false
  37. */
  38. public function file_get_contents(string $filename) {
  39. return file_get_contents($filename);
  40. }
  41. /**
  42. * Wrapper around file_exists($filename)
  43. *
  44. * @param string $filename
  45. * @return bool
  46. */
  47. public function file_exists(string $filename): bool {
  48. return file_exists($filename);
  49. }
  50. /**
  51. * Wrapper around file_put_contents($filename, $data)
  52. *
  53. * @param string $filename
  54. * @param string $data
  55. * @return int
  56. * @throws \Exception
  57. */
  58. public function file_put_contents(string $filename, string $data): int {
  59. $bytesWritten = @file_put_contents($filename, $data);
  60. if ($bytesWritten === false || $bytesWritten !== \strlen($data)){
  61. throw new \Exception('Failed to write into ' . $filename);
  62. }
  63. return $bytesWritten;
  64. }
  65. /**
  66. * @param string $path
  67. * @return bool
  68. */
  69. public function is_writable(string $path): bool {
  70. return is_writable($path);
  71. }
  72. /**
  73. * @param string $path
  74. * @throws \Exception
  75. */
  76. public function assertDirectoryExists(string $path) {
  77. if (!is_dir($path)) {
  78. throw new \Exception('Directory ' . $path . ' does not exist.');
  79. }
  80. }
  81. }