|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\CoreBundle\Builder; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
7
|
|
|
use Victoire\Bundle\CoreBundle\Entity\View; |
|
8
|
|
|
use Victoire\Bundle\WidgetBundle\Renderer\WidgetRenderer; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* View CSS Builder |
|
12
|
|
|
* ref: victoire_core.view_css_builder. |
|
13
|
|
|
*/ |
|
14
|
|
|
class ViewCssBuilder |
|
15
|
|
|
{ |
|
16
|
|
|
protected $container; |
|
17
|
|
|
protected $victoireTwigResponsive; |
|
18
|
|
|
private $widgetRenderer; |
|
|
|
|
|
|
19
|
|
|
private $webDir; |
|
20
|
|
|
private $viewCssDir; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Construct. |
|
24
|
|
|
* |
|
25
|
|
|
* @param EngineInterface $templating |
|
|
|
|
|
|
26
|
|
|
* @param $victoireTwigResponsive |
|
27
|
|
|
* @param $kernelRootDir |
|
28
|
|
|
* |
|
29
|
|
|
* @internal param WidgetRenderer $widgetRenderer |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Container $container, $victoireTwigResponsive, $kernelRootDir) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->webDir = '/view-css'; |
|
34
|
|
|
$this->viewCssDir = $kernelRootDir.'/../web'.$this->webDir; |
|
35
|
|
|
$this->container = $container; |
|
36
|
|
|
$this->victoireTwigResponsive = $victoireTwigResponsive; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Update css by removing old file and writing new file. |
|
41
|
|
|
* |
|
42
|
|
|
* @param $oldHash |
|
43
|
|
|
* @param View $view |
|
44
|
|
|
* @param array $widgets |
|
45
|
|
|
*/ |
|
46
|
|
|
public function updateViewCss($oldHash, View $view, array $widgets) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->removeCssFile($oldHash); |
|
49
|
|
|
$this->generateViewCss($view, $widgets); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Construct css file and write it. |
|
54
|
|
|
* |
|
55
|
|
|
* @param View $view |
|
56
|
|
|
* @param array $widgets |
|
57
|
|
|
*/ |
|
58
|
|
|
public function generateViewCss(View $view, array $widgets) |
|
59
|
|
|
{ |
|
60
|
|
|
$css = ''; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($widgets as $widget) { |
|
63
|
|
|
$style = $this->container->get('templating')->render( |
|
64
|
|
|
'VictoireCoreBundle:Widget:style/style.html.twig', |
|
65
|
|
|
[ |
|
66
|
|
|
'widget' => $widget, |
|
67
|
|
|
'victoire_twig_responsive' => $this->victoireTwigResponsive, |
|
68
|
|
|
] |
|
69
|
|
|
); |
|
70
|
|
|
$css .= trim($style); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($css !== '') { |
|
74
|
|
|
$this->writeCssFile($view, $css); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get css path for a View. |
|
80
|
|
|
* |
|
81
|
|
|
* @param View $view |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getViewCssFile(View $view) |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getViewCssFileFromHash($view->getCssHash()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get href for link markup for a View. |
|
92
|
|
|
* |
|
93
|
|
|
* @param View $view |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getHref(View $view) |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->webDir.'/'.$view->getCssHash().'.css'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Remove css file. |
|
104
|
|
|
* |
|
105
|
|
|
* @param $hash |
|
106
|
|
|
*/ |
|
107
|
|
|
public function removeCssFile($hash) |
|
108
|
|
|
{ |
|
109
|
|
|
$file = $this->getViewCssFileFromHash($hash); |
|
110
|
|
|
if (file_exists($file)) { |
|
111
|
|
|
unlink($file); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Remove all views css files. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function clearViewCssFolder() |
|
119
|
|
|
{ |
|
120
|
|
|
if (!is_dir($this->viewCssDir)) { |
|
121
|
|
|
return; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$files = glob($this->viewCssDir.DIRECTORY_SEPARATOR.'*'); |
|
125
|
|
|
foreach ($files as $file) { |
|
126
|
|
|
if (is_file($file)) { |
|
127
|
|
|
unlink($file); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Tell if css file exists for a given View. |
|
134
|
|
|
* |
|
135
|
|
|
* @param View $view |
|
136
|
|
|
* |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
|
|
public function cssFileExists(View $view) |
|
140
|
|
|
{ |
|
141
|
|
|
$file = $this->getViewCssFileFromHash($view->getCssHash()); |
|
142
|
|
|
|
|
143
|
|
|
return file_exists($file); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Construct and return css path from a hash. |
|
148
|
|
|
* |
|
149
|
|
|
* @param $hash |
|
150
|
|
|
* |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
private function getViewCssFileFromHash($hash) |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->viewCssDir.DIRECTORY_SEPARATOR.$hash.'.css'; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Write css file. |
|
160
|
|
|
* |
|
161
|
|
|
* @param $view |
|
162
|
|
|
* @param $css |
|
163
|
|
|
*/ |
|
164
|
|
|
private function writeCssFile(View $view, $css) |
|
165
|
|
|
{ |
|
166
|
|
|
$oldmask = umask(0); |
|
167
|
|
|
if (!is_dir($this->viewCssDir)) { |
|
168
|
|
|
mkdir($this->viewCssDir, 0777, true); |
|
169
|
|
|
} |
|
170
|
|
|
$file = $this->getViewCssFile($view); |
|
171
|
|
|
file_put_contents($file, $css); |
|
172
|
|
|
umask($oldmask); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.