暫無描述

AbstractReader.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Reader;
  18. use PhpOffice\PhpWord\Exception\Exception;
  19. /**
  20. * Reader abstract class
  21. *
  22. * @since 0.8.0
  23. * @codeCoverageIgnore Abstract class
  24. */
  25. abstract class AbstractReader implements ReaderInterface
  26. {
  27. /**
  28. * Read data only?
  29. *
  30. * @var bool
  31. */
  32. protected $readDataOnly = true;
  33. /**
  34. * File pointer
  35. *
  36. * @var bool|resource
  37. */
  38. protected $fileHandle;
  39. /**
  40. * Read data only?
  41. *
  42. * @return bool
  43. */
  44. public function isReadDataOnly()
  45. {
  46. // return $this->readDataOnly;
  47. return true;
  48. }
  49. /**
  50. * Set read data only
  51. *
  52. * @param bool $value
  53. * @return self
  54. */
  55. public function setReadDataOnly($value = true)
  56. {
  57. $this->readDataOnly = $value;
  58. return $this;
  59. }
  60. /**
  61. * Open file for reading
  62. *
  63. * @param string $filename
  64. * @return resource
  65. * @throws \PhpOffice\PhpWord\Exception\Exception
  66. */
  67. protected function openFile($filename)
  68. {
  69. // Check if file exists
  70. if (!file_exists($filename) || !is_readable($filename)) {
  71. throw new Exception("Could not open " . $filename . " for reading! File does not exist.");
  72. }
  73. // Open file
  74. $this->fileHandle = fopen($filename, 'r');
  75. if ($this->fileHandle === false) {
  76. throw new Exception("Could not open file " . $filename . " for reading.");
  77. }
  78. }
  79. /**
  80. * Can the current ReaderInterface read the file?
  81. *
  82. * @param string $filename
  83. * @return bool
  84. */
  85. public function canRead($filename)
  86. {
  87. // Check if file exists
  88. try {
  89. $this->openFile($filename);
  90. } catch (Exception $e) {
  91. return false;
  92. }
  93. if (is_resource($this->fileHandle)) {
  94. fclose($this->fileHandle);
  95. }
  96. return true;
  97. }
  98. /**
  99. * Read data only?
  100. *
  101. * @deprecated 0.10.0
  102. * @codeCoverageIgnore
  103. */
  104. public function getReadDataOnly()
  105. {
  106. return $this->isReadDataOnly();
  107. }
  108. }