Skip to content

Commit 2fea1e5

Browse files
author
Stan Hutcheon
committed
0.1.6
- Remove user_error, use throw cases instead - Remove use of ob_start/clean - documented instead
1 parent c8ec7ba commit 2fea1e5

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ $fullName = $file->name(); # full file name in zip, including path
4343
```
4444

4545
###### get($file, $output = false):
46-
Returns, or outputs the file fetched from the remote ZIP
46+
Returns, or outputs the file fetched from the remote ZIP.
47+
48+
**Note**: You should ensure no content is outputted before running ```->get($file, true)``` as this will cause the file download to contain invalid data.
49+
*Hint*: put ```ob_start()``` at the start of your script, then run ```ob_clean()``` before calling get.
4750
```php
4851
<?php
4952
/*...*/
@@ -63,11 +66,14 @@ require 'vendor/autoload.php';
6366

6467
use Stnvh\Partial\Zip as Partial;
6568

69+
ob_start(); # will capture all output
70+
6671
$p = new Partial('http://some.site.com/cats.zip', 'cat.png');
6772

6873
# Get file object
6974
$file = $p->find();
7075
if($file) {
76+
ob_clean(); # removes everything from current output to ensure file downloads correctly
7177
# Output to browser:
7278
$p->get($file, true);
7379
}

src/Stnvh/Partial/Data/PartialData.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Stnvh\Partial\Data;
44

5+
use \RuntimeException as RuntimeException;
6+
use \InvalidArgumentException as InvalidArgumentException;
7+
use \BadMethodCallException as BadMethodCallException;
8+
59
/**
610
* Class PartialData
711
* @package Stnvh\Partial\Data
@@ -41,8 +45,7 @@ public function __call($name, $args) {
4145
*/
4246
public function get() {
4347
if(!file_exists($this->tempName)) {
44-
user_error('Temporary filename not set, did you call get() directly on the file object?', E_USER_ERROR);
45-
exit;
48+
throw new BadMethodCallException('Called before being fetched with Zip->get(). Don\'t call this directly!');
4649
}
4750

4851
switch($this->method) {
@@ -58,8 +61,7 @@ public function get() {
5861
$_method = 'bzdecompress';
5962
break;
6063
} else {
61-
user_error('Unable to decompress, failed to load bz2 extension', E_USER_ERROR);
62-
exit;
64+
throw new RuntimeException('Unable to decompress, failed to load bz2 extension');
6365
}
6466
default:
6567
$_method = false;
@@ -76,7 +78,7 @@ public function get() {
7678
* Removes the cached item from the disk
7779
* @return void
7880
*/
79-
public function purge() {
81+
private function purge() {
8082
if(file_exists($this->tempName)) {
8183
@unlink($this->tempName);
8284
}
@@ -99,8 +101,7 @@ public function isDir() {
99101
public function format($raw, $map = false) {
100102
$map = $map ?: $this->map;
101103
if(!$map) {
102-
user_error('No byte map specified', E_USER_ERROR);
103-
exit;
104+
throw new InvalidArgumentException('No byte map specified');
104105
}
105106

106107
$i = 0;

src/Stnvh/Partial/Zip.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Stnvh\Partial\Data;
66

7+
use \RuntimeException as RuntimeException;
8+
use \InvalidArgumentException as InvalidArgumentException;
9+
710
/**
811
* Class Zip
912
* @package Stnvh\Partial
@@ -64,8 +67,6 @@ private function receiveData($ch, $data) {
6467
* @return void
6568
*/
6669
public function __construct($url, $file = false) {
67-
ob_start();
68-
6970
$this->info = new Data\ZipInfo();
7071
$this->info->url = $url;
7172
$this->info->file = $file;
@@ -87,13 +88,11 @@ public function init() {
8788
));
8889

8990
if($request['http_code'] > 400) {
90-
user_error(sprintf('Initial request failed, got HTTP status code: %d', $request['http_code']) , E_USER_ERROR);
91-
exit;
91+
throw new RuntimeException(sprintf('Initial request failed, got HTTP status code: %d', $request['http_code']));
9292
}
9393

9494
if(!$request['headers']['Accept-Ranges']) {
95-
user_error('Server does not support HTTP range requests', E_USER_ERROR);
96-
exit;
95+
throw new RuntimeException('Server does not support HTTP range requests');
9796
}
9897

9998
$this->info->length = intval($request['download_content_length']);
@@ -118,8 +117,7 @@ public function init() {
118117
if($_EOCD = strstr($this->info->centralDirectoryEnd, "\x50\x4b\x05\x06")) {
119118
$this->info->centralDirectoryDesc = new Data\EOCD($_EOCD);
120119
} else {
121-
user_error('End of central directory not found', E_USER_ERROR);
122-
exit;
120+
throw new RuntimeException('End of central directory not found');
123121
}
124122

125123
if($cdEnd = $this->info->centralDirectoryDesc) {
@@ -155,7 +153,7 @@ public function init() {
155153
if($entry->isDir()) {
156154
continue;
157155
}
158-
156+
159157
$this->info->centralDirectory[$entry->name] = $raw;
160158
unset($_entries[$i]); # free mem as we loop
161159
}
@@ -180,10 +178,9 @@ public function index() {
180178
*/
181179
public function find($fileName = false) {
182180
$fileName = $fileName ?: $this->info->file;
183-
181+
184182
if(!$fileName) {
185-
user_error('No filename specified to search', E_USER_ERROR);
186-
exit;
183+
throw new InvalidArgumentException('No filename specified to search');
187184
}
188185

189186
foreach($this->info->centralDirectory as $name => $raw) {
@@ -203,8 +200,7 @@ public function find($fileName = false) {
203200
*/
204201
public function get(Data\CDFile $file, $output = false) {
205202
if(!$file) {
206-
user_error('No CDFile object specified', E_USER_ERROR);
207-
exit;
203+
throw new InvalidArgumentException('No CDFile object specified');
208204
}
209205

210206
$this->tempName = $file->tempName;
@@ -236,7 +232,6 @@ public function get(Data\CDFile $file, $output = false) {
236232
));
237233

238234
if($output) {
239-
ob_clean(); # clean output
240235
header(sprintf('Content-Disposition: attachment; filename="%s"', $file->filename));
241236
header(sprintf('Content-Length: %d', $file->size));
242237
header('Pragma: public');

0 commit comments

Comments
 (0)