PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.6
JetFormBuilder — Dynamic Blocks Form Builder v3.6.6
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 134 releases
jetformbuilder / components / repository / repository-pattern-trait.php

repository-pattern-trait.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.6, at components/repository/repository-pattern-trait.php

335 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore Internal.Exception
2
3 namespace JFB_Components\Repository;
4
5 use Jet_Form_Builder\Exceptions\Repository_Exception;
6
7 // If this file is called directly, abort.
8 if ( ! defined( 'WPINC' ) ) {
9 die;
10 }
11
12 trait Repository_Pattern_Trait {
13
14 use Repository_Aborts_Trait;
15
16 // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
17 private $__repository = array();
18 private $__failed_installs = array();
19 private $__rep_installed = array();
20
21 // phpcs:enable PSR2.Classes.PropertyDeclaration.Underscore
22
23 abstract public function rep_instances(): array;
24
25 public function rep_save_fails(): bool {
26 return false;
27 }
28
29 public function rep_install( $instances = array() ) {
30 if ( empty( $instances ) ) {
31 $instances = $this->rep_instances();
32 }
33 try {
34 foreach ( $instances as $instance ) {
35 $this->rep_install_item( $instance );
36 }
37 } catch ( Repository_Exception $exception ) {
38 $this->_rep_save_fail( $exception );
39
40 switch ( $exception->getCode() ) {
41
42 case $this->_rep_abort_and_die_code():
43 _doing_it_wrong(
44 __METHOD__,
45 esc_html( $exception->getMessage() ),
46 '2.0.0'
47 );
48 break;
49 }
50 }
51
52 foreach ( $this->rep_get_items() as $item ) {
53 $this->rep_after_install( $item );
54 }
55 }
56
57 public function rep_allow_rewrite() {
58 return true;
59 }
60
61 public function rep_install_item_soft( $item_trait ) {
62 try {
63 $this->rep_item_check( $item_trait );
64 $this->rep_throw_if_cant_rewrite( $item_trait );
65 $this->rep_run_install_flow( $item_trait->rep_item_id(), $item_trait );
66
67 if ( ! $this->rep_isset_item( $item_trait->rep_item_id() ) ) {
68 return false;
69 }
70
71 $this->rep_after_install( $item_trait );
72
73 } catch ( Repository_Exception $exception ) {
74 $this->_rep_save_fail( $exception );
75
76 return false;
77 }
78
79 return true;
80 }
81
82 /**
83 * @param $item_trait
84 * @param $item_key
85 */
86 public function rep_run_install_flow( string $item_key, $item_trait ) {
87 $this->rep_before_install_item( $item_trait );
88
89 $this->__repository[ $item_key ] = $item_trait;
90 }
91
92 /**
93 * @param $item_trait
94 *
95 * @return $this
96 * @throws Repository_Exception
97 */
98 public function rep_install_item( $item_trait ) {
99 $this->rep_item_check( $item_trait );
100 $this->rep_throw_if_cant_rewrite( $item_trait );
101
102 try {
103 $this->rep_run_install_flow( $item_trait->rep_item_id(), $item_trait );
104 } catch ( Repository_Exception $exception ) {
105 $this->_rep_save_fail( $exception );
106
107 switch ( $exception->getCode() ) {
108
109 case $this->_rep_abort_all_code():
110 $item_class = get_class( $item_trait );
111
112 throw ( new Repository_Exception(
113 "The installation was aborted on the item: {$item_class}",
114 $exception->getMessage(),
115 ...$exception->get_additional()
116 ) )->set_code( $exception->getCode() );
117
118 case $this->_rep_abort_and_die_code():
119 $item_class = get_class( $item_trait );
120
121 throw ( new Repository_Exception(
122 "The installation was aborted and died on the item: {$item_class}",
123 $exception->getMessage(),
124 ...$exception->get_additional()
125 ) )->set_code( $exception->getCode() );
126 }
127 }
128
129 return $this;
130 }
131
132 public function rep_before_install_item( $item ) {
133 }
134
135 public function rep_after_install( $item ) {
136 if ( array_key_exists( $item->rep_item_id(), $this->__rep_installed ) ) {
137 return;
138 }
139 $this->__rep_installed[ $item->rep_item_id() ] = 1;
140
141 $this->rep_after_install_item( $item );
142 }
143
144 public function rep_after_install_item( $item ) {
145 }
146
147 public function rep_get_items(): array {
148 return $this->__repository;
149 }
150
151 /**
152 * @return \Generator
153 * @since 3.1.0
154 */
155 public function rep_generate_items(): \Generator {
156 foreach ( $this->__repository as $item ) {
157 yield $item;
158 }
159 }
160
161 public function rep_get_values(): array {
162 return array_values( $this->__repository );
163 }
164
165 public function rep_get_keys(): array {
166 return array_keys( $this->__repository );
167 }
168
169 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
170 private function _rep_get_item( $slug ) {
171 return $this->__repository[ $slug ];
172 }
173
174 /**
175 * @param $class_or_slug
176 *
177 * @return mixed
178 * @throws Repository_Exception
179 */
180 public function rep_get_item( $class_or_slug ) {
181 if ( is_a( $this, Repository_Dynamic_Items_It::class ) ) {
182 foreach ( $this->rep_get_items() as $current ) {
183 if ( ! is_a( $current, Repository_Item_Dynamic_Id::class ) ) {
184 continue;
185 }
186 $id = $current->create_dynamic_id( $class_or_slug );
187
188 if ( ! $id ) {
189 continue;
190 }
191
192 $current->set_dynamic_id( $id );
193
194 return $current;
195 }
196 }
197
198 // if we got normal slug
199 if ( ! class_exists( $class_or_slug ) ) {
200 $this->rep_throw_if_undefined( $class_or_slug );
201
202 return $this->_rep_get_item( $class_or_slug );
203
204 } elseif ( $this->rep_isset_item( $class_or_slug ) ) {
205
206 // if we got class string, which used as key
207 return $this->_rep_get_item( $class_or_slug );
208 }
209
210 foreach ( $this->rep_get_items() as $item ) {
211 if ( is_a( $item, $class_or_slug ) ) {
212 return $item;
213 }
214 }
215
216 throw new Repository_Exception( "Undefined item: {$class_or_slug}" );
217 }
218
219 public function rep_get_item_or_die( $slug ) {
220 $this->rep_die_if_undefined( $slug );
221
222 return $this->_rep_get_item( $slug );
223 }
224
225 /**
226 * @param $slug
227 *
228 * @return mixed
229 * @throws Repository_Exception
230 */
231 public function rep_clone_item( $slug ) {
232 return clone $this->rep_get_item( $slug );
233 }
234
235 public function rep_clone_item_or_die( $slug ) {
236 $this->rep_die_if_undefined( $slug );
237
238 return clone $this->_rep_get_item( $slug );
239 }
240
241 /**
242 * @param $item
243 *
244 * @param string $slug
245 *
246 * @throws Repository_Exception
247 */
248 // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
249 public function rep_item_check( $item, $slug = '' ) {
250 if ( ! method_exists( $item, 'rep_item_id' ) ) {
251 $class_name = get_class( $item );
252 throw new Repository_Exception( "Instance {$class_name} does not use the Repository_Item_Trait." );
253 }
254 }
255
256 public function rep_isset_item( $slug ) {
257 return isset( $this->__repository[ $slug ] );
258 }
259
260 /**
261 * @param $slug
262 *
263 * @return void
264 * @throws Repository_Exception
265 */
266 public function rep_throw_if_undefined( $slug ) {
267 if ( ! $this->rep_isset_item( $slug ) ) {
268 throw new Repository_Exception( "Undefined item: {$slug}" );
269 }
270 }
271
272 public function rep_die_if_undefined( $slug ) {
273 if ( $this->rep_isset_item( $slug ) ) {
274 return;
275 }
276
277 $name = static::class;
278
279 wp_die(
280 esc_html( "Undefined item: {$slug} in {$name}" ),
281 'Undefined repository item'
282 );
283 }
284
285 /**
286 * @param $item_trait
287 *
288 * @throws Repository_Exception
289 */
290 public function rep_throw_if_cant_rewrite( $item_trait ) {
291 if ( $this->rep_isset_item( $item_trait->rep_item_id() ) && ! $this->rep_allow_rewrite() ) {
292 throw new Repository_Exception(
293 "You can't rewrite instance: " . $item_trait->rep_item_id()
294 );
295 }
296 }
297
298 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
299 public function _rep_save_fail( Repository_Exception $exception ) {
300 if ( $this->rep_save_fails() ) {
301 $this->__failed_installs[] = $exception->getMessage();
302 }
303 }
304
305 /**
306 * @return array
307 */
308 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
309 public function _rep_get_fails(): array {
310 return $this->__failed_installs;
311 }
312
313 /**
314 * @since 3.1.0
315 *
316 * @param string|Repository_Item_Instance_Trait|Repository_Item_Dynamic_Id|Repository_Static_Item_It $item_trait
317 */
318 public function rep_remove( $item_trait ) {
319 if ( is_object( $item_trait ) ) {
320 $item_trait = $item_trait->rep_item_id();
321 }
322
323 if ( ! $this->rep_isset_item( $item_trait ) ) {
324 return;
325 }
326
327 unset( $this->__repository[ $item_trait ] );
328 }
329
330 public function rep_clear() {
331 $this->__repository = array();
332 }
333
334 }
335