1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the highcharts-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2017 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\HighchartsBundle\Twig\Extension; |
13
|
|
|
|
14
|
|
|
use Twig\Extension\AbstractExtension; |
15
|
|
|
use Twig\TwigFunction; |
16
|
|
|
use WBW\Bundle\HighchartsBundle\API\HighchartsChart; |
17
|
|
|
use WBW\Bundle\HighchartsBundle\API\HighchartsOptions; |
18
|
|
|
use WBW\Bundle\HighchartsBundle\Wrapper\HighchartsWrapper; |
19
|
|
|
use WBW\Library\Core\Exception\IO\FileNotFoundException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Highcharts Twig extension. |
23
|
|
|
* |
24
|
|
|
* @author webeweb <https://github.com/webeweb/> |
25
|
|
|
* @package WBW\Bundle\HighchartsBundle\Twig\Extension |
26
|
|
|
* @final |
27
|
|
|
*/ |
28
|
|
|
final class HighchartsTwigExtension extends AbstractExtension { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Service name. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
const SERVICE_NAME = "webeweb.bundle.highchartsbundle.twig.extension.highcharts"; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Directory. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $directory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Environment. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $environment; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Wrapper. |
53
|
|
|
* |
54
|
|
|
* @var HightchartsWrapper |
55
|
|
|
*/ |
56
|
|
|
private $wrapper; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructor. |
60
|
|
|
* |
61
|
|
|
* @param string $directory The directory. |
62
|
|
|
* @param string $environment The environment. |
63
|
|
|
*/ |
64
|
|
|
public function __construct($directory, $environment) { |
65
|
|
|
$this->directory = $directory; |
66
|
|
|
$this->environment = $environment; |
67
|
|
|
$this->wrapper = new HighchartsWrapper(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the Twig functions. |
72
|
|
|
* |
73
|
|
|
* @return TwigFunction[] Returns the Twig functions. |
74
|
|
|
*/ |
75
|
|
|
public function getFunctions() { |
76
|
|
|
return [ |
77
|
|
|
new TwigFunction('highchartsChart', [$this, 'highchartsChartFunction'], ['is_safe' => ['html']]), |
78
|
|
|
new TwigFunction('highchartsScript', [$this, 'highchartsScriptFunction'], ['is_safe' => ['html']]), |
79
|
|
|
new TwigFunction('highchartsSetOptions', [$this, 'highchartsSetOptionsFunction'], ['is_safe' => ['html']]), |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the resources directory. |
85
|
|
|
* |
86
|
|
|
* @return string Returns the resources directory. |
87
|
|
|
*/ |
88
|
|
|
private function getResourcesDirectory() { |
89
|
|
|
return $this->directory . "/Resources"; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Displays an Highcharts.chart(). |
94
|
|
|
* |
95
|
|
|
* @param string $selector The selector. |
96
|
|
|
* @param HighchartsChart $chart The chart. |
97
|
|
|
* @return string Returns the Highcharts.chart(). |
98
|
|
|
*/ |
99
|
|
|
public function highchartsChartFunction($selector, HighchartsChart $chart) { |
100
|
|
|
|
101
|
|
|
// Initialize the output. |
102
|
|
|
$output = []; |
103
|
|
|
$output[] = "<script type=\"text/javascript\">\n"; |
104
|
|
|
$output[] = "Highcharts.chart('" . $selector . "', "; |
105
|
|
|
if ($this->isDevEnvironment()) { |
106
|
|
|
$output[] = $this->wrapper->unwrap(json_encode($chart, JSON_PRETTY_PRINT)); |
107
|
|
|
} else { |
108
|
|
|
$output[] = $this->wrapper->unwrap(json_encode($chart)); |
109
|
|
|
} |
110
|
|
|
$output[] = ");\n"; |
111
|
|
|
$output[] = "</script>"; |
112
|
|
|
|
113
|
|
|
// Return the output. |
114
|
|
|
return implode("", $output); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Displays an Highcharts script. |
119
|
|
|
* |
120
|
|
|
* @param string $script The scriptname. |
121
|
|
|
* @param string $subdirectory The sub directory. |
122
|
|
|
* @return string Returns the Highcharts script. |
123
|
|
|
* @throws FileNotFoundException Throws a file not found exception if the script is not found. |
124
|
|
|
*/ |
125
|
|
|
public function highchartsScriptFunction($script, $subdirectory = "code") { |
126
|
|
|
|
127
|
|
|
// Initialize the filename. |
128
|
|
|
$filename = implode("/", ["highcharts-5.0.14", $subdirectory, $script . ".js"]); |
129
|
|
|
|
130
|
|
|
// Initialize and check the filepath. |
131
|
|
|
$filepath = $this->getResourcesDirectory() . "/public/" . $filename; |
132
|
|
|
if (!file_exists($filepath)) { |
133
|
|
|
throw new FileNotFoundException($filename); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// Return the output. |
137
|
|
|
return "<script src=\"/bundles/highcharts/" . $filename . "\" type=\"text/javascript\"></script>"; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Displays an Highcharts.setOptions() |
142
|
|
|
* |
143
|
|
|
* @param HighchartsOptions $options The options. |
144
|
|
|
* @return string Returns the Highcharts.setOptions(). |
145
|
|
|
*/ |
146
|
|
|
public function highchartsSetOptionsFunction(HighchartsOptions $options) { |
147
|
|
|
|
148
|
|
|
// Initialize the output. |
149
|
|
|
$output = []; |
150
|
|
|
$output[] = "<script type=\"text/javascript\">\n"; |
151
|
|
|
$output[] = "Highcharts.setOptions("; |
152
|
|
|
if ($this->isDevEnvironment()) { |
153
|
|
|
$output[] = $this->wrapper->unwrap(json_encode($options, JSON_PRETTY_PRINT)); |
154
|
|
|
} else { |
155
|
|
|
$output[] = $this->wrapper->unwrap(json_encode($options)); |
156
|
|
|
} |
157
|
|
|
$output[] = ");\n"; |
158
|
|
|
$output[] = "</script>"; |
159
|
|
|
|
160
|
|
|
// Return the output. |
161
|
|
|
return implode("", $output); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Determines if the environement is dev. |
166
|
|
|
* |
167
|
|
|
* @return boolean Returns true in case of success, false otherwise. |
168
|
|
|
*/ |
169
|
|
|
private function isDevEnvironment() { |
170
|
|
|
return $this->environment === "dev"; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..