@@ -67,12 +67,12 @@ discard block |
||
67 | 67 | public function heartbeatResponse($response = array(), $data = array()) |
68 | 68 | { |
69 | 69 | // does this heartbeat contain our data ? |
70 | - if (! isset($data[ $this->heartbeat ])) { |
|
70 | + if ( ! isset($data[$this->heartbeat])) { |
|
71 | 71 | return $response; |
72 | 72 | } |
73 | 73 | // check for reg_url_link in the incoming heartbeat data |
74 | - if (! isset($data[ $this->heartbeat ]['e_reg_url_link'])) { |
|
75 | - $response[ $this->heartbeat ] = array( |
|
74 | + if ( ! isset($data[$this->heartbeat]['e_reg_url_link'])) { |
|
75 | + $response[$this->heartbeat] = array( |
|
76 | 76 | 'errors' => ! empty($notices['errors']) |
77 | 77 | ? $notices['errors'] |
78 | 78 | : esc_html__( |
@@ -85,22 +85,22 @@ discard block |
||
85 | 85 | // kk heartbeat has our data |
86 | 86 | $response = $this->initializeThankYouPageAndTransaction($response, $data); |
87 | 87 | // if something went wrong... |
88 | - if (isset($response[ $this->heartbeat ]['errors'])) { |
|
88 | + if (isset($response[$this->heartbeat]['errors'])) { |
|
89 | 89 | return $response; |
90 | 90 | } |
91 | 91 | // grab transient of Transaction's status |
92 | - $txn_status = isset($data[ $this->heartbeat ]['txn_status']) |
|
93 | - ? $data[ $this->heartbeat ]['txn_status'] |
|
92 | + $txn_status = isset($data[$this->heartbeat]['txn_status']) |
|
93 | + ? $data[$this->heartbeat]['txn_status'] |
|
94 | 94 | : null; |
95 | 95 | $response = $this->getTransactionDetails($txn_status, $response, $data); |
96 | 96 | // no payment data yet? |
97 | - if (isset($response[ $this->heartbeat ]['still_waiting'])) { |
|
97 | + if (isset($response[$this->heartbeat]['still_waiting'])) { |
|
98 | 98 | return $response; |
99 | 99 | } |
100 | 100 | // TXN is happening so let's get the payments now |
101 | 101 | // if we've already gotten payments then the heartbeat data will contain the timestamp of the last time we checked |
102 | - $since = isset($data[ $this->heartbeat ]['get_payments_since']) |
|
103 | - ? $data[ $this->heartbeat ]['get_payments_since'] |
|
102 | + $since = isset($data[$this->heartbeat]['get_payments_since']) |
|
103 | + ? $data[$this->heartbeat]['get_payments_since'] |
|
104 | 104 | : 0; |
105 | 105 | return $this->paymentDetails($response, $since); |
106 | 106 | } |
@@ -117,18 +117,18 @@ discard block |
||
117 | 117 | */ |
118 | 118 | private function initializeThankYouPageAndTransaction($response, $data) |
119 | 119 | { |
120 | - require_once EE_MODULES . 'thank_you_page/EED_Thank_You_Page.module.php'; |
|
120 | + require_once EE_MODULES.'thank_you_page/EED_Thank_You_Page.module.php'; |
|
121 | 121 | // set_definitions, instantiate the thank you page class, and get the ball rolling |
122 | 122 | EED_Thank_You_Page::set_definitions(); |
123 | 123 | $this->thank_you_page = EED_Thank_You_Page::instance(); |
124 | - $this->thank_you_page->set_reg_url_link($data[ $this->heartbeat ]['e_reg_url_link']); |
|
124 | + $this->thank_you_page->set_reg_url_link($data[$this->heartbeat]['e_reg_url_link']); |
|
125 | 125 | $this->thank_you_page->init(); |
126 | 126 | // get TXN |
127 | 127 | $transaction = $this->thank_you_page->get_txn(); |
128 | 128 | // no TXN? then get out |
129 | - if (! $transaction instanceof EE_Transaction) { |
|
129 | + if ( ! $transaction instanceof EE_Transaction) { |
|
130 | 130 | $notices = EE_Error::get_notices(); |
131 | - $response[ $this->heartbeat ] = array( |
|
131 | + $response[$this->heartbeat] = array( |
|
132 | 132 | 'errors' => ! empty($notices['errors']) |
133 | 133 | ? $notices['errors'] |
134 | 134 | : sprintf( |
@@ -173,12 +173,12 @@ discard block |
||
173 | 173 | case EEM_Transaction::failed_status_code: |
174 | 174 | default: |
175 | 175 | // keep on waiting... |
176 | - return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
176 | + return $this->updateServerWaitTime($data[$this->heartbeat]); |
|
177 | 177 | } |
178 | 178 | // or is the TXN still failed (never been updated) ??? |
179 | 179 | } elseif ($this->transaction->failed()) { |
180 | 180 | // keep on waiting... |
181 | - return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
181 | + return $this->updateServerWaitTime($data[$this->heartbeat]); |
|
182 | 182 | } |
183 | 183 | return $response; |
184 | 184 | } |
@@ -196,11 +196,11 @@ discard block |
||
196 | 196 | */ |
197 | 197 | private function setTransactionDetails($response, $status_only = false) |
198 | 198 | { |
199 | - if (! $status_only && ! isset($response[ $this->heartbeat ]['transaction_details'])) { |
|
200 | - $response[ $this->heartbeat ]['transaction_details'] = $this->thank_you_page->get_transaction_details(); |
|
199 | + if ( ! $status_only && ! isset($response[$this->heartbeat]['transaction_details'])) { |
|
200 | + $response[$this->heartbeat]['transaction_details'] = $this->thank_you_page->get_transaction_details(); |
|
201 | 201 | } |
202 | - if (! isset($response[ $this->heartbeat ]['txn_status'])) { |
|
203 | - $response[ $this->heartbeat ]['txn_status'] = $this->transaction->status_ID(); |
|
202 | + if ( ! isset($response[$this->heartbeat]['txn_status'])) { |
|
203 | + $response[$this->heartbeat]['txn_status'] = $this->transaction->status_ID(); |
|
204 | 204 | } |
205 | 205 | return $response; |
206 | 206 | } |
@@ -221,19 +221,19 @@ discard block |
||
221 | 221 | // then check for payments |
222 | 222 | $payments = $this->thank_you_page->get_txn_payments($since); |
223 | 223 | // has a payment been processed ? |
224 | - if (! empty($payments) || $this->thank_you_page->isOfflinePaymentMethod()) { |
|
224 | + if ( ! empty($payments) || $this->thank_you_page->isOfflinePaymentMethod()) { |
|
225 | 225 | if ($since) { |
226 | - $response[ $this->heartbeat ]['new_payments'] = $this->thank_you_page->get_new_payments($payments); |
|
226 | + $response[$this->heartbeat]['new_payments'] = $this->thank_you_page->get_new_payments($payments); |
|
227 | 227 | $response = $this->setTransactionDetails($response); |
228 | 228 | } else { |
229 | - $response[ $this->heartbeat ]['payment_details'] = $this->thank_you_page->get_payment_details( |
|
229 | + $response[$this->heartbeat]['payment_details'] = $this->thank_you_page->get_payment_details( |
|
230 | 230 | $payments |
231 | 231 | ); |
232 | 232 | } |
233 | 233 | // reset time to check for payments |
234 | - $response[ $this->heartbeat ]['get_payments_since'] = time(); |
|
234 | + $response[$this->heartbeat]['get_payments_since'] = time(); |
|
235 | 235 | } else { |
236 | - $response[ $this->heartbeat ]['get_payments_since'] = $since; |
|
236 | + $response[$this->heartbeat]['get_payments_since'] = $since; |
|
237 | 237 | } |
238 | 238 | return $response; |
239 | 239 | } |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | */ |
252 | 252 | private function updateServerWaitTime($thank_you_page_data) |
253 | 253 | { |
254 | - $response[ $this->heartbeat ]['still_waiting'] = isset($thank_you_page_data['initial_access']) |
|
254 | + $response[$this->heartbeat]['still_waiting'] = isset($thank_you_page_data['initial_access']) |
|
255 | 255 | ? time() - $thank_you_page_data['initial_access'] |
256 | 256 | : 0; |
257 | 257 | $response = $this->setTransactionDetails($response, true); |
@@ -21,239 +21,239 @@ |
||
21 | 21 | */ |
22 | 22 | class ThankYouPageIpnMonitor |
23 | 23 | { |
24 | - /** |
|
25 | - * @var string $heartbeat |
|
26 | - */ |
|
27 | - private $heartbeat; |
|
24 | + /** |
|
25 | + * @var string $heartbeat |
|
26 | + */ |
|
27 | + private $heartbeat; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var EED_Thank_You_Page $thank_you_page |
|
31 | - */ |
|
32 | - private $thank_you_page; |
|
29 | + /** |
|
30 | + * @var EED_Thank_You_Page $thank_you_page |
|
31 | + */ |
|
32 | + private $thank_you_page; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var EE_Transaction $transaction |
|
36 | - */ |
|
37 | - private $transaction; |
|
34 | + /** |
|
35 | + * @var EE_Transaction $transaction |
|
36 | + */ |
|
37 | + private $transaction; |
|
38 | 38 | |
39 | 39 | |
40 | - /** |
|
41 | - * EventEditorHeartbeat constructor. |
|
42 | - */ |
|
43 | - public function __construct() |
|
44 | - { |
|
45 | - $this->heartbeat = WordpressHeartbeat::RESPONSE_KEY_THANK_YOU_PAGE; |
|
46 | - add_filter('heartbeat_received', array($this, 'heartbeatResponse'), 10, 3); |
|
47 | - add_filter('heartbeat_nopriv_received', array($this, 'heartbeatResponse'), 10, 3); |
|
48 | - } |
|
40 | + /** |
|
41 | + * EventEditorHeartbeat constructor. |
|
42 | + */ |
|
43 | + public function __construct() |
|
44 | + { |
|
45 | + $this->heartbeat = WordpressHeartbeat::RESPONSE_KEY_THANK_YOU_PAGE; |
|
46 | + add_filter('heartbeat_received', array($this, 'heartbeatResponse'), 10, 3); |
|
47 | + add_filter('heartbeat_nopriv_received', array($this, 'heartbeatResponse'), 10, 3); |
|
48 | + } |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * thank_you_page_IPN_monitor |
|
53 | - * this basically just pulls the TXN based on the reg_url_link sent from the server, |
|
54 | - * then checks that the TXN status is not failed, and that no other errors have been generated. |
|
55 | - * it also calculates the IPN wait time since the Thank You page was first loaded |
|
56 | - * |
|
57 | - * @param array $response |
|
58 | - * @param array $data |
|
59 | - * @return array |
|
60 | - * @throws EE_Error |
|
61 | - * @throws InvalidArgumentException |
|
62 | - * @throws InvalidDataTypeException |
|
63 | - * @throws InvalidInterfaceException |
|
64 | - * @throws ReflectionException |
|
65 | - */ |
|
66 | - public function heartbeatResponse($response = array(), $data = array()) |
|
67 | - { |
|
68 | - // does this heartbeat contain our data ? |
|
69 | - if (! isset($data[ $this->heartbeat ])) { |
|
70 | - return $response; |
|
71 | - } |
|
72 | - // check for reg_url_link in the incoming heartbeat data |
|
73 | - if (! isset($data[ $this->heartbeat ]['e_reg_url_link'])) { |
|
74 | - $response[ $this->heartbeat ] = array( |
|
75 | - 'errors' => ! empty($notices['errors']) |
|
76 | - ? $notices['errors'] |
|
77 | - : esc_html__( |
|
78 | - 'No transaction information could be retrieved because the registration URL link is missing or invalid.', |
|
79 | - 'event_espresso' |
|
80 | - ), |
|
81 | - ); |
|
82 | - return $response; |
|
83 | - } |
|
84 | - // kk heartbeat has our data |
|
85 | - $response = $this->initializeThankYouPageAndTransaction($response, $data); |
|
86 | - // if something went wrong... |
|
87 | - if (isset($response[ $this->heartbeat ]['errors'])) { |
|
88 | - return $response; |
|
89 | - } |
|
90 | - // grab transient of Transaction's status |
|
91 | - $txn_status = isset($data[ $this->heartbeat ]['txn_status']) |
|
92 | - ? $data[ $this->heartbeat ]['txn_status'] |
|
93 | - : null; |
|
94 | - $response = $this->getTransactionDetails($txn_status, $response, $data); |
|
95 | - // no payment data yet? |
|
96 | - if (isset($response[ $this->heartbeat ]['still_waiting'])) { |
|
97 | - return $response; |
|
98 | - } |
|
99 | - // TXN is happening so let's get the payments now |
|
100 | - // if we've already gotten payments then the heartbeat data will contain the timestamp of the last time we checked |
|
101 | - $since = isset($data[ $this->heartbeat ]['get_payments_since']) |
|
102 | - ? $data[ $this->heartbeat ]['get_payments_since'] |
|
103 | - : 0; |
|
104 | - return $this->paymentDetails($response, $since); |
|
105 | - } |
|
51 | + /** |
|
52 | + * thank_you_page_IPN_monitor |
|
53 | + * this basically just pulls the TXN based on the reg_url_link sent from the server, |
|
54 | + * then checks that the TXN status is not failed, and that no other errors have been generated. |
|
55 | + * it also calculates the IPN wait time since the Thank You page was first loaded |
|
56 | + * |
|
57 | + * @param array $response |
|
58 | + * @param array $data |
|
59 | + * @return array |
|
60 | + * @throws EE_Error |
|
61 | + * @throws InvalidArgumentException |
|
62 | + * @throws InvalidDataTypeException |
|
63 | + * @throws InvalidInterfaceException |
|
64 | + * @throws ReflectionException |
|
65 | + */ |
|
66 | + public function heartbeatResponse($response = array(), $data = array()) |
|
67 | + { |
|
68 | + // does this heartbeat contain our data ? |
|
69 | + if (! isset($data[ $this->heartbeat ])) { |
|
70 | + return $response; |
|
71 | + } |
|
72 | + // check for reg_url_link in the incoming heartbeat data |
|
73 | + if (! isset($data[ $this->heartbeat ]['e_reg_url_link'])) { |
|
74 | + $response[ $this->heartbeat ] = array( |
|
75 | + 'errors' => ! empty($notices['errors']) |
|
76 | + ? $notices['errors'] |
|
77 | + : esc_html__( |
|
78 | + 'No transaction information could be retrieved because the registration URL link is missing or invalid.', |
|
79 | + 'event_espresso' |
|
80 | + ), |
|
81 | + ); |
|
82 | + return $response; |
|
83 | + } |
|
84 | + // kk heartbeat has our data |
|
85 | + $response = $this->initializeThankYouPageAndTransaction($response, $data); |
|
86 | + // if something went wrong... |
|
87 | + if (isset($response[ $this->heartbeat ]['errors'])) { |
|
88 | + return $response; |
|
89 | + } |
|
90 | + // grab transient of Transaction's status |
|
91 | + $txn_status = isset($data[ $this->heartbeat ]['txn_status']) |
|
92 | + ? $data[ $this->heartbeat ]['txn_status'] |
|
93 | + : null; |
|
94 | + $response = $this->getTransactionDetails($txn_status, $response, $data); |
|
95 | + // no payment data yet? |
|
96 | + if (isset($response[ $this->heartbeat ]['still_waiting'])) { |
|
97 | + return $response; |
|
98 | + } |
|
99 | + // TXN is happening so let's get the payments now |
|
100 | + // if we've already gotten payments then the heartbeat data will contain the timestamp of the last time we checked |
|
101 | + $since = isset($data[ $this->heartbeat ]['get_payments_since']) |
|
102 | + ? $data[ $this->heartbeat ]['get_payments_since'] |
|
103 | + : 0; |
|
104 | + return $this->paymentDetails($response, $since); |
|
105 | + } |
|
106 | 106 | |
107 | 107 | |
108 | - /** |
|
109 | - * @param array $response |
|
110 | - * @param array $data |
|
111 | - * @return array |
|
112 | - * @throws EE_Error |
|
113 | - * @throws InvalidArgumentException |
|
114 | - * @throws InvalidDataTypeException |
|
115 | - * @throws InvalidInterfaceException |
|
116 | - */ |
|
117 | - private function initializeThankYouPageAndTransaction($response, $data) |
|
118 | - { |
|
119 | - require_once EE_MODULES . 'thank_you_page/EED_Thank_You_Page.module.php'; |
|
120 | - // set_definitions, instantiate the thank you page class, and get the ball rolling |
|
121 | - EED_Thank_You_Page::set_definitions(); |
|
122 | - $this->thank_you_page = EED_Thank_You_Page::instance(); |
|
123 | - $this->thank_you_page->set_reg_url_link($data[ $this->heartbeat ]['e_reg_url_link']); |
|
124 | - $this->thank_you_page->init(); |
|
125 | - // get TXN |
|
126 | - $transaction = $this->thank_you_page->get_txn(); |
|
127 | - // no TXN? then get out |
|
128 | - if (! $transaction instanceof EE_Transaction) { |
|
129 | - $notices = EE_Error::get_notices(); |
|
130 | - $response[ $this->heartbeat ] = array( |
|
131 | - 'errors' => ! empty($notices['errors']) |
|
132 | - ? $notices['errors'] |
|
133 | - : sprintf( |
|
134 | - esc_html__( |
|
135 | - 'The information for your transaction could not be retrieved from the server or the transaction data received was invalid because of a technical reason. (%s)', |
|
136 | - 'event_espresso' |
|
137 | - ), |
|
138 | - __LINE__ |
|
139 | - ), |
|
140 | - ); |
|
141 | - return $response; |
|
142 | - } |
|
143 | - $this->transaction = $transaction; |
|
144 | - return $response; |
|
145 | - } |
|
108 | + /** |
|
109 | + * @param array $response |
|
110 | + * @param array $data |
|
111 | + * @return array |
|
112 | + * @throws EE_Error |
|
113 | + * @throws InvalidArgumentException |
|
114 | + * @throws InvalidDataTypeException |
|
115 | + * @throws InvalidInterfaceException |
|
116 | + */ |
|
117 | + private function initializeThankYouPageAndTransaction($response, $data) |
|
118 | + { |
|
119 | + require_once EE_MODULES . 'thank_you_page/EED_Thank_You_Page.module.php'; |
|
120 | + // set_definitions, instantiate the thank you page class, and get the ball rolling |
|
121 | + EED_Thank_You_Page::set_definitions(); |
|
122 | + $this->thank_you_page = EED_Thank_You_Page::instance(); |
|
123 | + $this->thank_you_page->set_reg_url_link($data[ $this->heartbeat ]['e_reg_url_link']); |
|
124 | + $this->thank_you_page->init(); |
|
125 | + // get TXN |
|
126 | + $transaction = $this->thank_you_page->get_txn(); |
|
127 | + // no TXN? then get out |
|
128 | + if (! $transaction instanceof EE_Transaction) { |
|
129 | + $notices = EE_Error::get_notices(); |
|
130 | + $response[ $this->heartbeat ] = array( |
|
131 | + 'errors' => ! empty($notices['errors']) |
|
132 | + ? $notices['errors'] |
|
133 | + : sprintf( |
|
134 | + esc_html__( |
|
135 | + 'The information for your transaction could not be retrieved from the server or the transaction data received was invalid because of a technical reason. (%s)', |
|
136 | + 'event_espresso' |
|
137 | + ), |
|
138 | + __LINE__ |
|
139 | + ), |
|
140 | + ); |
|
141 | + return $response; |
|
142 | + } |
|
143 | + $this->transaction = $transaction; |
|
144 | + return $response; |
|
145 | + } |
|
146 | 146 | |
147 | 147 | |
148 | - /** |
|
149 | - * @param string $txn_status |
|
150 | - * @param array $response |
|
151 | - * @param array $data |
|
152 | - * @return array |
|
153 | - * @throws EE_Error |
|
154 | - * @throws InvalidArgumentException |
|
155 | - * @throws InvalidDataTypeException |
|
156 | - * @throws InvalidInterfaceException |
|
157 | - * @throws ReflectionException |
|
158 | - */ |
|
159 | - private function getTransactionDetails($txn_status, $response, $data) |
|
160 | - { |
|
161 | - // has the TXN status changed since we last checked (or empty because this is the first time running through this code)? |
|
162 | - if ($txn_status !== $this->transaction->status_ID()) { |
|
163 | - // switch between two possible basic outcomes |
|
164 | - switch ($this->transaction->status_ID()) { |
|
165 | - // TXN has been updated in some way |
|
166 | - case EEM_Transaction::overpaid_status_code: |
|
167 | - case EEM_Transaction::complete_status_code: |
|
168 | - case EEM_Transaction::incomplete_status_code: |
|
169 | - // send updated TXN results back to client, |
|
170 | - return $this->setTransactionDetails($response); |
|
171 | - // or we have a bad TXN, or really slow IPN, so calculate the wait time and send that back... |
|
172 | - case EEM_Transaction::failed_status_code: |
|
173 | - default: |
|
174 | - // keep on waiting... |
|
175 | - return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
176 | - } |
|
177 | - // or is the TXN still failed (never been updated) ??? |
|
178 | - } elseif ($this->transaction->failed()) { |
|
179 | - // keep on waiting... |
|
180 | - return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
181 | - } |
|
182 | - return $response; |
|
183 | - } |
|
148 | + /** |
|
149 | + * @param string $txn_status |
|
150 | + * @param array $response |
|
151 | + * @param array $data |
|
152 | + * @return array |
|
153 | + * @throws EE_Error |
|
154 | + * @throws InvalidArgumentException |
|
155 | + * @throws InvalidDataTypeException |
|
156 | + * @throws InvalidInterfaceException |
|
157 | + * @throws ReflectionException |
|
158 | + */ |
|
159 | + private function getTransactionDetails($txn_status, $response, $data) |
|
160 | + { |
|
161 | + // has the TXN status changed since we last checked (or empty because this is the first time running through this code)? |
|
162 | + if ($txn_status !== $this->transaction->status_ID()) { |
|
163 | + // switch between two possible basic outcomes |
|
164 | + switch ($this->transaction->status_ID()) { |
|
165 | + // TXN has been updated in some way |
|
166 | + case EEM_Transaction::overpaid_status_code: |
|
167 | + case EEM_Transaction::complete_status_code: |
|
168 | + case EEM_Transaction::incomplete_status_code: |
|
169 | + // send updated TXN results back to client, |
|
170 | + return $this->setTransactionDetails($response); |
|
171 | + // or we have a bad TXN, or really slow IPN, so calculate the wait time and send that back... |
|
172 | + case EEM_Transaction::failed_status_code: |
|
173 | + default: |
|
174 | + // keep on waiting... |
|
175 | + return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
176 | + } |
|
177 | + // or is the TXN still failed (never been updated) ??? |
|
178 | + } elseif ($this->transaction->failed()) { |
|
179 | + // keep on waiting... |
|
180 | + return $this->updateServerWaitTime($data[ $this->heartbeat ]); |
|
181 | + } |
|
182 | + return $response; |
|
183 | + } |
|
184 | 184 | |
185 | 185 | |
186 | - /** |
|
187 | - * @param array $response |
|
188 | - * @param boolean $status_only |
|
189 | - * @return array |
|
190 | - * @throws EE_Error |
|
191 | - * @throws InvalidArgumentException |
|
192 | - * @throws InvalidDataTypeException |
|
193 | - * @throws InvalidInterfaceException |
|
194 | - * @throws ReflectionException |
|
195 | - */ |
|
196 | - private function setTransactionDetails($response, $status_only = false) |
|
197 | - { |
|
198 | - if (! $status_only && ! isset($response[ $this->heartbeat ]['transaction_details'])) { |
|
199 | - $response[ $this->heartbeat ]['transaction_details'] = $this->thank_you_page->get_transaction_details(); |
|
200 | - } |
|
201 | - if (! isset($response[ $this->heartbeat ]['txn_status'])) { |
|
202 | - $response[ $this->heartbeat ]['txn_status'] = $this->transaction->status_ID(); |
|
203 | - } |
|
204 | - return $response; |
|
205 | - } |
|
186 | + /** |
|
187 | + * @param array $response |
|
188 | + * @param boolean $status_only |
|
189 | + * @return array |
|
190 | + * @throws EE_Error |
|
191 | + * @throws InvalidArgumentException |
|
192 | + * @throws InvalidDataTypeException |
|
193 | + * @throws InvalidInterfaceException |
|
194 | + * @throws ReflectionException |
|
195 | + */ |
|
196 | + private function setTransactionDetails($response, $status_only = false) |
|
197 | + { |
|
198 | + if (! $status_only && ! isset($response[ $this->heartbeat ]['transaction_details'])) { |
|
199 | + $response[ $this->heartbeat ]['transaction_details'] = $this->thank_you_page->get_transaction_details(); |
|
200 | + } |
|
201 | + if (! isset($response[ $this->heartbeat ]['txn_status'])) { |
|
202 | + $response[ $this->heartbeat ]['txn_status'] = $this->transaction->status_ID(); |
|
203 | + } |
|
204 | + return $response; |
|
205 | + } |
|
206 | 206 | |
207 | 207 | |
208 | - /** |
|
209 | - * @param array $response |
|
210 | - * @param int $since |
|
211 | - * @return array |
|
212 | - * @throws EE_Error |
|
213 | - * @throws InvalidArgumentException |
|
214 | - * @throws InvalidDataTypeException |
|
215 | - * @throws InvalidInterfaceException |
|
216 | - * @throws ReflectionException |
|
217 | - */ |
|
218 | - private function paymentDetails($response, $since) |
|
219 | - { |
|
220 | - // then check for payments |
|
221 | - $payments = $this->thank_you_page->get_txn_payments($since); |
|
222 | - // has a payment been processed ? |
|
223 | - if (! empty($payments) || $this->thank_you_page->isOfflinePaymentMethod()) { |
|
224 | - if ($since) { |
|
225 | - $response[ $this->heartbeat ]['new_payments'] = $this->thank_you_page->get_new_payments($payments); |
|
226 | - $response = $this->setTransactionDetails($response); |
|
227 | - } else { |
|
228 | - $response[ $this->heartbeat ]['payment_details'] = $this->thank_you_page->get_payment_details( |
|
229 | - $payments |
|
230 | - ); |
|
231 | - } |
|
232 | - // reset time to check for payments |
|
233 | - $response[ $this->heartbeat ]['get_payments_since'] = time(); |
|
234 | - } else { |
|
235 | - $response[ $this->heartbeat ]['get_payments_since'] = $since; |
|
236 | - } |
|
237 | - return $response; |
|
238 | - } |
|
208 | + /** |
|
209 | + * @param array $response |
|
210 | + * @param int $since |
|
211 | + * @return array |
|
212 | + * @throws EE_Error |
|
213 | + * @throws InvalidArgumentException |
|
214 | + * @throws InvalidDataTypeException |
|
215 | + * @throws InvalidInterfaceException |
|
216 | + * @throws ReflectionException |
|
217 | + */ |
|
218 | + private function paymentDetails($response, $since) |
|
219 | + { |
|
220 | + // then check for payments |
|
221 | + $payments = $this->thank_you_page->get_txn_payments($since); |
|
222 | + // has a payment been processed ? |
|
223 | + if (! empty($payments) || $this->thank_you_page->isOfflinePaymentMethod()) { |
|
224 | + if ($since) { |
|
225 | + $response[ $this->heartbeat ]['new_payments'] = $this->thank_you_page->get_new_payments($payments); |
|
226 | + $response = $this->setTransactionDetails($response); |
|
227 | + } else { |
|
228 | + $response[ $this->heartbeat ]['payment_details'] = $this->thank_you_page->get_payment_details( |
|
229 | + $payments |
|
230 | + ); |
|
231 | + } |
|
232 | + // reset time to check for payments |
|
233 | + $response[ $this->heartbeat ]['get_payments_since'] = time(); |
|
234 | + } else { |
|
235 | + $response[ $this->heartbeat ]['get_payments_since'] = $since; |
|
236 | + } |
|
237 | + return $response; |
|
238 | + } |
|
239 | 239 | |
240 | 240 | |
241 | - /** |
|
242 | - * @param array $thank_you_page_data thank you page portion of the incoming JSON array |
|
243 | - * from the WP heartbeat data |
|
244 | - * @return array |
|
245 | - * @throws EE_Error |
|
246 | - * @throws InvalidArgumentException |
|
247 | - * @throws InvalidDataTypeException |
|
248 | - * @throws InvalidInterfaceException |
|
249 | - * @throws ReflectionException |
|
250 | - */ |
|
251 | - private function updateServerWaitTime($thank_you_page_data) |
|
252 | - { |
|
253 | - $response[ $this->heartbeat ]['still_waiting'] = isset($thank_you_page_data['initial_access']) |
|
254 | - ? time() - $thank_you_page_data['initial_access'] |
|
255 | - : 0; |
|
256 | - $response = $this->setTransactionDetails($response, true); |
|
257 | - return $response; |
|
258 | - } |
|
241 | + /** |
|
242 | + * @param array $thank_you_page_data thank you page portion of the incoming JSON array |
|
243 | + * from the WP heartbeat data |
|
244 | + * @return array |
|
245 | + * @throws EE_Error |
|
246 | + * @throws InvalidArgumentException |
|
247 | + * @throws InvalidDataTypeException |
|
248 | + * @throws InvalidInterfaceException |
|
249 | + * @throws ReflectionException |
|
250 | + */ |
|
251 | + private function updateServerWaitTime($thank_you_page_data) |
|
252 | + { |
|
253 | + $response[ $this->heartbeat ]['still_waiting'] = isset($thank_you_page_data['initial_access']) |
|
254 | + ? time() - $thank_you_page_data['initial_access'] |
|
255 | + : 0; |
|
256 | + $response = $this->setTransactionDetails($response, true); |
|
257 | + return $response; |
|
258 | + } |
|
259 | 259 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | ); |
89 | 89 | $success = \EEH_Line_Item::add_item($transaction->total_line_item(), $cancelled_line_item); |
90 | 90 | } |
91 | - if (! $success) { |
|
91 | + if ( ! $success) { |
|
92 | 92 | throw new \RuntimeException( |
93 | 93 | sprintf( |
94 | 94 | esc_html__('An error occurred while attempting to cancel ticket line item %1$s', 'event_espresso'), |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | break; |
126 | 126 | } |
127 | 127 | } |
128 | - if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) { |
|
128 | + if ( ! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) { |
|
129 | 129 | throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID()); |
130 | 130 | } |
131 | 131 | return $line_item; |
@@ -18,114 +18,114 @@ |
||
18 | 18 | */ |
19 | 19 | class CancelTicketLineItemService extends DomainService |
20 | 20 | { |
21 | - /** |
|
22 | - * @param \EE_Registration $registration |
|
23 | - * @param int $quantity |
|
24 | - * @return bool|int |
|
25 | - */ |
|
26 | - public function forRegistration(\EE_Registration $registration, $quantity = 1) |
|
27 | - { |
|
28 | - return $this->cancel( |
|
29 | - $registration->transaction(), |
|
30 | - $registration->ticket(), |
|
31 | - $quantity, |
|
32 | - $registration->ticket_line_item() |
|
33 | - ); |
|
34 | - } |
|
21 | + /** |
|
22 | + * @param \EE_Registration $registration |
|
23 | + * @param int $quantity |
|
24 | + * @return bool|int |
|
25 | + */ |
|
26 | + public function forRegistration(\EE_Registration $registration, $quantity = 1) |
|
27 | + { |
|
28 | + return $this->cancel( |
|
29 | + $registration->transaction(), |
|
30 | + $registration->ticket(), |
|
31 | + $quantity, |
|
32 | + $registration->ticket_line_item() |
|
33 | + ); |
|
34 | + } |
|
35 | 35 | |
36 | 36 | |
37 | - /** |
|
38 | - * @param \EE_Transaction $transaction |
|
39 | - * @param \EE_Ticket $ticket |
|
40 | - * @param int $quantity |
|
41 | - * @param \EE_Line_Item $ticket_line_item |
|
42 | - * @return bool|int |
|
43 | - */ |
|
44 | - public function cancel( |
|
45 | - \EE_Transaction $transaction, |
|
46 | - \EE_Ticket $ticket, |
|
47 | - $quantity = 1, |
|
48 | - \EE_Line_Item $ticket_line_item = null |
|
49 | - ) { |
|
50 | - $ticket_line_item = $ticket_line_item instanceof \EE_Line_Item |
|
51 | - ? $ticket_line_item |
|
52 | - : $this->getTicketLineItem($transaction, $ticket); |
|
53 | - // first we need to decrement the ticket quantity |
|
54 | - \EEH_Line_Item::decrement_quantity($ticket_line_item, $quantity); |
|
55 | - // no tickets left for this line item ? |
|
56 | - if ((int) $ticket_line_item->quantity() === 0) { |
|
57 | - // then just set this line item as cancelled, save, and get out |
|
58 | - $ticket_line_item->set_type(\EEM_Line_Item::type_cancellation); |
|
59 | - $success = $ticket_line_item->save(); |
|
60 | - } else { |
|
61 | - // otherwise create a new cancelled line item, so that we have a record of the cancellation |
|
62 | - $items_subtotal = \EEH_Line_Item::get_pre_tax_subtotal( |
|
63 | - \EEH_Line_Item::get_event_line_item_for_ticket( |
|
64 | - $transaction->total_line_item(), |
|
65 | - $ticket |
|
66 | - ) |
|
67 | - ); |
|
68 | - $cancelled_line_item = \EE_Line_Item::new_instance( |
|
69 | - array( |
|
70 | - 'LIN_name' => $ticket_line_item->name(), |
|
71 | - 'LIN_desc' => sprintf( |
|
72 | - esc_html__('%1$s Cancelled: %2$s', 'event_espresso'), |
|
73 | - $ticket_line_item->desc(), |
|
74 | - date('Y-m-d h:i a') |
|
75 | - ), |
|
76 | - 'LIN_unit_price' => (float) $ticket_line_item->unit_price(), |
|
77 | - 'LIN_quantity' => $quantity, |
|
78 | - 'LIN_percent' => null, |
|
79 | - 'LIN_is_taxable' => false, |
|
80 | - 'LIN_order' => $items_subtotal instanceof \EE_Line_Item |
|
81 | - ? count($items_subtotal->children()) |
|
82 | - : 0, |
|
83 | - 'LIN_total' => (float) $ticket_line_item->unit_price(), |
|
84 | - 'LIN_type' => \EEM_Line_Item::type_cancellation, |
|
85 | - ) |
|
86 | - ); |
|
87 | - $success = \EEH_Line_Item::add_item($transaction->total_line_item(), $cancelled_line_item); |
|
88 | - } |
|
89 | - if (! $success) { |
|
90 | - throw new \RuntimeException( |
|
91 | - sprintf( |
|
92 | - esc_html__('An error occurred while attempting to cancel ticket line item %1$s', 'event_espresso'), |
|
93 | - $ticket_line_item->ID() |
|
94 | - ) |
|
95 | - ); |
|
96 | - } |
|
97 | - return $success; |
|
98 | - } |
|
37 | + /** |
|
38 | + * @param \EE_Transaction $transaction |
|
39 | + * @param \EE_Ticket $ticket |
|
40 | + * @param int $quantity |
|
41 | + * @param \EE_Line_Item $ticket_line_item |
|
42 | + * @return bool|int |
|
43 | + */ |
|
44 | + public function cancel( |
|
45 | + \EE_Transaction $transaction, |
|
46 | + \EE_Ticket $ticket, |
|
47 | + $quantity = 1, |
|
48 | + \EE_Line_Item $ticket_line_item = null |
|
49 | + ) { |
|
50 | + $ticket_line_item = $ticket_line_item instanceof \EE_Line_Item |
|
51 | + ? $ticket_line_item |
|
52 | + : $this->getTicketLineItem($transaction, $ticket); |
|
53 | + // first we need to decrement the ticket quantity |
|
54 | + \EEH_Line_Item::decrement_quantity($ticket_line_item, $quantity); |
|
55 | + // no tickets left for this line item ? |
|
56 | + if ((int) $ticket_line_item->quantity() === 0) { |
|
57 | + // then just set this line item as cancelled, save, and get out |
|
58 | + $ticket_line_item->set_type(\EEM_Line_Item::type_cancellation); |
|
59 | + $success = $ticket_line_item->save(); |
|
60 | + } else { |
|
61 | + // otherwise create a new cancelled line item, so that we have a record of the cancellation |
|
62 | + $items_subtotal = \EEH_Line_Item::get_pre_tax_subtotal( |
|
63 | + \EEH_Line_Item::get_event_line_item_for_ticket( |
|
64 | + $transaction->total_line_item(), |
|
65 | + $ticket |
|
66 | + ) |
|
67 | + ); |
|
68 | + $cancelled_line_item = \EE_Line_Item::new_instance( |
|
69 | + array( |
|
70 | + 'LIN_name' => $ticket_line_item->name(), |
|
71 | + 'LIN_desc' => sprintf( |
|
72 | + esc_html__('%1$s Cancelled: %2$s', 'event_espresso'), |
|
73 | + $ticket_line_item->desc(), |
|
74 | + date('Y-m-d h:i a') |
|
75 | + ), |
|
76 | + 'LIN_unit_price' => (float) $ticket_line_item->unit_price(), |
|
77 | + 'LIN_quantity' => $quantity, |
|
78 | + 'LIN_percent' => null, |
|
79 | + 'LIN_is_taxable' => false, |
|
80 | + 'LIN_order' => $items_subtotal instanceof \EE_Line_Item |
|
81 | + ? count($items_subtotal->children()) |
|
82 | + : 0, |
|
83 | + 'LIN_total' => (float) $ticket_line_item->unit_price(), |
|
84 | + 'LIN_type' => \EEM_Line_Item::type_cancellation, |
|
85 | + ) |
|
86 | + ); |
|
87 | + $success = \EEH_Line_Item::add_item($transaction->total_line_item(), $cancelled_line_item); |
|
88 | + } |
|
89 | + if (! $success) { |
|
90 | + throw new \RuntimeException( |
|
91 | + sprintf( |
|
92 | + esc_html__('An error occurred while attempting to cancel ticket line item %1$s', 'event_espresso'), |
|
93 | + $ticket_line_item->ID() |
|
94 | + ) |
|
95 | + ); |
|
96 | + } |
|
97 | + return $success; |
|
98 | + } |
|
99 | 99 | |
100 | 100 | |
101 | - /** |
|
102 | - * @param \EE_Transaction $transaction |
|
103 | - * @param \EE_Ticket $ticket |
|
104 | - * @return \EE_Line_Item |
|
105 | - * @throws EntityNotFoundException |
|
106 | - * @throws \EE_Error |
|
107 | - */ |
|
108 | - protected static function getTicketLineItem(\EE_Transaction $transaction, \EE_Ticket $ticket) |
|
109 | - { |
|
110 | - $line_item = null; |
|
111 | - $ticket_line_items = \EEH_Line_Item::get_line_items_by_object_type_and_IDs( |
|
112 | - $transaction->total_line_item(), |
|
113 | - 'Ticket', |
|
114 | - array($ticket->ID()) |
|
115 | - ); |
|
116 | - foreach ($ticket_line_items as $ticket_line_item) { |
|
117 | - if ( |
|
118 | - $ticket_line_item instanceof \EE_Line_Item |
|
119 | - && $ticket_line_item->OBJ_type() === 'Ticket' |
|
120 | - && $ticket_line_item->OBJ_ID() === $ticket->ID() |
|
121 | - ) { |
|
122 | - $line_item = $ticket_line_item; |
|
123 | - break; |
|
124 | - } |
|
125 | - } |
|
126 | - if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) { |
|
127 | - throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID()); |
|
128 | - } |
|
129 | - return $line_item; |
|
130 | - } |
|
101 | + /** |
|
102 | + * @param \EE_Transaction $transaction |
|
103 | + * @param \EE_Ticket $ticket |
|
104 | + * @return \EE_Line_Item |
|
105 | + * @throws EntityNotFoundException |
|
106 | + * @throws \EE_Error |
|
107 | + */ |
|
108 | + protected static function getTicketLineItem(\EE_Transaction $transaction, \EE_Ticket $ticket) |
|
109 | + { |
|
110 | + $line_item = null; |
|
111 | + $ticket_line_items = \EEH_Line_Item::get_line_items_by_object_type_and_IDs( |
|
112 | + $transaction->total_line_item(), |
|
113 | + 'Ticket', |
|
114 | + array($ticket->ID()) |
|
115 | + ); |
|
116 | + foreach ($ticket_line_items as $ticket_line_item) { |
|
117 | + if ( |
|
118 | + $ticket_line_item instanceof \EE_Line_Item |
|
119 | + && $ticket_line_item->OBJ_type() === 'Ticket' |
|
120 | + && $ticket_line_item->OBJ_ID() === $ticket->ID() |
|
121 | + ) { |
|
122 | + $line_item = $ticket_line_item; |
|
123 | + break; |
|
124 | + } |
|
125 | + } |
|
126 | + if (! ($line_item instanceof \EE_Line_Item && $line_item->OBJ_type() === 'Ticket')) { |
|
127 | + throw new EntityNotFoundException('Line Item Ticket ID', $ticket->ID()); |
|
128 | + } |
|
129 | + return $line_item; |
|
130 | + } |
|
131 | 131 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | // assume this if first route being called |
61 | 61 | $previous_route = false; |
62 | 62 | // but is it really ??? |
63 | - if (! empty(self::$_previous_routes)) { |
|
63 | + if ( ! empty(self::$_previous_routes)) { |
|
64 | 64 | // get last run route |
65 | 65 | $previous_routes = array_values(self::$_previous_routes); |
66 | 66 | $previous_route = array_pop($previous_routes); |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | $current_route = $this->get_forward($previous_route); |
72 | 72 | try { |
73 | 73 | // check for recursive forwarding |
74 | - if (isset(self::$_previous_routes[ $current_route ])) { |
|
74 | + if (isset(self::$_previous_routes[$current_route])) { |
|
75 | 75 | throw new EE_Error( |
76 | 76 | sprintf( |
77 | 77 | esc_html__( |
@@ -144,38 +144,38 @@ discard block |
||
144 | 144 | return false; |
145 | 145 | } |
146 | 146 | // verify that result is an array |
147 | - if (! is_array($module_method)) { |
|
147 | + if ( ! is_array($module_method)) { |
|
148 | 148 | $msg = sprintf(esc_html__('The %s route has not been properly registered.', 'event_espresso'), $current_route); |
149 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
149 | + EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__); |
|
150 | 150 | return false; |
151 | 151 | } |
152 | 152 | // grab module name |
153 | 153 | $module_name = $module_method[0]; |
154 | 154 | // verify that a class method was registered properly |
155 | - if (! isset($module_method[1])) { |
|
155 | + if ( ! isset($module_method[1])) { |
|
156 | 156 | $msg = sprintf( |
157 | 157 | esc_html__('A class method for the %s route has not been properly registered.', 'event_espresso'), |
158 | 158 | $current_route |
159 | 159 | ); |
160 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
160 | + EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__); |
|
161 | 161 | return false; |
162 | 162 | } |
163 | 163 | // grab method |
164 | 164 | $method = $module_method[1]; |
165 | 165 | // verify that class exists |
166 | - if (! class_exists($module_name)) { |
|
166 | + if ( ! class_exists($module_name)) { |
|
167 | 167 | $msg = sprintf(esc_html__('The requested %s class could not be found.', 'event_espresso'), $module_name); |
168 | 168 | EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
169 | 169 | return false; |
170 | 170 | } |
171 | 171 | // verify that method exists |
172 | - if (! method_exists($module_name, $method)) { |
|
172 | + if ( ! method_exists($module_name, $method)) { |
|
173 | 173 | $msg = sprintf( |
174 | 174 | esc_html__('The class method %s for the %s route is in invalid.', 'event_espresso'), |
175 | 175 | $method, |
176 | 176 | $current_route |
177 | 177 | ); |
178 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
178 | + EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__); |
|
179 | 179 | return false; |
180 | 180 | } |
181 | 181 | // instantiate module and call route method |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | // instantiate module class |
210 | 210 | $module = new $module_name(); |
211 | 211 | // ensure that class is actually a module |
212 | - if (! $module instanceof EED_Module) { |
|
212 | + if ( ! $module instanceof EED_Module) { |
|
213 | 213 | EE_Error::add_error( |
214 | 214 | sprintf(esc_html__('The requested %s module is not of the class EED_Module.', 'event_espresso'), $module_name), |
215 | 215 | __FILE__, |
@@ -15,254 +15,254 @@ |
||
15 | 15 | */ |
16 | 16 | final class EE_Module_Request_Router implements InterminableInterface |
17 | 17 | { |
18 | - /** |
|
19 | - * @var RequestInterface $request |
|
20 | - */ |
|
21 | - private $request; |
|
18 | + /** |
|
19 | + * @var RequestInterface $request |
|
20 | + */ |
|
21 | + private $request; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @var array $_previous_routes |
|
25 | - */ |
|
26 | - private static $_previous_routes = array(); |
|
23 | + /** |
|
24 | + * @var array $_previous_routes |
|
25 | + */ |
|
26 | + private static $_previous_routes = array(); |
|
27 | 27 | |
28 | - /** |
|
29 | - * @var WP_Query $WP_Query |
|
30 | - */ |
|
31 | - public $WP_Query; |
|
28 | + /** |
|
29 | + * @var WP_Query $WP_Query |
|
30 | + */ |
|
31 | + public $WP_Query; |
|
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * EE_Module_Request_Router constructor. |
|
36 | - * |
|
37 | - * @param RequestInterface $request |
|
38 | - */ |
|
39 | - public function __construct(RequestInterface $request) |
|
40 | - { |
|
41 | - $this->request = $request; |
|
42 | - } |
|
34 | + /** |
|
35 | + * EE_Module_Request_Router constructor. |
|
36 | + * |
|
37 | + * @param RequestInterface $request |
|
38 | + */ |
|
39 | + public function __construct(RequestInterface $request) |
|
40 | + { |
|
41 | + $this->request = $request; |
|
42 | + } |
|
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * on the first call to this method, it checks the Request for a "route" |
|
47 | - * on subsequent calls to this method, |
|
48 | - * instead of checking the Request for a route, it checks the previous routes array, |
|
49 | - * and checks if the last called route has any forwarding routes registered for it |
|
50 | - * |
|
51 | - * @param WP_Query $WP_Query |
|
52 | - * @return NULL|string |
|
53 | - * @throws EE_Error |
|
54 | - * @throws ReflectionException |
|
55 | - */ |
|
56 | - public function get_route(WP_Query $WP_Query) |
|
57 | - { |
|
58 | - $this->WP_Query = $WP_Query; |
|
59 | - // assume this if first route being called |
|
60 | - $previous_route = false; |
|
61 | - // but is it really ??? |
|
62 | - if (! empty(self::$_previous_routes)) { |
|
63 | - // get last run route |
|
64 | - $previous_routes = array_values(self::$_previous_routes); |
|
65 | - $previous_route = array_pop($previous_routes); |
|
66 | - } |
|
67 | - // has another route already been run ? |
|
68 | - if ($previous_route) { |
|
69 | - // check if forwarding has been set |
|
70 | - $current_route = $this->get_forward($previous_route); |
|
71 | - try { |
|
72 | - // check for recursive forwarding |
|
73 | - if (isset(self::$_previous_routes[ $current_route ])) { |
|
74 | - throw new EE_Error( |
|
75 | - sprintf( |
|
76 | - esc_html__( |
|
77 | - 'An error occurred. The %s route has already been called, and therefore can not be forwarded to, because an infinite loop would be created and break the interweb.', |
|
78 | - 'event_espresso' |
|
79 | - ), |
|
80 | - $current_route |
|
81 | - ) |
|
82 | - ); |
|
83 | - } |
|
84 | - } catch (EE_Error $e) { |
|
85 | - $e->get_error(); |
|
86 | - return null; |
|
87 | - } |
|
88 | - } else { |
|
89 | - // first route called |
|
90 | - $current_route = null; |
|
91 | - // grab all routes |
|
92 | - $routes = EE_Config::get_routes(); |
|
93 | - foreach ($routes as $key => $route) { |
|
94 | - // first determine if route key uses w?ldc*rds |
|
95 | - $uses_wildcards = strpos($key, '?') !== false |
|
96 | - || strpos($key, '*') !== false; |
|
97 | - // check request for module route |
|
98 | - $route_found = $uses_wildcards |
|
99 | - ? $this->request->matches($key) |
|
100 | - : $this->request->requestParamIsSet($key); |
|
101 | - if ($route_found) { |
|
102 | - $current_route = $uses_wildcards |
|
103 | - ? $this->request->getMatch($key) |
|
104 | - : $this->request->getRequestParam($key); |
|
105 | - $current_route = sanitize_text_field($current_route); |
|
106 | - if ($current_route) { |
|
107 | - $current_route = array($key, $current_route); |
|
108 | - break; |
|
109 | - } |
|
110 | - } |
|
111 | - } |
|
112 | - } |
|
113 | - // sorry, but I can't read what you route ! |
|
114 | - if (empty($current_route)) { |
|
115 | - return null; |
|
116 | - } |
|
117 | - // add route to previous routes array |
|
118 | - self::$_previous_routes[] = $current_route; |
|
119 | - return $current_route; |
|
120 | - } |
|
45 | + /** |
|
46 | + * on the first call to this method, it checks the Request for a "route" |
|
47 | + * on subsequent calls to this method, |
|
48 | + * instead of checking the Request for a route, it checks the previous routes array, |
|
49 | + * and checks if the last called route has any forwarding routes registered for it |
|
50 | + * |
|
51 | + * @param WP_Query $WP_Query |
|
52 | + * @return NULL|string |
|
53 | + * @throws EE_Error |
|
54 | + * @throws ReflectionException |
|
55 | + */ |
|
56 | + public function get_route(WP_Query $WP_Query) |
|
57 | + { |
|
58 | + $this->WP_Query = $WP_Query; |
|
59 | + // assume this if first route being called |
|
60 | + $previous_route = false; |
|
61 | + // but is it really ??? |
|
62 | + if (! empty(self::$_previous_routes)) { |
|
63 | + // get last run route |
|
64 | + $previous_routes = array_values(self::$_previous_routes); |
|
65 | + $previous_route = array_pop($previous_routes); |
|
66 | + } |
|
67 | + // has another route already been run ? |
|
68 | + if ($previous_route) { |
|
69 | + // check if forwarding has been set |
|
70 | + $current_route = $this->get_forward($previous_route); |
|
71 | + try { |
|
72 | + // check for recursive forwarding |
|
73 | + if (isset(self::$_previous_routes[ $current_route ])) { |
|
74 | + throw new EE_Error( |
|
75 | + sprintf( |
|
76 | + esc_html__( |
|
77 | + 'An error occurred. The %s route has already been called, and therefore can not be forwarded to, because an infinite loop would be created and break the interweb.', |
|
78 | + 'event_espresso' |
|
79 | + ), |
|
80 | + $current_route |
|
81 | + ) |
|
82 | + ); |
|
83 | + } |
|
84 | + } catch (EE_Error $e) { |
|
85 | + $e->get_error(); |
|
86 | + return null; |
|
87 | + } |
|
88 | + } else { |
|
89 | + // first route called |
|
90 | + $current_route = null; |
|
91 | + // grab all routes |
|
92 | + $routes = EE_Config::get_routes(); |
|
93 | + foreach ($routes as $key => $route) { |
|
94 | + // first determine if route key uses w?ldc*rds |
|
95 | + $uses_wildcards = strpos($key, '?') !== false |
|
96 | + || strpos($key, '*') !== false; |
|
97 | + // check request for module route |
|
98 | + $route_found = $uses_wildcards |
|
99 | + ? $this->request->matches($key) |
|
100 | + : $this->request->requestParamIsSet($key); |
|
101 | + if ($route_found) { |
|
102 | + $current_route = $uses_wildcards |
|
103 | + ? $this->request->getMatch($key) |
|
104 | + : $this->request->getRequestParam($key); |
|
105 | + $current_route = sanitize_text_field($current_route); |
|
106 | + if ($current_route) { |
|
107 | + $current_route = array($key, $current_route); |
|
108 | + break; |
|
109 | + } |
|
110 | + } |
|
111 | + } |
|
112 | + } |
|
113 | + // sorry, but I can't read what you route ! |
|
114 | + if (empty($current_route)) { |
|
115 | + return null; |
|
116 | + } |
|
117 | + // add route to previous routes array |
|
118 | + self::$_previous_routes[] = $current_route; |
|
119 | + return $current_route; |
|
120 | + } |
|
121 | 121 | |
122 | 122 | |
123 | - /** |
|
124 | - * this method simply takes a valid route, and resolves what module class method the route points to |
|
125 | - * |
|
126 | - * @param string $key |
|
127 | - * @param string $current_route |
|
128 | - * @return EED_Module|object|boolean|null |
|
129 | - * @throws EE_Error |
|
130 | - * @throws ReflectionException |
|
131 | - */ |
|
132 | - public function resolve_route($key, $current_route) |
|
133 | - { |
|
134 | - // get module method that route has been mapped to |
|
135 | - $module_method = EE_Config::get_route($current_route, $key); |
|
136 | - // verify result was returned |
|
137 | - if (empty($module_method)) { |
|
138 | - $msg = sprintf( |
|
139 | - esc_html__('The requested route %s could not be mapped to any registered modules.', 'event_espresso'), |
|
140 | - $current_route |
|
141 | - ); |
|
142 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
143 | - return false; |
|
144 | - } |
|
145 | - // verify that result is an array |
|
146 | - if (! is_array($module_method)) { |
|
147 | - $msg = sprintf(esc_html__('The %s route has not been properly registered.', 'event_espresso'), $current_route); |
|
148 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
149 | - return false; |
|
150 | - } |
|
151 | - // grab module name |
|
152 | - $module_name = $module_method[0]; |
|
153 | - // verify that a class method was registered properly |
|
154 | - if (! isset($module_method[1])) { |
|
155 | - $msg = sprintf( |
|
156 | - esc_html__('A class method for the %s route has not been properly registered.', 'event_espresso'), |
|
157 | - $current_route |
|
158 | - ); |
|
159 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
160 | - return false; |
|
161 | - } |
|
162 | - // grab method |
|
163 | - $method = $module_method[1]; |
|
164 | - // verify that class exists |
|
165 | - if (! class_exists($module_name)) { |
|
166 | - $msg = sprintf(esc_html__('The requested %s class could not be found.', 'event_espresso'), $module_name); |
|
167 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
168 | - return false; |
|
169 | - } |
|
170 | - // verify that method exists |
|
171 | - if (! method_exists($module_name, $method)) { |
|
172 | - $msg = sprintf( |
|
173 | - esc_html__('The class method %s for the %s route is in invalid.', 'event_espresso'), |
|
174 | - $method, |
|
175 | - $current_route |
|
176 | - ); |
|
177 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
178 | - return false; |
|
179 | - } |
|
180 | - // instantiate module and call route method |
|
181 | - return $this->_module_router($module_name, $method); |
|
182 | - } |
|
123 | + /** |
|
124 | + * this method simply takes a valid route, and resolves what module class method the route points to |
|
125 | + * |
|
126 | + * @param string $key |
|
127 | + * @param string $current_route |
|
128 | + * @return EED_Module|object|boolean|null |
|
129 | + * @throws EE_Error |
|
130 | + * @throws ReflectionException |
|
131 | + */ |
|
132 | + public function resolve_route($key, $current_route) |
|
133 | + { |
|
134 | + // get module method that route has been mapped to |
|
135 | + $module_method = EE_Config::get_route($current_route, $key); |
|
136 | + // verify result was returned |
|
137 | + if (empty($module_method)) { |
|
138 | + $msg = sprintf( |
|
139 | + esc_html__('The requested route %s could not be mapped to any registered modules.', 'event_espresso'), |
|
140 | + $current_route |
|
141 | + ); |
|
142 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
143 | + return false; |
|
144 | + } |
|
145 | + // verify that result is an array |
|
146 | + if (! is_array($module_method)) { |
|
147 | + $msg = sprintf(esc_html__('The %s route has not been properly registered.', 'event_espresso'), $current_route); |
|
148 | + EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
149 | + return false; |
|
150 | + } |
|
151 | + // grab module name |
|
152 | + $module_name = $module_method[0]; |
|
153 | + // verify that a class method was registered properly |
|
154 | + if (! isset($module_method[1])) { |
|
155 | + $msg = sprintf( |
|
156 | + esc_html__('A class method for the %s route has not been properly registered.', 'event_espresso'), |
|
157 | + $current_route |
|
158 | + ); |
|
159 | + EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
160 | + return false; |
|
161 | + } |
|
162 | + // grab method |
|
163 | + $method = $module_method[1]; |
|
164 | + // verify that class exists |
|
165 | + if (! class_exists($module_name)) { |
|
166 | + $msg = sprintf(esc_html__('The requested %s class could not be found.', 'event_espresso'), $module_name); |
|
167 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
168 | + return false; |
|
169 | + } |
|
170 | + // verify that method exists |
|
171 | + if (! method_exists($module_name, $method)) { |
|
172 | + $msg = sprintf( |
|
173 | + esc_html__('The class method %s for the %s route is in invalid.', 'event_espresso'), |
|
174 | + $method, |
|
175 | + $current_route |
|
176 | + ); |
|
177 | + EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
178 | + return false; |
|
179 | + } |
|
180 | + // instantiate module and call route method |
|
181 | + return $this->_module_router($module_name, $method); |
|
182 | + } |
|
183 | 183 | |
184 | 184 | |
185 | - /** |
|
186 | - * this method instantiates modules and calls the method that was defined when the route was registered |
|
187 | - * |
|
188 | - * @param string $module_name |
|
189 | - * @return EED_Module|object|null |
|
190 | - */ |
|
191 | - public static function module_factory($module_name) |
|
192 | - { |
|
193 | - if ($module_name === 'EED_Module') { |
|
194 | - EE_Error::add_error( |
|
195 | - sprintf( |
|
196 | - esc_html__( |
|
197 | - 'EED_Module is an abstract parent class an can not be instantiated. Please provide a proper module name.', |
|
198 | - 'event_espresso' |
|
199 | - ), |
|
200 | - $module_name |
|
201 | - ), |
|
202 | - __FILE__, |
|
203 | - __FUNCTION__, |
|
204 | - __LINE__ |
|
205 | - ); |
|
206 | - return null; |
|
207 | - } |
|
208 | - // instantiate module class |
|
209 | - $module = new $module_name(); |
|
210 | - // ensure that class is actually a module |
|
211 | - if (! $module instanceof EED_Module) { |
|
212 | - EE_Error::add_error( |
|
213 | - sprintf(esc_html__('The requested %s module is not of the class EED_Module.', 'event_espresso'), $module_name), |
|
214 | - __FILE__, |
|
215 | - __FUNCTION__, |
|
216 | - __LINE__ |
|
217 | - ); |
|
218 | - return null; |
|
219 | - } |
|
220 | - return $module; |
|
221 | - } |
|
185 | + /** |
|
186 | + * this method instantiates modules and calls the method that was defined when the route was registered |
|
187 | + * |
|
188 | + * @param string $module_name |
|
189 | + * @return EED_Module|object|null |
|
190 | + */ |
|
191 | + public static function module_factory($module_name) |
|
192 | + { |
|
193 | + if ($module_name === 'EED_Module') { |
|
194 | + EE_Error::add_error( |
|
195 | + sprintf( |
|
196 | + esc_html__( |
|
197 | + 'EED_Module is an abstract parent class an can not be instantiated. Please provide a proper module name.', |
|
198 | + 'event_espresso' |
|
199 | + ), |
|
200 | + $module_name |
|
201 | + ), |
|
202 | + __FILE__, |
|
203 | + __FUNCTION__, |
|
204 | + __LINE__ |
|
205 | + ); |
|
206 | + return null; |
|
207 | + } |
|
208 | + // instantiate module class |
|
209 | + $module = new $module_name(); |
|
210 | + // ensure that class is actually a module |
|
211 | + if (! $module instanceof EED_Module) { |
|
212 | + EE_Error::add_error( |
|
213 | + sprintf(esc_html__('The requested %s module is not of the class EED_Module.', 'event_espresso'), $module_name), |
|
214 | + __FILE__, |
|
215 | + __FUNCTION__, |
|
216 | + __LINE__ |
|
217 | + ); |
|
218 | + return null; |
|
219 | + } |
|
220 | + return $module; |
|
221 | + } |
|
222 | 222 | |
223 | 223 | |
224 | - /** |
|
225 | - * this method instantiates modules and calls the method that was defined when the route was registered |
|
226 | - * |
|
227 | - * @param string $module_name |
|
228 | - * @param string $method |
|
229 | - * @return EED_Module|null |
|
230 | - * @throws EE_Error |
|
231 | - * @throws ReflectionException |
|
232 | - */ |
|
233 | - private function _module_router($module_name, $method) |
|
234 | - { |
|
235 | - // instantiate module class |
|
236 | - $module = EE_Module_Request_Router::module_factory($module_name); |
|
237 | - if ($module instanceof EED_Module) { |
|
238 | - // and call whatever action the route was for |
|
239 | - try { |
|
240 | - $module->{$method}($this->WP_Query); |
|
241 | - } catch (EE_Error $e) { |
|
242 | - $e->get_error(); |
|
243 | - return null; |
|
244 | - } |
|
245 | - } |
|
246 | - return $module; |
|
247 | - } |
|
224 | + /** |
|
225 | + * this method instantiates modules and calls the method that was defined when the route was registered |
|
226 | + * |
|
227 | + * @param string $module_name |
|
228 | + * @param string $method |
|
229 | + * @return EED_Module|null |
|
230 | + * @throws EE_Error |
|
231 | + * @throws ReflectionException |
|
232 | + */ |
|
233 | + private function _module_router($module_name, $method) |
|
234 | + { |
|
235 | + // instantiate module class |
|
236 | + $module = EE_Module_Request_Router::module_factory($module_name); |
|
237 | + if ($module instanceof EED_Module) { |
|
238 | + // and call whatever action the route was for |
|
239 | + try { |
|
240 | + $module->{$method}($this->WP_Query); |
|
241 | + } catch (EE_Error $e) { |
|
242 | + $e->get_error(); |
|
243 | + return null; |
|
244 | + } |
|
245 | + } |
|
246 | + return $module; |
|
247 | + } |
|
248 | 248 | |
249 | 249 | |
250 | - /** |
|
251 | - * @param $current_route |
|
252 | - * @return string |
|
253 | - */ |
|
254 | - public function get_forward($current_route) |
|
255 | - { |
|
256 | - return EE_Config::get_forward($current_route); |
|
257 | - } |
|
250 | + /** |
|
251 | + * @param $current_route |
|
252 | + * @return string |
|
253 | + */ |
|
254 | + public function get_forward($current_route) |
|
255 | + { |
|
256 | + return EE_Config::get_forward($current_route); |
|
257 | + } |
|
258 | 258 | |
259 | 259 | |
260 | - /** |
|
261 | - * @param $current_route |
|
262 | - * @return string |
|
263 | - */ |
|
264 | - public function get_view($current_route) |
|
265 | - { |
|
266 | - return EE_Config::get_view($current_route); |
|
267 | - } |
|
260 | + /** |
|
261 | + * @param $current_route |
|
262 | + * @return string |
|
263 | + */ |
|
264 | + public function get_view($current_route) |
|
265 | + { |
|
266 | + return EE_Config::get_view($current_route); |
|
267 | + } |
|
268 | 268 | } |
@@ -42,7 +42,7 @@ |
||
42 | 42 | */ |
43 | 43 | public function set_group_template_id($GRP_ID = false) |
44 | 44 | { |
45 | - if (! $GRP_ID) { |
|
45 | + if ( ! $GRP_ID) { |
|
46 | 46 | throw new EE_Error(esc_html__('Missing required value for the message template group id', 'event_espresso')); |
47 | 47 | } |
48 | 48 | $this->set('GRP_ID', $GRP_ID); |
@@ -12,173 +12,173 @@ |
||
12 | 12 | */ |
13 | 13 | class EE_Message_Template extends EE_Base_Class |
14 | 14 | { |
15 | - /** |
|
16 | - * @param array $props_n_values |
|
17 | - * @param string $timezone |
|
18 | - * @return EE_Message_Template|mixed |
|
19 | - */ |
|
20 | - public static function new_instance($props_n_values = array(), $timezone = '') |
|
21 | - { |
|
22 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone); |
|
23 | - return $has_object ? $has_object : new self($props_n_values, false, $timezone); |
|
24 | - } |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * @param array $props_n_values |
|
29 | - * @param string $timezone |
|
30 | - * @return EE_Message_Template |
|
31 | - */ |
|
32 | - public static function new_instance_from_db($props_n_values = array(), $timezone = '') |
|
33 | - { |
|
34 | - return new self($props_n_values, true, $timezone); |
|
35 | - } |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * @param bool $GRP_ID |
|
40 | - * @throws EE_Error |
|
41 | - */ |
|
42 | - public function set_group_template_id($GRP_ID = false) |
|
43 | - { |
|
44 | - if (! $GRP_ID) { |
|
45 | - throw new EE_Error(esc_html__('Missing required value for the message template group id', 'event_espresso')); |
|
46 | - } |
|
47 | - $this->set('GRP_ID', $GRP_ID); |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * get Group ID |
|
53 | - * |
|
54 | - * @access public |
|
55 | - * @return int |
|
56 | - */ |
|
57 | - public function GRP_ID() |
|
58 | - { |
|
59 | - return $this->get('GRP_ID'); |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * get User ID |
|
65 | - * |
|
66 | - * @access public |
|
67 | - * @return int |
|
68 | - */ |
|
69 | - public function user() |
|
70 | - { |
|
71 | - return $this->get_first_related('Message_Template_Group')->get('MTP_user_id'); |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * get Message Messenger |
|
77 | - * |
|
78 | - * @access public |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - public function messenger() |
|
82 | - { |
|
83 | - return $this->get_first_related('Message_Template_Group')->messenger(); |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * get Message Messenger OBJECT |
|
89 | - * |
|
90 | - * @access public |
|
91 | - * @return object Messenger Object for the given messenger |
|
92 | - */ |
|
93 | - public function messenger_obj() |
|
94 | - { |
|
95 | - return $this->get_first_related('Message_Template_Group')->messenger_obj(); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * get Message Type |
|
101 | - * |
|
102 | - * @access public |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - public function message_type() |
|
106 | - { |
|
107 | - return $this->get_first_related('Message_Template_Group')->message_type(); |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - /** |
|
112 | - * get Message type OBJECT |
|
113 | - * |
|
114 | - * @access public |
|
115 | - * @return object Message Type object for the given message type |
|
116 | - */ |
|
117 | - public function message_type_obj() |
|
118 | - { |
|
119 | - return $this->get_first_related('Message_Template_Group')->message_type_obj(); |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * This returns the set context array configured in the message type object |
|
125 | - * |
|
126 | - * @access public |
|
127 | - * @return array array of contexts and their configuration. |
|
128 | - */ |
|
129 | - public function contexts_config() |
|
130 | - { |
|
131 | - return $this->get_first_related('Message_Template_Group')->contexts_config(); |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * This returns the context_label for contexts as set in the message type object |
|
137 | - * |
|
138 | - * @access public |
|
139 | - * @return string label for "context" |
|
140 | - */ |
|
141 | - public function context_label() |
|
142 | - { |
|
143 | - return $this->get_first_related('Message_Template_Group')->context_label(); |
|
144 | - } |
|
145 | - |
|
146 | - |
|
147 | - /** |
|
148 | - * this returns if the template group this template belongs to is global |
|
149 | - * |
|
150 | - * @return boolean true if it is, false if it isn't |
|
151 | - */ |
|
152 | - public function is_global() |
|
153 | - { |
|
154 | - return $this->get_first_related('Message_Template_Group')->is_global(); |
|
155 | - } |
|
156 | - |
|
157 | - |
|
158 | - /** |
|
159 | - * this returns if the template group this template belongs to is active (i.e. turned "on" or not) |
|
160 | - * |
|
161 | - * @return boolean true if it is, false if it isn't |
|
162 | - */ |
|
163 | - public function is_active() |
|
164 | - { |
|
165 | - return $this->get_first_related('Message_Template_Group')->is_active(); |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * This will return an array of shortcodes => labels from the messenger and message_type objects associated with |
|
171 | - * this template. |
|
172 | - * |
|
173 | - * @access public |
|
174 | - * @param string $context what context we're going to return shortcodes for |
|
175 | - * @param array $fields what fields we're returning valid shortcodes for. If empty then we assume all fields are |
|
176 | - * to be merged and returned. |
|
177 | - * @return mixed (array|bool) an array of shortcodes in the format array( '[shortcode] => 'label') OR FALSE if no |
|
178 | - * shortcodes found. |
|
179 | - */ |
|
180 | - public function get_shortcodes($context, $fields = array()) |
|
181 | - { |
|
182 | - return $this->get_first_related('Message_Template_Group')->get_shortcodes($context, $fields); |
|
183 | - } |
|
15 | + /** |
|
16 | + * @param array $props_n_values |
|
17 | + * @param string $timezone |
|
18 | + * @return EE_Message_Template|mixed |
|
19 | + */ |
|
20 | + public static function new_instance($props_n_values = array(), $timezone = '') |
|
21 | + { |
|
22 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone); |
|
23 | + return $has_object ? $has_object : new self($props_n_values, false, $timezone); |
|
24 | + } |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * @param array $props_n_values |
|
29 | + * @param string $timezone |
|
30 | + * @return EE_Message_Template |
|
31 | + */ |
|
32 | + public static function new_instance_from_db($props_n_values = array(), $timezone = '') |
|
33 | + { |
|
34 | + return new self($props_n_values, true, $timezone); |
|
35 | + } |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * @param bool $GRP_ID |
|
40 | + * @throws EE_Error |
|
41 | + */ |
|
42 | + public function set_group_template_id($GRP_ID = false) |
|
43 | + { |
|
44 | + if (! $GRP_ID) { |
|
45 | + throw new EE_Error(esc_html__('Missing required value for the message template group id', 'event_espresso')); |
|
46 | + } |
|
47 | + $this->set('GRP_ID', $GRP_ID); |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * get Group ID |
|
53 | + * |
|
54 | + * @access public |
|
55 | + * @return int |
|
56 | + */ |
|
57 | + public function GRP_ID() |
|
58 | + { |
|
59 | + return $this->get('GRP_ID'); |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * get User ID |
|
65 | + * |
|
66 | + * @access public |
|
67 | + * @return int |
|
68 | + */ |
|
69 | + public function user() |
|
70 | + { |
|
71 | + return $this->get_first_related('Message_Template_Group')->get('MTP_user_id'); |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * get Message Messenger |
|
77 | + * |
|
78 | + * @access public |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + public function messenger() |
|
82 | + { |
|
83 | + return $this->get_first_related('Message_Template_Group')->messenger(); |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * get Message Messenger OBJECT |
|
89 | + * |
|
90 | + * @access public |
|
91 | + * @return object Messenger Object for the given messenger |
|
92 | + */ |
|
93 | + public function messenger_obj() |
|
94 | + { |
|
95 | + return $this->get_first_related('Message_Template_Group')->messenger_obj(); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * get Message Type |
|
101 | + * |
|
102 | + * @access public |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + public function message_type() |
|
106 | + { |
|
107 | + return $this->get_first_related('Message_Template_Group')->message_type(); |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + /** |
|
112 | + * get Message type OBJECT |
|
113 | + * |
|
114 | + * @access public |
|
115 | + * @return object Message Type object for the given message type |
|
116 | + */ |
|
117 | + public function message_type_obj() |
|
118 | + { |
|
119 | + return $this->get_first_related('Message_Template_Group')->message_type_obj(); |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * This returns the set context array configured in the message type object |
|
125 | + * |
|
126 | + * @access public |
|
127 | + * @return array array of contexts and their configuration. |
|
128 | + */ |
|
129 | + public function contexts_config() |
|
130 | + { |
|
131 | + return $this->get_first_related('Message_Template_Group')->contexts_config(); |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * This returns the context_label for contexts as set in the message type object |
|
137 | + * |
|
138 | + * @access public |
|
139 | + * @return string label for "context" |
|
140 | + */ |
|
141 | + public function context_label() |
|
142 | + { |
|
143 | + return $this->get_first_related('Message_Template_Group')->context_label(); |
|
144 | + } |
|
145 | + |
|
146 | + |
|
147 | + /** |
|
148 | + * this returns if the template group this template belongs to is global |
|
149 | + * |
|
150 | + * @return boolean true if it is, false if it isn't |
|
151 | + */ |
|
152 | + public function is_global() |
|
153 | + { |
|
154 | + return $this->get_first_related('Message_Template_Group')->is_global(); |
|
155 | + } |
|
156 | + |
|
157 | + |
|
158 | + /** |
|
159 | + * this returns if the template group this template belongs to is active (i.e. turned "on" or not) |
|
160 | + * |
|
161 | + * @return boolean true if it is, false if it isn't |
|
162 | + */ |
|
163 | + public function is_active() |
|
164 | + { |
|
165 | + return $this->get_first_related('Message_Template_Group')->is_active(); |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * This will return an array of shortcodes => labels from the messenger and message_type objects associated with |
|
171 | + * this template. |
|
172 | + * |
|
173 | + * @access public |
|
174 | + * @param string $context what context we're going to return shortcodes for |
|
175 | + * @param array $fields what fields we're returning valid shortcodes for. If empty then we assume all fields are |
|
176 | + * to be merged and returned. |
|
177 | + * @return mixed (array|bool) an array of shortcodes in the format array( '[shortcode] => 'label') OR FALSE if no |
|
178 | + * shortcodes found. |
|
179 | + */ |
|
180 | + public function get_shortcodes($context, $fields = array()) |
|
181 | + { |
|
182 | + return $this->get_first_related('Message_Template_Group')->get_shortcodes($context, $fields); |
|
183 | + } |
|
184 | 184 | } |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | public static function validateType($type) |
69 | 69 | { |
70 | 70 | $types = CoffeeMaker::getTypes(); |
71 | - if (! in_array($type, $types, true)) { |
|
71 | + if ( ! in_array($type, $types, true)) { |
|
72 | 72 | throw new InvalidIdentifierException( |
73 | 73 | is_object($type) ? get_class($type) : gettype($type), |
74 | 74 | esc_html__( |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | protected function resolveClassAndFilepath(RecipeInterface $recipe) |
152 | 152 | { |
153 | 153 | $paths = $recipe->paths(); |
154 | - if (! empty($paths)) { |
|
154 | + if ( ! empty($paths)) { |
|
155 | 155 | foreach ($paths as $path) { |
156 | 156 | if (strpos($path, '*') === false && is_readable($path)) { |
157 | 157 | require_once($path); |
@@ -17,156 +17,156 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class CoffeeMaker implements CoffeeMakerInterface |
19 | 19 | { |
20 | - /** |
|
21 | - * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given) |
|
22 | - */ |
|
23 | - const BREW_NEW = 'new'; |
|
24 | - |
|
25 | - /** |
|
26 | - * Indicates that CoffeeMaker should always return a SHARED instance |
|
27 | - */ |
|
28 | - const BREW_SHARED = 'shared'; |
|
29 | - |
|
30 | - /** |
|
31 | - * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate |
|
32 | - */ |
|
33 | - const BREW_LOAD_ONLY = 'load_only'; |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * @var CoffeePotInterface $coffee_pot |
|
38 | - */ |
|
39 | - private $coffee_pot; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var DependencyInjector $injector |
|
43 | - */ |
|
44 | - private $injector; |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * @return array |
|
49 | - */ |
|
50 | - public static function getTypes() |
|
51 | - { |
|
52 | - return (array) apply_filters( |
|
53 | - 'FHEE__EventEspresso\core\services\container\CoffeeMaker__getTypes', |
|
54 | - array( |
|
55 | - CoffeeMaker::BREW_NEW, |
|
56 | - CoffeeMaker::BREW_SHARED, |
|
57 | - CoffeeMaker::BREW_LOAD_ONLY, |
|
58 | - ) |
|
59 | - ); |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * @param $type |
|
65 | - * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
66 | - */ |
|
67 | - public static function validateType($type) |
|
68 | - { |
|
69 | - $types = CoffeeMaker::getTypes(); |
|
70 | - if (! in_array($type, $types, true)) { |
|
71 | - throw new InvalidIdentifierException( |
|
72 | - is_object($type) ? get_class($type) : gettype($type), |
|
73 | - esc_html__( |
|
74 | - 'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)', |
|
75 | - 'event_espresso' |
|
76 | - ) |
|
77 | - ); |
|
78 | - } |
|
79 | - return $type; |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * CoffeeMaker constructor. |
|
85 | - * |
|
86 | - * @param CoffeePotInterface $coffee_pot |
|
87 | - * @param InjectorInterface $injector |
|
88 | - */ |
|
89 | - public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector) |
|
90 | - { |
|
91 | - $this->coffee_pot = $coffee_pot; |
|
92 | - $this->injector = $injector; |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * @return \EventEspresso\core\services\container\CoffeePotInterface |
|
98 | - */ |
|
99 | - protected function coffeePot() |
|
100 | - { |
|
101 | - return $this->coffee_pot; |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * @return \EventEspresso\core\services\container\DependencyInjector |
|
107 | - */ |
|
108 | - protected function injector() |
|
109 | - { |
|
110 | - return $this->injector; |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * Examines the constructor to determine which method should be used for instantiation |
|
116 | - * |
|
117 | - * @param \ReflectionClass $reflector |
|
118 | - * @return mixed |
|
119 | - * @throws InstantiationException |
|
120 | - */ |
|
121 | - protected function resolveInstantiationMethod(\ReflectionClass $reflector) |
|
122 | - { |
|
123 | - if ($reflector->getConstructor() === null) { |
|
124 | - return 'NewInstance'; |
|
125 | - } |
|
126 | - if ($reflector->isInstantiable()) { |
|
127 | - return 'NewInstanceArgs'; |
|
128 | - } |
|
129 | - if (method_exists($reflector->getName(), 'instance')) { |
|
130 | - return 'instance'; |
|
131 | - } |
|
132 | - if (method_exists($reflector->getName(), 'new_instance')) { |
|
133 | - return 'new_instance'; |
|
134 | - } |
|
135 | - if (method_exists($reflector->getName(), 'new_instance_from_db')) { |
|
136 | - return 'new_instance_from_db'; |
|
137 | - } |
|
138 | - throw new InstantiationException($reflector->getName()); |
|
139 | - } |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * Ensures files for classes that are not PSR-4 compatible are loaded |
|
144 | - * and then verifies that classes exist where applicable |
|
145 | - * |
|
146 | - * @param RecipeInterface $recipe |
|
147 | - * @return bool |
|
148 | - * @throws InvalidClassException |
|
149 | - */ |
|
150 | - protected function resolveClassAndFilepath(RecipeInterface $recipe) |
|
151 | - { |
|
152 | - $paths = $recipe->paths(); |
|
153 | - if (! empty($paths)) { |
|
154 | - foreach ($paths as $path) { |
|
155 | - if (strpos($path, '*') === false && is_readable($path)) { |
|
156 | - require_once($path); |
|
157 | - } |
|
158 | - } |
|
159 | - } |
|
160 | - // re: using "false" for class_exists() second param: |
|
161 | - // if a class name is not already known to PHP, then class_exists() will run through |
|
162 | - // all of the registered spl_autoload functions until it either finds the class, |
|
163 | - // or gets to the end of the registered spl_autoload functions. |
|
164 | - // When the second parameter is true, it will also attempt to load the class file, |
|
165 | - // but it will also trigger an error if the class can not be loaded. |
|
166 | - // We don't want that extra error in the mix, so we have set the second param to "false" |
|
167 | - if ($recipe->type() !== CoffeeMaker::BREW_LOAD_ONLY && ! class_exists($recipe->fqcn(), false)) { |
|
168 | - throw new InvalidClassException($recipe->identifier()); |
|
169 | - } |
|
170 | - return true; |
|
171 | - } |
|
20 | + /** |
|
21 | + * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given) |
|
22 | + */ |
|
23 | + const BREW_NEW = 'new'; |
|
24 | + |
|
25 | + /** |
|
26 | + * Indicates that CoffeeMaker should always return a SHARED instance |
|
27 | + */ |
|
28 | + const BREW_SHARED = 'shared'; |
|
29 | + |
|
30 | + /** |
|
31 | + * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate |
|
32 | + */ |
|
33 | + const BREW_LOAD_ONLY = 'load_only'; |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * @var CoffeePotInterface $coffee_pot |
|
38 | + */ |
|
39 | + private $coffee_pot; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var DependencyInjector $injector |
|
43 | + */ |
|
44 | + private $injector; |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * @return array |
|
49 | + */ |
|
50 | + public static function getTypes() |
|
51 | + { |
|
52 | + return (array) apply_filters( |
|
53 | + 'FHEE__EventEspresso\core\services\container\CoffeeMaker__getTypes', |
|
54 | + array( |
|
55 | + CoffeeMaker::BREW_NEW, |
|
56 | + CoffeeMaker::BREW_SHARED, |
|
57 | + CoffeeMaker::BREW_LOAD_ONLY, |
|
58 | + ) |
|
59 | + ); |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * @param $type |
|
65 | + * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
66 | + */ |
|
67 | + public static function validateType($type) |
|
68 | + { |
|
69 | + $types = CoffeeMaker::getTypes(); |
|
70 | + if (! in_array($type, $types, true)) { |
|
71 | + throw new InvalidIdentifierException( |
|
72 | + is_object($type) ? get_class($type) : gettype($type), |
|
73 | + esc_html__( |
|
74 | + 'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)', |
|
75 | + 'event_espresso' |
|
76 | + ) |
|
77 | + ); |
|
78 | + } |
|
79 | + return $type; |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * CoffeeMaker constructor. |
|
85 | + * |
|
86 | + * @param CoffeePotInterface $coffee_pot |
|
87 | + * @param InjectorInterface $injector |
|
88 | + */ |
|
89 | + public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector) |
|
90 | + { |
|
91 | + $this->coffee_pot = $coffee_pot; |
|
92 | + $this->injector = $injector; |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * @return \EventEspresso\core\services\container\CoffeePotInterface |
|
98 | + */ |
|
99 | + protected function coffeePot() |
|
100 | + { |
|
101 | + return $this->coffee_pot; |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * @return \EventEspresso\core\services\container\DependencyInjector |
|
107 | + */ |
|
108 | + protected function injector() |
|
109 | + { |
|
110 | + return $this->injector; |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * Examines the constructor to determine which method should be used for instantiation |
|
116 | + * |
|
117 | + * @param \ReflectionClass $reflector |
|
118 | + * @return mixed |
|
119 | + * @throws InstantiationException |
|
120 | + */ |
|
121 | + protected function resolveInstantiationMethod(\ReflectionClass $reflector) |
|
122 | + { |
|
123 | + if ($reflector->getConstructor() === null) { |
|
124 | + return 'NewInstance'; |
|
125 | + } |
|
126 | + if ($reflector->isInstantiable()) { |
|
127 | + return 'NewInstanceArgs'; |
|
128 | + } |
|
129 | + if (method_exists($reflector->getName(), 'instance')) { |
|
130 | + return 'instance'; |
|
131 | + } |
|
132 | + if (method_exists($reflector->getName(), 'new_instance')) { |
|
133 | + return 'new_instance'; |
|
134 | + } |
|
135 | + if (method_exists($reflector->getName(), 'new_instance_from_db')) { |
|
136 | + return 'new_instance_from_db'; |
|
137 | + } |
|
138 | + throw new InstantiationException($reflector->getName()); |
|
139 | + } |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * Ensures files for classes that are not PSR-4 compatible are loaded |
|
144 | + * and then verifies that classes exist where applicable |
|
145 | + * |
|
146 | + * @param RecipeInterface $recipe |
|
147 | + * @return bool |
|
148 | + * @throws InvalidClassException |
|
149 | + */ |
|
150 | + protected function resolveClassAndFilepath(RecipeInterface $recipe) |
|
151 | + { |
|
152 | + $paths = $recipe->paths(); |
|
153 | + if (! empty($paths)) { |
|
154 | + foreach ($paths as $path) { |
|
155 | + if (strpos($path, '*') === false && is_readable($path)) { |
|
156 | + require_once($path); |
|
157 | + } |
|
158 | + } |
|
159 | + } |
|
160 | + // re: using "false" for class_exists() second param: |
|
161 | + // if a class name is not already known to PHP, then class_exists() will run through |
|
162 | + // all of the registered spl_autoload functions until it either finds the class, |
|
163 | + // or gets to the end of the registered spl_autoload functions. |
|
164 | + // When the second parameter is true, it will also attempt to load the class file, |
|
165 | + // but it will also trigger an error if the class can not be loaded. |
|
166 | + // We don't want that extra error in the mix, so we have set the second param to "false" |
|
167 | + if ($recipe->type() !== CoffeeMaker::BREW_LOAD_ONLY && ! class_exists($recipe->fqcn(), false)) { |
|
168 | + throw new InvalidClassException($recipe->identifier()); |
|
169 | + } |
|
170 | + return true; |
|
171 | + } |
|
172 | 172 | } |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | */ |
190 | 190 | public function setIdentifier($identifier) |
191 | 191 | { |
192 | - if (! is_string($identifier) || empty($identifier)) { |
|
192 | + if ( ! is_string($identifier) || empty($identifier)) { |
|
193 | 193 | throw new InvalidIdentifierException( |
194 | 194 | is_object($identifier) ? get_class($identifier) : gettype($identifier), |
195 | 195 | esc_html__('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso') |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | public function setFqcn($fqcn) |
217 | 217 | { |
218 | 218 | $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier; |
219 | - if (! is_string($fqcn)) { |
|
219 | + if ( ! is_string($fqcn)) { |
|
220 | 220 | throw new InvalidDataTypeException( |
221 | 221 | '$fqcn', |
222 | 222 | is_object($fqcn) ? get_class($fqcn) : gettype($fqcn), |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | if (empty($ingredients)) { |
248 | 248 | return; |
249 | 249 | } |
250 | - if (! is_array($ingredients)) { |
|
250 | + if ( ! is_array($ingredients)) { |
|
251 | 251 | throw new InvalidDataTypeException( |
252 | 252 | '$ingredients', |
253 | 253 | is_object($ingredients) ? get_class($ingredients) : gettype($ingredients), |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | if (empty($filters)) { |
280 | 280 | return; |
281 | 281 | } |
282 | - if (! is_array($filters)) { |
|
282 | + if ( ! is_array($filters)) { |
|
283 | 283 | throw new InvalidDataTypeException( |
284 | 284 | '$filters', |
285 | 285 | is_object($filters) ? get_class($filters) : gettype($filters), |
@@ -306,7 +306,7 @@ discard block |
||
306 | 306 | if (empty($paths)) { |
307 | 307 | return; |
308 | 308 | } |
309 | - if (! (is_string($paths) || is_array($paths))) { |
|
309 | + if ( ! (is_string($paths) || is_array($paths))) { |
|
310 | 310 | throw new InvalidDataTypeException( |
311 | 311 | '$path', |
312 | 312 | is_object($paths) ? get_class($paths) : gettype($paths), |
@@ -18,311 +18,311 @@ |
||
18 | 18 | */ |
19 | 19 | class Recipe implements RecipeInterface |
20 | 20 | { |
21 | - /** |
|
22 | - * A default Recipe to use if none is specified for a class |
|
23 | - */ |
|
24 | - const DEFAULT_ID = '*'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Identifier for the entity class to be constructed. |
|
28 | - * Typically a Fully Qualified Class Name |
|
29 | - * |
|
30 | - * @var string $identifier |
|
31 | - */ |
|
32 | - private $identifier; |
|
33 | - |
|
34 | - /** |
|
35 | - * Fully Qualified Class Name |
|
36 | - * |
|
37 | - * @var string $fqcn |
|
38 | - */ |
|
39 | - private $fqcn; |
|
40 | - |
|
41 | - /** |
|
42 | - * a dependency class map array |
|
43 | - * If a Recipe is for a single class (or group of classes that shares the EXACT SAME constructor arguments), |
|
44 | - * and that class type hints for an interface, then this property allows you to configure what dependencies |
|
45 | - * get used when instantiating the class. |
|
46 | - * For example: |
|
47 | - * There's a class called Coffee, and one of its constructor arguments is BeanInterface |
|
48 | - * There are two implementations of BeanInterface: HonduranBean, and KenyanBean |
|
49 | - * We want one Coffee object to use HonduranBean for its BeanInterface, |
|
50 | - * and the 2nd Coffee object to use KenyanBean for its BeanInterface. |
|
51 | - * To do this, we need to create two Recipes: |
|
52 | - * one with an identifier of 'HonduranCoffee' using the following ingredients : |
|
53 | - * array('BeanInterface' => 'HonduranBean') |
|
54 | - * and the other with an identifier of 'KenyanCoffee' using the following ingredients : |
|
55 | - * array('BeanInterface' => 'KenyanBean') |
|
56 | - * Then, whenever the CoffeeShop brews an instance of HonduranCoffee, |
|
57 | - * an instance of HonduranBean will get injected for the BeanInterface dependency, |
|
58 | - * and whenever the CoffeeShop brews an instance of KenyanCoffee, |
|
59 | - * an instance of KenyanBean will get injected for the BeanInterface dependency |
|
60 | - * |
|
61 | - * @var array $ingredients |
|
62 | - */ |
|
63 | - private $ingredients = array(); |
|
64 | - |
|
65 | - /** |
|
66 | - * one of the class constants from CoffeeShop: |
|
67 | - * CoffeeMaker::BREW_NEW - creates a new instance |
|
68 | - * CoffeeMaker::BREW_SHARED - creates a shared instance |
|
69 | - * CoffeeMaker::BREW_LOAD_ONLY - loads but does not instantiate |
|
70 | - * |
|
71 | - * @var string $type |
|
72 | - */ |
|
73 | - private $type; |
|
74 | - |
|
75 | - /** |
|
76 | - * class name aliases - typically a Fully Qualified Interface that the class implements |
|
77 | - * identifiers passed to the CoffeeShop will be run through the filters to find the correct class name |
|
78 | - * |
|
79 | - * @var array $filters |
|
80 | - */ |
|
81 | - private $filters = array(); |
|
82 | - |
|
83 | - /** |
|
84 | - * array of full server filepaths to files that may contain the class |
|
85 | - * |
|
86 | - * @var array $paths |
|
87 | - */ |
|
88 | - private $paths = array(); |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Recipe constructor. |
|
93 | - * |
|
94 | - * @param string $identifier class identifier, can be an alias, or FQCN, or whatever |
|
95 | - * @param string $fqcn \Fully\Qualified\ClassName, optional if $identifier is FQCN |
|
96 | - * @param array $ingredients array of dependencies that can not be resolved automatically, |
|
97 | - * used for resolving concrete classes for type hinted interfaces |
|
98 | - * for the dependencies of THIS class |
|
99 | - * @param string $type recipe type: one of the class constants on |
|
100 | - * \EventEspresso\core\services\container\CoffeeMaker |
|
101 | - * @param array $filters array of class aliases, or class interfaces |
|
102 | - * this works somewhat opposite to the $ingredients array above, |
|
103 | - * in that this array specifies interfaces or aliases |
|
104 | - * that this Recipe can be used for when resolving OTHER class's dependencies |
|
105 | - * @param array $paths if class can not be loaded via PSR-4 autoloading, |
|
106 | - * then supply a filepath, or array of filepaths, so that it can be included |
|
107 | - * @throws InvalidIdentifierException |
|
108 | - * @throws RuntimeException |
|
109 | - * @throws InvalidInterfaceException |
|
110 | - * @throws InvalidClassException |
|
111 | - * @throws InvalidDataTypeException |
|
112 | - */ |
|
113 | - public function __construct( |
|
114 | - $identifier, |
|
115 | - $fqcn = '', |
|
116 | - array $filters = array(), |
|
117 | - array $ingredients = array(), |
|
118 | - $type = CoffeeMaker::BREW_NEW, |
|
119 | - array $paths = array() |
|
120 | - ) { |
|
121 | - $this->setIdentifier($identifier); |
|
122 | - $this->setFilters($filters); |
|
123 | - $this->setIngredients($ingredients); |
|
124 | - $this->setType($type); |
|
125 | - $this->setPaths($paths); |
|
126 | - $this->setFqcn($fqcn); |
|
127 | - } |
|
128 | - |
|
129 | - |
|
130 | - /** |
|
131 | - * @return string |
|
132 | - */ |
|
133 | - public function identifier() |
|
134 | - { |
|
135 | - return $this->identifier; |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * @return string |
|
141 | - */ |
|
142 | - public function fqcn() |
|
143 | - { |
|
144 | - return $this->fqcn; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * @return array |
|
150 | - */ |
|
151 | - public function filters() |
|
152 | - { |
|
153 | - return $this->filters; |
|
154 | - } |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * @return array |
|
159 | - */ |
|
160 | - public function ingredients() |
|
161 | - { |
|
162 | - return $this->ingredients; |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * @return string |
|
168 | - */ |
|
169 | - public function type() |
|
170 | - { |
|
171 | - return $this->type; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * @return array |
|
177 | - */ |
|
178 | - public function paths() |
|
179 | - { |
|
180 | - return $this->paths; |
|
181 | - } |
|
182 | - |
|
183 | - |
|
184 | - /** |
|
185 | - * @param string $identifier Identifier for the entity class that the Recipe applies to |
|
186 | - * Typically a Fully Qualified Class Name |
|
187 | - * @throws InvalidIdentifierException |
|
188 | - */ |
|
189 | - public function setIdentifier($identifier) |
|
190 | - { |
|
191 | - if (! is_string($identifier) || empty($identifier)) { |
|
192 | - throw new InvalidIdentifierException( |
|
193 | - is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
194 | - esc_html__('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso') |
|
195 | - ); |
|
196 | - } |
|
197 | - $this->identifier = $identifier; |
|
198 | - } |
|
199 | - |
|
200 | - |
|
201 | - /** |
|
202 | - * Ensures incoming string is a valid Fully Qualified Class Name, |
|
203 | - * except if this is the default wildcard Recipe ( * ), |
|
204 | - * or it's NOT an actual FQCN because the Recipe is using filepaths |
|
205 | - * for classes that are not PSR-4 compatible |
|
206 | - * PLZ NOTE: |
|
207 | - * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
208 | - * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
209 | - * |
|
210 | - * @param string $fqcn |
|
211 | - * @throws InvalidDataTypeException |
|
212 | - * @throws InvalidClassException |
|
213 | - * @throws InvalidInterfaceException |
|
214 | - */ |
|
215 | - public function setFqcn($fqcn) |
|
216 | - { |
|
217 | - $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier; |
|
218 | - if (! is_string($fqcn)) { |
|
219 | - throw new InvalidDataTypeException( |
|
220 | - '$fqcn', |
|
221 | - is_object($fqcn) ? get_class($fqcn) : gettype($fqcn), |
|
222 | - esc_html__('string (Fully\Qualified\ClassName)', 'event_espresso') |
|
223 | - ); |
|
224 | - } |
|
225 | - $fqcn = ltrim($fqcn, '\\'); |
|
226 | - if ( |
|
227 | - $fqcn !== Recipe::DEFAULT_ID |
|
228 | - && ! empty($fqcn) |
|
229 | - && empty($this->paths) |
|
230 | - && ! (class_exists($fqcn) || interface_exists($fqcn)) |
|
231 | - ) { |
|
232 | - throw new InvalidClassException($fqcn); |
|
233 | - } |
|
234 | - $this->fqcn = $fqcn; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * @param array $ingredients an array of dependencies where keys are the aliases and values are the FQCNs |
|
240 | - * example: |
|
241 | - * array( 'ClassInterface' => 'Fully\Qualified\ClassName' ) |
|
242 | - * @throws InvalidDataTypeException |
|
243 | - */ |
|
244 | - public function setIngredients(array $ingredients) |
|
245 | - { |
|
246 | - if (empty($ingredients)) { |
|
247 | - return; |
|
248 | - } |
|
249 | - if (! is_array($ingredients)) { |
|
250 | - throw new InvalidDataTypeException( |
|
251 | - '$ingredients', |
|
252 | - is_object($ingredients) ? get_class($ingredients) : gettype($ingredients), |
|
253 | - esc_html__('array of class dependencies', 'event_espresso') |
|
254 | - ); |
|
255 | - } |
|
256 | - $this->ingredients = array_merge($this->ingredients, $ingredients); |
|
257 | - } |
|
258 | - |
|
259 | - |
|
260 | - /** |
|
261 | - * @param string $type one of the class constants returned from CoffeeMaker::getTypes() |
|
262 | - * @throws InvalidIdentifierException |
|
263 | - */ |
|
264 | - public function setType($type = CoffeeMaker::BREW_NEW) |
|
265 | - { |
|
266 | - $this->type = CoffeeMaker::validateType($type); |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * @param array $filters an array of filters where keys are the aliases and values are the FQCNs |
|
272 | - * example: |
|
273 | - * array( 'ClassInterface' => 'Fully\Qualified\ClassName' ) |
|
274 | - * @throws InvalidDataTypeException |
|
275 | - */ |
|
276 | - public function setFilters(array $filters) |
|
277 | - { |
|
278 | - if (empty($filters)) { |
|
279 | - return; |
|
280 | - } |
|
281 | - if (! is_array($filters)) { |
|
282 | - throw new InvalidDataTypeException( |
|
283 | - '$filters', |
|
284 | - is_object($filters) ? get_class($filters) : gettype($filters), |
|
285 | - esc_html__('array of class aliases', 'event_espresso') |
|
286 | - ); |
|
287 | - } |
|
288 | - $this->filters = array_merge($this->filters, $filters); |
|
289 | - } |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * Ensures incoming paths is a valid filepath, or array of valid filepaths, |
|
294 | - * and merges them in with any existing filepaths |
|
295 | - * PLZ NOTE: |
|
296 | - * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
297 | - * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
298 | - * |
|
299 | - * @param string|array $paths |
|
300 | - * @throws RuntimeException |
|
301 | - * @throws InvalidDataTypeException |
|
302 | - */ |
|
303 | - public function setPaths($paths = array()) |
|
304 | - { |
|
305 | - if (empty($paths)) { |
|
306 | - return; |
|
307 | - } |
|
308 | - if (! (is_string($paths) || is_array($paths))) { |
|
309 | - throw new InvalidDataTypeException( |
|
310 | - '$path', |
|
311 | - is_object($paths) ? get_class($paths) : gettype($paths), |
|
312 | - esc_html__('string or array of strings (full server filepath(s))', 'event_espresso') |
|
313 | - ); |
|
314 | - } |
|
315 | - $paths = (array) $paths; |
|
316 | - foreach ($paths as $path) { |
|
317 | - if (strpos($path, '*') === false && ! is_readable($path)) { |
|
318 | - throw new RuntimeException( |
|
319 | - sprintf( |
|
320 | - esc_html__('The following filepath is not readable: "%1$s"', 'event_espresso'), |
|
321 | - $path |
|
322 | - ) |
|
323 | - ); |
|
324 | - } |
|
325 | - } |
|
326 | - $this->paths = array_merge($this->paths, $paths); |
|
327 | - } |
|
21 | + /** |
|
22 | + * A default Recipe to use if none is specified for a class |
|
23 | + */ |
|
24 | + const DEFAULT_ID = '*'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Identifier for the entity class to be constructed. |
|
28 | + * Typically a Fully Qualified Class Name |
|
29 | + * |
|
30 | + * @var string $identifier |
|
31 | + */ |
|
32 | + private $identifier; |
|
33 | + |
|
34 | + /** |
|
35 | + * Fully Qualified Class Name |
|
36 | + * |
|
37 | + * @var string $fqcn |
|
38 | + */ |
|
39 | + private $fqcn; |
|
40 | + |
|
41 | + /** |
|
42 | + * a dependency class map array |
|
43 | + * If a Recipe is for a single class (or group of classes that shares the EXACT SAME constructor arguments), |
|
44 | + * and that class type hints for an interface, then this property allows you to configure what dependencies |
|
45 | + * get used when instantiating the class. |
|
46 | + * For example: |
|
47 | + * There's a class called Coffee, and one of its constructor arguments is BeanInterface |
|
48 | + * There are two implementations of BeanInterface: HonduranBean, and KenyanBean |
|
49 | + * We want one Coffee object to use HonduranBean for its BeanInterface, |
|
50 | + * and the 2nd Coffee object to use KenyanBean for its BeanInterface. |
|
51 | + * To do this, we need to create two Recipes: |
|
52 | + * one with an identifier of 'HonduranCoffee' using the following ingredients : |
|
53 | + * array('BeanInterface' => 'HonduranBean') |
|
54 | + * and the other with an identifier of 'KenyanCoffee' using the following ingredients : |
|
55 | + * array('BeanInterface' => 'KenyanBean') |
|
56 | + * Then, whenever the CoffeeShop brews an instance of HonduranCoffee, |
|
57 | + * an instance of HonduranBean will get injected for the BeanInterface dependency, |
|
58 | + * and whenever the CoffeeShop brews an instance of KenyanCoffee, |
|
59 | + * an instance of KenyanBean will get injected for the BeanInterface dependency |
|
60 | + * |
|
61 | + * @var array $ingredients |
|
62 | + */ |
|
63 | + private $ingredients = array(); |
|
64 | + |
|
65 | + /** |
|
66 | + * one of the class constants from CoffeeShop: |
|
67 | + * CoffeeMaker::BREW_NEW - creates a new instance |
|
68 | + * CoffeeMaker::BREW_SHARED - creates a shared instance |
|
69 | + * CoffeeMaker::BREW_LOAD_ONLY - loads but does not instantiate |
|
70 | + * |
|
71 | + * @var string $type |
|
72 | + */ |
|
73 | + private $type; |
|
74 | + |
|
75 | + /** |
|
76 | + * class name aliases - typically a Fully Qualified Interface that the class implements |
|
77 | + * identifiers passed to the CoffeeShop will be run through the filters to find the correct class name |
|
78 | + * |
|
79 | + * @var array $filters |
|
80 | + */ |
|
81 | + private $filters = array(); |
|
82 | + |
|
83 | + /** |
|
84 | + * array of full server filepaths to files that may contain the class |
|
85 | + * |
|
86 | + * @var array $paths |
|
87 | + */ |
|
88 | + private $paths = array(); |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Recipe constructor. |
|
93 | + * |
|
94 | + * @param string $identifier class identifier, can be an alias, or FQCN, or whatever |
|
95 | + * @param string $fqcn \Fully\Qualified\ClassName, optional if $identifier is FQCN |
|
96 | + * @param array $ingredients array of dependencies that can not be resolved automatically, |
|
97 | + * used for resolving concrete classes for type hinted interfaces |
|
98 | + * for the dependencies of THIS class |
|
99 | + * @param string $type recipe type: one of the class constants on |
|
100 | + * \EventEspresso\core\services\container\CoffeeMaker |
|
101 | + * @param array $filters array of class aliases, or class interfaces |
|
102 | + * this works somewhat opposite to the $ingredients array above, |
|
103 | + * in that this array specifies interfaces or aliases |
|
104 | + * that this Recipe can be used for when resolving OTHER class's dependencies |
|
105 | + * @param array $paths if class can not be loaded via PSR-4 autoloading, |
|
106 | + * then supply a filepath, or array of filepaths, so that it can be included |
|
107 | + * @throws InvalidIdentifierException |
|
108 | + * @throws RuntimeException |
|
109 | + * @throws InvalidInterfaceException |
|
110 | + * @throws InvalidClassException |
|
111 | + * @throws InvalidDataTypeException |
|
112 | + */ |
|
113 | + public function __construct( |
|
114 | + $identifier, |
|
115 | + $fqcn = '', |
|
116 | + array $filters = array(), |
|
117 | + array $ingredients = array(), |
|
118 | + $type = CoffeeMaker::BREW_NEW, |
|
119 | + array $paths = array() |
|
120 | + ) { |
|
121 | + $this->setIdentifier($identifier); |
|
122 | + $this->setFilters($filters); |
|
123 | + $this->setIngredients($ingredients); |
|
124 | + $this->setType($type); |
|
125 | + $this->setPaths($paths); |
|
126 | + $this->setFqcn($fqcn); |
|
127 | + } |
|
128 | + |
|
129 | + |
|
130 | + /** |
|
131 | + * @return string |
|
132 | + */ |
|
133 | + public function identifier() |
|
134 | + { |
|
135 | + return $this->identifier; |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * @return string |
|
141 | + */ |
|
142 | + public function fqcn() |
|
143 | + { |
|
144 | + return $this->fqcn; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * @return array |
|
150 | + */ |
|
151 | + public function filters() |
|
152 | + { |
|
153 | + return $this->filters; |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * @return array |
|
159 | + */ |
|
160 | + public function ingredients() |
|
161 | + { |
|
162 | + return $this->ingredients; |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * @return string |
|
168 | + */ |
|
169 | + public function type() |
|
170 | + { |
|
171 | + return $this->type; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * @return array |
|
177 | + */ |
|
178 | + public function paths() |
|
179 | + { |
|
180 | + return $this->paths; |
|
181 | + } |
|
182 | + |
|
183 | + |
|
184 | + /** |
|
185 | + * @param string $identifier Identifier for the entity class that the Recipe applies to |
|
186 | + * Typically a Fully Qualified Class Name |
|
187 | + * @throws InvalidIdentifierException |
|
188 | + */ |
|
189 | + public function setIdentifier($identifier) |
|
190 | + { |
|
191 | + if (! is_string($identifier) || empty($identifier)) { |
|
192 | + throw new InvalidIdentifierException( |
|
193 | + is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
194 | + esc_html__('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso') |
|
195 | + ); |
|
196 | + } |
|
197 | + $this->identifier = $identifier; |
|
198 | + } |
|
199 | + |
|
200 | + |
|
201 | + /** |
|
202 | + * Ensures incoming string is a valid Fully Qualified Class Name, |
|
203 | + * except if this is the default wildcard Recipe ( * ), |
|
204 | + * or it's NOT an actual FQCN because the Recipe is using filepaths |
|
205 | + * for classes that are not PSR-4 compatible |
|
206 | + * PLZ NOTE: |
|
207 | + * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
208 | + * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
209 | + * |
|
210 | + * @param string $fqcn |
|
211 | + * @throws InvalidDataTypeException |
|
212 | + * @throws InvalidClassException |
|
213 | + * @throws InvalidInterfaceException |
|
214 | + */ |
|
215 | + public function setFqcn($fqcn) |
|
216 | + { |
|
217 | + $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier; |
|
218 | + if (! is_string($fqcn)) { |
|
219 | + throw new InvalidDataTypeException( |
|
220 | + '$fqcn', |
|
221 | + is_object($fqcn) ? get_class($fqcn) : gettype($fqcn), |
|
222 | + esc_html__('string (Fully\Qualified\ClassName)', 'event_espresso') |
|
223 | + ); |
|
224 | + } |
|
225 | + $fqcn = ltrim($fqcn, '\\'); |
|
226 | + if ( |
|
227 | + $fqcn !== Recipe::DEFAULT_ID |
|
228 | + && ! empty($fqcn) |
|
229 | + && empty($this->paths) |
|
230 | + && ! (class_exists($fqcn) || interface_exists($fqcn)) |
|
231 | + ) { |
|
232 | + throw new InvalidClassException($fqcn); |
|
233 | + } |
|
234 | + $this->fqcn = $fqcn; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * @param array $ingredients an array of dependencies where keys are the aliases and values are the FQCNs |
|
240 | + * example: |
|
241 | + * array( 'ClassInterface' => 'Fully\Qualified\ClassName' ) |
|
242 | + * @throws InvalidDataTypeException |
|
243 | + */ |
|
244 | + public function setIngredients(array $ingredients) |
|
245 | + { |
|
246 | + if (empty($ingredients)) { |
|
247 | + return; |
|
248 | + } |
|
249 | + if (! is_array($ingredients)) { |
|
250 | + throw new InvalidDataTypeException( |
|
251 | + '$ingredients', |
|
252 | + is_object($ingredients) ? get_class($ingredients) : gettype($ingredients), |
|
253 | + esc_html__('array of class dependencies', 'event_espresso') |
|
254 | + ); |
|
255 | + } |
|
256 | + $this->ingredients = array_merge($this->ingredients, $ingredients); |
|
257 | + } |
|
258 | + |
|
259 | + |
|
260 | + /** |
|
261 | + * @param string $type one of the class constants returned from CoffeeMaker::getTypes() |
|
262 | + * @throws InvalidIdentifierException |
|
263 | + */ |
|
264 | + public function setType($type = CoffeeMaker::BREW_NEW) |
|
265 | + { |
|
266 | + $this->type = CoffeeMaker::validateType($type); |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * @param array $filters an array of filters where keys are the aliases and values are the FQCNs |
|
272 | + * example: |
|
273 | + * array( 'ClassInterface' => 'Fully\Qualified\ClassName' ) |
|
274 | + * @throws InvalidDataTypeException |
|
275 | + */ |
|
276 | + public function setFilters(array $filters) |
|
277 | + { |
|
278 | + if (empty($filters)) { |
|
279 | + return; |
|
280 | + } |
|
281 | + if (! is_array($filters)) { |
|
282 | + throw new InvalidDataTypeException( |
|
283 | + '$filters', |
|
284 | + is_object($filters) ? get_class($filters) : gettype($filters), |
|
285 | + esc_html__('array of class aliases', 'event_espresso') |
|
286 | + ); |
|
287 | + } |
|
288 | + $this->filters = array_merge($this->filters, $filters); |
|
289 | + } |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * Ensures incoming paths is a valid filepath, or array of valid filepaths, |
|
294 | + * and merges them in with any existing filepaths |
|
295 | + * PLZ NOTE: |
|
296 | + * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
297 | + * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
298 | + * |
|
299 | + * @param string|array $paths |
|
300 | + * @throws RuntimeException |
|
301 | + * @throws InvalidDataTypeException |
|
302 | + */ |
|
303 | + public function setPaths($paths = array()) |
|
304 | + { |
|
305 | + if (empty($paths)) { |
|
306 | + return; |
|
307 | + } |
|
308 | + if (! (is_string($paths) || is_array($paths))) { |
|
309 | + throw new InvalidDataTypeException( |
|
310 | + '$path', |
|
311 | + is_object($paths) ? get_class($paths) : gettype($paths), |
|
312 | + esc_html__('string or array of strings (full server filepath(s))', 'event_espresso') |
|
313 | + ); |
|
314 | + } |
|
315 | + $paths = (array) $paths; |
|
316 | + foreach ($paths as $path) { |
|
317 | + if (strpos($path, '*') === false && ! is_readable($path)) { |
|
318 | + throw new RuntimeException( |
|
319 | + sprintf( |
|
320 | + esc_html__('The following filepath is not readable: "%1$s"', 'event_espresso'), |
|
321 | + $path |
|
322 | + ) |
|
323 | + ); |
|
324 | + } |
|
325 | + } |
|
326 | + $this->paths = array_merge($this->paths, $paths); |
|
327 | + } |
|
328 | 328 | } |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | } |
160 | 160 | // if the reservoir doesn't have a closure already for the requested identifier, |
161 | 161 | // then neither a shared service nor a closure for making entities has been built yet |
162 | - if (! $this->reservoir->has($identifier)) { |
|
162 | + if ( ! $this->reservoir->has($identifier)) { |
|
163 | 163 | // so let's brew something up and add it to the proper collection |
164 | 164 | $brewed = $this->makeCoffee($identifier, $arguments, $type); |
165 | 165 | } |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | */ |
285 | 285 | public function addClosure($identifier, $closure) |
286 | 286 | { |
287 | - if (! is_callable($closure)) { |
|
287 | + if ( ! is_callable($closure)) { |
|
288 | 288 | throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
289 | 289 | } |
290 | 290 | $identifier = $this->processIdentifier($identifier); |
@@ -393,7 +393,7 @@ discard block |
||
393 | 393 | // is the wildcard recipe prefix in the identifier ? |
394 | 394 | if (strpos($identifier, $wildcard) !== false) { |
395 | 395 | // track matches and use the number of wildcard characters matched for the key |
396 | - $matches[ strlen($wildcard) ] = $default_recipe; |
|
396 | + $matches[strlen($wildcard)] = $default_recipe; |
|
397 | 397 | } |
398 | 398 | } |
399 | 399 | if (count($matches) > 0) { |
@@ -433,7 +433,7 @@ discard block |
||
433 | 433 | } |
434 | 434 | $identifier = $this->processIdentifier($identifier); |
435 | 435 | foreach ((array) $aliases as $alias) { |
436 | - $this->filters[ $this->processIdentifier($alias) ] = $identifier; |
|
436 | + $this->filters[$this->processIdentifier($alias)] = $identifier; |
|
437 | 437 | } |
438 | 438 | } |
439 | 439 | |
@@ -475,8 +475,8 @@ discard block |
||
475 | 475 | private function filterIdentifier($identifier) |
476 | 476 | { |
477 | 477 | $identifier = $this->processIdentifier($identifier); |
478 | - return isset($this->filters[ $identifier ]) && ! empty($this->filters[ $identifier ]) |
|
479 | - ? $this->filters[ $identifier ] |
|
478 | + return isset($this->filters[$identifier]) && ! empty($this->filters[$identifier]) |
|
479 | + ? $this->filters[$identifier] |
|
480 | 480 | : $identifier; |
481 | 481 | } |
482 | 482 | |
@@ -491,7 +491,7 @@ discard block |
||
491 | 491 | */ |
492 | 492 | private function processIdentifier($identifier) |
493 | 493 | { |
494 | - if (! is_string($identifier)) { |
|
494 | + if ( ! is_string($identifier)) { |
|
495 | 495 | throw new InvalidIdentifierException( |
496 | 496 | is_object($identifier) ? get_class($identifier) : gettype($identifier), |
497 | 497 | '\Fully\Qualified\ClassName' |
@@ -510,7 +510,7 @@ discard block |
||
510 | 510 | */ |
511 | 511 | private function getCoffeeMaker($type) |
512 | 512 | { |
513 | - if (! $this->coffee_makers->has($type)) { |
|
513 | + if ( ! $this->coffee_makers->has($type)) { |
|
514 | 514 | throw new OutOfBoundsException( |
515 | 515 | esc_html__('The requested coffee maker is either missing or invalid.', 'event_espresso') |
516 | 516 | ); |
@@ -537,7 +537,7 @@ discard block |
||
537 | 537 | // does this recipe use a wildcard ? (but is NOT the global default) |
538 | 538 | if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
539 | 539 | // strip the wildcard and use identifier as key |
540 | - $default_recipes[ str_replace('*', '', $identifier) ] = $this->recipes->current(); |
|
540 | + $default_recipes[str_replace('*', '', $identifier)] = $this->recipes->current(); |
|
541 | 541 | } |
542 | 542 | $this->recipes->next(); |
543 | 543 | } |
@@ -557,7 +557,7 @@ discard block |
||
557 | 557 | private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
558 | 558 | { |
559 | 559 | $recipe = clone $default_recipe; |
560 | - if (! empty($type)) { |
|
560 | + if ( ! empty($type)) { |
|
561 | 561 | $recipe->setType($type); |
562 | 562 | } |
563 | 563 | // is this the base default recipe ? |
@@ -587,7 +587,7 @@ discard block |
||
587 | 587 | */ |
588 | 588 | private function validateService($identifier, $service) |
589 | 589 | { |
590 | - if (! is_object($service)) { |
|
590 | + if ( ! is_object($service)) { |
|
591 | 591 | throw new InvalidServiceException( |
592 | 592 | $identifier, |
593 | 593 | $service |
@@ -28,569 +28,569 @@ |
||
28 | 28 | */ |
29 | 29 | class CoffeeShop implements CoffeePotInterface |
30 | 30 | { |
31 | - /** |
|
32 | - * This was the best coffee related name I could think of to represent class name "aliases" |
|
33 | - * So classes can be found via an alias identifier, |
|
34 | - * that is revealed when it is run through... the filters... eh? get it? |
|
35 | - * |
|
36 | - * @var array $filters |
|
37 | - */ |
|
38 | - private $filters; |
|
39 | - |
|
40 | - /** |
|
41 | - * These are the classes that will actually build the objects (to order of course) |
|
42 | - * |
|
43 | - * @var array $coffee_makers |
|
44 | - */ |
|
45 | - private $coffee_makers; |
|
46 | - |
|
47 | - /** |
|
48 | - * where the instantiated "singleton" objects are stored |
|
49 | - * |
|
50 | - * @var CollectionInterface $carafe |
|
51 | - */ |
|
52 | - private $carafe; |
|
53 | - |
|
54 | - /** |
|
55 | - * collection of Recipes that instruct us how to brew objects |
|
56 | - * |
|
57 | - * @var CollectionInterface $recipes |
|
58 | - */ |
|
59 | - private $recipes; |
|
60 | - |
|
61 | - /** |
|
62 | - * collection of closures for brewing objects |
|
63 | - * |
|
64 | - * @var CollectionInterface $reservoir |
|
65 | - */ |
|
66 | - private $reservoir; |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * CoffeeShop constructor |
|
71 | - * |
|
72 | - * @throws InvalidInterfaceException |
|
73 | - */ |
|
74 | - public function __construct() |
|
75 | - { |
|
76 | - // array for storing class aliases |
|
77 | - $this->filters = array(); |
|
78 | - // create collection for storing shared services |
|
79 | - $this->carafe = new LooseCollection(''); |
|
80 | - // create collection for storing recipes that tell us how to build services and entities |
|
81 | - $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
|
82 | - // create collection for storing closures for constructing new entities |
|
83 | - $this->reservoir = new Collection('Closure'); |
|
84 | - // create collection for storing the generators that build our services and entity closures |
|
85 | - $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface'); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Returns true if the container can return an entry for the given identifier. |
|
91 | - * Returns false otherwise. |
|
92 | - * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
|
93 | - * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
|
94 | - * |
|
95 | - * @param string $identifier Identifier of the entry to look for. |
|
96 | - * Typically a Fully Qualified Class Name |
|
97 | - * @return boolean |
|
98 | - * @throws InvalidIdentifierException |
|
99 | - */ |
|
100 | - public function has($identifier) |
|
101 | - { |
|
102 | - $identifier = $this->filterIdentifier($identifier); |
|
103 | - return $this->carafe->has($identifier); |
|
104 | - } |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * finds a previously brewed (SHARED) service and returns it |
|
109 | - * |
|
110 | - * @param string $identifier Identifier for the entity class to be constructed. |
|
111 | - * Typically a Fully Qualified Class Name |
|
112 | - * @return mixed |
|
113 | - * @throws InvalidIdentifierException |
|
114 | - * @throws ServiceNotFoundException No service was found for this identifier. |
|
115 | - */ |
|
116 | - public function get($identifier) |
|
117 | - { |
|
118 | - $identifier = $this->filterIdentifier($identifier); |
|
119 | - if ($this->carafe->has($identifier)) { |
|
120 | - return $this->carafe->get($identifier); |
|
121 | - } |
|
122 | - throw new ServiceNotFoundException($identifier); |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * returns an instance of the requested entity type using the supplied arguments. |
|
128 | - * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
|
129 | - * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
|
130 | - * If the request is for a new entity and a closure exists in the reservoir for creating it, |
|
131 | - * then a new entity will be instantiated from the closure and returned. |
|
132 | - * If a closure does not exist, then one will be built and added to the reservoir |
|
133 | - * before instantiating the requested entity. |
|
134 | - * |
|
135 | - * @param string $identifier Identifier for the entity class to be constructed. |
|
136 | - * Typically a Fully Qualified Class Name |
|
137 | - * @param array $arguments an array of arguments to be passed to the entity constructor |
|
138 | - * @param string $type |
|
139 | - * @return mixed |
|
140 | - * @throws OutOfBoundsException |
|
141 | - * @throws InstantiationException |
|
142 | - * @throws InvalidDataTypeException |
|
143 | - * @throws InvalidClassException |
|
144 | - * @throws InvalidIdentifierException |
|
145 | - * @throws ServiceExistsException |
|
146 | - * @throws ServiceNotFoundException No service was found for this identifier. |
|
147 | - */ |
|
148 | - public function brew($identifier, $arguments = array(), $type = '') |
|
149 | - { |
|
150 | - // resolve any class aliases that may exist |
|
151 | - $identifier = $this->filterIdentifier($identifier); |
|
152 | - // is a shared service being requested and already exists in the carafe? |
|
153 | - $brewed = $this->getShared($identifier, $type); |
|
154 | - // then return whatever was found |
|
155 | - if ($brewed !== false) { |
|
156 | - return $brewed; |
|
157 | - } |
|
158 | - // if the reservoir doesn't have a closure already for the requested identifier, |
|
159 | - // then neither a shared service nor a closure for making entities has been built yet |
|
160 | - if (! $this->reservoir->has($identifier)) { |
|
161 | - // so let's brew something up and add it to the proper collection |
|
162 | - $brewed = $this->makeCoffee($identifier, $arguments, $type); |
|
163 | - } |
|
164 | - // did the requested class only require loading, and if so, was that successful? |
|
165 | - if ($this->brewedLoadOnly($brewed, $identifier, $type) === true) { |
|
166 | - return true; |
|
167 | - } |
|
168 | - // was the brewed item a callable factory function ? |
|
169 | - if (is_callable($brewed)) { |
|
170 | - // then instantiate a new entity from the cached closure |
|
171 | - return $brewed($arguments); |
|
172 | - } |
|
173 | - if ($brewed) { |
|
174 | - // requested object was a shared entity, so attempt to get it from the carafe again |
|
175 | - // because if it wasn't there before, then it should have just been brewed and added, |
|
176 | - // but if it still isn't there, then this time the thrown ServiceNotFoundException will not be caught |
|
177 | - return $this->get($identifier); |
|
178 | - } |
|
179 | - // if identifier is for a non-shared entity, |
|
180 | - // then either a cached closure already existed, or was just brewed |
|
181 | - return $this->brewedClosure($identifier, $arguments); |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * @param string $identifier |
|
187 | - * @param string $type |
|
188 | - * @return bool|mixed |
|
189 | - * @throws InvalidIdentifierException |
|
190 | - */ |
|
191 | - protected function getShared($identifier, $type) |
|
192 | - { |
|
193 | - try { |
|
194 | - if (empty($type) || $type === CoffeeMaker::BREW_SHARED) { |
|
195 | - // if a shared service was requested and an instance is in the carafe, then return it |
|
196 | - return $this->get($identifier); |
|
197 | - } |
|
198 | - } catch (ServiceNotFoundException $e) { |
|
199 | - // if not then we'll just catch the ServiceNotFoundException but not do anything just yet, |
|
200 | - // and instead, attempt to build whatever was requested |
|
201 | - } |
|
202 | - return false; |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * @param mixed $brewed |
|
208 | - * @param string $identifier |
|
209 | - * @param string $type |
|
210 | - * @return bool |
|
211 | - * @throws InvalidClassException |
|
212 | - * @throws InvalidDataTypeException |
|
213 | - * @throws InvalidIdentifierException |
|
214 | - * @throws OutOfBoundsException |
|
215 | - * @throws ServiceExistsException |
|
216 | - * @throws ServiceNotFoundException |
|
217 | - */ |
|
218 | - protected function brewedLoadOnly($brewed, $identifier, $type) |
|
219 | - { |
|
220 | - if ($type === CoffeeMaker::BREW_LOAD_ONLY) { |
|
221 | - if ($brewed !== true) { |
|
222 | - throw new ServiceNotFoundException( |
|
223 | - sprintf( |
|
224 | - esc_html__( |
|
225 | - 'The "%1$s" class could not be loaded.', |
|
226 | - 'event_espresso' |
|
227 | - ), |
|
228 | - $identifier |
|
229 | - ) |
|
230 | - ); |
|
231 | - } |
|
232 | - return true; |
|
233 | - } |
|
234 | - return false; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * @param string $identifier |
|
240 | - * @param array $arguments |
|
241 | - * @return mixed |
|
242 | - * @throws InstantiationException |
|
243 | - */ |
|
244 | - protected function brewedClosure($identifier, array $arguments) |
|
245 | - { |
|
246 | - $closure = $this->reservoir->get($identifier); |
|
247 | - if (empty($closure)) { |
|
248 | - throw new InstantiationException( |
|
249 | - sprintf( |
|
250 | - esc_html__( |
|
251 | - 'Could not brew an instance of "%1$s".', |
|
252 | - 'event_espresso' |
|
253 | - ), |
|
254 | - $identifier |
|
255 | - ) |
|
256 | - ); |
|
257 | - } |
|
258 | - return $closure($arguments); |
|
259 | - } |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * @param CoffeeMakerInterface $coffee_maker |
|
264 | - * @param string $type |
|
265 | - * @return bool |
|
266 | - * @throws InvalidIdentifierException |
|
267 | - * @throws InvalidEntityException |
|
268 | - */ |
|
269 | - public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
|
270 | - { |
|
271 | - $type = CoffeeMaker::validateType($type); |
|
272 | - return $this->coffee_makers->add($coffee_maker, $type); |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * @param string $identifier |
|
278 | - * @param callable $closure |
|
279 | - * @return callable|null |
|
280 | - * @throws InvalidIdentifierException |
|
281 | - * @throws InvalidDataTypeException |
|
282 | - */ |
|
283 | - public function addClosure($identifier, $closure) |
|
284 | - { |
|
285 | - if (! is_callable($closure)) { |
|
286 | - throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
|
287 | - } |
|
288 | - $identifier = $this->processIdentifier($identifier); |
|
289 | - if ($this->reservoir->add($closure, $identifier)) { |
|
290 | - return $closure; |
|
291 | - } |
|
292 | - return null; |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * @param string $identifier |
|
298 | - * @return boolean |
|
299 | - * @throws InvalidIdentifierException |
|
300 | - */ |
|
301 | - public function removeClosure($identifier) |
|
302 | - { |
|
303 | - $identifier = $this->processIdentifier($identifier); |
|
304 | - if ($this->reservoir->has($identifier)) { |
|
305 | - return $this->reservoir->remove($this->reservoir->get($identifier)); |
|
306 | - } |
|
307 | - return false; |
|
308 | - } |
|
309 | - |
|
310 | - |
|
311 | - /** |
|
312 | - * @param string $identifier Identifier for the entity class that the service applies to |
|
313 | - * Typically a Fully Qualified Class Name |
|
314 | - * @param mixed $service |
|
315 | - * @return bool |
|
316 | - * @throws \EventEspresso\core\services\container\exceptions\InvalidServiceException |
|
317 | - * @throws InvalidIdentifierException |
|
318 | - */ |
|
319 | - public function addService($identifier, $service) |
|
320 | - { |
|
321 | - $identifier = $this->processIdentifier($identifier); |
|
322 | - $service = $this->validateService($identifier, $service); |
|
323 | - return $this->carafe->add($service, $identifier); |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * @param string $identifier |
|
329 | - * @return boolean |
|
330 | - * @throws InvalidIdentifierException |
|
331 | - */ |
|
332 | - public function removeService($identifier) |
|
333 | - { |
|
334 | - $identifier = $this->processIdentifier($identifier); |
|
335 | - if ($this->carafe->has($identifier)) { |
|
336 | - return $this->carafe->remove($this->carafe->get($identifier)); |
|
337 | - } |
|
338 | - return false; |
|
339 | - } |
|
340 | - |
|
341 | - |
|
342 | - /** |
|
343 | - * Adds instructions on how to brew objects |
|
344 | - * |
|
345 | - * @param RecipeInterface $recipe |
|
346 | - * @return mixed |
|
347 | - * @throws InvalidIdentifierException |
|
348 | - */ |
|
349 | - public function addRecipe(RecipeInterface $recipe) |
|
350 | - { |
|
351 | - $this->addAliases($recipe->identifier(), $recipe->filters()); |
|
352 | - $identifier = $this->processIdentifier($recipe->identifier()); |
|
353 | - return $this->recipes->add($recipe, $identifier); |
|
354 | - } |
|
355 | - |
|
356 | - |
|
357 | - /** |
|
358 | - * @param string $identifier The Recipe's identifier |
|
359 | - * @return boolean |
|
360 | - * @throws InvalidIdentifierException |
|
361 | - */ |
|
362 | - public function removeRecipe($identifier) |
|
363 | - { |
|
364 | - $identifier = $this->processIdentifier($identifier); |
|
365 | - if ($this->recipes->has($identifier)) { |
|
366 | - return $this->recipes->remove($this->recipes->get($identifier)); |
|
367 | - } |
|
368 | - return false; |
|
369 | - } |
|
370 | - |
|
371 | - |
|
372 | - /** |
|
373 | - * Get instructions on how to brew objects |
|
374 | - * |
|
375 | - * @param string $identifier Identifier for the entity class that the recipe applies to |
|
376 | - * Typically a Fully Qualified Class Name |
|
377 | - * @param string $type |
|
378 | - * @return RecipeInterface |
|
379 | - * @throws OutOfBoundsException |
|
380 | - * @throws InvalidIdentifierException |
|
381 | - */ |
|
382 | - public function getRecipe($identifier, $type = '') |
|
383 | - { |
|
384 | - $identifier = $this->processIdentifier($identifier); |
|
385 | - if ($this->recipes->has($identifier)) { |
|
386 | - return $this->recipes->get($identifier); |
|
387 | - } |
|
388 | - $default_recipes = $this->getDefaultRecipes(); |
|
389 | - $matches = array(); |
|
390 | - foreach ($default_recipes as $wildcard => $default_recipe) { |
|
391 | - // is the wildcard recipe prefix in the identifier ? |
|
392 | - if (strpos($identifier, $wildcard) !== false) { |
|
393 | - // track matches and use the number of wildcard characters matched for the key |
|
394 | - $matches[ strlen($wildcard) ] = $default_recipe; |
|
395 | - } |
|
396 | - } |
|
397 | - if (count($matches) > 0) { |
|
398 | - // sort our recipes by the number of wildcard characters matched |
|
399 | - ksort($matches); |
|
400 | - // then grab the last recipe form the list, since it had the most matching characters |
|
401 | - $match = array_pop($matches); |
|
402 | - // since we are using a default recipe, we need to set it's identifier and fqcn |
|
403 | - return $this->copyDefaultRecipe($match, $identifier, $type); |
|
404 | - } |
|
405 | - if ($this->recipes->has(Recipe::DEFAULT_ID)) { |
|
406 | - // since we are using a default recipe, we need to set it's identifier and fqcn |
|
407 | - return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type); |
|
408 | - } |
|
409 | - throw new OutOfBoundsException( |
|
410 | - sprintf( |
|
411 | - esc_html__('Could not brew coffee because no recipes were found for class "%1$s".', 'event_espresso'), |
|
412 | - $identifier |
|
413 | - ) |
|
414 | - ); |
|
415 | - } |
|
416 | - |
|
417 | - |
|
418 | - /** |
|
419 | - * adds class name aliases to list of filters |
|
420 | - * |
|
421 | - * @param string $identifier Identifier for the entity class that the alias applies to |
|
422 | - * Typically a Fully Qualified Class Name |
|
423 | - * @param array|string $aliases |
|
424 | - * @return void |
|
425 | - * @throws InvalidIdentifierException |
|
426 | - */ |
|
427 | - public function addAliases($identifier, $aliases) |
|
428 | - { |
|
429 | - if (empty($aliases)) { |
|
430 | - return; |
|
431 | - } |
|
432 | - $identifier = $this->processIdentifier($identifier); |
|
433 | - foreach ((array) $aliases as $alias) { |
|
434 | - $this->filters[ $this->processIdentifier($alias) ] = $identifier; |
|
435 | - } |
|
436 | - } |
|
437 | - |
|
438 | - |
|
439 | - /** |
|
440 | - * Adds a service to one of the internal collections |
|
441 | - * |
|
442 | - * @param $identifier |
|
443 | - * @param array $arguments |
|
444 | - * @param string $type |
|
445 | - * @return mixed |
|
446 | - * @throws InvalidDataTypeException |
|
447 | - * @throws InvalidClassException |
|
448 | - * @throws OutOfBoundsException |
|
449 | - * @throws InvalidIdentifierException |
|
450 | - * @throws ServiceExistsException |
|
451 | - */ |
|
452 | - private function makeCoffee($identifier, $arguments = array(), $type = '') |
|
453 | - { |
|
454 | - if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) { |
|
455 | - throw new ServiceExistsException($identifier); |
|
456 | - } |
|
457 | - $identifier = $this->filterIdentifier($identifier); |
|
458 | - $recipe = $this->getRecipe($identifier, $type); |
|
459 | - $type = ! empty($type) ? $type : $recipe->type(); |
|
460 | - $coffee_maker = $this->getCoffeeMaker($type); |
|
461 | - return $coffee_maker->brew($recipe, $arguments); |
|
462 | - } |
|
463 | - |
|
464 | - |
|
465 | - /** |
|
466 | - * filters alias identifiers to find the real class name |
|
467 | - * |
|
468 | - * @param string $identifier Identifier for the entity class that the filter applies to |
|
469 | - * Typically a Fully Qualified Class Name |
|
470 | - * @return string |
|
471 | - * @throws InvalidIdentifierException |
|
472 | - */ |
|
473 | - private function filterIdentifier($identifier) |
|
474 | - { |
|
475 | - $identifier = $this->processIdentifier($identifier); |
|
476 | - return isset($this->filters[ $identifier ]) && ! empty($this->filters[ $identifier ]) |
|
477 | - ? $this->filters[ $identifier ] |
|
478 | - : $identifier; |
|
479 | - } |
|
480 | - |
|
481 | - |
|
482 | - /** |
|
483 | - * verifies and standardizes identifiers |
|
484 | - * |
|
485 | - * @param string $identifier Identifier for the entity class |
|
486 | - * Typically a Fully Qualified Class Name |
|
487 | - * @return string |
|
488 | - * @throws InvalidIdentifierException |
|
489 | - */ |
|
490 | - private function processIdentifier($identifier) |
|
491 | - { |
|
492 | - if (! is_string($identifier)) { |
|
493 | - throw new InvalidIdentifierException( |
|
494 | - is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
495 | - '\Fully\Qualified\ClassName' |
|
496 | - ); |
|
497 | - } |
|
498 | - return ltrim($identifier, '\\'); |
|
499 | - } |
|
500 | - |
|
501 | - |
|
502 | - /** |
|
503 | - * @param string $type |
|
504 | - * @return CoffeeMakerInterface |
|
505 | - * @throws OutOfBoundsException |
|
506 | - * @throws InvalidDataTypeException |
|
507 | - * @throws InvalidClassException |
|
508 | - */ |
|
509 | - private function getCoffeeMaker($type) |
|
510 | - { |
|
511 | - if (! $this->coffee_makers->has($type)) { |
|
512 | - throw new OutOfBoundsException( |
|
513 | - esc_html__('The requested coffee maker is either missing or invalid.', 'event_espresso') |
|
514 | - ); |
|
515 | - } |
|
516 | - return $this->coffee_makers->get($type); |
|
517 | - } |
|
518 | - |
|
519 | - |
|
520 | - /** |
|
521 | - * Retrieves all recipes that use a wildcard "*" in their identifier |
|
522 | - * This allows recipes to be set up for handling |
|
523 | - * legacy classes that do not support PSR-4 autoloading. |
|
524 | - * for example: |
|
525 | - * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
|
526 | - * |
|
527 | - * @return array |
|
528 | - */ |
|
529 | - private function getDefaultRecipes() |
|
530 | - { |
|
531 | - $default_recipes = array(); |
|
532 | - $this->recipes->rewind(); |
|
533 | - while ($this->recipes->valid()) { |
|
534 | - $identifier = $this->recipes->getInfo(); |
|
535 | - // does this recipe use a wildcard ? (but is NOT the global default) |
|
536 | - if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
|
537 | - // strip the wildcard and use identifier as key |
|
538 | - $default_recipes[ str_replace('*', '', $identifier) ] = $this->recipes->current(); |
|
539 | - } |
|
540 | - $this->recipes->next(); |
|
541 | - } |
|
542 | - return $default_recipes; |
|
543 | - } |
|
544 | - |
|
545 | - |
|
546 | - /** |
|
547 | - * clones a default recipe and then copies details |
|
548 | - * from the incoming request to it so that it can be used |
|
549 | - * |
|
550 | - * @param RecipeInterface $default_recipe |
|
551 | - * @param string $identifier |
|
552 | - * @param string $type |
|
553 | - * @return RecipeInterface |
|
554 | - */ |
|
555 | - private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
|
556 | - { |
|
557 | - $recipe = clone $default_recipe; |
|
558 | - if (! empty($type)) { |
|
559 | - $recipe->setType($type); |
|
560 | - } |
|
561 | - // is this the base default recipe ? |
|
562 | - if ($default_recipe->identifier() === Recipe::DEFAULT_ID) { |
|
563 | - $recipe->setIdentifier($identifier); |
|
564 | - $recipe->setFqcn($identifier); |
|
565 | - return $recipe; |
|
566 | - } |
|
567 | - $recipe->setIdentifier($identifier); |
|
568 | - foreach ($default_recipe->paths() as $path) { |
|
569 | - $path = str_replace('*', $identifier, $path); |
|
570 | - if (is_readable($path)) { |
|
571 | - $recipe->setPaths($path); |
|
572 | - } |
|
573 | - } |
|
574 | - $recipe->setFqcn($identifier); |
|
575 | - return $recipe; |
|
576 | - } |
|
577 | - |
|
578 | - |
|
579 | - /** |
|
580 | - * @param string $identifier Identifier for the entity class that the service applies to |
|
581 | - * Typically a Fully Qualified Class Name |
|
582 | - * @param mixed $service |
|
583 | - * @return mixed |
|
584 | - * @throws InvalidServiceException |
|
585 | - */ |
|
586 | - private function validateService($identifier, $service) |
|
587 | - { |
|
588 | - if (! is_object($service)) { |
|
589 | - throw new InvalidServiceException( |
|
590 | - $identifier, |
|
591 | - $service |
|
592 | - ); |
|
593 | - } |
|
594 | - return $service; |
|
595 | - } |
|
31 | + /** |
|
32 | + * This was the best coffee related name I could think of to represent class name "aliases" |
|
33 | + * So classes can be found via an alias identifier, |
|
34 | + * that is revealed when it is run through... the filters... eh? get it? |
|
35 | + * |
|
36 | + * @var array $filters |
|
37 | + */ |
|
38 | + private $filters; |
|
39 | + |
|
40 | + /** |
|
41 | + * These are the classes that will actually build the objects (to order of course) |
|
42 | + * |
|
43 | + * @var array $coffee_makers |
|
44 | + */ |
|
45 | + private $coffee_makers; |
|
46 | + |
|
47 | + /** |
|
48 | + * where the instantiated "singleton" objects are stored |
|
49 | + * |
|
50 | + * @var CollectionInterface $carafe |
|
51 | + */ |
|
52 | + private $carafe; |
|
53 | + |
|
54 | + /** |
|
55 | + * collection of Recipes that instruct us how to brew objects |
|
56 | + * |
|
57 | + * @var CollectionInterface $recipes |
|
58 | + */ |
|
59 | + private $recipes; |
|
60 | + |
|
61 | + /** |
|
62 | + * collection of closures for brewing objects |
|
63 | + * |
|
64 | + * @var CollectionInterface $reservoir |
|
65 | + */ |
|
66 | + private $reservoir; |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * CoffeeShop constructor |
|
71 | + * |
|
72 | + * @throws InvalidInterfaceException |
|
73 | + */ |
|
74 | + public function __construct() |
|
75 | + { |
|
76 | + // array for storing class aliases |
|
77 | + $this->filters = array(); |
|
78 | + // create collection for storing shared services |
|
79 | + $this->carafe = new LooseCollection(''); |
|
80 | + // create collection for storing recipes that tell us how to build services and entities |
|
81 | + $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
|
82 | + // create collection for storing closures for constructing new entities |
|
83 | + $this->reservoir = new Collection('Closure'); |
|
84 | + // create collection for storing the generators that build our services and entity closures |
|
85 | + $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface'); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Returns true if the container can return an entry for the given identifier. |
|
91 | + * Returns false otherwise. |
|
92 | + * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
|
93 | + * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
|
94 | + * |
|
95 | + * @param string $identifier Identifier of the entry to look for. |
|
96 | + * Typically a Fully Qualified Class Name |
|
97 | + * @return boolean |
|
98 | + * @throws InvalidIdentifierException |
|
99 | + */ |
|
100 | + public function has($identifier) |
|
101 | + { |
|
102 | + $identifier = $this->filterIdentifier($identifier); |
|
103 | + return $this->carafe->has($identifier); |
|
104 | + } |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * finds a previously brewed (SHARED) service and returns it |
|
109 | + * |
|
110 | + * @param string $identifier Identifier for the entity class to be constructed. |
|
111 | + * Typically a Fully Qualified Class Name |
|
112 | + * @return mixed |
|
113 | + * @throws InvalidIdentifierException |
|
114 | + * @throws ServiceNotFoundException No service was found for this identifier. |
|
115 | + */ |
|
116 | + public function get($identifier) |
|
117 | + { |
|
118 | + $identifier = $this->filterIdentifier($identifier); |
|
119 | + if ($this->carafe->has($identifier)) { |
|
120 | + return $this->carafe->get($identifier); |
|
121 | + } |
|
122 | + throw new ServiceNotFoundException($identifier); |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * returns an instance of the requested entity type using the supplied arguments. |
|
128 | + * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
|
129 | + * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
|
130 | + * If the request is for a new entity and a closure exists in the reservoir for creating it, |
|
131 | + * then a new entity will be instantiated from the closure and returned. |
|
132 | + * If a closure does not exist, then one will be built and added to the reservoir |
|
133 | + * before instantiating the requested entity. |
|
134 | + * |
|
135 | + * @param string $identifier Identifier for the entity class to be constructed. |
|
136 | + * Typically a Fully Qualified Class Name |
|
137 | + * @param array $arguments an array of arguments to be passed to the entity constructor |
|
138 | + * @param string $type |
|
139 | + * @return mixed |
|
140 | + * @throws OutOfBoundsException |
|
141 | + * @throws InstantiationException |
|
142 | + * @throws InvalidDataTypeException |
|
143 | + * @throws InvalidClassException |
|
144 | + * @throws InvalidIdentifierException |
|
145 | + * @throws ServiceExistsException |
|
146 | + * @throws ServiceNotFoundException No service was found for this identifier. |
|
147 | + */ |
|
148 | + public function brew($identifier, $arguments = array(), $type = '') |
|
149 | + { |
|
150 | + // resolve any class aliases that may exist |
|
151 | + $identifier = $this->filterIdentifier($identifier); |
|
152 | + // is a shared service being requested and already exists in the carafe? |
|
153 | + $brewed = $this->getShared($identifier, $type); |
|
154 | + // then return whatever was found |
|
155 | + if ($brewed !== false) { |
|
156 | + return $brewed; |
|
157 | + } |
|
158 | + // if the reservoir doesn't have a closure already for the requested identifier, |
|
159 | + // then neither a shared service nor a closure for making entities has been built yet |
|
160 | + if (! $this->reservoir->has($identifier)) { |
|
161 | + // so let's brew something up and add it to the proper collection |
|
162 | + $brewed = $this->makeCoffee($identifier, $arguments, $type); |
|
163 | + } |
|
164 | + // did the requested class only require loading, and if so, was that successful? |
|
165 | + if ($this->brewedLoadOnly($brewed, $identifier, $type) === true) { |
|
166 | + return true; |
|
167 | + } |
|
168 | + // was the brewed item a callable factory function ? |
|
169 | + if (is_callable($brewed)) { |
|
170 | + // then instantiate a new entity from the cached closure |
|
171 | + return $brewed($arguments); |
|
172 | + } |
|
173 | + if ($brewed) { |
|
174 | + // requested object was a shared entity, so attempt to get it from the carafe again |
|
175 | + // because if it wasn't there before, then it should have just been brewed and added, |
|
176 | + // but if it still isn't there, then this time the thrown ServiceNotFoundException will not be caught |
|
177 | + return $this->get($identifier); |
|
178 | + } |
|
179 | + // if identifier is for a non-shared entity, |
|
180 | + // then either a cached closure already existed, or was just brewed |
|
181 | + return $this->brewedClosure($identifier, $arguments); |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * @param string $identifier |
|
187 | + * @param string $type |
|
188 | + * @return bool|mixed |
|
189 | + * @throws InvalidIdentifierException |
|
190 | + */ |
|
191 | + protected function getShared($identifier, $type) |
|
192 | + { |
|
193 | + try { |
|
194 | + if (empty($type) || $type === CoffeeMaker::BREW_SHARED) { |
|
195 | + // if a shared service was requested and an instance is in the carafe, then return it |
|
196 | + return $this->get($identifier); |
|
197 | + } |
|
198 | + } catch (ServiceNotFoundException $e) { |
|
199 | + // if not then we'll just catch the ServiceNotFoundException but not do anything just yet, |
|
200 | + // and instead, attempt to build whatever was requested |
|
201 | + } |
|
202 | + return false; |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * @param mixed $brewed |
|
208 | + * @param string $identifier |
|
209 | + * @param string $type |
|
210 | + * @return bool |
|
211 | + * @throws InvalidClassException |
|
212 | + * @throws InvalidDataTypeException |
|
213 | + * @throws InvalidIdentifierException |
|
214 | + * @throws OutOfBoundsException |
|
215 | + * @throws ServiceExistsException |
|
216 | + * @throws ServiceNotFoundException |
|
217 | + */ |
|
218 | + protected function brewedLoadOnly($brewed, $identifier, $type) |
|
219 | + { |
|
220 | + if ($type === CoffeeMaker::BREW_LOAD_ONLY) { |
|
221 | + if ($brewed !== true) { |
|
222 | + throw new ServiceNotFoundException( |
|
223 | + sprintf( |
|
224 | + esc_html__( |
|
225 | + 'The "%1$s" class could not be loaded.', |
|
226 | + 'event_espresso' |
|
227 | + ), |
|
228 | + $identifier |
|
229 | + ) |
|
230 | + ); |
|
231 | + } |
|
232 | + return true; |
|
233 | + } |
|
234 | + return false; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * @param string $identifier |
|
240 | + * @param array $arguments |
|
241 | + * @return mixed |
|
242 | + * @throws InstantiationException |
|
243 | + */ |
|
244 | + protected function brewedClosure($identifier, array $arguments) |
|
245 | + { |
|
246 | + $closure = $this->reservoir->get($identifier); |
|
247 | + if (empty($closure)) { |
|
248 | + throw new InstantiationException( |
|
249 | + sprintf( |
|
250 | + esc_html__( |
|
251 | + 'Could not brew an instance of "%1$s".', |
|
252 | + 'event_espresso' |
|
253 | + ), |
|
254 | + $identifier |
|
255 | + ) |
|
256 | + ); |
|
257 | + } |
|
258 | + return $closure($arguments); |
|
259 | + } |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * @param CoffeeMakerInterface $coffee_maker |
|
264 | + * @param string $type |
|
265 | + * @return bool |
|
266 | + * @throws InvalidIdentifierException |
|
267 | + * @throws InvalidEntityException |
|
268 | + */ |
|
269 | + public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
|
270 | + { |
|
271 | + $type = CoffeeMaker::validateType($type); |
|
272 | + return $this->coffee_makers->add($coffee_maker, $type); |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * @param string $identifier |
|
278 | + * @param callable $closure |
|
279 | + * @return callable|null |
|
280 | + * @throws InvalidIdentifierException |
|
281 | + * @throws InvalidDataTypeException |
|
282 | + */ |
|
283 | + public function addClosure($identifier, $closure) |
|
284 | + { |
|
285 | + if (! is_callable($closure)) { |
|
286 | + throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
|
287 | + } |
|
288 | + $identifier = $this->processIdentifier($identifier); |
|
289 | + if ($this->reservoir->add($closure, $identifier)) { |
|
290 | + return $closure; |
|
291 | + } |
|
292 | + return null; |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * @param string $identifier |
|
298 | + * @return boolean |
|
299 | + * @throws InvalidIdentifierException |
|
300 | + */ |
|
301 | + public function removeClosure($identifier) |
|
302 | + { |
|
303 | + $identifier = $this->processIdentifier($identifier); |
|
304 | + if ($this->reservoir->has($identifier)) { |
|
305 | + return $this->reservoir->remove($this->reservoir->get($identifier)); |
|
306 | + } |
|
307 | + return false; |
|
308 | + } |
|
309 | + |
|
310 | + |
|
311 | + /** |
|
312 | + * @param string $identifier Identifier for the entity class that the service applies to |
|
313 | + * Typically a Fully Qualified Class Name |
|
314 | + * @param mixed $service |
|
315 | + * @return bool |
|
316 | + * @throws \EventEspresso\core\services\container\exceptions\InvalidServiceException |
|
317 | + * @throws InvalidIdentifierException |
|
318 | + */ |
|
319 | + public function addService($identifier, $service) |
|
320 | + { |
|
321 | + $identifier = $this->processIdentifier($identifier); |
|
322 | + $service = $this->validateService($identifier, $service); |
|
323 | + return $this->carafe->add($service, $identifier); |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * @param string $identifier |
|
329 | + * @return boolean |
|
330 | + * @throws InvalidIdentifierException |
|
331 | + */ |
|
332 | + public function removeService($identifier) |
|
333 | + { |
|
334 | + $identifier = $this->processIdentifier($identifier); |
|
335 | + if ($this->carafe->has($identifier)) { |
|
336 | + return $this->carafe->remove($this->carafe->get($identifier)); |
|
337 | + } |
|
338 | + return false; |
|
339 | + } |
|
340 | + |
|
341 | + |
|
342 | + /** |
|
343 | + * Adds instructions on how to brew objects |
|
344 | + * |
|
345 | + * @param RecipeInterface $recipe |
|
346 | + * @return mixed |
|
347 | + * @throws InvalidIdentifierException |
|
348 | + */ |
|
349 | + public function addRecipe(RecipeInterface $recipe) |
|
350 | + { |
|
351 | + $this->addAliases($recipe->identifier(), $recipe->filters()); |
|
352 | + $identifier = $this->processIdentifier($recipe->identifier()); |
|
353 | + return $this->recipes->add($recipe, $identifier); |
|
354 | + } |
|
355 | + |
|
356 | + |
|
357 | + /** |
|
358 | + * @param string $identifier The Recipe's identifier |
|
359 | + * @return boolean |
|
360 | + * @throws InvalidIdentifierException |
|
361 | + */ |
|
362 | + public function removeRecipe($identifier) |
|
363 | + { |
|
364 | + $identifier = $this->processIdentifier($identifier); |
|
365 | + if ($this->recipes->has($identifier)) { |
|
366 | + return $this->recipes->remove($this->recipes->get($identifier)); |
|
367 | + } |
|
368 | + return false; |
|
369 | + } |
|
370 | + |
|
371 | + |
|
372 | + /** |
|
373 | + * Get instructions on how to brew objects |
|
374 | + * |
|
375 | + * @param string $identifier Identifier for the entity class that the recipe applies to |
|
376 | + * Typically a Fully Qualified Class Name |
|
377 | + * @param string $type |
|
378 | + * @return RecipeInterface |
|
379 | + * @throws OutOfBoundsException |
|
380 | + * @throws InvalidIdentifierException |
|
381 | + */ |
|
382 | + public function getRecipe($identifier, $type = '') |
|
383 | + { |
|
384 | + $identifier = $this->processIdentifier($identifier); |
|
385 | + if ($this->recipes->has($identifier)) { |
|
386 | + return $this->recipes->get($identifier); |
|
387 | + } |
|
388 | + $default_recipes = $this->getDefaultRecipes(); |
|
389 | + $matches = array(); |
|
390 | + foreach ($default_recipes as $wildcard => $default_recipe) { |
|
391 | + // is the wildcard recipe prefix in the identifier ? |
|
392 | + if (strpos($identifier, $wildcard) !== false) { |
|
393 | + // track matches and use the number of wildcard characters matched for the key |
|
394 | + $matches[ strlen($wildcard) ] = $default_recipe; |
|
395 | + } |
|
396 | + } |
|
397 | + if (count($matches) > 0) { |
|
398 | + // sort our recipes by the number of wildcard characters matched |
|
399 | + ksort($matches); |
|
400 | + // then grab the last recipe form the list, since it had the most matching characters |
|
401 | + $match = array_pop($matches); |
|
402 | + // since we are using a default recipe, we need to set it's identifier and fqcn |
|
403 | + return $this->copyDefaultRecipe($match, $identifier, $type); |
|
404 | + } |
|
405 | + if ($this->recipes->has(Recipe::DEFAULT_ID)) { |
|
406 | + // since we are using a default recipe, we need to set it's identifier and fqcn |
|
407 | + return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type); |
|
408 | + } |
|
409 | + throw new OutOfBoundsException( |
|
410 | + sprintf( |
|
411 | + esc_html__('Could not brew coffee because no recipes were found for class "%1$s".', 'event_espresso'), |
|
412 | + $identifier |
|
413 | + ) |
|
414 | + ); |
|
415 | + } |
|
416 | + |
|
417 | + |
|
418 | + /** |
|
419 | + * adds class name aliases to list of filters |
|
420 | + * |
|
421 | + * @param string $identifier Identifier for the entity class that the alias applies to |
|
422 | + * Typically a Fully Qualified Class Name |
|
423 | + * @param array|string $aliases |
|
424 | + * @return void |
|
425 | + * @throws InvalidIdentifierException |
|
426 | + */ |
|
427 | + public function addAliases($identifier, $aliases) |
|
428 | + { |
|
429 | + if (empty($aliases)) { |
|
430 | + return; |
|
431 | + } |
|
432 | + $identifier = $this->processIdentifier($identifier); |
|
433 | + foreach ((array) $aliases as $alias) { |
|
434 | + $this->filters[ $this->processIdentifier($alias) ] = $identifier; |
|
435 | + } |
|
436 | + } |
|
437 | + |
|
438 | + |
|
439 | + /** |
|
440 | + * Adds a service to one of the internal collections |
|
441 | + * |
|
442 | + * @param $identifier |
|
443 | + * @param array $arguments |
|
444 | + * @param string $type |
|
445 | + * @return mixed |
|
446 | + * @throws InvalidDataTypeException |
|
447 | + * @throws InvalidClassException |
|
448 | + * @throws OutOfBoundsException |
|
449 | + * @throws InvalidIdentifierException |
|
450 | + * @throws ServiceExistsException |
|
451 | + */ |
|
452 | + private function makeCoffee($identifier, $arguments = array(), $type = '') |
|
453 | + { |
|
454 | + if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) { |
|
455 | + throw new ServiceExistsException($identifier); |
|
456 | + } |
|
457 | + $identifier = $this->filterIdentifier($identifier); |
|
458 | + $recipe = $this->getRecipe($identifier, $type); |
|
459 | + $type = ! empty($type) ? $type : $recipe->type(); |
|
460 | + $coffee_maker = $this->getCoffeeMaker($type); |
|
461 | + return $coffee_maker->brew($recipe, $arguments); |
|
462 | + } |
|
463 | + |
|
464 | + |
|
465 | + /** |
|
466 | + * filters alias identifiers to find the real class name |
|
467 | + * |
|
468 | + * @param string $identifier Identifier for the entity class that the filter applies to |
|
469 | + * Typically a Fully Qualified Class Name |
|
470 | + * @return string |
|
471 | + * @throws InvalidIdentifierException |
|
472 | + */ |
|
473 | + private function filterIdentifier($identifier) |
|
474 | + { |
|
475 | + $identifier = $this->processIdentifier($identifier); |
|
476 | + return isset($this->filters[ $identifier ]) && ! empty($this->filters[ $identifier ]) |
|
477 | + ? $this->filters[ $identifier ] |
|
478 | + : $identifier; |
|
479 | + } |
|
480 | + |
|
481 | + |
|
482 | + /** |
|
483 | + * verifies and standardizes identifiers |
|
484 | + * |
|
485 | + * @param string $identifier Identifier for the entity class |
|
486 | + * Typically a Fully Qualified Class Name |
|
487 | + * @return string |
|
488 | + * @throws InvalidIdentifierException |
|
489 | + */ |
|
490 | + private function processIdentifier($identifier) |
|
491 | + { |
|
492 | + if (! is_string($identifier)) { |
|
493 | + throw new InvalidIdentifierException( |
|
494 | + is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
495 | + '\Fully\Qualified\ClassName' |
|
496 | + ); |
|
497 | + } |
|
498 | + return ltrim($identifier, '\\'); |
|
499 | + } |
|
500 | + |
|
501 | + |
|
502 | + /** |
|
503 | + * @param string $type |
|
504 | + * @return CoffeeMakerInterface |
|
505 | + * @throws OutOfBoundsException |
|
506 | + * @throws InvalidDataTypeException |
|
507 | + * @throws InvalidClassException |
|
508 | + */ |
|
509 | + private function getCoffeeMaker($type) |
|
510 | + { |
|
511 | + if (! $this->coffee_makers->has($type)) { |
|
512 | + throw new OutOfBoundsException( |
|
513 | + esc_html__('The requested coffee maker is either missing or invalid.', 'event_espresso') |
|
514 | + ); |
|
515 | + } |
|
516 | + return $this->coffee_makers->get($type); |
|
517 | + } |
|
518 | + |
|
519 | + |
|
520 | + /** |
|
521 | + * Retrieves all recipes that use a wildcard "*" in their identifier |
|
522 | + * This allows recipes to be set up for handling |
|
523 | + * legacy classes that do not support PSR-4 autoloading. |
|
524 | + * for example: |
|
525 | + * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
|
526 | + * |
|
527 | + * @return array |
|
528 | + */ |
|
529 | + private function getDefaultRecipes() |
|
530 | + { |
|
531 | + $default_recipes = array(); |
|
532 | + $this->recipes->rewind(); |
|
533 | + while ($this->recipes->valid()) { |
|
534 | + $identifier = $this->recipes->getInfo(); |
|
535 | + // does this recipe use a wildcard ? (but is NOT the global default) |
|
536 | + if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
|
537 | + // strip the wildcard and use identifier as key |
|
538 | + $default_recipes[ str_replace('*', '', $identifier) ] = $this->recipes->current(); |
|
539 | + } |
|
540 | + $this->recipes->next(); |
|
541 | + } |
|
542 | + return $default_recipes; |
|
543 | + } |
|
544 | + |
|
545 | + |
|
546 | + /** |
|
547 | + * clones a default recipe and then copies details |
|
548 | + * from the incoming request to it so that it can be used |
|
549 | + * |
|
550 | + * @param RecipeInterface $default_recipe |
|
551 | + * @param string $identifier |
|
552 | + * @param string $type |
|
553 | + * @return RecipeInterface |
|
554 | + */ |
|
555 | + private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
|
556 | + { |
|
557 | + $recipe = clone $default_recipe; |
|
558 | + if (! empty($type)) { |
|
559 | + $recipe->setType($type); |
|
560 | + } |
|
561 | + // is this the base default recipe ? |
|
562 | + if ($default_recipe->identifier() === Recipe::DEFAULT_ID) { |
|
563 | + $recipe->setIdentifier($identifier); |
|
564 | + $recipe->setFqcn($identifier); |
|
565 | + return $recipe; |
|
566 | + } |
|
567 | + $recipe->setIdentifier($identifier); |
|
568 | + foreach ($default_recipe->paths() as $path) { |
|
569 | + $path = str_replace('*', $identifier, $path); |
|
570 | + if (is_readable($path)) { |
|
571 | + $recipe->setPaths($path); |
|
572 | + } |
|
573 | + } |
|
574 | + $recipe->setFqcn($identifier); |
|
575 | + return $recipe; |
|
576 | + } |
|
577 | + |
|
578 | + |
|
579 | + /** |
|
580 | + * @param string $identifier Identifier for the entity class that the service applies to |
|
581 | + * Typically a Fully Qualified Class Name |
|
582 | + * @param mixed $service |
|
583 | + * @return mixed |
|
584 | + * @throws InvalidServiceException |
|
585 | + */ |
|
586 | + private function validateService($identifier, $service) |
|
587 | + { |
|
588 | + if (! is_object($service)) { |
|
589 | + throw new InvalidServiceException( |
|
590 | + $identifier, |
|
591 | + $service |
|
592 | + ); |
|
593 | + } |
|
594 | + return $service; |
|
595 | + } |
|
596 | 596 | } |
@@ -40,10 +40,10 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public static function verify_is_array_of($variable_to_test, $name_of_variable, $class_name, $allow_null = 'allow_null') |
42 | 42 | { |
43 | - if (!WP_DEBUG) { |
|
43 | + if ( ! WP_DEBUG) { |
|
44 | 44 | return; |
45 | 45 | } |
46 | - self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null')); |
|
46 | + self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null', 'do_not_allow_null')); |
|
47 | 47 | if ('allow_null' == $allow_null && is_null($variable_to_test)) { |
48 | 48 | return; |
49 | 49 | } |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | */ |
67 | 67 | public static function verify_isnt_null($variable_to_test, $name_of_variable) |
68 | 68 | { |
69 | - if (!WP_DEBUG) { |
|
69 | + if ( ! WP_DEBUG) { |
|
70 | 70 | return; |
71 | 71 | } |
72 | 72 | if ($variable_to_test == null && $variable_to_test != 0 && $variable_to_test != false) { |
@@ -86,10 +86,10 @@ discard block |
||
86 | 86 | */ |
87 | 87 | public static function verify_is_true($expression_to_test, $expression_string_representation) |
88 | 88 | { |
89 | - if (!WP_DEBUG) { |
|
89 | + if ( ! WP_DEBUG) { |
|
90 | 90 | return; |
91 | 91 | } |
92 | - if (!$expression_to_test) { |
|
92 | + if ( ! $expression_to_test) { |
|
93 | 93 | $error[] = esc_html__('Template error.', 'event_espresso'); |
94 | 94 | $error[] = esc_html__("%s evaluated to false, but it must be true!", 'event_espresso'); |
95 | 95 | throw new EE_Error(sprintf(implode(",", $error), $expression_string_representation)); |
@@ -110,14 +110,14 @@ discard block |
||
110 | 110 | */ |
111 | 111 | public static function verify_instanceof($variable_to_test, $name_of_variable, $class_name, $allow_null = 'do_not_allow_null') |
112 | 112 | { |
113 | - if (!WP_DEBUG) { |
|
113 | + if ( ! WP_DEBUG) { |
|
114 | 114 | return; |
115 | 115 | } |
116 | - self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null')); |
|
116 | + self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null', 'do_not_allow_null')); |
|
117 | 117 | if ($allow_null == 'allow_null' && is_null($variable_to_test)) { |
118 | 118 | return; |
119 | 119 | } |
120 | - if ($variable_to_test == null || ! ( $variable_to_test instanceof $class_name )) { |
|
120 | + if ($variable_to_test == null || ! ($variable_to_test instanceof $class_name)) { |
|
121 | 121 | $msg[] = esc_html__('Variable %s is not of the correct type.', 'event_espresso'); |
122 | 122 | $msg[] = esc_html__("It should be of type %s", 'event_espresso'); |
123 | 123 | throw new EE_Error(sprintf(implode(",", $msg), $name_of_variable, $name_of_variable, $class_name)); |
@@ -138,14 +138,14 @@ discard block |
||
138 | 138 | */ |
139 | 139 | public static function verify_is_array($variable_to_test, $variable_name, $allow_empty = 'allow_empty') |
140 | 140 | { |
141 | - if (!WP_DEBUG) { |
|
141 | + if ( ! WP_DEBUG) { |
|
142 | 142 | return; |
143 | 143 | } |
144 | - self::verify_argument_is_one_of($allow_empty, $variable_name, array('allow_empty','do_not_allow_empty')); |
|
144 | + self::verify_argument_is_one_of($allow_empty, $variable_name, array('allow_empty', 'do_not_allow_empty')); |
|
145 | 145 | if (empty($variable_to_test) && 'allow_empty' == $allow_empty) { |
146 | 146 | return; |
147 | 147 | } |
148 | - if (!is_array($variable_to_test)) { |
|
148 | + if ( ! is_array($variable_to_test)) { |
|
149 | 149 | $error[] = esc_html__('Variable %s should be an array, but it is not.', 'event_espresso'); |
150 | 150 | $error[] = esc_html__("Its value is, instead '%s'", 'event_espresso'); |
151 | 151 | throw new EE_Error(sprintf(implode(",", $error), $variable_name, $variable_name, $variable_to_test)); |
@@ -168,10 +168,10 @@ discard block |
||
168 | 168 | */ |
169 | 169 | public static function verify_argument_is_one_of($variable_to_test, $variable_name, $string_options) |
170 | 170 | { |
171 | - if (!WP_DEBUG) { |
|
171 | + if ( ! WP_DEBUG) { |
|
172 | 172 | return; |
173 | 173 | } |
174 | - if (!in_array($variable_to_test, $string_options)) { |
|
174 | + if ( ! in_array($variable_to_test, $string_options)) { |
|
175 | 175 | $msg[0] = esc_html__('Malconfigured template.', 'event_espresso'); |
176 | 176 | $msg[1] = esc_html__("Variable named '%s' was set to '%s'. It can only be one of '%s'", 'event_espresso'); |
177 | 177 | throw new EE_Error(sprintf(implode("||", $msg), $variable_name, $variable_to_test, implode("', '", $string_options))); |
@@ -24,152 +24,152 @@ |
||
24 | 24 | */ |
25 | 25 | class EEH_Template_Validator |
26 | 26 | { |
27 | - /** |
|
28 | - * Throws an EE_Error if $variabel_to_test isn't an array of objects of class $class_name |
|
29 | - * @param mixed $variable_to_test |
|
30 | - * @param string $name_of_variable helpful in throwing intelligent errors |
|
31 | - * @param string $class_name eg EE_Answer, EE_Transaction, etc. |
|
32 | - * @param string $allow_null one of 'allow_null', or 'do_not_allow_null' |
|
33 | - * @return void |
|
34 | - * @throws EE_Error (indirectly) |
|
35 | - */ |
|
36 | - public static function verify_is_array_of($variable_to_test, $name_of_variable, $class_name, $allow_null = 'allow_null') |
|
37 | - { |
|
38 | - if (!WP_DEBUG) { |
|
39 | - return; |
|
40 | - } |
|
41 | - self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null')); |
|
42 | - if ('allow_null' == $allow_null && is_null($variable_to_test)) { |
|
43 | - return; |
|
44 | - } |
|
45 | - self::verify_is_array($variable_to_test, $name_of_variable); |
|
46 | - foreach ($variable_to_test as $key => $array_element) { |
|
47 | - self::verify_instanceof($array_element, $key, $class_name); |
|
48 | - } |
|
49 | - } |
|
50 | - |
|
51 | - |
|
52 | - |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * throws an EE_Error if $variable_to_test is null |
|
57 | - * @param mixed $variable_to_test |
|
58 | - * @param string $name_of_variable helpful for throwing intelligent errors |
|
59 | - * @return void |
|
60 | - * @throws EE_Error |
|
61 | - */ |
|
62 | - public static function verify_isnt_null($variable_to_test, $name_of_variable) |
|
63 | - { |
|
64 | - if (!WP_DEBUG) { |
|
65 | - return; |
|
66 | - } |
|
67 | - if ($variable_to_test == null && $variable_to_test != 0 && $variable_to_test != false) { |
|
68 | - $error[] = esc_html__('Variable named %s is null.', 'event_espresso'); |
|
69 | - $error[] = esc_html__("Consider looking at the stack trace to see why it wasn't set.", 'event_espresso'); |
|
70 | - throw new EE_Error(sprintf(implode(",", $error), $name_of_variable, $name_of_variable)); |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * When WP_DEBUG is activted, throws an error if $expression_to_test is false. |
|
76 | - * @param boolean $expression_to_test |
|
77 | - * @param string $expression_string_representation a string representation of your expression |
|
78 | - * for example, if your expression were $var1==23, then this should be '$var1==23' |
|
79 | - * @return void |
|
80 | - * @throws EE_Error |
|
81 | - */ |
|
82 | - public static function verify_is_true($expression_to_test, $expression_string_representation) |
|
83 | - { |
|
84 | - if (!WP_DEBUG) { |
|
85 | - return; |
|
86 | - } |
|
87 | - if (!$expression_to_test) { |
|
88 | - $error[] = esc_html__('Template error.', 'event_espresso'); |
|
89 | - $error[] = esc_html__("%s evaluated to false, but it must be true!", 'event_espresso'); |
|
90 | - throw new EE_Error(sprintf(implode(",", $error), $expression_string_representation)); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * For verifying that a variable is indeed an object of class $class_name |
|
100 | - * @param mixed $variable_to_test |
|
101 | - * @param string $name_of_variable helpful when throwing errors |
|
102 | - * @param string $class_name eg, EE_Answer, |
|
103 | - * @return void |
|
104 | - * @throws EE_Error |
|
105 | - */ |
|
106 | - public static function verify_instanceof($variable_to_test, $name_of_variable, $class_name, $allow_null = 'do_not_allow_null') |
|
107 | - { |
|
108 | - if (!WP_DEBUG) { |
|
109 | - return; |
|
110 | - } |
|
111 | - self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null')); |
|
112 | - if ($allow_null == 'allow_null' && is_null($variable_to_test)) { |
|
113 | - return; |
|
114 | - } |
|
115 | - if ($variable_to_test == null || ! ( $variable_to_test instanceof $class_name )) { |
|
116 | - $msg[] = esc_html__('Variable %s is not of the correct type.', 'event_espresso'); |
|
117 | - $msg[] = esc_html__("It should be of type %s", 'event_espresso'); |
|
118 | - throw new EE_Error(sprintf(implode(",", $msg), $name_of_variable, $name_of_variable, $class_name)); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * For verifying that a variable is indeed an array, else throw an EE_Error |
|
128 | - * @param type $variable_to_test |
|
129 | - * @param type $variable_name |
|
130 | - * @param type $allow_empty one of 'allow_empty' or 'do_not_allow_empty' |
|
131 | - * @return void |
|
132 | - * @throws EE_Error |
|
133 | - */ |
|
134 | - public static function verify_is_array($variable_to_test, $variable_name, $allow_empty = 'allow_empty') |
|
135 | - { |
|
136 | - if (!WP_DEBUG) { |
|
137 | - return; |
|
138 | - } |
|
139 | - self::verify_argument_is_one_of($allow_empty, $variable_name, array('allow_empty','do_not_allow_empty')); |
|
140 | - if (empty($variable_to_test) && 'allow_empty' == $allow_empty) { |
|
141 | - return; |
|
142 | - } |
|
143 | - if (!is_array($variable_to_test)) { |
|
144 | - $error[] = esc_html__('Variable %s should be an array, but it is not.', 'event_espresso'); |
|
145 | - $error[] = esc_html__("Its value is, instead '%s'", 'event_espresso'); |
|
146 | - throw new EE_Error(sprintf(implode(",", $error), $variable_name, $variable_name, $variable_to_test)); |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - |
|
152 | - |
|
153 | - |
|
154 | - |
|
155 | - |
|
156 | - /** |
|
157 | - * for verifying that a variable is one of the string optiosn supplied |
|
158 | - * @param mixed $variable_to_test |
|
159 | - * @param mixed $variable_name the name you've given the variable. Eg, '$foo'. THis helps in producing better error messages |
|
160 | - * @param array $string_options an array of acceptable values |
|
161 | - * @return void |
|
162 | - * @throws EE_Error |
|
163 | - */ |
|
164 | - public static function verify_argument_is_one_of($variable_to_test, $variable_name, $string_options) |
|
165 | - { |
|
166 | - if (!WP_DEBUG) { |
|
167 | - return; |
|
168 | - } |
|
169 | - if (!in_array($variable_to_test, $string_options)) { |
|
170 | - $msg[0] = esc_html__('Malconfigured template.', 'event_espresso'); |
|
171 | - $msg[1] = esc_html__("Variable named '%s' was set to '%s'. It can only be one of '%s'", 'event_espresso'); |
|
172 | - throw new EE_Error(sprintf(implode("||", $msg), $variable_name, $variable_to_test, implode("', '", $string_options))); |
|
173 | - } |
|
174 | - } |
|
27 | + /** |
|
28 | + * Throws an EE_Error if $variabel_to_test isn't an array of objects of class $class_name |
|
29 | + * @param mixed $variable_to_test |
|
30 | + * @param string $name_of_variable helpful in throwing intelligent errors |
|
31 | + * @param string $class_name eg EE_Answer, EE_Transaction, etc. |
|
32 | + * @param string $allow_null one of 'allow_null', or 'do_not_allow_null' |
|
33 | + * @return void |
|
34 | + * @throws EE_Error (indirectly) |
|
35 | + */ |
|
36 | + public static function verify_is_array_of($variable_to_test, $name_of_variable, $class_name, $allow_null = 'allow_null') |
|
37 | + { |
|
38 | + if (!WP_DEBUG) { |
|
39 | + return; |
|
40 | + } |
|
41 | + self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null')); |
|
42 | + if ('allow_null' == $allow_null && is_null($variable_to_test)) { |
|
43 | + return; |
|
44 | + } |
|
45 | + self::verify_is_array($variable_to_test, $name_of_variable); |
|
46 | + foreach ($variable_to_test as $key => $array_element) { |
|
47 | + self::verify_instanceof($array_element, $key, $class_name); |
|
48 | + } |
|
49 | + } |
|
50 | + |
|
51 | + |
|
52 | + |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * throws an EE_Error if $variable_to_test is null |
|
57 | + * @param mixed $variable_to_test |
|
58 | + * @param string $name_of_variable helpful for throwing intelligent errors |
|
59 | + * @return void |
|
60 | + * @throws EE_Error |
|
61 | + */ |
|
62 | + public static function verify_isnt_null($variable_to_test, $name_of_variable) |
|
63 | + { |
|
64 | + if (!WP_DEBUG) { |
|
65 | + return; |
|
66 | + } |
|
67 | + if ($variable_to_test == null && $variable_to_test != 0 && $variable_to_test != false) { |
|
68 | + $error[] = esc_html__('Variable named %s is null.', 'event_espresso'); |
|
69 | + $error[] = esc_html__("Consider looking at the stack trace to see why it wasn't set.", 'event_espresso'); |
|
70 | + throw new EE_Error(sprintf(implode(",", $error), $name_of_variable, $name_of_variable)); |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * When WP_DEBUG is activted, throws an error if $expression_to_test is false. |
|
76 | + * @param boolean $expression_to_test |
|
77 | + * @param string $expression_string_representation a string representation of your expression |
|
78 | + * for example, if your expression were $var1==23, then this should be '$var1==23' |
|
79 | + * @return void |
|
80 | + * @throws EE_Error |
|
81 | + */ |
|
82 | + public static function verify_is_true($expression_to_test, $expression_string_representation) |
|
83 | + { |
|
84 | + if (!WP_DEBUG) { |
|
85 | + return; |
|
86 | + } |
|
87 | + if (!$expression_to_test) { |
|
88 | + $error[] = esc_html__('Template error.', 'event_espresso'); |
|
89 | + $error[] = esc_html__("%s evaluated to false, but it must be true!", 'event_espresso'); |
|
90 | + throw new EE_Error(sprintf(implode(",", $error), $expression_string_representation)); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * For verifying that a variable is indeed an object of class $class_name |
|
100 | + * @param mixed $variable_to_test |
|
101 | + * @param string $name_of_variable helpful when throwing errors |
|
102 | + * @param string $class_name eg, EE_Answer, |
|
103 | + * @return void |
|
104 | + * @throws EE_Error |
|
105 | + */ |
|
106 | + public static function verify_instanceof($variable_to_test, $name_of_variable, $class_name, $allow_null = 'do_not_allow_null') |
|
107 | + { |
|
108 | + if (!WP_DEBUG) { |
|
109 | + return; |
|
110 | + } |
|
111 | + self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null')); |
|
112 | + if ($allow_null == 'allow_null' && is_null($variable_to_test)) { |
|
113 | + return; |
|
114 | + } |
|
115 | + if ($variable_to_test == null || ! ( $variable_to_test instanceof $class_name )) { |
|
116 | + $msg[] = esc_html__('Variable %s is not of the correct type.', 'event_espresso'); |
|
117 | + $msg[] = esc_html__("It should be of type %s", 'event_espresso'); |
|
118 | + throw new EE_Error(sprintf(implode(",", $msg), $name_of_variable, $name_of_variable, $class_name)); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * For verifying that a variable is indeed an array, else throw an EE_Error |
|
128 | + * @param type $variable_to_test |
|
129 | + * @param type $variable_name |
|
130 | + * @param type $allow_empty one of 'allow_empty' or 'do_not_allow_empty' |
|
131 | + * @return void |
|
132 | + * @throws EE_Error |
|
133 | + */ |
|
134 | + public static function verify_is_array($variable_to_test, $variable_name, $allow_empty = 'allow_empty') |
|
135 | + { |
|
136 | + if (!WP_DEBUG) { |
|
137 | + return; |
|
138 | + } |
|
139 | + self::verify_argument_is_one_of($allow_empty, $variable_name, array('allow_empty','do_not_allow_empty')); |
|
140 | + if (empty($variable_to_test) && 'allow_empty' == $allow_empty) { |
|
141 | + return; |
|
142 | + } |
|
143 | + if (!is_array($variable_to_test)) { |
|
144 | + $error[] = esc_html__('Variable %s should be an array, but it is not.', 'event_espresso'); |
|
145 | + $error[] = esc_html__("Its value is, instead '%s'", 'event_espresso'); |
|
146 | + throw new EE_Error(sprintf(implode(",", $error), $variable_name, $variable_name, $variable_to_test)); |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + |
|
152 | + |
|
153 | + |
|
154 | + |
|
155 | + |
|
156 | + /** |
|
157 | + * for verifying that a variable is one of the string optiosn supplied |
|
158 | + * @param mixed $variable_to_test |
|
159 | + * @param mixed $variable_name the name you've given the variable. Eg, '$foo'. THis helps in producing better error messages |
|
160 | + * @param array $string_options an array of acceptable values |
|
161 | + * @return void |
|
162 | + * @throws EE_Error |
|
163 | + */ |
|
164 | + public static function verify_argument_is_one_of($variable_to_test, $variable_name, $string_options) |
|
165 | + { |
|
166 | + if (!WP_DEBUG) { |
|
167 | + return; |
|
168 | + } |
|
169 | + if (!in_array($variable_to_test, $string_options)) { |
|
170 | + $msg[0] = esc_html__('Malconfigured template.', 'event_espresso'); |
|
171 | + $msg[1] = esc_html__("Variable named '%s' was set to '%s'. It can only be one of '%s'", 'event_espresso'); |
|
172 | + throw new EE_Error(sprintf(implode("||", $msg), $variable_name, $variable_to_test, implode("', '", $string_options))); |
|
173 | + } |
|
174 | + } |
|
175 | 175 | } |
@@ -7,26 +7,26 @@ |
||
7 | 7 | |
8 | 8 | class InvalidStatusException extends InvalidArgumentException |
9 | 9 | { |
10 | - /** |
|
11 | - * InvalidStatusException constructor. |
|
12 | - * @param string $status the invalid status id that was supplied |
|
13 | - * @param string $domain the name of the domain, model, or class that the status belongs to |
|
14 | - * @param string $message custom message |
|
15 | - * @param int $code |
|
16 | - * @param Exception|null $previous |
|
17 | - */ |
|
18 | - public function __construct($status, $domain, $message = '', $code = 0, Exception $previous = null) |
|
19 | - { |
|
20 | - if (empty($message)) { |
|
21 | - $message = sprintf( |
|
22 | - esc_html__( |
|
23 | - '"%1$s" is not a valid %2$s status', |
|
24 | - 'event_espresso' |
|
25 | - ), |
|
26 | - $status, |
|
27 | - $domain |
|
28 | - ); |
|
29 | - } |
|
30 | - parent::__construct($message, $code, $previous); |
|
31 | - } |
|
10 | + /** |
|
11 | + * InvalidStatusException constructor. |
|
12 | + * @param string $status the invalid status id that was supplied |
|
13 | + * @param string $domain the name of the domain, model, or class that the status belongs to |
|
14 | + * @param string $message custom message |
|
15 | + * @param int $code |
|
16 | + * @param Exception|null $previous |
|
17 | + */ |
|
18 | + public function __construct($status, $domain, $message = '', $code = 0, Exception $previous = null) |
|
19 | + { |
|
20 | + if (empty($message)) { |
|
21 | + $message = sprintf( |
|
22 | + esc_html__( |
|
23 | + '"%1$s" is not a valid %2$s status', |
|
24 | + 'event_espresso' |
|
25 | + ), |
|
26 | + $status, |
|
27 | + $domain |
|
28 | + ); |
|
29 | + } |
|
30 | + parent::__construct($message, $code, $previous); |
|
31 | + } |
|
32 | 32 | } |