| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | declare(strict_types=1);
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | namespace OpenSpout\Writer;
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | use OpenSpout\Common\Entity\Row;
 | 
        
           |  |  | 8 | use OpenSpout\Common\Exception\IOException;
 | 
        
           |  |  | 9 | use OpenSpout\Writer\Exception\WriterNotOpenedException;
 | 
        
           |  |  | 10 |   | 
        
           |  |  | 11 | abstract class AbstractWriter implements WriterInterface
 | 
        
           |  |  | 12 | {
 | 
        
           |  |  | 13 |     /** @var resource Pointer to the file/stream we will write to */
 | 
        
           |  |  | 14 |     protected $filePointer;
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 |     /** @var string Content-Type value for the header - to be defined by child class */
 | 
        
           |  |  | 17 |     protected static string $headerContentType;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 |     /** @var string Path to the output file */
 | 
        
           |  |  | 20 |     private string $outputFilePath;
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 |     /** @var bool Indicates whether the writer has been opened or not */
 | 
        
           |  |  | 23 |     private bool $isWriterOpened = false;
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 |     /** @var 0|positive-int */
 | 
        
           |  |  | 26 |     private int $writtenRowCount = 0;
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 |     final public function openToFile($outputFilePath): void
 | 
        
           |  |  | 29 |     {
 | 
        
           |  |  | 30 |         $this->outputFilePath = $outputFilePath;
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 |         $errorMessage = null;
 | 
        
           |  |  | 33 |         set_error_handler(static function ($nr, $message) use (&$errorMessage): bool {
 | 
        
           |  |  | 34 |             $errorMessage = $message;
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |             return true;
 | 
        
           |  |  | 37 |         });
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |         $resource = fopen($this->outputFilePath, 'w');
 | 
        
           |  |  | 40 |         restore_error_handler();
 | 
        
           |  |  | 41 |         if (null !== $errorMessage) {
 | 
        
           |  |  | 42 |             throw new IOException("Unable to open file {$this->outputFilePath}: {$errorMessage}");
 | 
        
           |  |  | 43 |         }
 | 
        
           |  |  | 44 |         \assert(false !== $resource);
 | 
        
           |  |  | 45 |         $this->filePointer = $resource;
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |         $this->openWriter();
 | 
        
           |  |  | 48 |         $this->isWriterOpened = true;
 | 
        
           |  |  | 49 |     }
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 |     /**
 | 
        
           |  |  | 52 |      * @codeCoverageIgnore
 | 
        
           |  |  | 53 |      *
 | 
        
           |  |  | 54 |      * @param mixed $outputFileName
 | 
        
           |  |  | 55 |      */
 | 
        
           |  |  | 56 |     final public function openToBrowser($outputFileName): void
 | 
        
           |  |  | 57 |     {
 | 
        
           |  |  | 58 |         $this->outputFilePath = basename($outputFileName);
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |         $resource = fopen('php://output', 'w');
 | 
        
           |  |  | 61 |         \assert(false !== $resource);
 | 
        
           |  |  | 62 |         $this->filePointer = $resource;
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |         // Clear any previous output (otherwise the generated file will be corrupted)
 | 
        
           |  |  | 65 |         // @see https://github.com/box/spout/issues/241
 | 
        
           |  |  | 66 |         if (ob_get_length() > 0) {
 | 
        
           |  |  | 67 |             ob_end_clean();
 | 
        
           |  |  | 68 |         }
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |         /*
 | 
        
           |  |  | 71 |          * Set headers
 | 
        
           |  |  | 72 |          *
 | 
        
           |  |  | 73 |          * For newer browsers such as Firefox, Chrome, Opera, Safari, etc., they all support and use `filename*`
 | 
        
           |  |  | 74 |          * specified by the new standard, even if they do not automatically decode filename; it does not matter;
 | 
        
           |  |  | 75 |          * and for older versions of Internet Explorer, they are not recognized `filename*`, will automatically
 | 
        
           |  |  | 76 |          * ignore it and use the old `filename` (the only minor flaw is that there must be an English suffix name).
 | 
        
           |  |  | 77 |          * In this way, the multi-browser multi-language compatibility problem is perfectly solved, which does not
 | 
        
           |  |  | 78 |          * require UA judgment and is more in line with the standard.
 | 
        
           |  |  | 79 |          *
 | 
        
           |  |  | 80 |          * @see https://github.com/box/spout/issues/745
 | 
        
           |  |  | 81 |          * @see https://tools.ietf.org/html/rfc6266
 | 
        
           |  |  | 82 |          * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
 | 
        
           |  |  | 83 |          */
 | 
        
           |  |  | 84 |         header('Content-Type: '.static::$headerContentType);
 | 
        
           |  |  | 85 |         header(
 | 
        
           |  |  | 86 |             'Content-Disposition: attachment; '.
 | 
        
           |  |  | 87 |             'filename="'.rawurlencode($this->outputFilePath).'"; '.
 | 
        
           |  |  | 88 |             'filename*=UTF-8\'\''.rawurlencode($this->outputFilePath)
 | 
        
           |  |  | 89 |         );
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |         /*
 | 
        
           |  |  | 92 |          * When forcing the download of a file over SSL,IE8 and lower browsers fail
 | 
        
           |  |  | 93 |          * if the Cache-Control and Pragma headers are not set.
 | 
        
           |  |  | 94 |          *
 | 
        
           |  |  | 95 |          * @see http://support.microsoft.com/KB/323308
 | 
        
           |  |  | 96 |          * @see https://github.com/liuggio/ExcelBundle/issues/45
 | 
        
           |  |  | 97 |          */
 | 
        
           |  |  | 98 |         header('Cache-Control: max-age=0');
 | 
        
           |  |  | 99 |         header('Pragma: public');
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |         $this->openWriter();
 | 
        
           |  |  | 102 |         $this->isWriterOpened = true;
 | 
        
           |  |  | 103 |     }
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |     final public function addRow(Row $row): void
 | 
        
           |  |  | 106 |     {
 | 
        
           |  |  | 107 |         if (!$this->isWriterOpened) {
 | 
        
           |  |  | 108 |             throw new WriterNotOpenedException('The writer needs to be opened before adding row.');
 | 
        
           |  |  | 109 |         }
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |         $this->addRowToWriter($row);
 | 
        
           |  |  | 112 |         ++$this->writtenRowCount;
 | 
        
           |  |  | 113 |     }
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 |     final public function addRows(array $rows): void
 | 
        
           |  |  | 116 |     {
 | 
        
           |  |  | 117 |         foreach ($rows as $row) {
 | 
        
           |  |  | 118 |             $this->addRow($row);
 | 
        
           |  |  | 119 |         }
 | 
        
           |  |  | 120 |     }
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |     final public function getWrittenRowCount(): int
 | 
        
           |  |  | 123 |     {
 | 
        
           |  |  | 124 |         return $this->writtenRowCount;
 | 
        
           |  |  | 125 |     }
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 |     final public function close(): void
 | 
        
           |  |  | 128 |     {
 | 
        
           |  |  | 129 |         if (!$this->isWriterOpened) {
 | 
        
           |  |  | 130 |             return;
 | 
        
           |  |  | 131 |         }
 | 
        
           |  |  | 132 |   | 
        
           |  |  | 133 |         $this->closeWriter();
 | 
        
           |  |  | 134 |   | 
        
           |  |  | 135 |         fclose($this->filePointer);
 | 
        
           |  |  | 136 |   | 
        
           |  |  | 137 |         $this->isWriterOpened = false;
 | 
        
           |  |  | 138 |     }
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |     /**
 | 
        
           |  |  | 141 |      * Opens the streamer and makes it ready to accept data.
 | 
        
           |  |  | 142 |      *
 | 
        
           |  |  | 143 |      * @throws IOException If the writer cannot be opened
 | 
        
           |  |  | 144 |      */
 | 
        
           |  |  | 145 |     abstract protected function openWriter(): void;
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |     /**
 | 
        
           |  |  | 148 |      * Adds a row to the currently opened writer.
 | 
        
           |  |  | 149 |      *
 | 
        
           |  |  | 150 |      * @param Row $row The row containing cells and styles
 | 
        
           |  |  | 151 |      *
 | 
        
           |  |  | 152 |      * @throws WriterNotOpenedException If the workbook is not created yet
 | 
        
           |  |  | 153 |      * @throws IOException              If unable to write data
 | 
        
           |  |  | 154 |      */
 | 
        
           |  |  | 155 |     abstract protected function addRowToWriter(Row $row): void;
 | 
        
           |  |  | 156 |   | 
        
           |  |  | 157 |     /**
 | 
        
           |  |  | 158 |      * Closes the streamer, preventing any additional writing.
 | 
        
           |  |  | 159 |      */
 | 
        
           |  |  | 160 |     abstract protected function closeWriter(): void;
 | 
        
           |  |  | 161 | }
 |