PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.1
JetFormBuilder — Dynamic Blocks Form Builder v2.1.1
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
246 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
44 public function rep_allow_rewrite() {
45 return true;
46 }
47
48 public function rep_install_item_soft( $item_trait ) {
49 try {
50 $this->rep_item_check( $item_trait );
51 $this->rep_throw_if_cant_rewrite( $item_trait );
52 $this->rep_run_install_flow( $item_trait->rep_item_id(), $item_trait );
53
54 } catch ( Repository_Exception $exception ) {
55 $this->_rep_save_fail( $exception );
56 }
57 }
58
59 /**
60 * @param $item_trait
61 * @param $item_key
62 */
63 public function rep_run_install_flow( string $item_key, $item_trait ) {
64 $this->rep_before_install_item( $item_trait );
65
66 $this->__repository[ $item_key ] = $item_trait;
67
68 $this->rep_after_install_item( $item_trait );
69 }
70
71 /**
72 * @param $item_trait
73 *
74 * @return $this
75 * @throws Repository_Exception
76 */
77 public function rep_install_item( $item_trait ) {
78 $this->rep_item_check( $item_trait );
79 $this->rep_throw_if_cant_rewrite( $item_trait );
80
81 try {
82 $this->rep_run_install_flow( $item_trait->rep_item_id(), $item_trait );
83 } catch ( Repository_Exception $exception ) {
84 $this->_rep_save_fail( $exception );
85
86 switch ( $exception->getCode() ) {
87
88 case $this->_rep_abort_all_code():
89 $item_class = get_class( $item_trait );
90
91 throw ( new Repository_Exception(
92 "The installation was aborted on the item: {$item_class}",
93 $exception->getMessage(),
94 ...$exception->get_additional()
95 ) )->set_code( $exception->getCode() );
96
97 case $this->_rep_abort_and_die_code():
98 $item_class = get_class( $item_trait );
99
100 throw ( new Repository_Exception(
101 "The installation was aborted and died on the item: {$item_class}",
102 $exception->getMessage(),
103 ...$exception->get_additional()
104 ) )->set_code( $exception->getCode() );
105 }
106 }
107
108 return $this;
109 }
110
111 public function rep_before_install_item( $item ) {
112 }
113
114 public function rep_after_install_item( $item ) {
115 }
116
117 public function rep_get_items(): array {
118 return $this->__repository;
119 }
120
121 public function rep_get_values(): array {
122 return array_values( $this->__repository );
123 }
124
125 public function rep_get_keys(): array {
126 return array_keys( $this->__repository );
127 }
128
129 private function _rep_get_item( $slug ) {
130 return $this->__repository[ $slug ];
131 }
132
133 /**
134 * @param $class_or_slug
135 *
136 * @return mixed
137 * @throws Repository_Exception
138 */
139 public function rep_get_item( $class_or_slug ) {
140 // if we got normal slug
141 if ( ! class_exists( $class_or_slug ) ) {
142 $this->rep_throw_if_undefined( $class_or_slug );
143
144 return $this->_rep_get_item( $class_or_slug );
145
146 } elseif ( $this->rep_isset_item( $class_or_slug ) ) {
147
148 // if we got class string, which used as key
149 return $this->_rep_get_item( $class_or_slug );
150 }
151
152 foreach ( $this->rep_get_items() as $item ) {
153 if ( is_a( $item, $class_or_slug ) ) {
154 return $item;
155 }
156 }
157
158 throw new Repository_Exception( "Undefined item: {$class_or_slug}" );
159 }
160
161 public function rep_get_item_or_die( $slug ) {
162 if ( ! $this->rep_isset_item( $slug ) ) {
163 _doing_it_wrong( __METHOD__, "Undefined item: {$slug}", '2.0.0' );
164 }
165
166 return $this->_rep_get_item( $slug );
167 }
168
169 /**
170 * @param $slug
171 *
172 * @return mixed
173 * @throws Repository_Exception
174 */
175 public function rep_clone_item( $slug ) {
176 $this->rep_throw_if_undefined( $slug );
177
178 return clone $this->_rep_get_item( $slug );
179 }
180
181 public function rep_clone_item_or_die( $slug ) {
182 if ( ! $this->rep_isset_item( $slug ) ) {
183 _doing_it_wrong( __METHOD__, "Undefined item: {$slug}", '2.0.0' );
184 }
185
186 return clone $this->_rep_get_item( $slug );
187 }
188
189 /**
190 * @param $item
191 *
192 * @param string $slug
193 *
194 * @throws Repository_Exception
195 */
196 public function rep_item_check( $item, $slug = '' ) {
197 if ( ! method_exists( $item, 'rep_item_id' ) ) {
198 $class_name = get_class( $item );
199 throw new Repository_Exception( "Instance {$class_name} does not use the Repository_Item_Trait." );
200 }
201 }
202
203 public function rep_isset_item( $slug ) {
204 return isset( $this->__repository[ $slug ] );
205 }
206
207 /**
208 * @param $slug
209 *
210 * @return void
211 * @throws Repository_Exception
212 */
213 public function rep_throw_if_undefined( $slug ) {
214 if ( ! $this->rep_isset_item( $slug ) ) {
215 throw new Repository_Exception( "Undefined item: {$slug}" );
216 }
217 }
218
219 /**
220 * @param $item_trait
221 *
222 * @throws Repository_Exception
223 */
224 public function rep_throw_if_cant_rewrite( $item_trait ) {
225 if ( $this->rep_isset_item( $item_trait->rep_item_id() ) && ! $this->rep_allow_rewrite() ) {
226 throw new Repository_Exception(
227 "You can't rewrite instance: " . $item_trait->rep_item_id()
228 );
229 }
230 }
231
232 public function _rep_save_fail( Repository_Exception $exception ) {
233 if ( $this->rep_save_fails() ) {
234 $this->__failed_installs[] = $exception->getMessage();
235 }
236 }
237
238 /**
239 * @return array
240 */
241 public function _rep_get_fails(): array {
242 return $this->__failed_installs;
243 }
244
245 }
246