PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.5
JetFormBuilder — Dynamic Blocks Form Builder v2.1.5
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-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
250 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 we got normal slug
145 if ( ! class_exists( $class_or_slug ) ) {
146 $this->rep_throw_if_undefined( $class_or_slug );
147
148 return $this->_rep_get_item( $class_or_slug );
149
150 } elseif ( $this->rep_isset_item( $class_or_slug ) ) {
151
152 // if we got class string, which used as key
153 return $this->_rep_get_item( $class_or_slug );
154 }
155
156 foreach ( $this->rep_get_items() as $item ) {
157 if ( is_a( $item, $class_or_slug ) ) {
158 return $item;
159 }
160 }
161
162 throw new Repository_Exception( "Undefined item: {$class_or_slug}" );
163 }
164
165 public function rep_get_item_or_die( $slug ) {
166 if ( ! $this->rep_isset_item( $slug ) ) {
167 _doing_it_wrong( __METHOD__, "Undefined item: {$slug}", '2.0.0' );
168 }
169
170 return $this->_rep_get_item( $slug );
171 }
172
173 /**
174 * @param $slug
175 *
176 * @return mixed
177 * @throws Repository_Exception
178 */
179 public function rep_clone_item( $slug ) {
180 $this->rep_throw_if_undefined( $slug );
181
182 return clone $this->_rep_get_item( $slug );
183 }
184
185 public function rep_clone_item_or_die( $slug ) {
186 if ( ! $this->rep_isset_item( $slug ) ) {
187 _doing_it_wrong( __METHOD__, "Undefined item: {$slug}", '2.0.0' );
188 }
189
190 return clone $this->_rep_get_item( $slug );
191 }
192
193 /**
194 * @param $item
195 *
196 * @param string $slug
197 *
198 * @throws Repository_Exception
199 */
200 public function rep_item_check( $item, $slug = '' ) {
201 if ( ! method_exists( $item, 'rep_item_id' ) ) {
202 $class_name = get_class( $item );
203 throw new Repository_Exception( "Instance {$class_name} does not use the Repository_Item_Trait." );
204 }
205 }
206
207 public function rep_isset_item( $slug ) {
208 return isset( $this->__repository[ $slug ] );
209 }
210
211 /**
212 * @param $slug
213 *
214 * @return void
215 * @throws Repository_Exception
216 */
217 public function rep_throw_if_undefined( $slug ) {
218 if ( ! $this->rep_isset_item( $slug ) ) {
219 throw new Repository_Exception( "Undefined item: {$slug}" );
220 }
221 }
222
223 /**
224 * @param $item_trait
225 *
226 * @throws Repository_Exception
227 */
228 public function rep_throw_if_cant_rewrite( $item_trait ) {
229 if ( $this->rep_isset_item( $item_trait->rep_item_id() ) && ! $this->rep_allow_rewrite() ) {
230 throw new Repository_Exception(
231 "You can't rewrite instance: " . $item_trait->rep_item_id()
232 );
233 }
234 }
235
236 public function _rep_save_fail( Repository_Exception $exception ) {
237 if ( $this->rep_save_fails() ) {
238 $this->__failed_installs[] = $exception->getMessage();
239 }
240 }
241
242 /**
243 * @return array
244 */
245 public function _rep_get_fails(): array {
246 return $this->__failed_installs;
247 }
248
249 }
250