admin
2 days ago
api
4 weeks ago
campaigns
2 days ago
forms
2 days ago
integrations
2 days ago
views
1 week ago
class-container.php
2 days ago
class-debug-log-reader.php
2 days ago
class-debug-log.php
2 days ago
class-dynamic-content-tags.php
2 days ago
class-field-formatter.php
1 year ago
class-field-guesser.php
1 week ago
class-list-data-mapper.php
4 months ago
class-mailchimp-subscriber.php
2 months ago
class-mailchimp.php
2 days ago
class-personal-data-exporter.php
4 weeks ago
class-plugin.php
1 year ago
class-queue-job.php
4 weeks ago
class-queue.php
1 year ago
class-tools.php
1 year ago
class-tracking-pixel.php
2 days ago
default-actions.php
1 year ago
default-filters.php
1 year ago
deprecated-functions.php
3 years ago
functions.php
2 days ago
class-container.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class MC4WP_Service_Container |
| 5 | * |
| 6 | * @access private |
| 7 | * @ignore |
| 8 | */ |
| 9 | class MC4WP_Container implements ArrayAccess |
| 10 | { |
| 11 | /** |
| 12 | * @var array |
| 13 | */ |
| 14 | protected $services = []; |
| 15 | |
| 16 | /** |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $resolved_services = []; |
| 20 | |
| 21 | /** |
| 22 | * @param string $name |
| 23 | * @return boolean |
| 24 | */ |
| 25 | public function has($name) |
| 26 | { |
| 27 | return isset($this->services[ $name ]); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param string $name |
| 32 | * |
| 33 | * @return mixed |
| 34 | * @throws Exception |
| 35 | */ |
| 36 | public function get($name) |
| 37 | { |
| 38 | if (! $this->has($name)) { |
| 39 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Exception text is not direct output and is escaped at render time. |
| 40 | throw new Exception(sprintf('No service named %s was registered.', $name)); |
| 41 | } |
| 42 | |
| 43 | $service = $this->services[ $name ]; |
| 44 | |
| 45 | // is this a resolvable service? |
| 46 | if (is_callable($service)) { |
| 47 | // resolve service if it's not resolved yet |
| 48 | if (! isset($this->resolved_services[ $name ])) { |
| 49 | $this->resolved_services[ $name ] = call_user_func($service); |
| 50 | } |
| 51 | |
| 52 | return $this->resolved_services[ $name ]; |
| 53 | } |
| 54 | |
| 55 | return $this->services[ $name ]; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * (PHP 5 >= 5.0.0)<br/> |
| 60 | * Whether a offset exists |
| 61 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| 62 | * |
| 63 | * @param mixed $offset <p> |
| 64 | * An offset to check for. |
| 65 | * </p> |
| 66 | * |
| 67 | * @return boolean true on success or false on failure. |
| 68 | * </p> |
| 69 | * <p> |
| 70 | * The return value will be casted to boolean if non-boolean was returned. |
| 71 | */ |
| 72 | public function offsetExists($offset): bool |
| 73 | { |
| 74 | return $this->has($offset); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * (PHP 5 >= 5.0.0)<br/> |
| 79 | * Offset to retrieve |
| 80 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
| 81 | * |
| 82 | * @param mixed $offset <p> |
| 83 | * The offset to retrieve. |
| 84 | * </p> |
| 85 | * |
| 86 | * @return mixed Can return all value types. |
| 87 | */ |
| 88 | // PHP 8.1 expects a mixed return type here, but the plugin still supports PHP 7.4. |
| 89 | #[\ReturnTypeWillChange] |
| 90 | public function offsetGet($offset) |
| 91 | { |
| 92 | return $this->get($offset); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * (PHP 5 >= 5.0.0)<br/> |
| 97 | * Offset to set |
| 98 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
| 99 | * |
| 100 | * @param mixed $offset <p> |
| 101 | * The offset to assign the value to. |
| 102 | * </p> |
| 103 | * @param mixed $value <p> |
| 104 | * The value to set. |
| 105 | * </p> |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | public function offsetSet($offset, $value): void |
| 110 | { |
| 111 | $this->services[ $offset ] = $value; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * (PHP 5 >= 5.0.0)<br/> |
| 116 | * Offset to unset |
| 117 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| 118 | * |
| 119 | * @param mixed $offset <p> |
| 120 | * The offset to unset. |
| 121 | * </p> |
| 122 | * |
| 123 | * @return void |
| 124 | */ |
| 125 | public function offsetUnset($offset): void |
| 126 | { |
| 127 | unset($this->services[ $offset ]); |
| 128 | } |
| 129 | } |
| 130 |