-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.php
174 lines (145 loc) · 4.75 KB
/
Extension.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
<?php
// Shariff extension for Bolt
namespace Bolt\Extension\DanielKulbe\Shariff;
use Bolt\Application;
use Bolt\BaseExtension;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
class Extension extends BaseExtension
{
/**
* Extension name
*
* @var string
*/
const NAME = 'Shariff';
/**
* Backend
*
* @var Backend
*/
protected $shariff;
/**
* Available themes
*
* @var array
*/
protected static $THEMES = array(
'standard', 'white', 'custom'
);
/**
* Available counter backend implementations
*
* @var array
*/
protected static $COUNTER = array(
'AddThis', 'Facebook', 'Flattr', 'GooglePlus', 'LinkedIn', 'Pinterest', 'Reddit', 'StumbleUpon', 'Xing'
);
/**
* Add Twig function, initialize Shariff backend
*
* @return void
*/
public function initialize()
{
// Register Shariff Backend
$counters = Extension::getAvailableCounter();
$services = array_map(function ($service) use ($counters) {
if ( in_array($service['name'], $counters) ) return $service['name'];
return false;
}, $this->config['services']);
/** @var \Guzzle\Service\Client $client */
$client = $this->app['guzzle.client'];
if(isset($this->config['server']['guzzle'])) $client->configureDefaults($this->config['server']['guzzle']);
// Backwards compatibility
$domains = $this->config['server']['domains'];
if (isset($this->config['server']['domain'])) {
array_push($domains, $this->config['server']['domain']);
}
$serviceFactory = new ServiceFactory($client);
$this->shariff = new BackendManager(
md5(json_encode($this->config['server'])),
$this->app['cache'],
$client,
$domains,
$serviceFactory->getServicesByName($services, $this->config['server'])
);
$this->shariff->setLogger($this->app['logger.system']);
// Register service route
$this->app->match('/shariff/counter', array($this, 'counter'));
// Add Extension template path, register Twig function "shariff", add script and stylesheet
if ($this->app['config']->getWhichEnd() == 'frontend') {
$this->app['twig.loader.filesystem']->addPath(__DIR__ . '/assets', 'Shariff');
$this->addTwigFunction('shariff', 'shariff');
if ($this->config['fontawesome']) {
$this->addCss('assets/font-awesome.min.css');
}
if ($this->config['theme'] != 'custom') {
$this->addCss('assets/shariff.min.css');
}
$this->addJavascript('assets/shariff.min.js', true);
}
}
/**
* Get the extension's human readable name
*
* @return string
*/
public function getName()
{
return Extension::NAME;
}
/**
* Set the defaults for configuration parameters
*
* @return array
*/
protected function getDefaultConfig()
{
return array(
'count' => false,
'fontawesome' => true,
'theme' => 'standard',
'orientation' => 'horizontal',
'client' => array(),
'services' => array(),
'extra' => array(),
'server' => array(
'domains' => array($this->app['paths']['hostname'])
),
);
}
/**
* Receive the supported backend classes
*
* @return array
*/
public static function getAvailableCounter ()
{
return Extension::$COUNTER;
}
/**
* Shariff "shariff" Twig function handler
*
* @param $config array custom instance client setup
* @return Twig_Markup Rendered html
*/
public function shariff (array $config = array())
{
$twigValues = array(
'count' => $this->config['count'] == true ? true : false,
'theme' => in_array($this->config['theme'], Extension::$THEMES) ? $this->config['theme'] : 'standard',
'orientation' => $this->config['orientation'] == 'vertical' ? 'vertical' : 'horizontal',
'config' => array_merge($this->config['client'], $config, array('backend' => $this->config['count'] == true ? '/shariff/counter' : null)),
'services' => $this->config['services'],
'extra' => $this->config['extra']
);
$str = $this->app['render']->render('@Shariff/shariff.twig', $twigValues);
return new \Twig_Markup($str, 'UTF-8');
}
public function counter (Application $app, HttpRequest $request)
{
return $app->json(
$this->shariff->get($request->get('url'))
);
}
}