-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHooker.php
58 lines (47 loc) · 954 Bytes
/
FileHooker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* Created by PhpStorm.
* User: andy
* Date: 10/01/14
* Time: 02:20
*/
require_once("Hooker.php");
require_once("ProgressBar.php");
use Cli\ProgressBar;
class FileHooker implements \Curl\Hooker {
private $_label;
private $_fileName;
private $_fd;
/**
* @var ProgressBar
*/
private $_bar;
function __construct($fileName, $label)
{
$this->_fileName = $fileName;
$this->_label = $label;
}
public function onInit()
{
echo $this->_label;
$this->_bar = new ProgressBar(80, 20);
$directory = dirname($this->_fileName);
if (!file_exists($directory))
mkdir($directory, 0777, true);
$this->_fd = fopen($this->_fileName, "w");
}
public function onProgress($fullSize, $completed)
{
$this->_bar->setValue($completed / $fullSize);
}
public function onWrite($data)
{
if (strlen($data) > 0)
fwrite($this->_fd, $data);
}
public function onFinish()
{
fclose($this->_fd);
$this->_bar = null;
}
}