Test Failed
Push — master ( 74cda5...6f16a3 )
by Brent
03:33
created

TemplatePlugin::file()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Template;
4
5
use Brendt\Image\ResponsiveFactory;
6
use Brendt\Stitcher\Config;
7
use Brendt\Stitcher\Factory\ParserFactory;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Finder\SplFileInfo;
11
12
/**
13
 * This class provides functionality which can be used by template plugins/functions.
14
 */
15
class TemplatePlugin
16
{
17
18
    /**
19
     * This function will read meta configuration from `meta` 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 ParserFactory $factory */
60
        $factory = Config::getDependency('factory.parser');
61
62
        $parser = $factory->getParser($src);
63
        $data = $parser->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
        if ($inline) {
72
            return "<style>{$data}</style>";
73
        }
74
75
        $publicDir = Config::get('directories.public');
76
        $srcParsed = preg_replace('/\.scss|\.sass/', '.css', $src);
77
        $fs = new Filesystem();
78
        $dst = "{$publicDir}/$srcParsed";
79
80
        if ($fs->exists($dst)) {
81
            $fs->remove($dst);
82
        }
83
84
        $fs->dumpFile($dst, $data);
85
86
        return "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$srcParsed}\">";
87
    }
88
89
    /**
90
     * This function will take a source path and an optional inline parameter.
91
     * The JS file will be copied from the source path to the public directory.
92
     * If the `minify` option is set to true in config.yml, the output will be minified.
93
     *
94
     * If the inline parameter is set, the output won't be copied to a public file,
95
     * but instead be outputted to an HTML string which can be included in a template.
96
     *
97
     * @param string $src
98
     * @param bool   $inline
99
     * @param bool   $async
100
     *
101
     * @return string
102
     */
103
    public function js($src, $inline = false, $async = false) {
104
        /** @var ParserFactory $factory */
105
        $factory = Config::getDependency('factory.parser');
106
107
        $parser = $factory->getParser($src);
108
        $data = $parser->parse($src);
109
110
        if (Config::get('minify')) {
111
            $data = \JSMin::minify($data);
112
        }
113
114
        if ($inline) {
115
            return "<script>{$data}</script>";
116
        }
117
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
        return $result;
136
    }
137
138
    /**
139
     * Create a responsive image using brendt\responsive-images.
140
     *
141
     * @param $src
142
     *
143
     * @return array
144
     *
145
     * @see \Brendt\Image\ResponsiveFactory
146
     */
147
    public function image($src) {
148
        /** @var ResponsiveFactory $factory */
149
        $factory = Config::getDependency('factory.image');
150
        $image = $factory->create($src);
151
152
        if (!$image) {
153
            return ['src' => null, 'srcset' => null, 'sizes' => null];
154
        }
155
156
        return [
157
            'src'    => $image->src(),
158
            'srcset' => $image->srcset(),
159
            'sizes'  => $image->sizes(),
160
        ];
161
    }
162
163
    /**
164
     * Create a public file from the src directory and return its path.
165
     *
166
     * @param $src
167
     *
168
     * @return null|string
169
     */
170
    public function file($src) {
171
        $src = trim($src, '/');
172
        $srcDir = Config::get('directories.src');
173
        $files = Finder::create()->in($srcDir)->path($src)->getIterator();
174
        $files->rewind();
175
        /** @var SplFileInfo $file */
176
        $file = $files->current();
177
178
        if (!$file) {
179
            return null;
180
        }
181
182
        $fs = new Filesystem();
183
        $publicDir = Config::get('directories.public');
184
        $dst = "{$publicDir}/{$src}";
185
186
        if ($fs->exists($dst)) {
187
            $fs->remove($dst);
188
        }
189
190
        $fs->dumpFile($dst, $file->getContents());
191
192
        return "/{$src}";
193
    }
194
195
}
196