Skip to content

Commit f32e6a5

Browse files
committed
Added 'append' parameter to Client::setCallback() to disable response append to save memory as requested on #24
1 parent f86ce8e commit f32e6a5

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

src/Client.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ abstract class Client
6565
*/
6666
protected $callback = null;
6767

68+
/**
69+
* Enable or disable appending when using callback
70+
*
71+
* @var bool
72+
*/
73+
protected $callbackAppend = true;
74+
6875
/**
6976
* Size of chunks for callback
7077
*
@@ -140,14 +147,16 @@ public function getCallback()
140147
* @return $this
141148
* @throws \Exception
142149
*/
143-
public function setCallback($callback)
150+
public function setCallback($callback, $append = true)
144151
{
145152
if($callback instanceof Closure)
146153
{
154+
$this->callbackAppend = (bool) $append;
147155
$this->callback = $callback;
148156
}
149157
elseif(is_callable($callback))
150158
{
159+
$this->callbackAppend = (bool) $append;
151160
$this->callback = function($chunk) use($callback)
152161
{
153162
return call_user_func_array($callback, [$chunk]);
@@ -288,14 +297,15 @@ public function getMIME($file)
288297
*
289298
* @param string $file
290299
* @param mixed $callback
300+
* @param bool $append
291301
* @return string
292302
* @throws \Exception
293303
*/
294-
public function getHTML($file, $callback = null)
304+
public function getHTML($file, $callback = null, $append = true)
295305
{
296306
if(!is_null($callback))
297307
{
298-
$this->setCallback($callback);
308+
$this->setCallback($callback, $append);
299309
}
300310

301311
return $this->request('html', $file);
@@ -306,14 +316,15 @@ public function getHTML($file, $callback = null)
306316
*
307317
* @param string $file
308318
* @param mixed $callback
319+
* @param bool $append
309320
* @return string
310321
* @throws \Exception
311322
*/
312-
public function getText($file, $callback = null)
323+
public function getText($file, $callback = null, $append = true)
313324
{
314325
if(!is_null($callback))
315326
{
316-
$this->setCallback($callback);
327+
$this->setCallback($callback, $append);
317328
}
318329

319330
return $this->request('text', $file);
@@ -324,14 +335,15 @@ public function getText($file, $callback = null)
324335
*
325336
* @param string $file
326337
* @param mixed $callback
338+
* @param bool $append
327339
* @return string
328340
* @throws \Exception
329341
*/
330-
public function getMainText($file, $callback = null)
342+
public function getMainText($file, $callback = null, $append = true)
331343
{
332344
if(!is_null($callback))
333345
{
334-
$this->setCallback($callback);
346+
$this->setCallback($callback, $append);
335347
}
336348

337349
return $this->request('text-main', $file);

src/Clients/CLIClient.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ public function exec($command)
219219
$callback($chunk);
220220
}
221221

222-
$this->response .= $chunk;
222+
if($this->callbackAppend === true)
223+
{
224+
$this->response .= $chunk;
225+
}
223226
}
224227
fclose($pipes[1]);
225228
$exit = proc_close($process);

src/Clients/WebClient.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public function request($type, $file = null)
323323
}
324324

325325
// parameters for cURL request
326-
list($resource, $headers) = $this->getParameters($type, $file);
326+
[$resource, $headers] = $this->getParameters($type, $file);
327327

328328
// check the request
329329
$file = $this->checkRequest($type, $file);
@@ -341,7 +341,7 @@ public function request($type, $file = null)
341341
$options[CURLOPT_URL] = $this->getUrl() . "/$resource";
342342

343343
// get the response and the HTTP status code
344-
list($response, $status) = $this->exec($options);
344+
[$response, $status] = $this->exec($options);
345345

346346
// reduce memory usage closing cURL resource
347347
if(isset($options[CURLOPT_INFILE]) && is_resource($options[CURLOPT_INFILE]))
@@ -560,7 +560,10 @@ protected function getCurlOptions($type, $file = null)
560560

561561
$options[CURLOPT_WRITEFUNCTION] = function($handler, $data) use($callback)
562562
{
563-
$this->response .= $data;
563+
if($this->callbackAppend === true)
564+
{
565+
$this->response .= $data;
566+
}
564567

565568
$callback($data);
566569

tests/BaseTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public function testTextCallback($file)
449449
{
450450
$client =& self::$client;
451451

452-
if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
452+
if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
453453
{
454454
$this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
455455
}
@@ -463,6 +463,32 @@ public function testTextCallback($file)
463463
}
464464
}
465465

466+
/**
467+
* Text callback test
468+
*
469+
* @dataProvider callbackProvider
470+
*
471+
* @param string $file
472+
* @throws \Exception
473+
*/
474+
public function testTextCallbackWithoutAppend($file)
475+
{
476+
$client =& self::$client;
477+
478+
if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
479+
{
480+
$this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
481+
}
482+
else
483+
{
484+
BaseTest::$shared = 0;
485+
486+
$response = self::$client->getText($file, [$this, 'callableCallback'], false);
487+
488+
$this->assertEmpty($response);
489+
}
490+
}
491+
466492
/**
467493
* Main text callback test
468494
*

0 commit comments

Comments
 (0)