|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Export; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Templating\EngineInterface; |
|
6
|
|
|
use Symfony\Component\Templating\TemplateReferenceInterface; |
|
7
|
|
|
|
|
8
|
|
|
class FeedWriter |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var EngineInterface |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $templating; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $rootNode; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $itemNode; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var null|resource |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $pointer; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param EngineInterface $templating |
|
32
|
|
|
* @param string $rootNode |
|
33
|
|
|
* @param string $itemNode |
|
34
|
|
|
*/ |
|
35
|
22 |
|
public function __construct(EngineInterface $templating, $rootNode, $itemNode) |
|
36
|
|
|
{ |
|
37
|
22 |
|
$this->templating = $templating; |
|
38
|
22 |
|
$this->rootNode = $rootNode; |
|
39
|
22 |
|
$this->itemNode = $itemNode; |
|
40
|
22 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
20 |
|
public function isStarted() |
|
46
|
|
|
{ |
|
47
|
20 |
|
return is_resource($this->pointer); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $filename |
|
52
|
|
|
* @param string $namespaces |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \RuntimeException |
|
55
|
|
|
*/ |
|
56
|
12 |
|
public function start($filename, $namespaces = '') |
|
57
|
|
|
{ |
|
58
|
12 |
|
if ($this->isStarted()) { |
|
59
|
2 |
|
throw new \RuntimeException('Writer has already started'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
12 |
|
$this->pointer = fopen($filename, 'w'); |
|
63
|
|
|
|
|
64
|
12 |
|
$this->writeStart($namespaces); |
|
65
|
12 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @throws \RuntimeException |
|
69
|
|
|
*/ |
|
70
|
6 |
|
public function finish() |
|
71
|
|
|
{ |
|
72
|
6 |
|
if (!$this->isStarted()) { |
|
73
|
4 |
|
throw new \RuntimeException('Writer has not yet started'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
$this->writeEnd(); |
|
77
|
|
|
|
|
78
|
2 |
|
fclose($this->pointer); |
|
79
|
|
|
|
|
80
|
2 |
|
$this->pointer = null; |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $content |
|
85
|
|
|
* |
|
86
|
|
|
* @throws \RuntimeException |
|
87
|
|
|
*/ |
|
88
|
16 |
|
public function writeContent($content) |
|
89
|
|
|
{ |
|
90
|
16 |
|
if (!$this->isStarted()) { |
|
91
|
4 |
|
throw new \RuntimeException('Writer has not yet started'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
12 |
|
fwrite($this->pointer, $content); |
|
95
|
12 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param object $item |
|
99
|
|
|
* @param string|TemplateReferenceInterface $template |
|
100
|
|
|
* |
|
101
|
|
|
* @throws \RuntimeException |
|
102
|
|
|
*/ |
|
103
|
4 |
|
public function writeItem($item, $template) |
|
104
|
|
|
{ |
|
105
|
4 |
|
$this->writeContent($this->renderItem($item, $template)); |
|
106
|
2 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param object $item |
|
110
|
|
|
* @param string|TemplateReferenceInterface $template |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
4 |
|
public function renderItem($item, $template) |
|
115
|
|
|
{ |
|
116
|
4 |
|
return $this->templating->render($template, ['item' => $item, 'itemNode' => $this->itemNode]); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Writes the XML prolog. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $namespaces |
|
123
|
|
|
*/ |
|
124
|
12 |
|
protected function writeStart($namespaces = '') |
|
125
|
|
|
{ |
|
126
|
12 |
|
$this->writeContent('<?xml version="1.0" encoding="UTF-8"?>'); |
|
127
|
12 |
|
$this->writeContent(sprintf('<%s%s>', $this->rootNode, rtrim(' ' . $namespaces))); |
|
128
|
12 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Writes the end part of the XML, closing the rootnode. |
|
132
|
|
|
* |
|
133
|
|
|
* @throws \RuntimeException If open() is not called yet |
|
134
|
|
|
*/ |
|
135
|
2 |
|
protected function writeEnd() |
|
136
|
|
|
{ |
|
137
|
2 |
|
if (!$this->isStarted()) { |
|
138
|
|
|
throw new \RuntimeException('Writer has not yet started'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
2 |
|
$this->writeContent(sprintf('</%s>', $this->rootNode)); |
|
142
|
|
|
|
|
143
|
2 |
|
$this->rootNode = null; |
|
144
|
2 |
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|