PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.0.4
JetFormBuilder — Dynamic Blocks Form Builder v2.0.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 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 4 years ago repository-item-instance-trait.php 4 years ago repository-item-with-class.php 4 years ago repository-pattern-trait.php 4 years ago repository-static-item-it.php 4 years ago
repository-pattern-trait.php
230 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 $slug
135 *
136 * @return mixed
137 * @throws Repository_Exception
138 */
139 public function rep_get_item( $slug ) {
140 $this->rep_throw_if_undefined( $slug );
141
142 return $this->_rep_get_item( $slug );
143 }
144
145 public function rep_get_item_or_die( $slug ) {
146 if ( ! $this->rep_isset_item( $slug ) ) {
147 _doing_it_wrong( __METHOD__, "Undefined item: {$slug}", '2.0.0' );
148 }
149
150 return $this->_rep_get_item( $slug );
151 }
152
153 /**
154 * @param $slug
155 *
156 * @return mixed
157 * @throws Repository_Exception
158 */
159 public function rep_clone_item( $slug ) {
160 $this->rep_throw_if_undefined( $slug );
161
162 return clone $this->_rep_get_item( $slug );
163 }
164
165 public function rep_clone_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 clone $this->_rep_get_item( $slug );
171 }
172
173 /**
174 * @param $item
175 *
176 * @param string $slug
177 *
178 * @throws Repository_Exception
179 */
180 public function rep_item_check( $item, $slug = '' ) {
181 if ( ! method_exists( $item, 'rep_item_id' ) ) {
182 $class_name = get_class( $item );
183 throw new Repository_Exception( "Instance {$class_name} does not use the Repository_Item_Trait." );
184 }
185 }
186
187 public function rep_isset_item( $slug ) {
188 return isset( $this->__repository[ $slug ] );
189 }
190
191 /**
192 * @param $slug
193 *
194 * @return void
195 * @throws Repository_Exception
196 */
197 public function rep_throw_if_undefined( $slug ) {
198 if ( ! $this->rep_isset_item( $slug ) ) {
199 throw new Repository_Exception( "Undefined item: {$slug}" );
200 }
201 }
202
203 /**
204 * @param $item_trait
205 *
206 * @throws Repository_Exception
207 */
208 public function rep_throw_if_cant_rewrite( $item_trait ) {
209 if ( $this->rep_isset_item( $item_trait->rep_item_id() ) && ! $this->rep_allow_rewrite() ) {
210 throw new Repository_Exception(
211 "You can't rewrite instance: " . $item_trait->rep_item_id()
212 );
213 }
214 }
215
216 public function _rep_save_fail( Repository_Exception $exception ) {
217 if ( $this->rep_save_fails() ) {
218 $this->__failed_installs[] = $exception->getMessage();
219 }
220 }
221
222 /**
223 * @return array
224 */
225 public function _rep_get_fails(): array {
226 return $this->__failed_installs;
227 }
228
229 }
230