/library/Zend/Pdf/BinaryParser/DataSource/File.php

https://github.com/Exercise/zf2 · PHP · 193 lines · 72 code · 20 blank · 101 comment · 15 complexity · 1b7042383e77f8178f6211656ef5389a MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_PDF
  17. * @subpackage Zend_PDF_BinaryParser
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\Pdf\BinaryParser\DataSource;
  26. use Zend\Pdf;
  27. /**
  28. * Concrete subclass of {@link \Zend\Pdf\BinaryParser\DataSource\AbstractDataSource}
  29. * that provides an interface to filesystem objects.
  30. *
  31. * Note that this class cannot be used for other sources that may be supported
  32. * by {@link fopen()} (through URL wrappers). It may be used for local
  33. * filesystem objects only.
  34. *
  35. * @uses \Zend\Pdf\Exception
  36. * @uses \Zend\Pdf\BinaryParser\DataSource\AbstractDataSource
  37. * @package Zend_PDF
  38. * @subpackage Zend_PDF_BinaryParser
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class File extends AbstractDataSource
  43. {
  44. /**** Instance Variables ****/
  45. /**
  46. * Fully-qualified path to the file.
  47. * @var string
  48. */
  49. protected $_filePath = '';
  50. /**
  51. * File resource handle .
  52. * @var resource
  53. */
  54. protected $_fileResource = null;
  55. /**** Public Interface ****/
  56. /* Concrete Class Implementation */
  57. /**
  58. * Object constructor.
  59. *
  60. * Validates the path to the file, ensures that it is readable, then opens
  61. * it for reading.
  62. *
  63. * Throws an exception if the file is missing or cannot be opened.
  64. *
  65. * @param string $filePath Fully-qualified path to the file.
  66. * @throws \Zend\Pdf\Exception
  67. */
  68. public function __construct($filePath)
  69. {
  70. if (! (is_file($filePath) || is_link($filePath))) {
  71. throw new Pdf\Exception("Invalid file path: $filePath",
  72. Pdf\Exception::BAD_FILE_PATH);
  73. }
  74. if (! is_readable($filePath)) {
  75. throw new Pdf\Exception("File is not readable: $filePath",
  76. Pdf\Exception::NOT_READABLE);
  77. }
  78. if (($this->_size = @filesize($filePath)) === false) {
  79. throw new Pdf\Exception("Error while obtaining file size: $filePath",
  80. Pdf\Exception::CANT_GET_FILE_SIZE);
  81. }
  82. if (($this->_fileResource = @fopen($filePath, 'rb')) === false) {
  83. throw new Pdf\Exception("Cannot open file for reading: $filePath",
  84. Pdf\Exception::CANT_OPEN_FILE);
  85. }
  86. $this->_filePath = $filePath;
  87. }
  88. /**
  89. * Object destructor.
  90. *
  91. * Closes the file if it had been successfully opened.
  92. */
  93. public function __destruct()
  94. {
  95. if (is_resource($this->_fileResource)) {
  96. @fclose($this->_fileResource);
  97. }
  98. }
  99. /**
  100. * Returns the specified number of raw bytes from the file at the byte
  101. * offset of the current read position.
  102. *
  103. * Advances the read position by the number of bytes read.
  104. *
  105. * Throws an exception if an error was encountered while reading the file or
  106. * if there is insufficient data to completely fulfill the request.
  107. *
  108. * @param integer $byteCount Number of bytes to read.
  109. * @return string
  110. * @throws \Zend\Pdf\Exception
  111. */
  112. public function readBytes($byteCount)
  113. {
  114. $bytes = @fread($this->_fileResource, $byteCount);
  115. if ($bytes === false) {
  116. throw new Pdf\Exception('Unexpected error while reading file',
  117. Pdf\Exception::ERROR_DURING_READ);
  118. }
  119. if (strlen($bytes) != $byteCount) {
  120. throw new Pdf\Exception("Insufficient data to read $byteCount bytes",
  121. Pdf\Exception::INSUFFICIENT_DATA);
  122. }
  123. $this->_offset += $byteCount;
  124. return $bytes;
  125. }
  126. /**
  127. * Returns the entire contents of the file as a string.
  128. *
  129. * Preserves the current file seek position.
  130. *
  131. * @return string
  132. */
  133. public function readAllBytes()
  134. {
  135. return file_get_contents($this->_filePath);
  136. }
  137. /* Object Magic Methods */
  138. /**
  139. * Returns the full filesystem path of the file.
  140. *
  141. * @return string
  142. */
  143. public function __toString()
  144. {
  145. return $this->_filePath;
  146. }
  147. /* Primitive Methods */
  148. /**
  149. * Seeks the file read position to the specified byte offset.
  150. *
  151. * Throws an exception if the file pointer cannot be moved or if it is
  152. * moved beyond EOF (end of file).
  153. *
  154. * @param integer $offset Destination byte offset.
  155. * @throws \Zend\Pdf\Exception
  156. */
  157. public function moveToOffset($offset)
  158. {
  159. if ($this->_offset == $offset) {
  160. return; // Not moving; do nothing.
  161. }
  162. parent::moveToOffset($offset);
  163. $result = @fseek($this->_fileResource, $offset, SEEK_SET);
  164. if ($result !== 0) {
  165. throw new Pdf\Exception('Error while setting new file position',
  166. Pdf\Exception::CANT_SET_FILE_POSITION);
  167. }
  168. if (feof($this->_fileResource)) {
  169. throw new Pdf\Exception('Moved beyond the end of the file',
  170. Pdf\Exception::MOVE_BEYOND_END_OF_FILE);
  171. }
  172. }
  173. }