-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Uuids.php
122 lines (108 loc) · 2.83 KB
/
Uuids.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
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace Kirby\Uuid;
use Closure;
use Kirby\Cache\Cache;
use Kirby\Cms\App;
use Kirby\Exception\LogicException;
/**
* Helper methods that deal with the entirety of UUIDs in the system
* @since 3.8.0
*
* @package Kirby Uuid
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
class Uuids
{
/**
* Returns the instance for the lookup cache
*/
public static function cache(): Cache
{
return App::instance()->cache('uuid');
}
/**
* Runs the callback for each identifiable model of type
*
* @param string $type which models to include (`all`|`page`|`file`|`block`|`struct`)
*/
public static function each(Closure $callback, string $type = 'all'): void
{
if ($type === 'all' || $type === 'page' || $type === 'file') {
foreach (PageUuid::index() as $page) {
if ($type === 'all' || $type === 'page') {
$callback($page);
}
if ($type === 'all' || $type === 'file') {
foreach ($page->files() as $file) {
$callback($file);
}
}
}
}
if ($type === 'all' || $type === 'file') {
foreach (SiteUuid::index() as $site) {
foreach ($site->files() as $file) {
$callback($file);
}
}
foreach (UserUuid::index() as $user) {
foreach ($user->files() as $file) {
$callback($file);
}
}
}
// TODO: activate for uuid-block-structure-support
// if ($type === 'all' || $type === 'block') {
// foreach (BlockUuid::index() as $blocks) {
// foreach ($blocks as $block) {
// $callback($block);
// }
// }
// }
// if ($type === 'all' || $type === 'struct') {
// foreach (StructureUuid::index() as $structure) {
// foreach ($structure as $entry) {
// $callback($entry);
// }
// }
// }
}
public static function enabled(): bool
{
return App::instance()->option('content.uuid') !== false;
}
/**
* Generates UUID for all identifiable models of type
*
* @param string $type which models to include (`all`|`page`|`file`|`block`|`struct`)
*/
public static function generate(string $type = 'all'): void
{
if (static::enabled() === false) {
throw new LogicException('UUIDs have been disabled via the `content.uuid` config option.');
}
static::each(
fn (Identifiable $model) => Uuid::for($model)->id(),
$type
);
}
/**
* Populates cache with UUIDs for all identifiable models
* that need to be cached (not site and users)
*
* @param string $type which models to include (`all`|`page`|`file`|`block`|`struct`)
*/
public static function populate(string $type = 'all'): void
{
if (static::enabled() === false) {
throw new LogicException('UUIDs have been disabled via the `content.uuid` config option.');
}
static::each(
fn (Identifiable $model) => Uuid::for($model)->populate(),
$type
);
}
}