|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pim\Bundle\ExcelConnectorBundle\Reader; |
|
4
|
|
|
|
|
5
|
|
|
use Akeneo\Component\Batch\Model\StepExecution; |
|
6
|
|
|
use Akeneo\Component\Batch\Item\AbstractConfigurableStepElement; |
|
7
|
|
|
use Akeneo\Component\Batch\Item\ItemReaderInterface; |
|
8
|
|
|
use Akeneo\Component\Batch\Step\StepExecutionAwareInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Abstract iterator based reader |
|
12
|
|
|
* |
|
13
|
|
|
* @author Antoine Guigan <[email protected]> |
|
14
|
|
|
* @copyright 2013 Akeneo SAS (http://www.akeneo.com) |
|
15
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractIteratorReader extends AbstractConfigurableStepElement implements ItemReaderInterface, |
|
|
|
|
|
|
18
|
|
|
StepExecutionAwareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var \Iterator */ |
|
21
|
|
|
protected $iterator; |
|
22
|
|
|
|
|
23
|
|
|
/** @var StepExecution */ |
|
24
|
|
|
protected $stepExecution; |
|
25
|
|
|
|
|
26
|
|
|
/** @var boolean */ |
|
27
|
|
|
protected $batchMode; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param boolean $batchMode |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($batchMode = false) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->batchMode = $batchMode; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setStepExecution(StepExecution $stepExecution) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->stepExecution = $stepExecution; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function read() |
|
49
|
|
|
{ |
|
50
|
|
|
if (!isset($this->iterator)) { |
|
51
|
|
|
$this->initializeIterator(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (!$this->iterator->valid()) { |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$current = $this->iterator->current(); |
|
59
|
|
|
if ($this->stepExecution) { |
|
60
|
|
|
$this->stepExecution->incrementSummaryInfo('read'); |
|
61
|
|
|
} |
|
62
|
|
|
$this->iterator->next(); |
|
63
|
|
|
|
|
64
|
|
|
return $this->convertNumericIdentifierToString($current); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Resets the state of the reader |
|
69
|
|
|
* |
|
70
|
|
|
* @deprecated will be remove in 1.7. Use initialize method instead. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function reset() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->iterator = null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Initializes the iterator allowing to use the same service in two successive steps |
|
79
|
|
|
* |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function initialize() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->iterator = null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Converts an entity numerical identifier ('sku' for products, |
|
89
|
|
|
* 'code' for other entities) into string to allow import. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $item |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function convertNumericIdentifierToString(array $item) |
|
96
|
|
|
{ |
|
97
|
|
|
return $item; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Initializes the iterator |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function initializeIterator() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->iterator = $this->createIterator(); |
|
106
|
|
|
if ($this->batchMode) { |
|
107
|
|
|
$this->iterator = new \ArrayIterator(array(iterator_to_array($this->iterator))); |
|
108
|
|
|
} |
|
109
|
|
|
$this->iterator->rewind(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Creates the iterator |
|
114
|
|
|
* |
|
115
|
|
|
* @return \Iterator |
|
116
|
|
|
*/ |
|
117
|
|
|
abstract protected function createIterator(); |
|
118
|
|
|
} |
|
119
|
|
|
|