-
Notifications
You must be signed in to change notification settings - Fork 7
/
media.class.php
110 lines (87 loc) · 2.64 KB
/
media.class.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
<?php
namespace D4L;
use Kirby\Cms\App as Kirby;
$versionFn = kirby()->component('file::version');
$urlFn = kirby()->component('file::url');
Kirby::plugin('d4l/static-site-generator-media', [
'components' => [
'file::version' => function (Kirby $kirby, $file, array $options = []) use ($versionFn) {
$version = $versionFn($kirby, $file, $options);
if ($mediaPlugin = $kirby->option('d4l.static_site_generator.media_plugin', null)) {
$version = Kirby::plugin($mediaPlugin)->extends()['components']['file::version']($kirby, $file, $options);
}
if (!StaticSiteGeneratorMedia::isActive()) {
return $version;
}
if (!$version->exists()) {
$version->save();
}
$url = $version->url();
if ($urlTransform = $kirby->option('d4l.static_site_generator.media_url_transform', null)) {
$url = $urlTransform($url, $kirby);
}
StaticSiteGeneratorMedia::register($version->root(), $url);
return $version;
},
'file::url' => function (Kirby $kirby, $file, array $options = []) use ($urlFn) {
$url = $urlFn($kirby, $file, $options);
$mediaPlugin = $kirby->option('d4l.static_site_generator.media_plugin', null);
if ($mediaPlugin) {
$url = Kirby::plugin($mediaPlugin)->extends()['components']['file::url']($kirby, $file, $options);
}
if (!StaticSiteGeneratorMedia::isActive()) {
return $url;
}
if ($urlTransform = $kirby->option('d4l.static_site_generator.media_url_transform', null)) {
$url = $urlTransform($url, $kirby);
}
StaticSiteGeneratorMedia::register($file->root(), $url);
return $url;
}
]
]);
class StaticSiteGeneratorMedia
{
protected static $_instance;
protected $_active = false;
protected $_list = [];
public static function getInstance()
{
if (!static::$_instance) {
static::$_instance = new static();
}
return static::$_instance;
}
public static function register($root, $url)
{
$instance = static::getInstance();
$item = [
'root' => $root,
'url' => $url
];
if (in_array($item, $instance->_list)) {
return;
}
$instance->_list[] = $item;
}
public static function getList()
{
$instance = static::getInstance();
return $instance->_list;
}
public static function clearList()
{
$instance = static::getInstance();
$instance->_list = [];
}
public static function isActive()
{
$instance = static::getInstance();
return $instance->_active;
}
public static function setActive(bool $active)
{
$instance = static::getInstance();
$instance->_active = $active;
}
}