/** * A simple method to quickly send attachment stream download. * * @param string|resource $source The file source, can be file path or resource. * @param ResponseInterface $response A custom Response object to contain your headers. * @param array $options Options to provide some settings, currently supports * "delay" and "filename". * * @return void */ public static function sendAttachment($source, ResponseInterface $response = null, $options = array()) { $stream = new Stream($source, 'r'); /** @var MessageInterface|ResponseInterface $response */ $response = $response ?: new Response(); $filename = null; if (is_string($source)) { $filename = pathinfo($source, PATHINFO_BASENAME); } if (isset($options['filename'])) { $filename = $options['filename']; } $response = HeaderHelper::prepareAttachmentHeaders($response, $filename); $response = $response->withBody($stream); $output = static::$outputObject; if (!$output instanceof StreamOutput) { $output = new StreamOutput(); } if (isset($options['delay'])) { $output->setDelay($options['delay']); } $output->respond($response); }
/** * testPrepareAttachmentHeaders * * @return void * * @covers \Windwalker\Http\Helper\HeaderHelper::prepareAttachmentHeaders */ public function testPrepareAttachmentHeaders() { $response = HeaderHelper::prepareAttachmentHeaders(new Response()); $this->assertEquals(array('application/octet-stream'), $response->getHeader('Content-Type')); $this->assertEquals(array('binary'), $response->getHeader('Content-Transfer-Encoding')); $this->assertEquals(array('no-store, no-cache, must-revalidate'), $response->getHeader('Cache-Control')); $this->assertEquals(array(), $response->getHeader('Content-Disposition')); $response = HeaderHelper::prepareAttachmentHeaders(new Response(), 'foo.zip'); $this->assertEquals(array('attachment; filename="foo.zip"'), $response->getHeader('Content-Disposition')); }