-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTheme.php
140 lines (124 loc) · 3.24 KB
/
Theme.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* The Theme class is a simple container used to store and reference the various
* theme components. It doesn't support automatic dependency injection (manual
* only) because it would be overkill for this project.
*
* @author Justin Tadlock <[email protected]>
* @copyright Copyright (c) 2023-2024, Justin Tadlock
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0-or-later
* @link https://github.com/x3p0-dev/x3p0-ideas
*/
declare(strict_types=1);
namespace X3P0\Ideas;
use WP_Block_Bindings_Registry;
use WP_Block_Patterns_Registry;
use WP_Block_Pattern_Categories_Registry;
use WP_Block_Styles_Registry;
use WP_Block_Type_Registry;
use X3P0\Ideas\Bindings;
use X3P0\Ideas\Contracts\Bootable;
use X3P0\Ideas\Tools\BlockRules;
use X3P0\Ideas\Views\Engine;
class Theme implements Bootable
{
/**
* Stored definitions of single instances.
*
* @since 1.0.0
* @var mixed[]
*/
private array $instances = [];
/**
* Sets up the object state.
*
* @since 1.0.0
*/
public function __construct()
{
$this->registerDefaultBindings();
}
/**
* Boots the component by calling bootable bindings.
*
* @since 1.0.0
*/
#[\Override]
public function boot(): void
{
array_walk(
$this->instances,
fn($binding) => $binding instanceof Bootable && $binding->boot()
);
}
/**
* Registers the default bindings we need to run the theme.
*
* @since 1.0.0
*/
private function registerDefaultBindings(): void
{
$this->instance('bindings', new Bindings\Component(
WP_Block_Bindings_Registry::get_instance(),
[
Bindings\Media::class,
Bindings\Post::class,
Bindings\Site::class,
Bindings\Theme::class
]
));
$this->instance('blocks', new Blocks(
WP_Block_Type_Registry::get_instance(),
new BlockRules(),
new Engine()
));
$this->instance('patterns', new Patterns(
WP_Block_Patterns_Registry::get_instance(),
WP_Block_Pattern_Categories_Registry::get_instance(),
WP_Block_Type_Registry::get_instance()
));
$this->instance('styles', new Styles(
WP_Block_Styles_Registry::get_instance()
));
$this->instance('editor', new Editor());
$this->instance('embeds', new Embeds());
$this->instance('frontend', new Frontend());
$this->instance('media', new Media());
$this->instance('parts', new Parts());
$this->instance('templates', new Templates());
$this->instance('variations', new Variations());
}
/**
* Registers a single instance of a binding. Note that this is marked as
* a `private` method for now since this class is meant to be used
* internally only.
*
* @since 1.0.0
* @param mixed $instance
* @todo Add `mixed` param type declaration with PHP 8-only support.
*/
private function instance(string $abstract, $instance): void
{
$this->instances[ $abstract ] = $instance;
}
/**
* Returns a binding or `null`.
*
* @since 1.0.0
* @return mixed
* @todo Add `mixed` return type declaration with PHP 8-only support.
*/
public function get(string $abstract)
{
return $this->has($abstract) ? $this->instances[ $abstract ] : null;
}
/**
* Checks if a binding exists.
*
* @since 1.0.0
*/
public function has(string $abstract): bool
{
return isset($this->instances[ $abstract ]);
}
}