1 | <?php |
||
12 | abstract class FilePersistenceHandler implements PersistenceHandlerInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var string the path where all spider results should be persisted. |
||
16 | * The results will be grouped in a directory by spider ID. |
||
17 | */ |
||
18 | protected $path = ''; |
||
19 | |||
20 | protected $spiderId = ''; |
||
21 | |||
22 | protected $totalSizePersisted = 0; |
||
23 | |||
24 | /** @var \Iterator */ |
||
25 | protected $iterator; |
||
26 | |||
27 | /** @var Finder */ |
||
28 | protected $finder; |
||
29 | |||
30 | /** |
||
31 | * @param string $path the path where all spider results should be persisted. |
||
32 | * The results will be grouped in a directory by spider ID. |
||
33 | */ |
||
34 | public function __construct($path) |
||
38 | |||
39 | public function setSpiderId($spiderId) |
||
40 | { |
||
41 | $this->spiderId = $spiderId; |
||
42 | |||
43 | // create the path |
||
44 | if (!file_exists($this->getResultPath())) { |
||
45 | mkdir($this->getResultPath(), 0700, true); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | public function count() |
||
53 | |||
54 | protected function getResultPath() |
||
58 | |||
59 | abstract public function persist(Resource $resource); |
||
60 | |||
61 | /** |
||
62 | * @return Finder |
||
63 | */ |
||
64 | protected function getFinder() |
||
71 | |||
72 | /** |
||
73 | * @return \Iterator |
||
74 | */ |
||
75 | protected function getIterator() |
||
82 | |||
83 | /** |
||
84 | * Encodes file name into suitable format and trims its size. |
||
85 | * File name length is limited 255 chars, on Win32 a whole file path is limited 260 chars. |
||
86 | * @param string $fileName |
||
87 | * @return string |
||
88 | */ |
||
89 | protected function encodeFileName($fileName) |
||
93 | |||
94 | /** |
||
95 | * @return Resource |
||
96 | */ |
||
97 | abstract public function current(); |
||
98 | |||
99 | /** |
||
100 | * @return void |
||
101 | */ |
||
102 | public function next() |
||
106 | |||
107 | /** |
||
108 | * @return integer|double|string|boolean|null |
||
109 | */ |
||
110 | public function key() |
||
114 | |||
115 | /** |
||
116 | * @return boolean |
||
117 | */ |
||
118 | public function valid() |
||
122 | |||
123 | /** |
||
124 | * @return void |
||
125 | */ |
||
126 | public function rewind() |
||
130 | } |
||
131 |