@@ -18,84 +18,84 @@ |
||
18 | 18 | class CapabilitiesChecker implements CapabilitiesCheckerInterface |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @type EE_Capabilities $capabilities |
|
23 | - */ |
|
24 | - private $capabilities; |
|
21 | + /** |
|
22 | + * @type EE_Capabilities $capabilities |
|
23 | + */ |
|
24 | + private $capabilities; |
|
25 | 25 | |
26 | 26 | |
27 | - /** |
|
28 | - * CapabilitiesChecker constructor |
|
29 | - * |
|
30 | - * @param EE_Capabilities $capabilities |
|
31 | - */ |
|
32 | - public function __construct(EE_Capabilities $capabilities) |
|
33 | - { |
|
34 | - $this->capabilities = $capabilities; |
|
35 | - } |
|
27 | + /** |
|
28 | + * CapabilitiesChecker constructor |
|
29 | + * |
|
30 | + * @param EE_Capabilities $capabilities |
|
31 | + */ |
|
32 | + public function __construct(EE_Capabilities $capabilities) |
|
33 | + { |
|
34 | + $this->capabilities = $capabilities; |
|
35 | + } |
|
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * @return EE_Capabilities |
|
40 | - */ |
|
41 | - protected function capabilities() |
|
42 | - { |
|
43 | - return $this->capabilities; |
|
44 | - } |
|
38 | + /** |
|
39 | + * @return EE_Capabilities |
|
40 | + */ |
|
41 | + protected function capabilities() |
|
42 | + { |
|
43 | + return $this->capabilities; |
|
44 | + } |
|
45 | 45 | |
46 | 46 | |
47 | - /** |
|
48 | - * Verifies that the current user has ALL of the capabilities listed in the CapCheck DTO. |
|
49 | - * If any of the individual capability checks fails, then the command will NOT be executed. |
|
50 | - * |
|
51 | - * @param CapCheckInterface|CapCheckInterface[] $cap_check |
|
52 | - * @return bool |
|
53 | - * @throws InvalidClassException |
|
54 | - * @throws InsufficientPermissionsException |
|
55 | - */ |
|
56 | - public function processCapCheck($cap_check) |
|
57 | - { |
|
58 | - if (is_array($cap_check)) { |
|
59 | - foreach ($cap_check as $check) { |
|
60 | - $this->processCapCheck($check); |
|
61 | - } |
|
62 | - return true; |
|
63 | - } |
|
64 | - // at this point, $cap_check should be an individual instance of CapCheck |
|
65 | - if (! $cap_check instanceof CapCheckInterface) { |
|
66 | - throw new InvalidClassException( |
|
67 | - '\EventEspresso\core\domain\services\capabilities\CapCheckInterface' |
|
68 | - ); |
|
69 | - } |
|
70 | - // sometimes cap checks are conditional, and no capabilities are required |
|
71 | - if ($cap_check instanceof PublicCapabilities) { |
|
72 | - return true; |
|
73 | - } |
|
74 | - $capabilities = (array) $cap_check->capability(); |
|
75 | - foreach ($capabilities as $capability) { |
|
76 | - if (! $this->capabilities()->current_user_can( |
|
77 | - $capability, |
|
78 | - $cap_check->context(), |
|
79 | - $cap_check->ID() |
|
80 | - )) { |
|
81 | - throw new InsufficientPermissionsException($cap_check->context()); |
|
82 | - } |
|
83 | - } |
|
84 | - return true; |
|
85 | - } |
|
47 | + /** |
|
48 | + * Verifies that the current user has ALL of the capabilities listed in the CapCheck DTO. |
|
49 | + * If any of the individual capability checks fails, then the command will NOT be executed. |
|
50 | + * |
|
51 | + * @param CapCheckInterface|CapCheckInterface[] $cap_check |
|
52 | + * @return bool |
|
53 | + * @throws InvalidClassException |
|
54 | + * @throws InsufficientPermissionsException |
|
55 | + */ |
|
56 | + public function processCapCheck($cap_check) |
|
57 | + { |
|
58 | + if (is_array($cap_check)) { |
|
59 | + foreach ($cap_check as $check) { |
|
60 | + $this->processCapCheck($check); |
|
61 | + } |
|
62 | + return true; |
|
63 | + } |
|
64 | + // at this point, $cap_check should be an individual instance of CapCheck |
|
65 | + if (! $cap_check instanceof CapCheckInterface) { |
|
66 | + throw new InvalidClassException( |
|
67 | + '\EventEspresso\core\domain\services\capabilities\CapCheckInterface' |
|
68 | + ); |
|
69 | + } |
|
70 | + // sometimes cap checks are conditional, and no capabilities are required |
|
71 | + if ($cap_check instanceof PublicCapabilities) { |
|
72 | + return true; |
|
73 | + } |
|
74 | + $capabilities = (array) $cap_check->capability(); |
|
75 | + foreach ($capabilities as $capability) { |
|
76 | + if (! $this->capabilities()->current_user_can( |
|
77 | + $capability, |
|
78 | + $cap_check->context(), |
|
79 | + $cap_check->ID() |
|
80 | + )) { |
|
81 | + throw new InsufficientPermissionsException($cap_check->context()); |
|
82 | + } |
|
83 | + } |
|
84 | + return true; |
|
85 | + } |
|
86 | 86 | |
87 | 87 | |
88 | - /** |
|
89 | - * @param string $capability - the capability to be checked, like: 'ee_edit_registrations' |
|
90 | - * @param string $context - what the user is attempting to do, like: 'Edit Registration' |
|
91 | - * @param int $ID - (optional) ID for item where current_user_can is being called from |
|
92 | - * @return bool |
|
93 | - * @throws InvalidDataTypeException |
|
94 | - * @throws InsufficientPermissionsException |
|
95 | - * @throws InvalidClassException |
|
96 | - */ |
|
97 | - public function process($capability, $context, $ID = 0) |
|
98 | - { |
|
99 | - return $this->processCapCheck(new CapCheck($capability, $context, $ID)); |
|
100 | - } |
|
88 | + /** |
|
89 | + * @param string $capability - the capability to be checked, like: 'ee_edit_registrations' |
|
90 | + * @param string $context - what the user is attempting to do, like: 'Edit Registration' |
|
91 | + * @param int $ID - (optional) ID for item where current_user_can is being called from |
|
92 | + * @return bool |
|
93 | + * @throws InvalidDataTypeException |
|
94 | + * @throws InsufficientPermissionsException |
|
95 | + * @throws InvalidClassException |
|
96 | + */ |
|
97 | + public function process($capability, $context, $ID = 0) |
|
98 | + { |
|
99 | + return $this->processCapCheck(new CapCheck($capability, $context, $ID)); |
|
100 | + } |
|
101 | 101 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | return true; |
63 | 63 | } |
64 | 64 | // at this point, $cap_check should be an individual instance of CapCheck |
65 | - if (! $cap_check instanceof CapCheckInterface) { |
|
65 | + if ( ! $cap_check instanceof CapCheckInterface) { |
|
66 | 66 | throw new InvalidClassException( |
67 | 67 | '\EventEspresso\core\domain\services\capabilities\CapCheckInterface' |
68 | 68 | ); |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | } |
74 | 74 | $capabilities = (array) $cap_check->capability(); |
75 | 75 | foreach ($capabilities as $capability) { |
76 | - if (! $this->capabilities()->current_user_can( |
|
76 | + if ( ! $this->capabilities()->current_user_can( |
|
77 | 77 | $capability, |
78 | 78 | $cap_check->context(), |
79 | 79 | $cap_check->ID() |
@@ -27,84 +27,84 @@ |
||
27 | 27 | { |
28 | 28 | |
29 | 29 | |
30 | - protected $attendee; |
|
30 | + protected $attendee; |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * AttendeeContactDetailsMetaboxFormHandler constructor. |
|
35 | - * |
|
36 | - * @param EE_Attendee $attendee |
|
37 | - * @param EE_Registry $registry |
|
38 | - * @throws DomainException |
|
39 | - * @throws InvalidDataTypeException |
|
40 | - * @throws InvalidArgumentException |
|
41 | - */ |
|
42 | - public function __construct(EE_Attendee $attendee, EE_Registry $registry) |
|
43 | - { |
|
44 | - $this->attendee = $attendee; |
|
45 | - $label = esc_html__('Contact Details', 'event_espresso'); |
|
46 | - parent::__construct( |
|
47 | - $label, |
|
48 | - $label, |
|
49 | - 'attendee_contact_details', |
|
50 | - '', |
|
51 | - FormHandler::DO_NOT_SETUP_FORM, |
|
52 | - $registry |
|
53 | - ); |
|
54 | - } |
|
33 | + /** |
|
34 | + * AttendeeContactDetailsMetaboxFormHandler constructor. |
|
35 | + * |
|
36 | + * @param EE_Attendee $attendee |
|
37 | + * @param EE_Registry $registry |
|
38 | + * @throws DomainException |
|
39 | + * @throws InvalidDataTypeException |
|
40 | + * @throws InvalidArgumentException |
|
41 | + */ |
|
42 | + public function __construct(EE_Attendee $attendee, EE_Registry $registry) |
|
43 | + { |
|
44 | + $this->attendee = $attendee; |
|
45 | + $label = esc_html__('Contact Details', 'event_espresso'); |
|
46 | + parent::__construct( |
|
47 | + $label, |
|
48 | + $label, |
|
49 | + 'attendee_contact_details', |
|
50 | + '', |
|
51 | + FormHandler::DO_NOT_SETUP_FORM, |
|
52 | + $registry |
|
53 | + ); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * creates and returns the actual form |
|
58 | - * |
|
59 | - * @return EE_Form_Section_Proper |
|
60 | - * @throws EE_Error |
|
61 | - */ |
|
62 | - public function generate() |
|
63 | - { |
|
64 | - return new EE_Form_Section_Proper( |
|
65 | - array( |
|
66 | - 'name' => 'attendee_contact_details', |
|
67 | - 'html_id' => 'attendee-contact-details', |
|
68 | - 'html_class' => 'form-table', |
|
69 | - 'layout_strategy' => new EE_Admin_One_Column_Layout(), |
|
70 | - 'subsections' => array( |
|
71 | - 'ATT_email' => new EE_Email_Input( |
|
72 | - array( |
|
73 | - 'default' => $this->attendee->email(), |
|
74 | - 'html_label_text' => esc_html__('Email Address', 'event_espresso'), |
|
75 | - 'required' => true, |
|
76 | - ) |
|
77 | - ), |
|
78 | - 'ATT_phone' => new EE_Text_Input( |
|
79 | - array( |
|
80 | - 'default' => $this->attendee->phone(), |
|
81 | - 'html_label_text' => esc_html__('Phone Number', 'event_espresso'), |
|
82 | - ) |
|
83 | - ), |
|
84 | - ), |
|
85 | - ) |
|
86 | - ); |
|
87 | - } |
|
56 | + /** |
|
57 | + * creates and returns the actual form |
|
58 | + * |
|
59 | + * @return EE_Form_Section_Proper |
|
60 | + * @throws EE_Error |
|
61 | + */ |
|
62 | + public function generate() |
|
63 | + { |
|
64 | + return new EE_Form_Section_Proper( |
|
65 | + array( |
|
66 | + 'name' => 'attendee_contact_details', |
|
67 | + 'html_id' => 'attendee-contact-details', |
|
68 | + 'html_class' => 'form-table', |
|
69 | + 'layout_strategy' => new EE_Admin_One_Column_Layout(), |
|
70 | + 'subsections' => array( |
|
71 | + 'ATT_email' => new EE_Email_Input( |
|
72 | + array( |
|
73 | + 'default' => $this->attendee->email(), |
|
74 | + 'html_label_text' => esc_html__('Email Address', 'event_espresso'), |
|
75 | + 'required' => true, |
|
76 | + ) |
|
77 | + ), |
|
78 | + 'ATT_phone' => new EE_Text_Input( |
|
79 | + array( |
|
80 | + 'default' => $this->attendee->phone(), |
|
81 | + 'html_label_text' => esc_html__('Phone Number', 'event_espresso'), |
|
82 | + ) |
|
83 | + ), |
|
84 | + ), |
|
85 | + ) |
|
86 | + ); |
|
87 | + } |
|
88 | 88 | |
89 | 89 | |
90 | - /** |
|
91 | - * Process form data. |
|
92 | - * |
|
93 | - * @param array $form_data |
|
94 | - * @return bool |
|
95 | - * @throws EE_Error |
|
96 | - * @throws InvalidFormSubmissionException |
|
97 | - * @throws LogicException |
|
98 | - */ |
|
99 | - public function process($form_data = array()) |
|
100 | - { |
|
101 | - $valid_data = (array) parent::process($form_data); |
|
102 | - if (empty($valid_data)) { |
|
103 | - return false; |
|
104 | - } |
|
105 | - $this->attendee->set_email($valid_data['ATT_email']); |
|
106 | - $this->attendee->set_phone($valid_data['ATT_phone']); |
|
107 | - $updated = $this->attendee->save(); |
|
108 | - return $updated !== false; |
|
109 | - } |
|
90 | + /** |
|
91 | + * Process form data. |
|
92 | + * |
|
93 | + * @param array $form_data |
|
94 | + * @return bool |
|
95 | + * @throws EE_Error |
|
96 | + * @throws InvalidFormSubmissionException |
|
97 | + * @throws LogicException |
|
98 | + */ |
|
99 | + public function process($form_data = array()) |
|
100 | + { |
|
101 | + $valid_data = (array) parent::process($form_data); |
|
102 | + if (empty($valid_data)) { |
|
103 | + return false; |
|
104 | + } |
|
105 | + $this->attendee->set_email($valid_data['ATT_email']); |
|
106 | + $this->attendee->set_phone($valid_data['ATT_phone']); |
|
107 | + $updated = $this->attendee->save(); |
|
108 | + return $updated !== false; |
|
109 | + } |
|
110 | 110 | } |
@@ -15,56 +15,56 @@ |
||
15 | 15 | class EmailAddress |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var string $email_address |
|
20 | - */ |
|
21 | - private $email_address; |
|
18 | + /** |
|
19 | + * @var string $email_address |
|
20 | + */ |
|
21 | + private $email_address; |
|
22 | 22 | |
23 | 23 | |
24 | - /** |
|
25 | - * EmailAddress constructor. |
|
26 | - * |
|
27 | - * @param string $email_address |
|
28 | - * @param EmailValidatorInterface $validator |
|
29 | - * @throws EmailValidationException |
|
30 | - */ |
|
31 | - public function __construct($email_address, EmailValidatorInterface $validator) |
|
32 | - { |
|
33 | - $validator->validate($email_address); |
|
34 | - $this->email_address = $email_address; |
|
35 | - } |
|
24 | + /** |
|
25 | + * EmailAddress constructor. |
|
26 | + * |
|
27 | + * @param string $email_address |
|
28 | + * @param EmailValidatorInterface $validator |
|
29 | + * @throws EmailValidationException |
|
30 | + */ |
|
31 | + public function __construct($email_address, EmailValidatorInterface $validator) |
|
32 | + { |
|
33 | + $validator->validate($email_address); |
|
34 | + $this->email_address = $email_address; |
|
35 | + } |
|
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * returns the string value for this EmailAddress |
|
40 | - * |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function get() |
|
44 | - { |
|
45 | - return $this->email_address; |
|
46 | - } |
|
38 | + /** |
|
39 | + * returns the string value for this EmailAddress |
|
40 | + * |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function get() |
|
44 | + { |
|
45 | + return $this->email_address; |
|
46 | + } |
|
47 | 47 | |
48 | 48 | |
49 | - /** |
|
50 | - * compare another EmailAddress to this one to determine if they are the same |
|
51 | - * |
|
52 | - * @param EmailAddress $address |
|
53 | - * @return bool |
|
54 | - */ |
|
55 | - public function equals(EmailAddress $address) |
|
56 | - { |
|
57 | - return strtolower((string) $this) === strtolower((string) $address); |
|
58 | - } |
|
49 | + /** |
|
50 | + * compare another EmailAddress to this one to determine if they are the same |
|
51 | + * |
|
52 | + * @param EmailAddress $address |
|
53 | + * @return bool |
|
54 | + */ |
|
55 | + public function equals(EmailAddress $address) |
|
56 | + { |
|
57 | + return strtolower((string) $this) === strtolower((string) $address); |
|
58 | + } |
|
59 | 59 | |
60 | 60 | |
61 | - /** |
|
62 | - * allows an EmailAddress object to be used as a string |
|
63 | - * |
|
64 | - * @return string |
|
65 | - */ |
|
66 | - public function __toString() |
|
67 | - { |
|
68 | - return $this->email_address; |
|
69 | - } |
|
61 | + /** |
|
62 | + * allows an EmailAddress object to be used as a string |
|
63 | + * |
|
64 | + * @return string |
|
65 | + */ |
|
66 | + public function __toString() |
|
67 | + { |
|
68 | + return $this->email_address; |
|
69 | + } |
|
70 | 70 | } |
@@ -17,339 +17,339 @@ |
||
17 | 17 | */ |
18 | 18 | class CustomSelects |
19 | 19 | { |
20 | - const TYPE_SIMPLE = 'simple'; |
|
21 | - const TYPE_COMPLEX = 'complex'; |
|
22 | - const TYPE_STRUCTURED = 'structured'; |
|
23 | - |
|
24 | - private $valid_operators = array('COUNT', 'SUM'); |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * Original incoming select array |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - private $original_selects; |
|
33 | - |
|
34 | - /** |
|
35 | - * Select string that can be added to the query |
|
36 | - * |
|
37 | - * @var string |
|
38 | - */ |
|
39 | - private $columns_to_select_expression; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * An array of aliases for the columns included in the incoming select array. |
|
44 | - * |
|
45 | - * @var array |
|
46 | - */ |
|
47 | - private $column_aliases_in_select; |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Enum representation of the "type" of array coming into this value object. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - private $type = ''; |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * CustomSelects constructor. |
|
60 | - * Incoming selects can be in one of the following formats: |
|
61 | - * ---- self::TYPE_SIMPLE array ---- |
|
62 | - * This is considered the "simple" type. In this case the array is an numerically indexed array with single or |
|
63 | - * multiple columns to select as the values. |
|
64 | - * eg. array( 'ATT_ID', 'REG_ID' ) |
|
65 | - * eg. array( '*' ) |
|
66 | - * If you want to use the columns in any WHERE, GROUP BY, or HAVING clauses, you must instead use the "complex" or |
|
67 | - * "structured" method. |
|
68 | - * ---- self::TYPE_COMPLEX array ---- |
|
69 | - * This is considered the "complex" type. In this case the array is indexed by arbitrary strings that serve as |
|
70 | - * column alias, and the value is an numerically indexed array where there are two values. The first value (0) is |
|
71 | - * the selection and the second value (1) is the data type. Data types must be one of the types defined in |
|
72 | - * EEM_Base::$_valid_wpdb_data_types. |
|
73 | - * eg. array( 'count' => array('count(REG_ID)', '%d') ) |
|
74 | - * Complex array configuration allows for using the column alias in any WHERE, GROUP BY, or HAVING clauses. |
|
75 | - * ---- self::TYPE_STRUCTURED array --- |
|
76 | - * This is considered the "structured" type. This type is similar to the complex type except that the array attached |
|
77 | - * to the column alias contains three values. The first value is the qualified column name (which can include |
|
78 | - * join syntax for models). The second value is the operator performed on the column (i.e. 'COUNT', 'SUM' etc)., |
|
79 | - * the third value is the data type. Note, if the select does not have an operator, you can use an empty string for |
|
80 | - * the second value. |
|
81 | - * Note: for now SUM is only for simple single column expressions (i.e. SUM(Transaction.TXN_total)) |
|
82 | - * eg. array( 'registration_count' => array('Registration.REG_ID', 'count', '%d') ); |
|
83 | - * NOTE: mixing array types in the incoming $select will cause errors. |
|
84 | - * |
|
85 | - * @param array $selects |
|
86 | - * @throws InvalidArgumentException |
|
87 | - */ |
|
88 | - public function __construct(array $selects) |
|
89 | - { |
|
90 | - $this->original_selects = $selects; |
|
91 | - $this->deriveType($selects); |
|
92 | - $this->deriveParts($selects); |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * Derives what type of custom select has been sent in. |
|
98 | - * |
|
99 | - * @param array $selects |
|
100 | - * @throws InvalidArgumentException |
|
101 | - */ |
|
102 | - private function deriveType(array $selects) |
|
103 | - { |
|
104 | - // first if the first key for this array is an integer then its coming in as a simple format, so we'll also |
|
105 | - // ensure all elements of the array are simple. |
|
106 | - if (is_int(key($selects))) { |
|
107 | - // let's ensure all keys are ints |
|
108 | - $invalid_keys = array_filter( |
|
109 | - array_keys($selects), |
|
110 | - function ($value) { |
|
111 | - return ! is_int($value); |
|
112 | - } |
|
113 | - ); |
|
114 | - if (! empty($invalid_keys)) { |
|
115 | - throw new InvalidArgumentException( |
|
116 | - sprintf( |
|
117 | - esc_html__( |
|
118 | - 'Incoming array looks like its formatted for "%1$s" type selects, however it has elements that are not indexed numerically', |
|
119 | - 'event_espresso' |
|
120 | - ), |
|
121 | - self::TYPE_SIMPLE |
|
122 | - ) |
|
123 | - ); |
|
124 | - } |
|
125 | - $this->type = self::TYPE_SIMPLE; |
|
126 | - return; |
|
127 | - } |
|
128 | - // made it here so that means we've got either complex or structured selects. Let's find out which by popping |
|
129 | - // the first array element off. |
|
130 | - $first_element = reset($selects); |
|
131 | - |
|
132 | - if (! is_array($first_element)) { |
|
133 | - throw new InvalidArgumentException( |
|
134 | - sprintf( |
|
135 | - esc_html__( |
|
136 | - 'Incoming array looks like its formatted as a "%1$s" or "%2$s" type. However, the values in the array must be arrays themselves and they are not.', |
|
137 | - 'event_espresso' |
|
138 | - ), |
|
139 | - self::TYPE_COMPLEX, |
|
140 | - self::TYPE_STRUCTURED |
|
141 | - ) |
|
142 | - ); |
|
143 | - } |
|
144 | - $this->type = count($first_element) === 2 |
|
145 | - ? self::TYPE_COMPLEX |
|
146 | - : self::TYPE_STRUCTURED; |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * Sets up the various properties for the vo depending on type. |
|
152 | - * |
|
153 | - * @param array $selects |
|
154 | - * @throws InvalidArgumentException |
|
155 | - */ |
|
156 | - private function deriveParts(array $selects) |
|
157 | - { |
|
158 | - $column_parts = array(); |
|
159 | - switch ($this->type) { |
|
160 | - case self::TYPE_SIMPLE: |
|
161 | - $column_parts = $selects; |
|
162 | - $this->column_aliases_in_select = $selects; |
|
163 | - break; |
|
164 | - case self::TYPE_COMPLEX: |
|
165 | - foreach ($selects as $alias => $parts) { |
|
166 | - $this->validateSelectValueForType($parts, $alias); |
|
167 | - $column_parts[] = "{$parts[0]} AS {$alias}"; |
|
168 | - $this->column_aliases_in_select[] = $alias; |
|
169 | - } |
|
170 | - break; |
|
171 | - case self::TYPE_STRUCTURED: |
|
172 | - foreach ($selects as $alias => $parts) { |
|
173 | - $this->validateSelectValueForType($parts, $alias); |
|
174 | - $column_parts[] = $parts[1] !== '' |
|
175 | - ? $this->assembleSelectStringWithOperator($parts, $alias) |
|
176 | - : "{$parts[0]} AS {$alias}"; |
|
177 | - $this->column_aliases_in_select[] = $alias; |
|
178 | - } |
|
179 | - break; |
|
180 | - } |
|
181 | - $this->columns_to_select_expression = implode(', ', $column_parts); |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * Validates self::TYPE_COMPLEX and self::TYPE_STRUCTURED select statement parts. |
|
187 | - * |
|
188 | - * @param array $select_parts |
|
189 | - * @param string $alias |
|
190 | - * @throws InvalidArgumentException |
|
191 | - */ |
|
192 | - private function validateSelectValueForType(array $select_parts, $alias) |
|
193 | - { |
|
194 | - $valid_data_types = array('%d', '%s', '%f'); |
|
195 | - if (count($select_parts) !== $this->expectedSelectPartCountForType()) { |
|
196 | - throw new InvalidArgumentException( |
|
197 | - sprintf( |
|
198 | - esc_html__( |
|
199 | - 'The provided select part array for the %1$s column is expected to have a count of %2$d because the incoming select array is of type %3$s. However the count was %4$d.', |
|
200 | - 'event_espresso' |
|
201 | - ), |
|
202 | - $alias, |
|
203 | - $this->expectedSelectPartCountForType(), |
|
204 | - $this->type, |
|
205 | - count($select_parts) |
|
206 | - ) |
|
207 | - ); |
|
208 | - } |
|
209 | - // validate data type. |
|
210 | - $data_type = $this->type === self::TYPE_COMPLEX ? $select_parts[1] : ''; |
|
211 | - $data_type = $this->type === self::TYPE_STRUCTURED ? $select_parts[2] : $data_type; |
|
212 | - |
|
213 | - if (! in_array($data_type, $valid_data_types, true)) { |
|
214 | - throw new InvalidArgumentException( |
|
215 | - sprintf( |
|
216 | - esc_html__( |
|
217 | - 'Datatype %1$s (for selection "%2$s" and alias "%3$s") is not a valid wpdb datatype (eg %%s)', |
|
218 | - 'event_espresso' |
|
219 | - ), |
|
220 | - $data_type, |
|
221 | - $select_parts[0], |
|
222 | - $alias, |
|
223 | - implode(', ', $valid_data_types) |
|
224 | - ) |
|
225 | - ); |
|
226 | - } |
|
227 | - } |
|
228 | - |
|
229 | - |
|
230 | - /** |
|
231 | - * Each type will have an expected count of array elements, this returns what that expected count is. |
|
232 | - * |
|
233 | - * @param string $type |
|
234 | - * @return int |
|
235 | - */ |
|
236 | - private function expectedSelectPartCountForType($type = '') |
|
237 | - { |
|
238 | - $type = $type === '' ? $this->type : $type; |
|
239 | - $types_count_map = array( |
|
240 | - self::TYPE_COMPLEX => 2, |
|
241 | - self::TYPE_STRUCTURED => 3, |
|
242 | - ); |
|
243 | - return isset($types_count_map[ $type ]) ? $types_count_map[ $type ] : 0; |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - /** |
|
248 | - * Prepares the select statement part for for structured type selects. |
|
249 | - * |
|
250 | - * @param array $select_parts |
|
251 | - * @param string $alias |
|
252 | - * @return string |
|
253 | - * @throws InvalidArgumentException |
|
254 | - */ |
|
255 | - private function assembleSelectStringWithOperator(array $select_parts, $alias) |
|
256 | - { |
|
257 | - $operator = strtoupper($select_parts[1]); |
|
258 | - // validate operator |
|
259 | - if (! in_array($operator, $this->valid_operators, true)) { |
|
260 | - throw new InvalidArgumentException( |
|
261 | - sprintf( |
|
262 | - esc_html__( |
|
263 | - 'An invalid operator has been provided (%1$s) for the column %2$s. Valid operators must be one of the following: %3$s.', |
|
264 | - 'event_espresso' |
|
265 | - ), |
|
266 | - $operator, |
|
267 | - $alias, |
|
268 | - implode(', ', $this->valid_operators) |
|
269 | - ) |
|
270 | - ); |
|
271 | - } |
|
272 | - return $operator . '(' . $select_parts[0] . ') AS ' . $alias; |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * Return the datatype from the given select part. |
|
278 | - * Remember the select_part has already been validated on object instantiation. |
|
279 | - * |
|
280 | - * @param array $select_part |
|
281 | - * @return string |
|
282 | - */ |
|
283 | - private function getDataTypeForSelectType(array $select_part) |
|
284 | - { |
|
285 | - switch ($this->type) { |
|
286 | - case self::TYPE_COMPLEX: |
|
287 | - return $select_part[1]; |
|
288 | - case self::TYPE_STRUCTURED: |
|
289 | - return $select_part[2]; |
|
290 | - default: |
|
291 | - return ''; |
|
292 | - } |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * Returns the original select array sent into the VO. |
|
298 | - * |
|
299 | - * @return array |
|
300 | - */ |
|
301 | - public function originalSelects() |
|
302 | - { |
|
303 | - return $this->original_selects; |
|
304 | - } |
|
305 | - |
|
306 | - |
|
307 | - /** |
|
308 | - * Returns the final assembled select expression derived from the incoming select array. |
|
309 | - * |
|
310 | - * @return string |
|
311 | - */ |
|
312 | - public function columnsToSelectExpression() |
|
313 | - { |
|
314 | - return $this->columns_to_select_expression; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /** |
|
319 | - * Returns all the column aliases derived from the incoming select array. |
|
320 | - * |
|
321 | - * @return array |
|
322 | - */ |
|
323 | - public function columnAliases() |
|
324 | - { |
|
325 | - return $this->column_aliases_in_select; |
|
326 | - } |
|
327 | - |
|
328 | - |
|
329 | - /** |
|
330 | - * Returns the enum type for the incoming select array. |
|
331 | - * |
|
332 | - * @return string |
|
333 | - */ |
|
334 | - public function type() |
|
335 | - { |
|
336 | - return $this->type; |
|
337 | - } |
|
338 | - |
|
339 | - |
|
340 | - /** |
|
341 | - * Return the datatype for the given column_alias |
|
342 | - * |
|
343 | - * @param string $column_alias |
|
344 | - * @return string (if there's no data type we return string as the default). |
|
345 | - */ |
|
346 | - public function getDataTypeForAlias($column_alias) |
|
347 | - { |
|
348 | - if (isset($this->original_selects[ $column_alias ]) |
|
349 | - && in_array($column_alias, $this->columnAliases(), true) |
|
350 | - ) { |
|
351 | - return $this->getDataTypeForSelectType($this->original_selects[ $column_alias ]); |
|
352 | - } |
|
353 | - return '%s'; |
|
354 | - } |
|
20 | + const TYPE_SIMPLE = 'simple'; |
|
21 | + const TYPE_COMPLEX = 'complex'; |
|
22 | + const TYPE_STRUCTURED = 'structured'; |
|
23 | + |
|
24 | + private $valid_operators = array('COUNT', 'SUM'); |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * Original incoming select array |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + private $original_selects; |
|
33 | + |
|
34 | + /** |
|
35 | + * Select string that can be added to the query |
|
36 | + * |
|
37 | + * @var string |
|
38 | + */ |
|
39 | + private $columns_to_select_expression; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * An array of aliases for the columns included in the incoming select array. |
|
44 | + * |
|
45 | + * @var array |
|
46 | + */ |
|
47 | + private $column_aliases_in_select; |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Enum representation of the "type" of array coming into this value object. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + private $type = ''; |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * CustomSelects constructor. |
|
60 | + * Incoming selects can be in one of the following formats: |
|
61 | + * ---- self::TYPE_SIMPLE array ---- |
|
62 | + * This is considered the "simple" type. In this case the array is an numerically indexed array with single or |
|
63 | + * multiple columns to select as the values. |
|
64 | + * eg. array( 'ATT_ID', 'REG_ID' ) |
|
65 | + * eg. array( '*' ) |
|
66 | + * If you want to use the columns in any WHERE, GROUP BY, or HAVING clauses, you must instead use the "complex" or |
|
67 | + * "structured" method. |
|
68 | + * ---- self::TYPE_COMPLEX array ---- |
|
69 | + * This is considered the "complex" type. In this case the array is indexed by arbitrary strings that serve as |
|
70 | + * column alias, and the value is an numerically indexed array where there are two values. The first value (0) is |
|
71 | + * the selection and the second value (1) is the data type. Data types must be one of the types defined in |
|
72 | + * EEM_Base::$_valid_wpdb_data_types. |
|
73 | + * eg. array( 'count' => array('count(REG_ID)', '%d') ) |
|
74 | + * Complex array configuration allows for using the column alias in any WHERE, GROUP BY, or HAVING clauses. |
|
75 | + * ---- self::TYPE_STRUCTURED array --- |
|
76 | + * This is considered the "structured" type. This type is similar to the complex type except that the array attached |
|
77 | + * to the column alias contains three values. The first value is the qualified column name (which can include |
|
78 | + * join syntax for models). The second value is the operator performed on the column (i.e. 'COUNT', 'SUM' etc)., |
|
79 | + * the third value is the data type. Note, if the select does not have an operator, you can use an empty string for |
|
80 | + * the second value. |
|
81 | + * Note: for now SUM is only for simple single column expressions (i.e. SUM(Transaction.TXN_total)) |
|
82 | + * eg. array( 'registration_count' => array('Registration.REG_ID', 'count', '%d') ); |
|
83 | + * NOTE: mixing array types in the incoming $select will cause errors. |
|
84 | + * |
|
85 | + * @param array $selects |
|
86 | + * @throws InvalidArgumentException |
|
87 | + */ |
|
88 | + public function __construct(array $selects) |
|
89 | + { |
|
90 | + $this->original_selects = $selects; |
|
91 | + $this->deriveType($selects); |
|
92 | + $this->deriveParts($selects); |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * Derives what type of custom select has been sent in. |
|
98 | + * |
|
99 | + * @param array $selects |
|
100 | + * @throws InvalidArgumentException |
|
101 | + */ |
|
102 | + private function deriveType(array $selects) |
|
103 | + { |
|
104 | + // first if the first key for this array is an integer then its coming in as a simple format, so we'll also |
|
105 | + // ensure all elements of the array are simple. |
|
106 | + if (is_int(key($selects))) { |
|
107 | + // let's ensure all keys are ints |
|
108 | + $invalid_keys = array_filter( |
|
109 | + array_keys($selects), |
|
110 | + function ($value) { |
|
111 | + return ! is_int($value); |
|
112 | + } |
|
113 | + ); |
|
114 | + if (! empty($invalid_keys)) { |
|
115 | + throw new InvalidArgumentException( |
|
116 | + sprintf( |
|
117 | + esc_html__( |
|
118 | + 'Incoming array looks like its formatted for "%1$s" type selects, however it has elements that are not indexed numerically', |
|
119 | + 'event_espresso' |
|
120 | + ), |
|
121 | + self::TYPE_SIMPLE |
|
122 | + ) |
|
123 | + ); |
|
124 | + } |
|
125 | + $this->type = self::TYPE_SIMPLE; |
|
126 | + return; |
|
127 | + } |
|
128 | + // made it here so that means we've got either complex or structured selects. Let's find out which by popping |
|
129 | + // the first array element off. |
|
130 | + $first_element = reset($selects); |
|
131 | + |
|
132 | + if (! is_array($first_element)) { |
|
133 | + throw new InvalidArgumentException( |
|
134 | + sprintf( |
|
135 | + esc_html__( |
|
136 | + 'Incoming array looks like its formatted as a "%1$s" or "%2$s" type. However, the values in the array must be arrays themselves and they are not.', |
|
137 | + 'event_espresso' |
|
138 | + ), |
|
139 | + self::TYPE_COMPLEX, |
|
140 | + self::TYPE_STRUCTURED |
|
141 | + ) |
|
142 | + ); |
|
143 | + } |
|
144 | + $this->type = count($first_element) === 2 |
|
145 | + ? self::TYPE_COMPLEX |
|
146 | + : self::TYPE_STRUCTURED; |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * Sets up the various properties for the vo depending on type. |
|
152 | + * |
|
153 | + * @param array $selects |
|
154 | + * @throws InvalidArgumentException |
|
155 | + */ |
|
156 | + private function deriveParts(array $selects) |
|
157 | + { |
|
158 | + $column_parts = array(); |
|
159 | + switch ($this->type) { |
|
160 | + case self::TYPE_SIMPLE: |
|
161 | + $column_parts = $selects; |
|
162 | + $this->column_aliases_in_select = $selects; |
|
163 | + break; |
|
164 | + case self::TYPE_COMPLEX: |
|
165 | + foreach ($selects as $alias => $parts) { |
|
166 | + $this->validateSelectValueForType($parts, $alias); |
|
167 | + $column_parts[] = "{$parts[0]} AS {$alias}"; |
|
168 | + $this->column_aliases_in_select[] = $alias; |
|
169 | + } |
|
170 | + break; |
|
171 | + case self::TYPE_STRUCTURED: |
|
172 | + foreach ($selects as $alias => $parts) { |
|
173 | + $this->validateSelectValueForType($parts, $alias); |
|
174 | + $column_parts[] = $parts[1] !== '' |
|
175 | + ? $this->assembleSelectStringWithOperator($parts, $alias) |
|
176 | + : "{$parts[0]} AS {$alias}"; |
|
177 | + $this->column_aliases_in_select[] = $alias; |
|
178 | + } |
|
179 | + break; |
|
180 | + } |
|
181 | + $this->columns_to_select_expression = implode(', ', $column_parts); |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * Validates self::TYPE_COMPLEX and self::TYPE_STRUCTURED select statement parts. |
|
187 | + * |
|
188 | + * @param array $select_parts |
|
189 | + * @param string $alias |
|
190 | + * @throws InvalidArgumentException |
|
191 | + */ |
|
192 | + private function validateSelectValueForType(array $select_parts, $alias) |
|
193 | + { |
|
194 | + $valid_data_types = array('%d', '%s', '%f'); |
|
195 | + if (count($select_parts) !== $this->expectedSelectPartCountForType()) { |
|
196 | + throw new InvalidArgumentException( |
|
197 | + sprintf( |
|
198 | + esc_html__( |
|
199 | + 'The provided select part array for the %1$s column is expected to have a count of %2$d because the incoming select array is of type %3$s. However the count was %4$d.', |
|
200 | + 'event_espresso' |
|
201 | + ), |
|
202 | + $alias, |
|
203 | + $this->expectedSelectPartCountForType(), |
|
204 | + $this->type, |
|
205 | + count($select_parts) |
|
206 | + ) |
|
207 | + ); |
|
208 | + } |
|
209 | + // validate data type. |
|
210 | + $data_type = $this->type === self::TYPE_COMPLEX ? $select_parts[1] : ''; |
|
211 | + $data_type = $this->type === self::TYPE_STRUCTURED ? $select_parts[2] : $data_type; |
|
212 | + |
|
213 | + if (! in_array($data_type, $valid_data_types, true)) { |
|
214 | + throw new InvalidArgumentException( |
|
215 | + sprintf( |
|
216 | + esc_html__( |
|
217 | + 'Datatype %1$s (for selection "%2$s" and alias "%3$s") is not a valid wpdb datatype (eg %%s)', |
|
218 | + 'event_espresso' |
|
219 | + ), |
|
220 | + $data_type, |
|
221 | + $select_parts[0], |
|
222 | + $alias, |
|
223 | + implode(', ', $valid_data_types) |
|
224 | + ) |
|
225 | + ); |
|
226 | + } |
|
227 | + } |
|
228 | + |
|
229 | + |
|
230 | + /** |
|
231 | + * Each type will have an expected count of array elements, this returns what that expected count is. |
|
232 | + * |
|
233 | + * @param string $type |
|
234 | + * @return int |
|
235 | + */ |
|
236 | + private function expectedSelectPartCountForType($type = '') |
|
237 | + { |
|
238 | + $type = $type === '' ? $this->type : $type; |
|
239 | + $types_count_map = array( |
|
240 | + self::TYPE_COMPLEX => 2, |
|
241 | + self::TYPE_STRUCTURED => 3, |
|
242 | + ); |
|
243 | + return isset($types_count_map[ $type ]) ? $types_count_map[ $type ] : 0; |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + /** |
|
248 | + * Prepares the select statement part for for structured type selects. |
|
249 | + * |
|
250 | + * @param array $select_parts |
|
251 | + * @param string $alias |
|
252 | + * @return string |
|
253 | + * @throws InvalidArgumentException |
|
254 | + */ |
|
255 | + private function assembleSelectStringWithOperator(array $select_parts, $alias) |
|
256 | + { |
|
257 | + $operator = strtoupper($select_parts[1]); |
|
258 | + // validate operator |
|
259 | + if (! in_array($operator, $this->valid_operators, true)) { |
|
260 | + throw new InvalidArgumentException( |
|
261 | + sprintf( |
|
262 | + esc_html__( |
|
263 | + 'An invalid operator has been provided (%1$s) for the column %2$s. Valid operators must be one of the following: %3$s.', |
|
264 | + 'event_espresso' |
|
265 | + ), |
|
266 | + $operator, |
|
267 | + $alias, |
|
268 | + implode(', ', $this->valid_operators) |
|
269 | + ) |
|
270 | + ); |
|
271 | + } |
|
272 | + return $operator . '(' . $select_parts[0] . ') AS ' . $alias; |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * Return the datatype from the given select part. |
|
278 | + * Remember the select_part has already been validated on object instantiation. |
|
279 | + * |
|
280 | + * @param array $select_part |
|
281 | + * @return string |
|
282 | + */ |
|
283 | + private function getDataTypeForSelectType(array $select_part) |
|
284 | + { |
|
285 | + switch ($this->type) { |
|
286 | + case self::TYPE_COMPLEX: |
|
287 | + return $select_part[1]; |
|
288 | + case self::TYPE_STRUCTURED: |
|
289 | + return $select_part[2]; |
|
290 | + default: |
|
291 | + return ''; |
|
292 | + } |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * Returns the original select array sent into the VO. |
|
298 | + * |
|
299 | + * @return array |
|
300 | + */ |
|
301 | + public function originalSelects() |
|
302 | + { |
|
303 | + return $this->original_selects; |
|
304 | + } |
|
305 | + |
|
306 | + |
|
307 | + /** |
|
308 | + * Returns the final assembled select expression derived from the incoming select array. |
|
309 | + * |
|
310 | + * @return string |
|
311 | + */ |
|
312 | + public function columnsToSelectExpression() |
|
313 | + { |
|
314 | + return $this->columns_to_select_expression; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /** |
|
319 | + * Returns all the column aliases derived from the incoming select array. |
|
320 | + * |
|
321 | + * @return array |
|
322 | + */ |
|
323 | + public function columnAliases() |
|
324 | + { |
|
325 | + return $this->column_aliases_in_select; |
|
326 | + } |
|
327 | + |
|
328 | + |
|
329 | + /** |
|
330 | + * Returns the enum type for the incoming select array. |
|
331 | + * |
|
332 | + * @return string |
|
333 | + */ |
|
334 | + public function type() |
|
335 | + { |
|
336 | + return $this->type; |
|
337 | + } |
|
338 | + |
|
339 | + |
|
340 | + /** |
|
341 | + * Return the datatype for the given column_alias |
|
342 | + * |
|
343 | + * @param string $column_alias |
|
344 | + * @return string (if there's no data type we return string as the default). |
|
345 | + */ |
|
346 | + public function getDataTypeForAlias($column_alias) |
|
347 | + { |
|
348 | + if (isset($this->original_selects[ $column_alias ]) |
|
349 | + && in_array($column_alias, $this->columnAliases(), true) |
|
350 | + ) { |
|
351 | + return $this->getDataTypeForSelectType($this->original_selects[ $column_alias ]); |
|
352 | + } |
|
353 | + return '%s'; |
|
354 | + } |
|
355 | 355 | } |
@@ -107,11 +107,11 @@ discard block |
||
107 | 107 | // let's ensure all keys are ints |
108 | 108 | $invalid_keys = array_filter( |
109 | 109 | array_keys($selects), |
110 | - function ($value) { |
|
110 | + function($value) { |
|
111 | 111 | return ! is_int($value); |
112 | 112 | } |
113 | 113 | ); |
114 | - if (! empty($invalid_keys)) { |
|
114 | + if ( ! empty($invalid_keys)) { |
|
115 | 115 | throw new InvalidArgumentException( |
116 | 116 | sprintf( |
117 | 117 | esc_html__( |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | // the first array element off. |
130 | 130 | $first_element = reset($selects); |
131 | 131 | |
132 | - if (! is_array($first_element)) { |
|
132 | + if ( ! is_array($first_element)) { |
|
133 | 133 | throw new InvalidArgumentException( |
134 | 134 | sprintf( |
135 | 135 | esc_html__( |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | $data_type = $this->type === self::TYPE_COMPLEX ? $select_parts[1] : ''; |
211 | 211 | $data_type = $this->type === self::TYPE_STRUCTURED ? $select_parts[2] : $data_type; |
212 | 212 | |
213 | - if (! in_array($data_type, $valid_data_types, true)) { |
|
213 | + if ( ! in_array($data_type, $valid_data_types, true)) { |
|
214 | 214 | throw new InvalidArgumentException( |
215 | 215 | sprintf( |
216 | 216 | esc_html__( |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | self::TYPE_COMPLEX => 2, |
241 | 241 | self::TYPE_STRUCTURED => 3, |
242 | 242 | ); |
243 | - return isset($types_count_map[ $type ]) ? $types_count_map[ $type ] : 0; |
|
243 | + return isset($types_count_map[$type]) ? $types_count_map[$type] : 0; |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | { |
257 | 257 | $operator = strtoupper($select_parts[1]); |
258 | 258 | // validate operator |
259 | - if (! in_array($operator, $this->valid_operators, true)) { |
|
259 | + if ( ! in_array($operator, $this->valid_operators, true)) { |
|
260 | 260 | throw new InvalidArgumentException( |
261 | 261 | sprintf( |
262 | 262 | esc_html__( |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | ) |
270 | 270 | ); |
271 | 271 | } |
272 | - return $operator . '(' . $select_parts[0] . ') AS ' . $alias; |
|
272 | + return $operator.'('.$select_parts[0].') AS '.$alias; |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | |
@@ -345,10 +345,10 @@ discard block |
||
345 | 345 | */ |
346 | 346 | public function getDataTypeForAlias($column_alias) |
347 | 347 | { |
348 | - if (isset($this->original_selects[ $column_alias ]) |
|
348 | + if (isset($this->original_selects[$column_alias]) |
|
349 | 349 | && in_array($column_alias, $this->columnAliases(), true) |
350 | 350 | ) { |
351 | - return $this->getDataTypeForSelectType($this->original_selects[ $column_alias ]); |
|
351 | + return $this->getDataTypeForSelectType($this->original_selects[$column_alias]); |
|
352 | 352 | } |
353 | 353 | return '%s'; |
354 | 354 | } |
@@ -29,108 +29,108 @@ |
||
29 | 29 | class CoreLoader implements LoaderDecoratorInterface |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * @var EE_Registry|CoffeeShop $generator |
|
34 | - */ |
|
35 | - private $generator; |
|
32 | + /** |
|
33 | + * @var EE_Registry|CoffeeShop $generator |
|
34 | + */ |
|
35 | + private $generator; |
|
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * CoreLoader constructor. |
|
40 | - * |
|
41 | - * @param EE_Registry|CoffeeShop $generator |
|
42 | - * @throws InvalidArgumentException |
|
43 | - */ |
|
44 | - public function __construct($generator) |
|
45 | - { |
|
46 | - if (! ($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
|
47 | - throw new InvalidArgumentException( |
|
48 | - esc_html__( |
|
49 | - 'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
|
50 | - 'event_espresso' |
|
51 | - ) |
|
52 | - ); |
|
53 | - } |
|
54 | - $this->generator = $generator; |
|
55 | - } |
|
38 | + /** |
|
39 | + * CoreLoader constructor. |
|
40 | + * |
|
41 | + * @param EE_Registry|CoffeeShop $generator |
|
42 | + * @throws InvalidArgumentException |
|
43 | + */ |
|
44 | + public function __construct($generator) |
|
45 | + { |
|
46 | + if (! ($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
|
47 | + throw new InvalidArgumentException( |
|
48 | + esc_html__( |
|
49 | + 'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
|
50 | + 'event_espresso' |
|
51 | + ) |
|
52 | + ); |
|
53 | + } |
|
54 | + $this->generator = $generator; |
|
55 | + } |
|
56 | 56 | |
57 | 57 | |
58 | - /** |
|
59 | - * Calls the appropriate loading method from the installed generator; |
|
60 | - * If EE_Registry is being used, then the additional parameters for the EE_Registry::create() method |
|
61 | - * can be added to the $arguments array and they will be extracted and passed to EE_Registry::create(), |
|
62 | - * but NOT to the class being instantiated. |
|
63 | - * This is done by adding the parameters to the $arguments array as follows: |
|
64 | - * array( |
|
65 | - * 'EE_Registry::create(from_db)' => true, // boolean value, default = false |
|
66 | - * 'EE_Registry::create(load_only)' => true, // boolean value, default = false |
|
67 | - * 'EE_Registry::create(addon)' => true, // boolean value, default = false |
|
68 | - * ) |
|
69 | - * |
|
70 | - * @param string $fqcn |
|
71 | - * @param array $arguments |
|
72 | - * @param bool $shared |
|
73 | - * @return mixed |
|
74 | - * @throws OutOfBoundsException |
|
75 | - * @throws ServiceExistsException |
|
76 | - * @throws InstantiationException |
|
77 | - * @throws InvalidIdentifierException |
|
78 | - * @throws InvalidDataTypeException |
|
79 | - * @throws InvalidClassException |
|
80 | - * @throws EE_Error |
|
81 | - * @throws ServiceNotFoundException |
|
82 | - * @throws ReflectionException |
|
83 | - * @throws InvalidInterfaceException |
|
84 | - * @throws InvalidArgumentException |
|
85 | - */ |
|
86 | - public function load($fqcn, $arguments = array(), $shared = true) |
|
87 | - { |
|
88 | - $shared = filter_var($shared, FILTER_VALIDATE_BOOLEAN); |
|
89 | - if ($this->generator instanceof EE_Registry) { |
|
90 | - // check if additional EE_Registry::create() arguments have been passed |
|
91 | - // from_db |
|
92 | - $from_db = isset($arguments['EE_Registry::create(from_db)']) |
|
93 | - ? filter_var($arguments['EE_Registry::create(from_db)'], FILTER_VALIDATE_BOOLEAN) |
|
94 | - : false; |
|
95 | - // load_only |
|
96 | - $load_only = isset($arguments['EE_Registry::create(load_only)']) |
|
97 | - ? filter_var($arguments['EE_Registry::create(load_only)'], FILTER_VALIDATE_BOOLEAN) |
|
98 | - : false; |
|
99 | - // addon |
|
100 | - $addon = isset($arguments['EE_Registry::create(addon)']) |
|
101 | - ? filter_var($arguments['EE_Registry::create(addon)'], FILTER_VALIDATE_BOOLEAN) |
|
102 | - : false; |
|
103 | - unset( |
|
104 | - $arguments['EE_Registry::create(from_db)'], |
|
105 | - $arguments['EE_Registry::create(load_only)'], |
|
106 | - $arguments['EE_Registry::create(addon)'] |
|
107 | - ); |
|
108 | - // addons need to be cached on EE_Registry |
|
109 | - $shared = $addon ? true : $shared; |
|
110 | - return $this->generator->create( |
|
111 | - $fqcn, |
|
112 | - $arguments, |
|
113 | - $shared, |
|
114 | - $from_db, |
|
115 | - $load_only, |
|
116 | - $addon |
|
117 | - ); |
|
118 | - } |
|
119 | - return $this->generator->brew( |
|
120 | - $fqcn, |
|
121 | - $arguments, |
|
122 | - $shared ? CoffeeMaker::BREW_SHARED : CoffeeMaker::BREW_NEW |
|
123 | - ); |
|
124 | - } |
|
58 | + /** |
|
59 | + * Calls the appropriate loading method from the installed generator; |
|
60 | + * If EE_Registry is being used, then the additional parameters for the EE_Registry::create() method |
|
61 | + * can be added to the $arguments array and they will be extracted and passed to EE_Registry::create(), |
|
62 | + * but NOT to the class being instantiated. |
|
63 | + * This is done by adding the parameters to the $arguments array as follows: |
|
64 | + * array( |
|
65 | + * 'EE_Registry::create(from_db)' => true, // boolean value, default = false |
|
66 | + * 'EE_Registry::create(load_only)' => true, // boolean value, default = false |
|
67 | + * 'EE_Registry::create(addon)' => true, // boolean value, default = false |
|
68 | + * ) |
|
69 | + * |
|
70 | + * @param string $fqcn |
|
71 | + * @param array $arguments |
|
72 | + * @param bool $shared |
|
73 | + * @return mixed |
|
74 | + * @throws OutOfBoundsException |
|
75 | + * @throws ServiceExistsException |
|
76 | + * @throws InstantiationException |
|
77 | + * @throws InvalidIdentifierException |
|
78 | + * @throws InvalidDataTypeException |
|
79 | + * @throws InvalidClassException |
|
80 | + * @throws EE_Error |
|
81 | + * @throws ServiceNotFoundException |
|
82 | + * @throws ReflectionException |
|
83 | + * @throws InvalidInterfaceException |
|
84 | + * @throws InvalidArgumentException |
|
85 | + */ |
|
86 | + public function load($fqcn, $arguments = array(), $shared = true) |
|
87 | + { |
|
88 | + $shared = filter_var($shared, FILTER_VALIDATE_BOOLEAN); |
|
89 | + if ($this->generator instanceof EE_Registry) { |
|
90 | + // check if additional EE_Registry::create() arguments have been passed |
|
91 | + // from_db |
|
92 | + $from_db = isset($arguments['EE_Registry::create(from_db)']) |
|
93 | + ? filter_var($arguments['EE_Registry::create(from_db)'], FILTER_VALIDATE_BOOLEAN) |
|
94 | + : false; |
|
95 | + // load_only |
|
96 | + $load_only = isset($arguments['EE_Registry::create(load_only)']) |
|
97 | + ? filter_var($arguments['EE_Registry::create(load_only)'], FILTER_VALIDATE_BOOLEAN) |
|
98 | + : false; |
|
99 | + // addon |
|
100 | + $addon = isset($arguments['EE_Registry::create(addon)']) |
|
101 | + ? filter_var($arguments['EE_Registry::create(addon)'], FILTER_VALIDATE_BOOLEAN) |
|
102 | + : false; |
|
103 | + unset( |
|
104 | + $arguments['EE_Registry::create(from_db)'], |
|
105 | + $arguments['EE_Registry::create(load_only)'], |
|
106 | + $arguments['EE_Registry::create(addon)'] |
|
107 | + ); |
|
108 | + // addons need to be cached on EE_Registry |
|
109 | + $shared = $addon ? true : $shared; |
|
110 | + return $this->generator->create( |
|
111 | + $fqcn, |
|
112 | + $arguments, |
|
113 | + $shared, |
|
114 | + $from_db, |
|
115 | + $load_only, |
|
116 | + $addon |
|
117 | + ); |
|
118 | + } |
|
119 | + return $this->generator->brew( |
|
120 | + $fqcn, |
|
121 | + $arguments, |
|
122 | + $shared ? CoffeeMaker::BREW_SHARED : CoffeeMaker::BREW_NEW |
|
123 | + ); |
|
124 | + } |
|
125 | 125 | |
126 | 126 | |
127 | - /** |
|
128 | - * calls reset() on generator if method exists |
|
129 | - */ |
|
130 | - public function reset() |
|
131 | - { |
|
132 | - if ($this->generator instanceof ResettableInterface) { |
|
133 | - $this->generator->reset(); |
|
134 | - } |
|
135 | - } |
|
127 | + /** |
|
128 | + * calls reset() on generator if method exists |
|
129 | + */ |
|
130 | + public function reset() |
|
131 | + { |
|
132 | + if ($this->generator instanceof ResettableInterface) { |
|
133 | + $this->generator->reset(); |
|
134 | + } |
|
135 | + } |
|
136 | 136 | } |
@@ -15,115 +15,115 @@ |
||
15 | 15 | class Loader implements LoaderInterface |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var LoaderDecoratorInterface $new_loader |
|
20 | - */ |
|
21 | - private $new_loader; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var LoaderDecoratorInterface $shared_loader |
|
25 | - */ |
|
26 | - private $shared_loader; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var ClassInterfaceCache $class_cache |
|
30 | - */ |
|
31 | - private $class_cache; |
|
32 | - |
|
33 | - /** |
|
34 | - * Loader constructor. |
|
35 | - * |
|
36 | - * @param LoaderDecoratorInterface $new_loader |
|
37 | - * @param CachingLoaderDecoratorInterface $shared_loader |
|
38 | - * @param ClassInterfaceCache $class_cache |
|
39 | - */ |
|
40 | - public function __construct( |
|
41 | - LoaderDecoratorInterface $new_loader, |
|
42 | - CachingLoaderDecoratorInterface $shared_loader, |
|
43 | - ClassInterfaceCache $class_cache |
|
44 | - ) { |
|
45 | - $this->new_loader = $new_loader; |
|
46 | - $this->shared_loader = $shared_loader; |
|
47 | - $this->class_cache = $class_cache; |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * @return LoaderDecoratorInterface |
|
53 | - */ |
|
54 | - public function getNewLoader() |
|
55 | - { |
|
56 | - return $this->new_loader; |
|
57 | - } |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * @return CachingLoaderDecoratorInterface |
|
62 | - */ |
|
63 | - public function getSharedLoader() |
|
64 | - { |
|
65 | - return $this->shared_loader; |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * @param FullyQualifiedName|string $fqcn |
|
71 | - * @param array $arguments |
|
72 | - * @param bool $shared |
|
73 | - * @return mixed |
|
74 | - */ |
|
75 | - public function load($fqcn, array $arguments = array(), $shared = true) |
|
76 | - { |
|
77 | - $fqcn = $this->class_cache->getFqn($fqcn); |
|
78 | - if ($this->class_cache->hasInterface($fqcn, 'EventEspresso\core\interfaces\ReservedInstanceInterface')) { |
|
79 | - $shared = true; |
|
80 | - } |
|
81 | - return $shared |
|
82 | - ? $this->getSharedLoader()->load($fqcn, $arguments, $shared) |
|
83 | - : $this->getNewLoader()->load($fqcn, $arguments, $shared); |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * @param FullyQualifiedName|string $fqcn |
|
89 | - * @param array $arguments |
|
90 | - * @return mixed |
|
91 | - */ |
|
92 | - public function getNew($fqcn, array $arguments = array()) |
|
93 | - { |
|
94 | - return $this->load($fqcn, $arguments, false); |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * @param FullyQualifiedName|string $fqcn |
|
100 | - * @param array $arguments |
|
101 | - * @return mixed |
|
102 | - */ |
|
103 | - public function getShared($fqcn, array $arguments = array()) |
|
104 | - { |
|
105 | - return $this->load($fqcn, $arguments); |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * @param FullyQualifiedName|string $fqcn |
|
111 | - * @param mixed $object |
|
112 | - * @return bool |
|
113 | - * @throws InvalidArgumentException |
|
114 | - */ |
|
115 | - public function share($fqcn, $object) |
|
116 | - { |
|
117 | - $fqcn = $this->class_cache->getFqn($fqcn); |
|
118 | - return $this->getSharedLoader()->share($fqcn, $object); |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * calls reset() on loaders if that method exists |
|
124 | - */ |
|
125 | - public function reset() |
|
126 | - { |
|
127 | - $this->shared_loader->reset(); |
|
128 | - } |
|
18 | + /** |
|
19 | + * @var LoaderDecoratorInterface $new_loader |
|
20 | + */ |
|
21 | + private $new_loader; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var LoaderDecoratorInterface $shared_loader |
|
25 | + */ |
|
26 | + private $shared_loader; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var ClassInterfaceCache $class_cache |
|
30 | + */ |
|
31 | + private $class_cache; |
|
32 | + |
|
33 | + /** |
|
34 | + * Loader constructor. |
|
35 | + * |
|
36 | + * @param LoaderDecoratorInterface $new_loader |
|
37 | + * @param CachingLoaderDecoratorInterface $shared_loader |
|
38 | + * @param ClassInterfaceCache $class_cache |
|
39 | + */ |
|
40 | + public function __construct( |
|
41 | + LoaderDecoratorInterface $new_loader, |
|
42 | + CachingLoaderDecoratorInterface $shared_loader, |
|
43 | + ClassInterfaceCache $class_cache |
|
44 | + ) { |
|
45 | + $this->new_loader = $new_loader; |
|
46 | + $this->shared_loader = $shared_loader; |
|
47 | + $this->class_cache = $class_cache; |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * @return LoaderDecoratorInterface |
|
53 | + */ |
|
54 | + public function getNewLoader() |
|
55 | + { |
|
56 | + return $this->new_loader; |
|
57 | + } |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * @return CachingLoaderDecoratorInterface |
|
62 | + */ |
|
63 | + public function getSharedLoader() |
|
64 | + { |
|
65 | + return $this->shared_loader; |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * @param FullyQualifiedName|string $fqcn |
|
71 | + * @param array $arguments |
|
72 | + * @param bool $shared |
|
73 | + * @return mixed |
|
74 | + */ |
|
75 | + public function load($fqcn, array $arguments = array(), $shared = true) |
|
76 | + { |
|
77 | + $fqcn = $this->class_cache->getFqn($fqcn); |
|
78 | + if ($this->class_cache->hasInterface($fqcn, 'EventEspresso\core\interfaces\ReservedInstanceInterface')) { |
|
79 | + $shared = true; |
|
80 | + } |
|
81 | + return $shared |
|
82 | + ? $this->getSharedLoader()->load($fqcn, $arguments, $shared) |
|
83 | + : $this->getNewLoader()->load($fqcn, $arguments, $shared); |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * @param FullyQualifiedName|string $fqcn |
|
89 | + * @param array $arguments |
|
90 | + * @return mixed |
|
91 | + */ |
|
92 | + public function getNew($fqcn, array $arguments = array()) |
|
93 | + { |
|
94 | + return $this->load($fqcn, $arguments, false); |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * @param FullyQualifiedName|string $fqcn |
|
100 | + * @param array $arguments |
|
101 | + * @return mixed |
|
102 | + */ |
|
103 | + public function getShared($fqcn, array $arguments = array()) |
|
104 | + { |
|
105 | + return $this->load($fqcn, $arguments); |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * @param FullyQualifiedName|string $fqcn |
|
111 | + * @param mixed $object |
|
112 | + * @return bool |
|
113 | + * @throws InvalidArgumentException |
|
114 | + */ |
|
115 | + public function share($fqcn, $object) |
|
116 | + { |
|
117 | + $fqcn = $this->class_cache->getFqn($fqcn); |
|
118 | + return $this->getSharedLoader()->share($fqcn, $object); |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * calls reset() on loaders if that method exists |
|
124 | + */ |
|
125 | + public function reset() |
|
126 | + { |
|
127 | + $this->shared_loader->reset(); |
|
128 | + } |
|
129 | 129 | } |
@@ -17,154 +17,154 @@ |
||
17 | 17 | class CachingLoader extends CachingLoaderDecorator |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var string $identifier |
|
22 | - */ |
|
23 | - protected $identifier; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var CollectionInterface $cache |
|
27 | - */ |
|
28 | - protected $cache; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var ObjectIdentifier |
|
32 | - */ |
|
33 | - private $object_identifier; |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * CachingLoader constructor. |
|
38 | - * |
|
39 | - * @param LoaderDecoratorInterface $loader |
|
40 | - * @param CollectionInterface $cache |
|
41 | - * @param ObjectIdentifier $object_identifier |
|
42 | - * @param string $identifier |
|
43 | - * @throws InvalidDataTypeException |
|
44 | - */ |
|
45 | - public function __construct( |
|
46 | - LoaderDecoratorInterface $loader, |
|
47 | - CollectionInterface $cache, |
|
48 | - ObjectIdentifier $object_identifier, |
|
49 | - $identifier = '' |
|
50 | - ) { |
|
51 | - parent::__construct($loader); |
|
52 | - $this->cache = $cache; |
|
53 | - $this->object_identifier = $object_identifier; |
|
54 | - $this->setIdentifier($identifier); |
|
55 | - if ($this->identifier !== '') { |
|
56 | - // to only clear this cache, and assuming an identifier has been set, simply do the following: |
|
57 | - // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__IDENTIFIER'); |
|
58 | - // where "IDENTIFIER" = the string that was set during construction |
|
59 | - add_action( |
|
60 | - "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}", |
|
61 | - array($this, 'reset') |
|
62 | - ); |
|
63 | - } |
|
64 | - // to clear ALL caches, simply do the following: |
|
65 | - // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
|
66 | - add_action( |
|
67 | - 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
|
68 | - array($this, 'reset') |
|
69 | - ); |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * @return string |
|
75 | - */ |
|
76 | - public function identifier() |
|
77 | - { |
|
78 | - return $this->identifier; |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * @param string $identifier |
|
84 | - * @throws InvalidDataTypeException |
|
85 | - */ |
|
86 | - private function setIdentifier($identifier) |
|
87 | - { |
|
88 | - if (! is_string($identifier)) { |
|
89 | - throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
|
90 | - } |
|
91 | - $this->identifier = $identifier; |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * @param FullyQualifiedName|string $fqcn |
|
97 | - * @param mixed $object |
|
98 | - * @return bool |
|
99 | - * @throws InvalidArgumentException |
|
100 | - */ |
|
101 | - public function share($fqcn, $object) |
|
102 | - { |
|
103 | - if ($object instanceof $fqcn) { |
|
104 | - return $this->cache->add($object, md5($fqcn)); |
|
105 | - } |
|
106 | - throw new InvalidArgumentException( |
|
107 | - sprintf( |
|
108 | - esc_html__( |
|
109 | - 'The supplied class name "%1$s" must match the class of the supplied object.', |
|
110 | - 'event_espresso' |
|
111 | - ), |
|
112 | - $fqcn |
|
113 | - ) |
|
114 | - ); |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * @param FullyQualifiedName|string $fqcn |
|
120 | - * @param array $arguments |
|
121 | - * @param bool $shared |
|
122 | - * @param array $interfaces |
|
123 | - * @return mixed |
|
124 | - */ |
|
125 | - public function load($fqcn, $arguments = array(), $shared = true, array $interfaces = array()) |
|
126 | - { |
|
127 | - $fqcn = ltrim($fqcn, '\\'); |
|
128 | - // caching can be turned off via the following code: |
|
129 | - // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
130 | - if (apply_filters( |
|
131 | - 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
|
132 | - false, |
|
133 | - $this |
|
134 | - )) { |
|
135 | - // even though $shared might be true, caching could be bypassed for whatever reason, |
|
136 | - // so we don't want the core loader to cache anything, therefore caching is turned off |
|
137 | - return $this->loader->load($fqcn, $arguments, false); |
|
138 | - } |
|
139 | - $object_identifier = $this->object_identifier->getIdentifier($fqcn, $arguments); |
|
140 | - if ($this->cache->has($object_identifier)) { |
|
141 | - return $this->cache->get($object_identifier); |
|
142 | - } |
|
143 | - $object = $this->loader->load($fqcn, $arguments, $shared); |
|
144 | - if ($object instanceof $fqcn) { |
|
145 | - $this->cache->add($object, $object_identifier); |
|
146 | - } |
|
147 | - return $object; |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * empties cache and calls reset() on loader if method exists |
|
153 | - */ |
|
154 | - public function reset() |
|
155 | - { |
|
156 | - $this->clearCache(); |
|
157 | - $this->loader->reset(); |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * unsets and detaches ALL objects from the cache |
|
163 | - * |
|
164 | - * @since $VID:$ |
|
165 | - */ |
|
166 | - public function clearCache() |
|
167 | - { |
|
168 | - $this->cache->trashAndDetachAll(); |
|
169 | - } |
|
20 | + /** |
|
21 | + * @var string $identifier |
|
22 | + */ |
|
23 | + protected $identifier; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var CollectionInterface $cache |
|
27 | + */ |
|
28 | + protected $cache; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var ObjectIdentifier |
|
32 | + */ |
|
33 | + private $object_identifier; |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * CachingLoader constructor. |
|
38 | + * |
|
39 | + * @param LoaderDecoratorInterface $loader |
|
40 | + * @param CollectionInterface $cache |
|
41 | + * @param ObjectIdentifier $object_identifier |
|
42 | + * @param string $identifier |
|
43 | + * @throws InvalidDataTypeException |
|
44 | + */ |
|
45 | + public function __construct( |
|
46 | + LoaderDecoratorInterface $loader, |
|
47 | + CollectionInterface $cache, |
|
48 | + ObjectIdentifier $object_identifier, |
|
49 | + $identifier = '' |
|
50 | + ) { |
|
51 | + parent::__construct($loader); |
|
52 | + $this->cache = $cache; |
|
53 | + $this->object_identifier = $object_identifier; |
|
54 | + $this->setIdentifier($identifier); |
|
55 | + if ($this->identifier !== '') { |
|
56 | + // to only clear this cache, and assuming an identifier has been set, simply do the following: |
|
57 | + // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__IDENTIFIER'); |
|
58 | + // where "IDENTIFIER" = the string that was set during construction |
|
59 | + add_action( |
|
60 | + "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}", |
|
61 | + array($this, 'reset') |
|
62 | + ); |
|
63 | + } |
|
64 | + // to clear ALL caches, simply do the following: |
|
65 | + // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
|
66 | + add_action( |
|
67 | + 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
|
68 | + array($this, 'reset') |
|
69 | + ); |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * @return string |
|
75 | + */ |
|
76 | + public function identifier() |
|
77 | + { |
|
78 | + return $this->identifier; |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * @param string $identifier |
|
84 | + * @throws InvalidDataTypeException |
|
85 | + */ |
|
86 | + private function setIdentifier($identifier) |
|
87 | + { |
|
88 | + if (! is_string($identifier)) { |
|
89 | + throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
|
90 | + } |
|
91 | + $this->identifier = $identifier; |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * @param FullyQualifiedName|string $fqcn |
|
97 | + * @param mixed $object |
|
98 | + * @return bool |
|
99 | + * @throws InvalidArgumentException |
|
100 | + */ |
|
101 | + public function share($fqcn, $object) |
|
102 | + { |
|
103 | + if ($object instanceof $fqcn) { |
|
104 | + return $this->cache->add($object, md5($fqcn)); |
|
105 | + } |
|
106 | + throw new InvalidArgumentException( |
|
107 | + sprintf( |
|
108 | + esc_html__( |
|
109 | + 'The supplied class name "%1$s" must match the class of the supplied object.', |
|
110 | + 'event_espresso' |
|
111 | + ), |
|
112 | + $fqcn |
|
113 | + ) |
|
114 | + ); |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * @param FullyQualifiedName|string $fqcn |
|
120 | + * @param array $arguments |
|
121 | + * @param bool $shared |
|
122 | + * @param array $interfaces |
|
123 | + * @return mixed |
|
124 | + */ |
|
125 | + public function load($fqcn, $arguments = array(), $shared = true, array $interfaces = array()) |
|
126 | + { |
|
127 | + $fqcn = ltrim($fqcn, '\\'); |
|
128 | + // caching can be turned off via the following code: |
|
129 | + // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
130 | + if (apply_filters( |
|
131 | + 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
|
132 | + false, |
|
133 | + $this |
|
134 | + )) { |
|
135 | + // even though $shared might be true, caching could be bypassed for whatever reason, |
|
136 | + // so we don't want the core loader to cache anything, therefore caching is turned off |
|
137 | + return $this->loader->load($fqcn, $arguments, false); |
|
138 | + } |
|
139 | + $object_identifier = $this->object_identifier->getIdentifier($fqcn, $arguments); |
|
140 | + if ($this->cache->has($object_identifier)) { |
|
141 | + return $this->cache->get($object_identifier); |
|
142 | + } |
|
143 | + $object = $this->loader->load($fqcn, $arguments, $shared); |
|
144 | + if ($object instanceof $fqcn) { |
|
145 | + $this->cache->add($object, $object_identifier); |
|
146 | + } |
|
147 | + return $object; |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * empties cache and calls reset() on loader if method exists |
|
153 | + */ |
|
154 | + public function reset() |
|
155 | + { |
|
156 | + $this->clearCache(); |
|
157 | + $this->loader->reset(); |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * unsets and detaches ALL objects from the cache |
|
163 | + * |
|
164 | + * @since $VID:$ |
|
165 | + */ |
|
166 | + public function clearCache() |
|
167 | + { |
|
168 | + $this->cache->trashAndDetachAll(); |
|
169 | + } |
|
170 | 170 | } |
@@ -16,119 +16,119 @@ |
||
16 | 16 | class Response implements ResponseInterface, ReservedInstanceInterface |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * @var array $notice |
|
21 | - */ |
|
22 | - protected $notice = array(); |
|
23 | - |
|
24 | - /** |
|
25 | - * rendered output to be returned to WP |
|
26 | - * |
|
27 | - * @var string $output |
|
28 | - */ |
|
29 | - protected $output = ''; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var bool |
|
33 | - */ |
|
34 | - protected $request_terminated = false; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var bool $deactivate_plugin |
|
38 | - */ |
|
39 | - protected $deactivate_plugin = false; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * EE_Response constructor. |
|
44 | - */ |
|
45 | - public function __construct() |
|
46 | - { |
|
47 | - $this->terminateRequest(false); |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * @param $key |
|
53 | - * @param $value |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - public function setNotice($key, $value) |
|
57 | - { |
|
58 | - $this->notice[ $key ] = $value; |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * @param $key |
|
64 | - * @return mixed |
|
65 | - */ |
|
66 | - public function getNotice($key) |
|
67 | - { |
|
68 | - return isset($this->notice[ $key ]) ? $this->notice[ $key ] : null; |
|
69 | - } |
|
70 | - |
|
71 | - |
|
72 | - /** |
|
73 | - * @return array |
|
74 | - */ |
|
75 | - public function getNotices() |
|
76 | - { |
|
77 | - return $this->notice; |
|
78 | - } |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * @param string $string |
|
83 | - * @param bool $append |
|
84 | - */ |
|
85 | - public function addOutput($string, $append = true) |
|
86 | - { |
|
87 | - $this->output = $append ? $this->output . $string : $string . $this->output; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public function getOutput() |
|
95 | - { |
|
96 | - return $this->output; |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * @return boolean |
|
102 | - */ |
|
103 | - public function requestTerminated() |
|
104 | - { |
|
105 | - return $this->request_terminated; |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * @param boolean $request_terminated |
|
111 | - */ |
|
112 | - public function terminateRequest($request_terminated = true) |
|
113 | - { |
|
114 | - $this->request_terminated = filter_var($request_terminated, FILTER_VALIDATE_BOOLEAN); |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * @return boolean |
|
120 | - */ |
|
121 | - public function pluginDeactivated() |
|
122 | - { |
|
123 | - return $this->deactivate_plugin; |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * sets $deactivate_plugin to true |
|
129 | - */ |
|
130 | - public function deactivatePlugin() |
|
131 | - { |
|
132 | - $this->deactivate_plugin = true; |
|
133 | - } |
|
19 | + /** |
|
20 | + * @var array $notice |
|
21 | + */ |
|
22 | + protected $notice = array(); |
|
23 | + |
|
24 | + /** |
|
25 | + * rendered output to be returned to WP |
|
26 | + * |
|
27 | + * @var string $output |
|
28 | + */ |
|
29 | + protected $output = ''; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var bool |
|
33 | + */ |
|
34 | + protected $request_terminated = false; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var bool $deactivate_plugin |
|
38 | + */ |
|
39 | + protected $deactivate_plugin = false; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * EE_Response constructor. |
|
44 | + */ |
|
45 | + public function __construct() |
|
46 | + { |
|
47 | + $this->terminateRequest(false); |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * @param $key |
|
53 | + * @param $value |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + public function setNotice($key, $value) |
|
57 | + { |
|
58 | + $this->notice[ $key ] = $value; |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * @param $key |
|
64 | + * @return mixed |
|
65 | + */ |
|
66 | + public function getNotice($key) |
|
67 | + { |
|
68 | + return isset($this->notice[ $key ]) ? $this->notice[ $key ] : null; |
|
69 | + } |
|
70 | + |
|
71 | + |
|
72 | + /** |
|
73 | + * @return array |
|
74 | + */ |
|
75 | + public function getNotices() |
|
76 | + { |
|
77 | + return $this->notice; |
|
78 | + } |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * @param string $string |
|
83 | + * @param bool $append |
|
84 | + */ |
|
85 | + public function addOutput($string, $append = true) |
|
86 | + { |
|
87 | + $this->output = $append ? $this->output . $string : $string . $this->output; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public function getOutput() |
|
95 | + { |
|
96 | + return $this->output; |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * @return boolean |
|
102 | + */ |
|
103 | + public function requestTerminated() |
|
104 | + { |
|
105 | + return $this->request_terminated; |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * @param boolean $request_terminated |
|
111 | + */ |
|
112 | + public function terminateRequest($request_terminated = true) |
|
113 | + { |
|
114 | + $this->request_terminated = filter_var($request_terminated, FILTER_VALIDATE_BOOLEAN); |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * @return boolean |
|
120 | + */ |
|
121 | + public function pluginDeactivated() |
|
122 | + { |
|
123 | + return $this->deactivate_plugin; |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * sets $deactivate_plugin to true |
|
129 | + */ |
|
130 | + public function deactivatePlugin() |
|
131 | + { |
|
132 | + $this->deactivate_plugin = true; |
|
133 | + } |
|
134 | 134 | } |
@@ -25,123 +25,123 @@ |
||
25 | 25 | class BootstrapDependencyInjectionContainer |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * @var EE_Dependency_Map $dependency_map |
|
30 | - */ |
|
31 | - protected $dependency_map; |
|
32 | - |
|
33 | - /** |
|
34 | - * @type LoaderInterface $loader |
|
35 | - */ |
|
36 | - protected $loader; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var EE_Registry $registry |
|
40 | - */ |
|
41 | - protected $registry; |
|
42 | - |
|
43 | - /** |
|
44 | - * @var ClassInterfaceCache $class_cache |
|
45 | - */ |
|
46 | - private $class_cache; |
|
47 | - |
|
48 | - /** |
|
49 | - * @var Mirror |
|
50 | - */ |
|
51 | - private $mirror; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var ObjectIdentifier |
|
55 | - */ |
|
56 | - private $object_identifier; |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * Can't use this just yet until we exorcise some more of our singleton usage from core |
|
61 | - */ |
|
62 | - public function buildDependencyInjectionContainer() |
|
63 | - { |
|
64 | - // build DI container |
|
65 | - // $OpenCoffeeShop = new EventEspresso\core\services\container\OpenCoffeeShop(); |
|
66 | - // $OpenCoffeeShop->addRecipes(); |
|
67 | - // $CoffeeShop = $OpenCoffeeShop->CoffeeShop(); |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * Setups EE_Registry and EE_Dependency_Map |
|
73 | - * |
|
74 | - * @throws EE_Error |
|
75 | - */ |
|
76 | - public function buildLegacyDependencyInjectionContainer() |
|
77 | - { |
|
78 | - $this->class_cache = new ClassInterfaceCache(); |
|
79 | - $this->object_identifier = new ObjectIdentifier($this->class_cache); |
|
80 | - $this->mirror = new Mirror(); |
|
81 | - // EE_Dependency_Map: info about how to load classes required by other classes |
|
82 | - espresso_load_required( |
|
83 | - 'EE_Dependency_Map', |
|
84 | - EE_CORE . 'EE_Dependency_Map.core.php' |
|
85 | - ); |
|
86 | - $this->dependency_map = EE_Dependency_Map::instance($this->class_cache); |
|
87 | - // EE_Registry: central repository for classes (legacy) |
|
88 | - espresso_load_required( |
|
89 | - 'EE_Registry', |
|
90 | - EE_CORE . 'EE_Registry.core.php' |
|
91 | - ); |
|
92 | - $this->registry = EE_Registry::instance( |
|
93 | - $this->dependency_map, |
|
94 | - $this->mirror, |
|
95 | - $this->class_cache, |
|
96 | - $this->object_identifier |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Performs initial setup for the generic Loader |
|
103 | - * |
|
104 | - * @throws InvalidDataTypeException |
|
105 | - * @throws InvalidInterfaceException |
|
106 | - * @throws InvalidArgumentException |
|
107 | - */ |
|
108 | - public function buildLoader() |
|
109 | - { |
|
110 | - $this->loader = LoaderFactory::getLoader( |
|
111 | - $this->registry, |
|
112 | - $this->class_cache, |
|
113 | - $this->object_identifier |
|
114 | - ); |
|
115 | - $this->loader->share('EventEspresso\core\services\loaders\ClassInterfaceCache', $this->class_cache); |
|
116 | - $this->loader->share('EventEspresso\core\services\loaders\ObjectIdentifier', $this->object_identifier); |
|
117 | - $this->loader->share('EventEspresso\core\services\container\Mirror', $this->mirror); |
|
118 | - $this->dependency_map->setLoader($this->loader); |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * @return EE_Dependency_Map |
|
124 | - */ |
|
125 | - public function getDependencyMap() |
|
126 | - { |
|
127 | - return $this->dependency_map; |
|
128 | - } |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * @return EE_Registry |
|
133 | - */ |
|
134 | - public function getRegistry() |
|
135 | - { |
|
136 | - return $this->registry; |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - /** |
|
141 | - * @return LoaderInterface |
|
142 | - */ |
|
143 | - public function getLoader() |
|
144 | - { |
|
145 | - return $this->loader; |
|
146 | - } |
|
28 | + /** |
|
29 | + * @var EE_Dependency_Map $dependency_map |
|
30 | + */ |
|
31 | + protected $dependency_map; |
|
32 | + |
|
33 | + /** |
|
34 | + * @type LoaderInterface $loader |
|
35 | + */ |
|
36 | + protected $loader; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var EE_Registry $registry |
|
40 | + */ |
|
41 | + protected $registry; |
|
42 | + |
|
43 | + /** |
|
44 | + * @var ClassInterfaceCache $class_cache |
|
45 | + */ |
|
46 | + private $class_cache; |
|
47 | + |
|
48 | + /** |
|
49 | + * @var Mirror |
|
50 | + */ |
|
51 | + private $mirror; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var ObjectIdentifier |
|
55 | + */ |
|
56 | + private $object_identifier; |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * Can't use this just yet until we exorcise some more of our singleton usage from core |
|
61 | + */ |
|
62 | + public function buildDependencyInjectionContainer() |
|
63 | + { |
|
64 | + // build DI container |
|
65 | + // $OpenCoffeeShop = new EventEspresso\core\services\container\OpenCoffeeShop(); |
|
66 | + // $OpenCoffeeShop->addRecipes(); |
|
67 | + // $CoffeeShop = $OpenCoffeeShop->CoffeeShop(); |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * Setups EE_Registry and EE_Dependency_Map |
|
73 | + * |
|
74 | + * @throws EE_Error |
|
75 | + */ |
|
76 | + public function buildLegacyDependencyInjectionContainer() |
|
77 | + { |
|
78 | + $this->class_cache = new ClassInterfaceCache(); |
|
79 | + $this->object_identifier = new ObjectIdentifier($this->class_cache); |
|
80 | + $this->mirror = new Mirror(); |
|
81 | + // EE_Dependency_Map: info about how to load classes required by other classes |
|
82 | + espresso_load_required( |
|
83 | + 'EE_Dependency_Map', |
|
84 | + EE_CORE . 'EE_Dependency_Map.core.php' |
|
85 | + ); |
|
86 | + $this->dependency_map = EE_Dependency_Map::instance($this->class_cache); |
|
87 | + // EE_Registry: central repository for classes (legacy) |
|
88 | + espresso_load_required( |
|
89 | + 'EE_Registry', |
|
90 | + EE_CORE . 'EE_Registry.core.php' |
|
91 | + ); |
|
92 | + $this->registry = EE_Registry::instance( |
|
93 | + $this->dependency_map, |
|
94 | + $this->mirror, |
|
95 | + $this->class_cache, |
|
96 | + $this->object_identifier |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * Performs initial setup for the generic Loader |
|
103 | + * |
|
104 | + * @throws InvalidDataTypeException |
|
105 | + * @throws InvalidInterfaceException |
|
106 | + * @throws InvalidArgumentException |
|
107 | + */ |
|
108 | + public function buildLoader() |
|
109 | + { |
|
110 | + $this->loader = LoaderFactory::getLoader( |
|
111 | + $this->registry, |
|
112 | + $this->class_cache, |
|
113 | + $this->object_identifier |
|
114 | + ); |
|
115 | + $this->loader->share('EventEspresso\core\services\loaders\ClassInterfaceCache', $this->class_cache); |
|
116 | + $this->loader->share('EventEspresso\core\services\loaders\ObjectIdentifier', $this->object_identifier); |
|
117 | + $this->loader->share('EventEspresso\core\services\container\Mirror', $this->mirror); |
|
118 | + $this->dependency_map->setLoader($this->loader); |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * @return EE_Dependency_Map |
|
124 | + */ |
|
125 | + public function getDependencyMap() |
|
126 | + { |
|
127 | + return $this->dependency_map; |
|
128 | + } |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * @return EE_Registry |
|
133 | + */ |
|
134 | + public function getRegistry() |
|
135 | + { |
|
136 | + return $this->registry; |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + /** |
|
141 | + * @return LoaderInterface |
|
142 | + */ |
|
143 | + public function getLoader() |
|
144 | + { |
|
145 | + return $this->loader; |
|
146 | + } |
|
147 | 147 | } |