|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * This file is part of the PdfParser library. |
| 6 | + * |
| 7 | + * @author Andreas Gohr <gohr@cosmocode.de> |
| 8 | + * |
| 9 | + * @date 2026-06-18 |
| 10 | + * |
| 11 | + * @license LGPLv3 |
| 12 | + * |
| 13 | + * @url <https://github.com/smalot/pdfparser> |
| 14 | + * |
| 15 | + * PdfParser is a pdf library written in PHP, extraction oriented. |
| 16 | + * Copyright (C) 2017 - Sébastien MALOT <sebastien@malot.fr> |
| 17 | + * |
| 18 | + * This program is free software: you can redistribute it and/or modify |
| 19 | + * it under the terms of the GNU Lesser General Public License as published by |
| 20 | + * the Free Software Foundation, either version 3 of the License, or |
| 21 | + * (at your option) any later version. |
| 22 | + * |
| 23 | + * This program is distributed in the hope that it will be useful, |
| 24 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | + * GNU Lesser General Public License for more details. |
| 27 | + * |
| 28 | + * You should have received a copy of the GNU Lesser General Public License |
| 29 | + * along with this program. |
| 30 | + * If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>. |
| 31 | + */ |
| 32 | + |
| 33 | +namespace Smalot\PdfParser; |
| 34 | + |
| 35 | +/** |
| 36 | + * Temporary on-disk store for decoded stream content. |
| 37 | + * |
| 38 | + * When content spooling is enabled (see Config::setContentSpooling()), the |
| 39 | + * decoded content of each parsed object is appended to a single temporary file |
| 40 | + * instead of being held in memory, and read back on demand. This trades a small |
| 41 | + * amount of disk I/O for a markedly lower peak memory footprint, since the full |
| 42 | + * set of decoded streams - usually the largest thing a parsed Document keeps |
| 43 | + * alive - no longer has to reside in RAM all at once. |
| 44 | + * |
| 45 | + * The backing temporary file is created lazily on first use and removed |
| 46 | + * automatically once the spool (and the Document owning it) is destroyed. |
| 47 | + * |
| 48 | + * @internal |
| 49 | + */ |
| 50 | +class ContentSpool |
| 51 | +{ |
| 52 | + /** |
| 53 | + * @var resource|null |
| 54 | + */ |
| 55 | + private $handle; |
| 56 | + |
| 57 | + /** |
| 58 | + * Current size of the spool file, i.e. the offset at which the next chunk |
| 59 | + * of content will be written. |
| 60 | + * |
| 61 | + * @var int |
| 62 | + */ |
| 63 | + private $size = 0; |
| 64 | + |
| 65 | + /** |
| 66 | + * Append a chunk of content to the spool. |
| 67 | + * |
| 68 | + * @return array{0: int, 1: int}|null [offset, length] locating the stored |
| 69 | + * content, or null if it could not be |
| 70 | + * stored (the caller should then keep |
| 71 | + * the content in memory) |
| 72 | + */ |
| 73 | + public function store(string $content): ?array |
| 74 | + { |
| 75 | + $length = \strlen($content); |
| 76 | + if (0 === $length) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + $handle = $this->handle(); |
| 81 | + if (null === $handle) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + $offset = $this->size; |
| 86 | + if (0 === fseek($handle, $offset) && fwrite($handle, $content) === $length) { |
| 87 | + $this->size += $length; |
| 88 | + |
| 89 | + return [$offset, $length]; |
| 90 | + } |
| 91 | + |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Read back content previously stored via store(). |
| 97 | + * |
| 98 | + * @param int $offset offset returned by store() |
| 99 | + * @param int $length length returned by store() |
| 100 | + */ |
| 101 | + public function fetch(int $offset, int $length): string |
| 102 | + { |
| 103 | + if (!\is_resource($this->handle) || $length <= 0) { |
| 104 | + return ''; |
| 105 | + } |
| 106 | + |
| 107 | + // Seeking before reading also flushes any pending write buffer, which |
| 108 | + // is required when reading data that was just written to the handle. |
| 109 | + if (0 !== fseek($this->handle, $offset)) { |
| 110 | + return ''; |
| 111 | + } |
| 112 | + |
| 113 | + $content = ''; |
| 114 | + $remaining = $length; |
| 115 | + while ($remaining > 0 && !feof($this->handle)) { |
| 116 | + $chunk = fread($this->handle, $remaining); |
| 117 | + if (false === $chunk || '' === $chunk) { |
| 118 | + break; |
| 119 | + } |
| 120 | + $content .= $chunk; |
| 121 | + $remaining -= \strlen($chunk); |
| 122 | + } |
| 123 | + |
| 124 | + return $content; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Lazily open the backing temporary file. |
| 129 | + * |
| 130 | + * @return resource|null |
| 131 | + */ |
| 132 | + private function handle() |
| 133 | + { |
| 134 | + if (null === $this->handle) { |
| 135 | + // tmpfile() opens a binary-safe read-write handle and removes the |
| 136 | + // underlying file automatically when the handle is closed. |
| 137 | + $handle = tmpfile(); |
| 138 | + $this->handle = false !== $handle ? $handle : null; |
| 139 | + } |
| 140 | + |
| 141 | + return $this->handle; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Ensure the backing file is closed and removed when the spool is destroyed. |
| 146 | + */ |
| 147 | + public function __destruct() |
| 148 | + { |
| 149 | + if (\is_resource($this->handle)) { |
| 150 | + fclose($this->handle); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments