admin
8 years ago
api
8 years ago
forms
8 years ago
integrations
8 years ago
mailchimp
8 years ago
views
8 years ago
class-array-bag.php
8 years ago
class-container.php
10 years ago
class-debug-log-reader.php
9 years ago
class-debug-log.php
9 years ago
class-dynamic-content-tags.php
9 years ago
class-field-formatter.php
9 years ago
class-field-guesser.php
8 years ago
class-list-data-mapper.php
8 years ago
class-mailchimp.php
8 years ago
class-plugin.php
10 years ago
class-queue-job.php
10 years ago
class-queue.php
9 years ago
class-request.php
10 years ago
class-tools.php
9 years ago
default-actions.php
9 years ago
default-filters.php
9 years ago
deprecated-functions.php
8 years ago
functions.php
8 years ago
class-container.php
121 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 = array(); |
| 15 | |
| 16 | /** |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $resolved_services = array(); |
| 20 | |
| 21 | /** |
| 22 | * @param $name |
| 23 | * @return boolean |
| 24 | */ |
| 25 | public function has( $name ) { |
| 26 | return isset( $this->services[ $name ] ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param $name |
| 31 | * |
| 32 | * @return mixed |
| 33 | * @throws Exception |
| 34 | */ |
| 35 | public function get( $name ) { |
| 36 | |
| 37 | if( ! $this->has( $name ) ) { |
| 38 | throw new Exception( sprintf( 'No service named %s was registered.', $name ) ); |
| 39 | } |
| 40 | |
| 41 | $service = $this->services[ $name ]; |
| 42 | |
| 43 | // is this a resolvable service? |
| 44 | if( is_callable( $service ) ) { |
| 45 | |
| 46 | // resolve service if it's not resolved yet |
| 47 | if( ! isset( $this->resolved_services[ $name ] ) ) { |
| 48 | $this->resolved_services[ $name ] = call_user_func( $service ); |
| 49 | } |
| 50 | |
| 51 | return $this->resolved_services[ $name ]; |
| 52 | } |
| 53 | |
| 54 | return $this->services[ $name ]; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * (PHP 5 >= 5.0.0)<br/> |
| 59 | * Whether a offset exists |
| 60 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| 61 | * |
| 62 | * @param mixed $offset <p> |
| 63 | * An offset to check for. |
| 64 | * </p> |
| 65 | * |
| 66 | * @return boolean true on success or false on failure. |
| 67 | * </p> |
| 68 | * <p> |
| 69 | * The return value will be casted to boolean if non-boolean was returned. |
| 70 | */ |
| 71 | public function offsetExists( $offset ) { |
| 72 | return $this->has( $offset ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * (PHP 5 >= 5.0.0)<br/> |
| 77 | * Offset to retrieve |
| 78 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
| 79 | * |
| 80 | * @param mixed $offset <p> |
| 81 | * The offset to retrieve. |
| 82 | * </p> |
| 83 | * |
| 84 | * @return mixed Can return all value types. |
| 85 | */ |
| 86 | public function offsetGet( $offset ) { |
| 87 | return $this->get( $offset ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * (PHP 5 >= 5.0.0)<br/> |
| 92 | * Offset to set |
| 93 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
| 94 | * |
| 95 | * @param mixed $offset <p> |
| 96 | * The offset to assign the value to. |
| 97 | * </p> |
| 98 | * @param mixed $value <p> |
| 99 | * The value to set. |
| 100 | * </p> |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | public function offsetSet( $offset, $value ) { |
| 105 | $this->services[ $offset ] = $value; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * (PHP 5 >= 5.0.0)<br/> |
| 110 | * Offset to unset |
| 111 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| 112 | * |
| 113 | * @param mixed $offset <p> |
| 114 | * The offset to unset. |
| 115 | * </p> |
| 116 | * |
| 117 | * @return void |
| 118 | */ |
| 119 | public function offsetUnset( $offset ) { |
| 120 | unset( $this->services[ $offset ] ); |
| 121 | }} |