PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.8
JetFormBuilder — Dynamic Blocks Form Builder v3.0.8
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 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / classes / repository / repository-pattern-trait.php
jetformbuilder / includes / classes / repository Last commit date
repository-aborts-trait.php 3 years ago repository-dynamic-items-it.php 3 years ago repository-item-dynamic-id.php 3 years ago repository-item-instance-trait.php 3 years ago repository-item-with-class.php 3 years ago repository-pattern-trait.php 3 years ago repository-static-item-it.php 3 years ago
repository-pattern-trait.php
265 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Classes\Repository;
5
6 use Jet_Form_Builder\Exceptions\Repository_Exception;
7
8 trait Repository_Pattern_Trait {
9
10 use Repository_Aborts_Trait;
11
12 private $__repository = array();
13 private $__failed_installs = array();
14
15 abstract public function rep_instances(): array;
16
17 public function rep_save_fails(): bool {
18 return false;
19 }
20
21 public function rep_install( $instances = array() ) {
22 if ( empty( $instances ) ) {
23 $instances = $this->rep_instances();
24 }
25 try {
26 foreach ( $instances as $instance ) {
27 $this->rep_install_item( $instance );
28 }
29 } catch ( Repository_Exception $exception ) {
30 $this->_rep_save_fail( $exception );
31
32 switch ( $exception->getCode() ) {
33
34 case $this->_rep_abort_and_die_code():
35 _doing_it_wrong(
36 __METHOD__,
37 $exception->getMessage(),
38 '2.0.0'
39 );
40 }
41 }
42
43 foreach ( $this->rep_get_items() as $item ) {
44 $this->rep_after_install_item( $item );
45 }
46 }
47
48 public function rep_allow_rewrite() {
49 return true;
50 }
51
52 public function rep_install_item_soft( $item_trait ) {
53 try {
54 $this->rep_item_check( $item_trait );
55 $this->rep_throw_if_cant_rewrite( $item_trait );
56 $this->rep_run_install_flow( $item_trait->rep_item_id(), $item_trait );
57
58 $this->rep_after_install_item( $item_trait );
59
60 } catch ( Repository_Exception $exception ) {
61 $this->_rep_save_fail( $exception );
62 }
63 }
64
65 /**
66 * @param $item_trait
67 * @param $item_key
68 */
69 public function rep_run_install_flow( string $item_key, $item_trait ) {
70 $this->rep_before_install_item( $item_trait );
71
72 $this->__repository[ $item_key ] = $item_trait;
73 }
74
75 /**
76 * @param $item_trait
77 *
78 * @return $this
79 * @throws Repository_Exception
80 */
81 public function rep_install_item( $item_trait ) {
82 $this->rep_item_check( $item_trait );
83 $this->rep_throw_if_cant_rewrite( $item_trait );
84
85 try {
86 $this->rep_run_install_flow( $item_trait->rep_item_id(), $item_trait );
87 } catch ( Repository_Exception $exception ) {
88 $this->_rep_save_fail( $exception );
89
90 switch ( $exception->getCode() ) {
91
92 case $this->_rep_abort_all_code():
93 $item_class = get_class( $item_trait );
94
95 throw ( new Repository_Exception(
96 "The installation was aborted on the item: {$item_class}",
97 $exception->getMessage(),
98 ...$exception->get_additional()
99 ) )->set_code( $exception->getCode() );
100
101 case $this->_rep_abort_and_die_code():
102 $item_class = get_class( $item_trait );
103
104 throw ( new Repository_Exception(
105 "The installation was aborted and died on the item: {$item_class}",
106 $exception->getMessage(),
107 ...$exception->get_additional()
108 ) )->set_code( $exception->getCode() );
109 }
110 }
111
112 return $this;
113 }
114
115 public function rep_before_install_item( $item ) {
116 }
117
118 public function rep_after_install_item( $item ) {
119 }
120
121 public function rep_get_items(): array {
122 return $this->__repository;
123 }
124
125 public function rep_get_values(): array {
126 return array_values( $this->__repository );
127 }
128
129 public function rep_get_keys(): array {
130 return array_keys( $this->__repository );
131 }
132
133 private function _rep_get_item( $slug ) {
134 return $this->__repository[ $slug ];
135 }
136
137 /**
138 * @param $class_or_slug
139 *
140 * @return mixed
141 * @throws Repository_Exception
142 */
143 public function rep_get_item( $class_or_slug ) {
144 if ( is_a( $this, Repository_Dynamic_Items_It::class ) ) {
145 foreach ( $this->rep_get_items() as $current ) {
146 if ( ! is_a( $current, Repository_Item_Dynamic_Id::class ) ) {
147 continue;
148 }
149 $id = $current->create_dynamic_id( $class_or_slug );
150
151 if ( ! $id ) {
152 continue;
153 }
154
155 $current->set_dynamic_id( $id );
156
157 return $current;
158 }
159 }
160
161 // if we got normal slug
162 if ( ! class_exists( $class_or_slug ) ) {
163 $this->rep_throw_if_undefined( $class_or_slug );
164
165 return $this->_rep_get_item( $class_or_slug );
166
167 } elseif ( $this->rep_isset_item( $class_or_slug ) ) {
168
169 // if we got class string, which used as key
170 return $this->_rep_get_item( $class_or_slug );
171 }
172
173 foreach ( $this->rep_get_items() as $item ) {
174 if ( is_a( $item, $class_or_slug ) ) {
175 return $item;
176 }
177 }
178
179 throw new Repository_Exception( "Undefined item: {$class_or_slug}" );
180 }
181
182 public function rep_get_item_or_die( $slug ) {
183 if ( ! $this->rep_isset_item( $slug ) ) {
184 _doing_it_wrong( __METHOD__, "Undefined item: {$slug}", '2.0.0' );
185 }
186
187 return $this->_rep_get_item( $slug );
188 }
189
190 /**
191 * @param $slug
192 *
193 * @return mixed
194 * @throws Repository_Exception
195 */
196 public function rep_clone_item( $slug ) {
197 return clone $this->rep_get_item( $slug );
198 }
199
200 public function rep_clone_item_or_die( $slug ) {
201 if ( ! $this->rep_isset_item( $slug ) ) {
202 _doing_it_wrong( __METHOD__, "Undefined item: {$slug}", '2.0.0' );
203 }
204
205 return clone $this->_rep_get_item( $slug );
206 }
207
208 /**
209 * @param $item
210 *
211 * @param string $slug
212 *
213 * @throws Repository_Exception
214 */
215 public function rep_item_check( $item, $slug = '' ) {
216 if ( ! method_exists( $item, 'rep_item_id' ) ) {
217 $class_name = get_class( $item );
218 throw new Repository_Exception( "Instance {$class_name} does not use the Repository_Item_Trait." );
219 }
220 }
221
222 public function rep_isset_item( $slug ) {
223 return isset( $this->__repository[ $slug ] );
224 }
225
226 /**
227 * @param $slug
228 *
229 * @return void
230 * @throws Repository_Exception
231 */
232 public function rep_throw_if_undefined( $slug ) {
233 if ( ! $this->rep_isset_item( $slug ) ) {
234 throw new Repository_Exception( "Undefined item: {$slug}" );
235 }
236 }
237
238 /**
239 * @param $item_trait
240 *
241 * @throws Repository_Exception
242 */
243 public function rep_throw_if_cant_rewrite( $item_trait ) {
244 if ( $this->rep_isset_item( $item_trait->rep_item_id() ) && ! $this->rep_allow_rewrite() ) {
245 throw new Repository_Exception(
246 "You can't rewrite instance: " . $item_trait->rep_item_id()
247 );
248 }
249 }
250
251 public function _rep_save_fail( Repository_Exception $exception ) {
252 if ( $this->rep_save_fails() ) {
253 $this->__failed_installs[] = $exception->getMessage();
254 }
255 }
256
257 /**
258 * @return array
259 */
260 public function _rep_get_fails(): array {
261 return $this->__failed_installs;
262 }
263
264 }
265