1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace brendt\stitcher\engine; |
4
|
|
|
|
5
|
|
|
use brendt\image\ResponsiveFactory; |
6
|
|
|
use brendt\stitcher\Config; |
7
|
|
|
use brendt\stitcher\factory\ProviderFactory; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class EnginePlugin |
12
|
|
|
* @package brendt\stitcher\engine |
13
|
|
|
* |
14
|
|
|
* This class provides functionality which can be used by template plugins/functions. |
15
|
|
|
*/ |
16
|
|
|
class EnginePlugin { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This function will read meta configuration and output the corresponding meta tags. |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function meta() { |
24
|
|
|
$meta = Config::get('meta'); |
25
|
|
|
|
26
|
|
|
if (!is_array($meta)) { |
27
|
|
|
$meta = [$meta]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$result = []; |
31
|
|
|
|
32
|
|
|
foreach ($meta as $name => $content) { |
33
|
|
|
if (!is_string($content)) { |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$result[] = "<meta name=\"{$name}\" content=\"{$content}\">"; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return implode("\n", $result); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This function will take a source path and an optional inline parameter. |
45
|
|
|
* The CSS file will be copied from the source path to the public directory. |
46
|
|
|
* If the "minify" option is set to true in config.yml, the output will be minified. |
47
|
|
|
* |
48
|
|
|
* If the inline parameter is set, the output won't be copied to a public file, |
49
|
|
|
* but instead be outputted to an HTML string which can be included in a template. |
50
|
|
|
* |
51
|
|
|
* Files with the .scss and .sass extensions will be compiled to normal CSS files. |
52
|
|
|
* |
53
|
|
|
* @param string $src |
54
|
|
|
* @param bool $inline |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function css($src, $inline = false) { |
59
|
|
|
/** @var ProviderFactory $factory */ |
60
|
|
|
$factory = Config::getDependency('factory.provider'); |
61
|
|
|
|
62
|
|
|
$provider = $factory->getProvider($src); |
63
|
|
|
$data = $provider->parse($src); |
64
|
|
|
|
65
|
|
|
if (Config::get('minify')) { |
66
|
|
|
/** @var \CSSmin $minifier */ |
67
|
|
|
$minifier = Config::getDependency('engine.minify.css'); |
68
|
|
|
$data = $minifier->run($data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
if ($inline) { |
|
|
|
|
72
|
|
|
$result = "<style>{$data}</style>"; |
73
|
|
|
} else { |
74
|
|
|
$publicDir = Config::get('directories.public'); |
75
|
|
|
$srcParsed = preg_replace('/\.scss|\.sass/', '.css', $src); |
76
|
|
|
$fs = new Filesystem(); |
77
|
|
|
$dst = "{$publicDir}/$srcParsed"; |
78
|
|
|
|
79
|
|
|
if ($fs->exists($dst)) { |
80
|
|
|
$fs->remove($dst); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$fs->dumpFile($dst, $data); |
84
|
|
|
$result = "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$srcParsed}\">"; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $result; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* This function will take a source path and an optional inline parameter. |
92
|
|
|
* The JS file will be copied from the source path to the public directory. |
93
|
|
|
* If the "minify" option is set to true in config.yml, the output will be minified. |
94
|
|
|
* |
95
|
|
|
* If the inline parameter is set, the output won't be copied to a public file, |
96
|
|
|
* but instead be outputted to an HTML string which can be included in a template. |
97
|
|
|
* |
98
|
|
|
* @param string $src |
99
|
|
|
* @param bool $inline |
100
|
|
|
* @param bool $async |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function js($src, $inline = false, $async = false) { |
105
|
|
|
/** @var ProviderFactory $factory */ |
106
|
|
|
$factory = Config::getDependency('factory.provider'); |
107
|
|
|
|
108
|
|
|
$provider = $factory->getProvider($src); |
109
|
|
|
$data = $provider->parse($src); |
110
|
|
|
|
111
|
|
|
if (Config::get('minify')) { |
112
|
|
|
$data = \JSMin::minify($data); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
if ($inline) { |
|
|
|
|
116
|
|
|
$result = "<script>{$data}</script>"; |
117
|
|
|
} else { |
118
|
|
|
$publicDir = Config::get('directories.public'); |
119
|
|
|
$fs = new Filesystem(); |
120
|
|
|
$dst = "{$publicDir}/$src"; |
121
|
|
|
|
122
|
|
|
if ($fs->exists($dst)) { |
123
|
|
|
$fs->remove($dst); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$fs->dumpFile($dst, $data); |
127
|
|
|
$result = "<script src=\"{$src}\""; |
128
|
|
|
|
129
|
|
|
if ($async) { |
130
|
|
|
$result .= ' async'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$result .= "></script>"; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Create a responsive image using brendt\responsive-images. |
141
|
|
|
* |
142
|
|
|
* @param $src |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function image($src) { |
147
|
|
|
/** @var ResponsiveFactory $factory */ |
148
|
|
|
$factory = Config::getDependency('factory.image'); |
149
|
|
|
$image = $factory->create($src); |
150
|
|
|
|
151
|
|
|
if (!$image) { |
152
|
|
|
return ['src' => null, 'srcset' => null, 'sizes' => null]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return [ |
156
|
|
|
'src' => $image->src(), |
157
|
|
|
'srcset' => $image->srcset(), |
158
|
|
|
'sizes' => $image->sizes(), |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.