This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
/
Control.php
270 lines (220 loc) · 6.1 KB
/
Control.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* TwitterControl for Nette Framework 2.0, http://github.com/smasty/TwitterControl
* Copyright 2011 Martin Srank (http://smasty.net)
* Licensed under terms of the MIT License (http://opensource.org/licenses/mit-license)
*/
namespace Smasty\Components\Twitter;
use Nette,
Nette\InvalidStateException,
Nette\Diagnostics\Debugger,
Nette\Application\UI\Control as NetteControl;
/**
* TwitterControl renderable component.
*
* Available config options:
* - screenName => Twitter screen name (either screenName or userId is required)
* - userId => Twitter user ID (takes precedence over screenName, if both specified)
* - tweetCount => Number of tweets to load (max. 200)
*
* - header => Render component header with user info
* - avatars => Render avatars next to tweets
* - retweets => Include retweets
* - replies => Include replies
* - intents => Render tweet intents (reply, retweet, favorite)
*
* @author Martin Srank, http://smasty.net
*/
class Control extends NetteControl {
/** @var string */
public static $templateDirectory = '/templates';
/** @var string */
private $templateFile = '/TwitterControl.latte';
/** @var array */
private $config;
/** @var ILoader */
private $loader;
/** @var IFormatter */
private $formatter;
const VERSION = '2.0.2';
/**
* Create the TwitterControl.
* @param array|string|int $config Config options (array) or Twitter screen name (string) or Twitter user ID (int)
* @return void
*/
public function __construct($config){
if(!$config)
throw new InvalidStateException('No configuration given.');
if(is_scalar($config))
$config = array((is_numeric($config) ? 'userId' : 'screenName') => $config);
$defaults = array(
'screenName' => null,
'userId' => null,
'tweetCount' => 5,
'header' => true,
'avatars' => true,
'retweets' => true,
'replies' => true,
'intents' => true
);
$this->config = $config + $defaults;
if(!$this->config['userId'] && !$this->config['screenName'])
throw new InvalidStateException('No screenName/userId specified.');
}
/**
* Render with predefined config.
* @param array $config Config overrides
* @return void
*/
public function render(array $config = null){
if($config !== null)
$this->config = $config + $this->config;
$this->doRender();
}
/**
* Render full control (header, avatars, retweets, replies, intents)
* @param array $config Config overrides
* @return void
*/
public function renderFull(array $config = null){
$overrides = array(
'header' => true,
'avatars' => true,
'retweets' => true,
'replies' => true,
'intents' => true
);
$this->config = $overrides + (array) $this->config;
if($config !== null)
$this->config = $config + $this->config;
$this->doRender();
}
/**
* Render medium control (avatars, retweets, replies; no header, no intents)
* @param array $config Config overrides
* @return void
*/
public function renderMedium(array $config = null){
$overrides = array(
'header' => false,
'avatars' => true,
'retweets' => true,
'replies' => true,
'intents' => false
);
$this->config = $overrides + $this->config;
if($config !== null)
$this->config = $config + $this->config;
$this->doRender();
}
/**
* Render minimal control (replies, retweets; no header, no avatars, no intents)
* @param array $config Config overrides
* @return void
*/
public function renderMinimal(array $config = null){
$overrides = array(
'header' => false,
'avatars' => false,
'retweets' => true,
'replies' => true,
'intents' => false
);
$this->config = $overrides + $this->config;
if($config !== null)
$this->config = $config + $this->config;
$this->doRender();
}
/**
* Get the tweet loader.
* @return ILoader;
*/
public function getLoader(){
if($this->loader === null)
$this->loader = new StandardLoader;
return $this->loader;
}
/**
* Set the tweet loader.
* @param ILoader $loader
* @return void
*/
public function setLoader(ILoader $loader){
$this->loader = $loader;
}
/**
* Get the tweet formatter.
* @return IFormatter
*/
public function getFormatter(){
if($this->formatter === null)
$this->formatter = new StandardFormatter;
return $this->formatter;
}
/**
* Set the tweet formatter.
* @param IFormatter $formatter
* @return void
*/
public function setFormatter(IFormatter $formatter){
$this->formatter = $formatter;
}
/**
* Get the template file name.
* @return string
*/
public function getTemplateFile(){
return dirname($this->reflection->fileName)
. static::$templateDirectory
. $this->templateFile;
}
/**
* Set the template file name, relative to the template directory.
* @param string $filename
* @return TwitterControl fluent interface
*/
public function setTemplateFile($filename){
$this->templateFile = $filename;
return $this;
}
/**
* Render the component.
* @return void
*/
protected function doRender(){
$this->template->setFile($this->getTemplateFile());
$this->template->config = (object) $this->config;
ob_start();
try{
$this->template->tweets = $this->getLoader()->getTweets($this->config);
$this->template->render();
ob_end_flush();
} catch(TwitterException $e){
if(Debugger::$productionMode){
Debugger::log($e, Debugger::WARNING);
ob_end_clean();
} else{
throw $e;
ob_end_flush();
}
}
}
/**
* Custom helpers registration.
* @param string $class
* @return Nette\Templating\FileTemplate
*/
protected function createTemplate($class = NULL){
$template = parent::createTemplate($class);
$formatter = $this->getFormatter();
$template->registerHelper('avatar', function($url){
return str_replace('_normal.', '_reasonably_small.', $url);
});
$template->registerHelper('tweetFormat', callback($formatter, 'formatTweet'));
$template->registerHelper('timeFormat', callback($formatter, 'formatTime'));
$template->registerHelper('userLink', callback($formatter, 'formatUserUrl'));
$template->registerHelper('intentLink', callback($formatter, 'formatIntentUrl'));
return $template;
}
}
class TwitterException extends \Exception {}