@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | use RuntimeException; |
| 8 | 8 | |
| 9 | 9 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 10 | - exit('No direct script access allowed'); |
|
| 10 | + exit('No direct script access allowed'); |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | |
@@ -23,51 +23,50 @@ discard block |
||
| 23 | 23 | class Recipe implements RecipeInterface |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * A default Recipe to use if none is specified for a class |
|
| 28 | - */ |
|
| 29 | - const DEFAULT_ID = '*'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Identifier for the entity class to be constructed. |
|
| 33 | - * Typically a Fully Qualified Class Name |
|
| 34 | - * |
|
| 35 | - * @var string $identifier |
|
| 36 | - */ |
|
| 37 | - private $identifier; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Fully Qualified Class Name |
|
| 41 | - * |
|
| 42 | - * @var string $fqcn |
|
| 43 | - */ |
|
| 44 | - private $fqcn; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * class name aliases - typically a Fully Qualified Interface that the class implements |
|
| 48 | - * identifiers passed to the coffee pot will be run through the filters to find the correct class name |
|
| 49 | - * |
|
| 50 | - * @var array $filters |
|
| 51 | - */ |
|
| 52 | - private $filters = array(); |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * one of the class constants from CoffeeShop: |
|
| 56 | - * CoffeeMaker::BREW_NEW - creates a new instance |
|
| 57 | - * CoffeeMaker::BREW_SHARED - creates a shared instance |
|
| 58 | - * CoffeeMaker::BREW_LOAD_ONLY - loads but does not instantiate |
|
| 59 | - |
|
| 60 | -* |
|
| 26 | + /** |
|
| 27 | + * A default Recipe to use if none is specified for a class |
|
| 28 | + */ |
|
| 29 | + const DEFAULT_ID = '*'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Identifier for the entity class to be constructed. |
|
| 33 | + * Typically a Fully Qualified Class Name |
|
| 34 | + * |
|
| 35 | + * @var string $identifier |
|
| 36 | + */ |
|
| 37 | + private $identifier; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Fully Qualified Class Name |
|
| 41 | + * |
|
| 42 | + * @var string $fqcn |
|
| 43 | + */ |
|
| 44 | + private $fqcn; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * class name aliases - typically a Fully Qualified Interface that the class implements |
|
| 48 | + * identifiers passed to the coffee pot will be run through the filters to find the correct class name |
|
| 49 | + * |
|
| 50 | + * @var array $filters |
|
| 51 | + */ |
|
| 52 | + private $filters = array(); |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * one of the class constants from CoffeeShop: |
|
| 56 | + * CoffeeMaker::BREW_NEW - creates a new instance |
|
| 57 | + * CoffeeMaker::BREW_SHARED - creates a shared instance |
|
| 58 | + * CoffeeMaker::BREW_LOAD_ONLY - loads but does not instantiate |
|
| 59 | + * |
|
| 61 | 60 | *@var string $type |
| 62 | - */ |
|
| 63 | - private $type; |
|
| 61 | + */ |
|
| 62 | + private $type; |
|
| 64 | 63 | |
| 65 | - /** |
|
| 66 | - * array of full server filepaths to files that may contain the class |
|
| 67 | - * |
|
| 68 | - * @var array $paths |
|
| 69 | - */ |
|
| 70 | - private $paths = array(); |
|
| 64 | + /** |
|
| 65 | + * array of full server filepaths to files that may contain the class |
|
| 66 | + * |
|
| 67 | + * @var array $paths |
|
| 68 | + */ |
|
| 69 | + private $paths = array(); |
|
| 71 | 70 | |
| 72 | 71 | |
| 73 | 72 | |
@@ -82,192 +81,192 @@ discard block |
||
| 82 | 81 | * then supply a filepath, or array of filepaths, so that it can be included |
| 83 | 82 | * @param string $fqcn |
| 84 | 83 | */ |
| 85 | - public function __construct( |
|
| 86 | - $identifier, |
|
| 87 | - $type = CoffeeMaker::BREW_NEW, |
|
| 88 | - $filters = array(), |
|
| 89 | - $paths = array(), |
|
| 90 | - $fqcn = '' |
|
| 91 | - ) |
|
| 92 | - { |
|
| 93 | - $this->setIdentifier($identifier); |
|
| 94 | - $this->setType($type); |
|
| 95 | - $this->setPaths($paths); |
|
| 96 | - $this->setFqcn($fqcn); |
|
| 97 | - $this->setFilters((array)$filters); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @return string |
|
| 104 | - */ |
|
| 105 | - public function identifier() |
|
| 106 | - { |
|
| 107 | - return $this->identifier; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @return string |
|
| 114 | - */ |
|
| 115 | - public function fqcn() |
|
| 116 | - { |
|
| 117 | - return $this->fqcn; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @return array |
|
| 124 | - */ |
|
| 125 | - public function filters() |
|
| 126 | - { |
|
| 127 | - return (array)$this->filters; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @return string |
|
| 134 | - */ |
|
| 135 | - public function type() |
|
| 136 | - { |
|
| 137 | - return $this->type; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @return array |
|
| 144 | - */ |
|
| 145 | - public function paths() |
|
| 146 | - { |
|
| 147 | - return (array)$this->paths; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @param string $identifier Identifier for the entity class that the Recipe applies to |
|
| 154 | - * Typically a Fully Qualified Class Name |
|
| 155 | - */ |
|
| 156 | - public function setIdentifier($identifier) |
|
| 157 | - { |
|
| 158 | - if ( ! is_string($identifier) || empty($identifier)) { |
|
| 159 | - throw new InvalidIdentifierException( |
|
| 160 | - is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
| 161 | - __('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso') |
|
| 162 | - ); |
|
| 163 | - } |
|
| 164 | - $this->identifier = $identifier; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Ensures incoming string is a valid Fully Qualified Class Name, |
|
| 171 | - * except if this is the default wildcard Recipe ( * ), |
|
| 172 | - * or it's NOT an actual FQCN because the Recipe is using filepaths |
|
| 173 | - * for classes that are not PSR-4 compatible |
|
| 174 | - * PLZ NOTE: |
|
| 175 | - * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
| 176 | - * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
| 177 | - * |
|
| 178 | - * @param string $fqcn |
|
| 179 | - */ |
|
| 180 | - public function setFqcn($fqcn) |
|
| 181 | - { |
|
| 182 | - $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier; |
|
| 183 | - if ( ! is_string($fqcn)) { |
|
| 184 | - throw new InvalidDataTypeException( |
|
| 185 | - '$fqcn', |
|
| 186 | - is_object($fqcn) ? get_class($fqcn) : gettype($fqcn), |
|
| 187 | - __('string (Fully\Qualified\ClassName)', 'event_espresso') |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - $fqcn = ltrim($fqcn, '\\'); |
|
| 191 | - if ( |
|
| 192 | - $fqcn !== Recipe::DEFAULT_ID |
|
| 193 | - && ! empty($fqcn) |
|
| 194 | - && empty($this->paths) |
|
| 195 | - && ! class_exists($fqcn) |
|
| 196 | - ) { |
|
| 197 | - throw new InvalidClassException($fqcn); |
|
| 198 | - } |
|
| 199 | - $this->fqcn = $fqcn; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * @param array $filters an array of filters where keys are the aliases and values are the FQCNs |
|
| 206 | - * example: |
|
| 207 | - * array( 'ClassInterface' => 'Fully\Qualified\ClassName' ) |
|
| 208 | - */ |
|
| 209 | - public function setFilters(array $filters) |
|
| 210 | - { |
|
| 211 | - if (empty($filters)) { |
|
| 212 | - return; |
|
| 213 | - } |
|
| 214 | - if ( ! is_array($filters)) { |
|
| 215 | - throw new InvalidDataTypeException( |
|
| 216 | - '$filters', |
|
| 217 | - is_object($filters) ? get_class($filters) : gettype($filters), |
|
| 218 | - __('array of class aliases', 'event_espresso') |
|
| 219 | - ); |
|
| 220 | - } |
|
| 221 | - $this->filters = array_merge($this->filters, $filters); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @param string $type one of the class constants returned from CoffeeMaker::getTypes() |
|
| 228 | - */ |
|
| 229 | - public function setType($type = CoffeeMaker::BREW_NEW) |
|
| 230 | - { |
|
| 231 | - $this->type = CoffeeMaker::validateType($type); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Ensures incoming paths is a valid filepath, or array of valid filepaths, |
|
| 238 | - * and merges them in with any existing filepaths |
|
| 239 | - * |
|
| 240 | - * PLZ NOTE: |
|
| 241 | - * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
| 242 | - * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
| 243 | - * |
|
| 244 | - * @param string|array $paths |
|
| 245 | - */ |
|
| 246 | - public function setPaths($paths = array()) |
|
| 247 | - { |
|
| 248 | - if (empty($paths)) { |
|
| 249 | - return; |
|
| 250 | - } |
|
| 251 | - if ( ! (is_string($paths) || is_array($paths))) { |
|
| 252 | - throw new InvalidDataTypeException( |
|
| 253 | - '$path', |
|
| 254 | - is_object($paths) ? get_class($paths) : gettype($paths), |
|
| 255 | - __('string or array of strings (full server filepath(s))', 'event_espresso') |
|
| 256 | - ); |
|
| 257 | - } |
|
| 258 | - $paths = (array)$paths; |
|
| 259 | - foreach ($paths as $path) { |
|
| 260 | - if (strpos($path, '*') === false && ! is_readable($path)) { |
|
| 261 | - throw new RuntimeException( |
|
| 262 | - sprintf( |
|
| 263 | - __('The following filepath is not readable: "%1$s"', 'event_espresso'), |
|
| 264 | - $path |
|
| 265 | - ) |
|
| 266 | - ); |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - $this->paths = array_merge($this->paths, $paths); |
|
| 270 | - } |
|
| 84 | + public function __construct( |
|
| 85 | + $identifier, |
|
| 86 | + $type = CoffeeMaker::BREW_NEW, |
|
| 87 | + $filters = array(), |
|
| 88 | + $paths = array(), |
|
| 89 | + $fqcn = '' |
|
| 90 | + ) |
|
| 91 | + { |
|
| 92 | + $this->setIdentifier($identifier); |
|
| 93 | + $this->setType($type); |
|
| 94 | + $this->setPaths($paths); |
|
| 95 | + $this->setFqcn($fqcn); |
|
| 96 | + $this->setFilters((array)$filters); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @return string |
|
| 103 | + */ |
|
| 104 | + public function identifier() |
|
| 105 | + { |
|
| 106 | + return $this->identifier; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + public function fqcn() |
|
| 115 | + { |
|
| 116 | + return $this->fqcn; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @return array |
|
| 123 | + */ |
|
| 124 | + public function filters() |
|
| 125 | + { |
|
| 126 | + return (array)$this->filters; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function type() |
|
| 135 | + { |
|
| 136 | + return $this->type; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @return array |
|
| 143 | + */ |
|
| 144 | + public function paths() |
|
| 145 | + { |
|
| 146 | + return (array)$this->paths; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param string $identifier Identifier for the entity class that the Recipe applies to |
|
| 153 | + * Typically a Fully Qualified Class Name |
|
| 154 | + */ |
|
| 155 | + public function setIdentifier($identifier) |
|
| 156 | + { |
|
| 157 | + if ( ! is_string($identifier) || empty($identifier)) { |
|
| 158 | + throw new InvalidIdentifierException( |
|
| 159 | + is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
| 160 | + __('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso') |
|
| 161 | + ); |
|
| 162 | + } |
|
| 163 | + $this->identifier = $identifier; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Ensures incoming string is a valid Fully Qualified Class Name, |
|
| 170 | + * except if this is the default wildcard Recipe ( * ), |
|
| 171 | + * or it's NOT an actual FQCN because the Recipe is using filepaths |
|
| 172 | + * for classes that are not PSR-4 compatible |
|
| 173 | + * PLZ NOTE: |
|
| 174 | + * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
| 175 | + * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
| 176 | + * |
|
| 177 | + * @param string $fqcn |
|
| 178 | + */ |
|
| 179 | + public function setFqcn($fqcn) |
|
| 180 | + { |
|
| 181 | + $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier; |
|
| 182 | + if ( ! is_string($fqcn)) { |
|
| 183 | + throw new InvalidDataTypeException( |
|
| 184 | + '$fqcn', |
|
| 185 | + is_object($fqcn) ? get_class($fqcn) : gettype($fqcn), |
|
| 186 | + __('string (Fully\Qualified\ClassName)', 'event_espresso') |
|
| 187 | + ); |
|
| 188 | + } |
|
| 189 | + $fqcn = ltrim($fqcn, '\\'); |
|
| 190 | + if ( |
|
| 191 | + $fqcn !== Recipe::DEFAULT_ID |
|
| 192 | + && ! empty($fqcn) |
|
| 193 | + && empty($this->paths) |
|
| 194 | + && ! class_exists($fqcn) |
|
| 195 | + ) { |
|
| 196 | + throw new InvalidClassException($fqcn); |
|
| 197 | + } |
|
| 198 | + $this->fqcn = $fqcn; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * @param array $filters an array of filters where keys are the aliases and values are the FQCNs |
|
| 205 | + * example: |
|
| 206 | + * array( 'ClassInterface' => 'Fully\Qualified\ClassName' ) |
|
| 207 | + */ |
|
| 208 | + public function setFilters(array $filters) |
|
| 209 | + { |
|
| 210 | + if (empty($filters)) { |
|
| 211 | + return; |
|
| 212 | + } |
|
| 213 | + if ( ! is_array($filters)) { |
|
| 214 | + throw new InvalidDataTypeException( |
|
| 215 | + '$filters', |
|
| 216 | + is_object($filters) ? get_class($filters) : gettype($filters), |
|
| 217 | + __('array of class aliases', 'event_espresso') |
|
| 218 | + ); |
|
| 219 | + } |
|
| 220 | + $this->filters = array_merge($this->filters, $filters); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @param string $type one of the class constants returned from CoffeeMaker::getTypes() |
|
| 227 | + */ |
|
| 228 | + public function setType($type = CoffeeMaker::BREW_NEW) |
|
| 229 | + { |
|
| 230 | + $this->type = CoffeeMaker::validateType($type); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Ensures incoming paths is a valid filepath, or array of valid filepaths, |
|
| 237 | + * and merges them in with any existing filepaths |
|
| 238 | + * |
|
| 239 | + * PLZ NOTE: |
|
| 240 | + * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
| 241 | + * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
| 242 | + * |
|
| 243 | + * @param string|array $paths |
|
| 244 | + */ |
|
| 245 | + public function setPaths($paths = array()) |
|
| 246 | + { |
|
| 247 | + if (empty($paths)) { |
|
| 248 | + return; |
|
| 249 | + } |
|
| 250 | + if ( ! (is_string($paths) || is_array($paths))) { |
|
| 251 | + throw new InvalidDataTypeException( |
|
| 252 | + '$path', |
|
| 253 | + is_object($paths) ? get_class($paths) : gettype($paths), |
|
| 254 | + __('string or array of strings (full server filepath(s))', 'event_espresso') |
|
| 255 | + ); |
|
| 256 | + } |
|
| 257 | + $paths = (array)$paths; |
|
| 258 | + foreach ($paths as $path) { |
|
| 259 | + if (strpos($path, '*') === false && ! is_readable($path)) { |
|
| 260 | + throw new RuntimeException( |
|
| 261 | + sprintf( |
|
| 262 | + __('The following filepath is not readable: "%1$s"', 'event_espresso'), |
|
| 263 | + $path |
|
| 264 | + ) |
|
| 265 | + ); |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + $this->paths = array_merge($this->paths, $paths); |
|
| 269 | + } |
|
| 271 | 270 | |
| 272 | 271 | |
| 273 | 272 | |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | use OutOfBoundsException; |
| 14 | 14 | |
| 15 | 15 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 16 | - exit('No direct script access allowed'); |
|
| 16 | + exit('No direct script access allowed'); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | |
@@ -32,441 +32,441 @@ discard block |
||
| 32 | 32 | { |
| 33 | 33 | |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * This was the best coffee related name I could think of to represent class name "aliases" |
|
| 37 | - * So classes can be found via an alias identifier, |
|
| 38 | - * that is revealed when it is run through... the filters... eh? get it? |
|
| 39 | - * |
|
| 40 | - * @var array $filters |
|
| 41 | - */ |
|
| 42 | - private $filters = array(); |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * These are the classes that will actually build the objects (to order of course) |
|
| 46 | - * |
|
| 47 | - * @var array $coffee_makers |
|
| 48 | - */ |
|
| 49 | - private $coffee_makers = array(); |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * where the instantiated "singleton" objects are stored |
|
| 53 | - * |
|
| 54 | - * @var CollectionInterface $carafe |
|
| 55 | - */ |
|
| 56 | - private $carafe; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * collection of Recipes that instruct us how to brew objects |
|
| 60 | - * |
|
| 61 | - * @var CollectionInterface $recipes |
|
| 62 | - */ |
|
| 63 | - private $recipes; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * collection of closures for brewing objects |
|
| 67 | - * |
|
| 68 | - * @var CollectionInterface $reservoir |
|
| 69 | - */ |
|
| 70 | - private $reservoir; |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * CoffeeShop constructor |
|
| 76 | - */ |
|
| 77 | - public function __construct() |
|
| 78 | - { |
|
| 79 | - // array for storing class aliases |
|
| 80 | - $this->filters = array(); |
|
| 81 | - // create collection for storing shared services |
|
| 82 | - $this->carafe = new LooseCollection( '' ); |
|
| 83 | - // create collection for storing recipes that tell how to build services and entities |
|
| 84 | - $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
|
| 85 | - // create collection for storing closures for constructing new entities |
|
| 86 | - $this->reservoir = new Collection('Closure'); |
|
| 87 | - // create collection for storing the generators that build our services and entity closures |
|
| 88 | - $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Returns true if the container can return an entry for the given identifier. |
|
| 95 | - * Returns false otherwise. |
|
| 96 | - * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
|
| 97 | - * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
|
| 98 | - * |
|
| 99 | - * @param string $identifier Identifier of the entry to look for. |
|
| 100 | - * Typically a Fully Qualified Class Name |
|
| 101 | - * @return boolean |
|
| 102 | - */ |
|
| 103 | - public function has($identifier) |
|
| 104 | - { |
|
| 105 | - $identifier = $this->filterIdentifier($identifier); |
|
| 106 | - return $this->carafe->has($identifier); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * finds a previously brewed (SHARED) service and returns it |
|
| 113 | - * |
|
| 114 | - * @param string $identifier Identifier for the entity class to be constructed. |
|
| 115 | - * Typically a Fully Qualified Class Name |
|
| 116 | - * @return mixed |
|
| 117 | - * @throws ServiceNotFoundException No service was found for this identifier. |
|
| 118 | - */ |
|
| 119 | - public function get($identifier) |
|
| 120 | - { |
|
| 121 | - $identifier = $this->filterIdentifier($identifier); |
|
| 122 | - if ($this->carafe->has($identifier)) { |
|
| 123 | - return $this->carafe->get($identifier); |
|
| 124 | - } |
|
| 125 | - throw new ServiceNotFoundException($identifier); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * returns an instance of the requested entity type using the supplied arguments. |
|
| 132 | - * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
|
| 133 | - * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
|
| 134 | - * If the request is for a new entity and a closure exists in the reservoir for creating it, |
|
| 135 | - * then a new entity will be instantiated from the closure and returned. |
|
| 136 | - * If a closure does not exist, then one will be built and added to the reservoir |
|
| 137 | - * before instantiating the requested entity. |
|
| 138 | - * |
|
| 139 | - * @param string $identifier Identifier for the entity class to be constructed. |
|
| 140 | - * Typically a Fully Qualified Class Name |
|
| 141 | - * @param array $arguments an array of arguments to be passed to the entity constructor |
|
| 142 | - * @param string $type |
|
| 143 | - * @return mixed |
|
| 144 | - * @throws ServiceNotFoundException No service was found for this identifier. |
|
| 145 | - */ |
|
| 146 | - public function brew($identifier, $arguments = array(), $type = '') |
|
| 147 | - { |
|
| 148 | - // resolve any class aliases that may exist |
|
| 149 | - $identifier = $this->filterIdentifier($identifier); |
|
| 150 | - try { |
|
| 151 | - // is a shared service being requested? |
|
| 152 | - if (empty($type) || $type === CoffeeMaker::BREW_SHARED) { |
|
| 153 | - // if a shared service was requested and an instance is in the carafe, then return it |
|
| 154 | - return $this->get($identifier); |
|
| 155 | - } |
|
| 156 | - } catch (ServiceNotFoundException $e) { |
|
| 157 | - // if not then we'll just catch the ServiceNotFoundException but not do anything just yet, |
|
| 158 | - // and instead, attempt to build whatever was requested |
|
| 159 | - } |
|
| 160 | - $brewed = false; |
|
| 161 | - // if the reservoir doesn't have a closure already for the requested identifier, |
|
| 162 | - // then neither a shared service nor a closure for making entities has been built yet |
|
| 163 | - if ( ! $this->reservoir->has($identifier)) { |
|
| 164 | - // so let's brew something up and add it to the proper collection |
|
| 165 | - $brewed = $this->makeCoffee($identifier, $arguments, $type); |
|
| 166 | - } |
|
| 167 | - // was the brewed item a callable factory function ? |
|
| 168 | - if (is_callable($brewed)) { |
|
| 169 | - // then instantiate a new entity from the cached closure |
|
| 170 | - $entity = $brewed($arguments); |
|
| 171 | - } else if ($brewed) { |
|
| 172 | - // requested object was a shared entity, so attempt to get it from the carafe again |
|
| 173 | - // because if it wasn't there before, then it should have just been brewed and added, |
|
| 174 | - // but if it still isn't there, then this time |
|
| 175 | - // the thrown ServiceNotFoundException will not be caught |
|
| 176 | - $entity = $this->get($identifier); |
|
| 177 | - } else { |
|
| 178 | - // if identifier is for a non-shared entity, |
|
| 179 | - // then either a cached closure already existed, or was just brewed |
|
| 180 | - $closure = $this->reservoir->get($identifier); |
|
| 181 | - $entity = $closure($arguments); |
|
| 182 | - } |
|
| 183 | - return $entity; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * @param CoffeeMakerInterface $coffee_maker |
|
| 190 | - * @param string $type |
|
| 191 | - * @return bool |
|
| 192 | - */ |
|
| 193 | - public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
|
| 194 | - { |
|
| 195 | - $type = CoffeeMaker::validateType($type); |
|
| 196 | - return $this->coffee_makers->add($coffee_maker, $type); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * @param string $identifier |
|
| 203 | - * @param callable $closure |
|
| 204 | - * @return callable|null |
|
| 205 | - */ |
|
| 206 | - public function addClosure($identifier, $closure) |
|
| 207 | - { |
|
| 208 | - if ( ! is_callable($closure)) { |
|
| 209 | - throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
|
| 210 | - } |
|
| 211 | - $identifier = $this->processIdentifier($identifier); |
|
| 212 | - if ($this->reservoir->add($closure, $identifier)) { |
|
| 213 | - return $closure; |
|
| 214 | - } |
|
| 215 | - return null; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @param string $identifier Identifier for the entity class that the service applies to |
|
| 222 | - * Typically a Fully Qualified Class Name |
|
| 223 | - * @param mixed $service |
|
| 224 | - * @return bool |
|
| 225 | - */ |
|
| 226 | - public function addService($identifier, $service) |
|
| 227 | - { |
|
| 228 | - $identifier = $this->processIdentifier($identifier); |
|
| 229 | - $service = $this->validateService($identifier, $service); |
|
| 230 | - return $this->carafe->add($service, $identifier); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Adds instructions on how to brew objects |
|
| 237 | - * |
|
| 238 | - * @param RecipeInterface $recipe |
|
| 239 | - * @return mixed |
|
| 240 | - */ |
|
| 241 | - public function addRecipe(RecipeInterface $recipe) |
|
| 242 | - { |
|
| 243 | - $this->addAliases($recipe->identifier(), $recipe->filters()); |
|
| 244 | - $identifier = $this->processIdentifier($recipe->identifier()); |
|
| 245 | - return $this->recipes->add($recipe, $identifier); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Get instructions on how to brew objects |
|
| 252 | - * |
|
| 253 | - * @param string $identifier Identifier for the entity class that the recipe applies to |
|
| 254 | - * Typically a Fully Qualified Class Name |
|
| 255 | - * @param string $type |
|
| 256 | - * @return RecipeInterface |
|
| 257 | - */ |
|
| 258 | - public function getRecipe($identifier, $type = '') |
|
| 259 | - { |
|
| 260 | - $identifier = $this->processIdentifier($identifier); |
|
| 261 | - if ($this->recipes->has($identifier)) { |
|
| 262 | - return $this->recipes->get($identifier); |
|
| 263 | - } |
|
| 264 | - $default_recipes = $this->getDefaultRecipes(); |
|
| 265 | - $matches = array(); |
|
| 266 | - foreach ($default_recipes as $wildcard => $default_recipe) { |
|
| 267 | - // is the wildcard recipe prefix in the identifier ? |
|
| 268 | - if (strpos($identifier, $wildcard) !== false) { |
|
| 269 | - // track matches and use the number of wildcard characters matched for the key |
|
| 270 | - $matches[strlen($wildcard)] = $default_recipe; |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - if (count($matches) > 0) { |
|
| 274 | - // sort our recipes by the number of wildcard characters matched |
|
| 275 | - ksort($matches); |
|
| 276 | - // then grab the last recipe form the list, since it had the most matching characters |
|
| 277 | - $match = array_pop($matches); |
|
| 278 | - // since we are using a default recipe, we need to set it's identifier and fqcn |
|
| 279 | - return $this->copyDefaultRecipe($match, $identifier, $type); |
|
| 280 | - } |
|
| 281 | - if ($this->recipes->has(Recipe::DEFAULT_ID)) { |
|
| 282 | - // since we are using a default recipe, we need to set it's identifier and fqcn |
|
| 283 | - return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type); |
|
| 284 | - } |
|
| 285 | - throw new OutOfBoundsException(__('Could not brew coffee because no recipes were found.', 'event_espresso')); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * adds class name aliases to list of filters |
|
| 292 | - * |
|
| 293 | - * @param string $identifier Identifier for the entity class that the alias applies to |
|
| 294 | - * Typically a Fully Qualified Class Name |
|
| 295 | - * @param array $aliases |
|
| 296 | - * @return void |
|
| 297 | - * @throws InvalidIdentifierException |
|
| 298 | - */ |
|
| 299 | - public function addAliases($identifier, $aliases) |
|
| 300 | - { |
|
| 301 | - if (empty($aliases)) { |
|
| 302 | - return; |
|
| 303 | - } |
|
| 304 | - $identifier = $this->processIdentifier($identifier); |
|
| 305 | - foreach ((array)$aliases as $alias) { |
|
| 306 | - $this->filters[$this->processIdentifier($alias)] = $identifier; |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Adds a service to one of the internal collections |
|
| 314 | - * |
|
| 315 | - * @param $identifier |
|
| 316 | - * @param array $arguments |
|
| 317 | - * @param string $type |
|
| 318 | - * @return mixed |
|
| 319 | - * @throws ServiceExistsException |
|
| 320 | - */ |
|
| 321 | - private function makeCoffee($identifier, $arguments = array(), $type = '') |
|
| 322 | - { |
|
| 323 | - if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) { |
|
| 324 | - throw new ServiceExistsException($identifier); |
|
| 325 | - } |
|
| 326 | - $identifier = $this->filterIdentifier($identifier); |
|
| 327 | - $recipe = $this->getRecipe($identifier, $type); |
|
| 328 | - $type = ! empty($type) ? $type : $recipe->type(); |
|
| 329 | - $coffee_maker = $this->getCoffeeMaker($type); |
|
| 330 | - return $coffee_maker->brew($recipe, $arguments); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * filters alias identifiers to find the real class name |
|
| 337 | - * |
|
| 338 | - * @param string $identifier Identifier for the entity class that the filter applies to |
|
| 339 | - * Typically a Fully Qualified Class Name |
|
| 340 | - * @return string |
|
| 341 | - * @throws InvalidIdentifierException |
|
| 342 | - */ |
|
| 343 | - private function filterIdentifier($identifier) |
|
| 344 | - { |
|
| 345 | - $identifier = $this->processIdentifier($identifier); |
|
| 346 | - return isset($this->filters[$identifier]) && ! empty($this->filters[$identifier]) |
|
| 347 | - ? $this->filters[$identifier] |
|
| 348 | - : $identifier; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * verifies and standardizes identifiers |
|
| 355 | - * |
|
| 356 | - * @param string $identifier Identifier for the entity class |
|
| 357 | - * Typically a Fully Qualified Class Name |
|
| 358 | - * @return string |
|
| 359 | - * @throws InvalidIdentifierException |
|
| 360 | - */ |
|
| 361 | - private function processIdentifier($identifier) |
|
| 362 | - { |
|
| 363 | - if ( ! is_string($identifier)) { |
|
| 364 | - throw new InvalidIdentifierException( |
|
| 365 | - is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
| 366 | - '\Fully\Qualified\ClassName' |
|
| 367 | - ); |
|
| 368 | - } |
|
| 369 | - return ltrim($identifier, '\\'); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * @param string $type |
|
| 376 | - * @return CoffeeMakerInterface |
|
| 377 | - * @throws InvalidDataTypeException |
|
| 378 | - * @throws InvalidClassException |
|
| 379 | - */ |
|
| 380 | - private function getCoffeeMaker($type) |
|
| 381 | - { |
|
| 382 | - if ( ! $this->coffee_makers->has($type)) { |
|
| 383 | - throw new OutOfBoundsException( |
|
| 384 | - __('The requested coffee maker is either missing or invalid.', 'event_espresso') |
|
| 385 | - ); |
|
| 386 | - } |
|
| 387 | - return $this->coffee_makers->get($type); |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Retrieves all recipes that use a wildcard "*" in their identifier |
|
| 394 | - * This allows recipes to be set up for handling |
|
| 395 | - * legacy classes that do not support PSR-4 autoloading. |
|
| 396 | - * for example: |
|
| 397 | - * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
|
| 398 | - * |
|
| 399 | - * @return array |
|
| 400 | - */ |
|
| 401 | - private function getDefaultRecipes() |
|
| 402 | - { |
|
| 403 | - $default_recipes = array(); |
|
| 404 | - $this->recipes->rewind(); |
|
| 405 | - while ($this->recipes->valid()) { |
|
| 406 | - $identifier = $this->recipes->getInfo(); |
|
| 407 | - // does this recipe use a wildcard ? (but is NOT the global default) |
|
| 408 | - if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
|
| 409 | - // strip the wildcard and use identifier as key |
|
| 410 | - $default_recipes[str_replace('*', '', $identifier)] = $this->recipes->current(); |
|
| 411 | - } |
|
| 412 | - $this->recipes->next(); |
|
| 413 | - } |
|
| 414 | - return $default_recipes; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * clones a default recipe and then copies details |
|
| 421 | - * from the incoming request to it so that it can be used |
|
| 422 | - * |
|
| 423 | - * @param RecipeInterface $default_recipe |
|
| 424 | - * @param string $identifier |
|
| 425 | - * @param string $type |
|
| 426 | - * @return RecipeInterface |
|
| 427 | - */ |
|
| 428 | - private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
|
| 429 | - { |
|
| 430 | - $recipe = clone($default_recipe); |
|
| 431 | - if ( ! empty($type)) { |
|
| 432 | - $recipe->setType($type); |
|
| 433 | - } |
|
| 434 | - // is this the base default recipe ? |
|
| 435 | - if ($default_recipe->identifier() === Recipe::DEFAULT_ID) { |
|
| 436 | - $recipe->setIdentifier($identifier); |
|
| 437 | - $recipe->setFqcn($identifier); |
|
| 438 | - return $recipe; |
|
| 439 | - } |
|
| 440 | - $recipe->setIdentifier($identifier); |
|
| 441 | - foreach ($default_recipe->paths() as $path) { |
|
| 442 | - $path = str_replace('*', $identifier, $path); |
|
| 443 | - if (is_readable($path)) { |
|
| 444 | - $recipe->setPaths($path); |
|
| 445 | - } |
|
| 446 | - } |
|
| 447 | - $recipe->setFqcn($identifier); |
|
| 448 | - return $recipe; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * @param string $identifier Identifier for the entity class that the service applies to |
|
| 455 | - * Typically a Fully Qualified Class Name |
|
| 456 | - * @param mixed $service |
|
| 457 | - * @return object |
|
| 458 | - * @throws InvalidServiceException |
|
| 459 | - */ |
|
| 460 | - private function validateService($identifier, $service) |
|
| 461 | - { |
|
| 462 | - if ( ! is_object($service)) { |
|
| 463 | - throw new InvalidServiceException( |
|
| 464 | - $identifier, |
|
| 465 | - $service |
|
| 466 | - ); |
|
| 467 | - } |
|
| 468 | - return $service; |
|
| 469 | - } |
|
| 35 | + /** |
|
| 36 | + * This was the best coffee related name I could think of to represent class name "aliases" |
|
| 37 | + * So classes can be found via an alias identifier, |
|
| 38 | + * that is revealed when it is run through... the filters... eh? get it? |
|
| 39 | + * |
|
| 40 | + * @var array $filters |
|
| 41 | + */ |
|
| 42 | + private $filters = array(); |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * These are the classes that will actually build the objects (to order of course) |
|
| 46 | + * |
|
| 47 | + * @var array $coffee_makers |
|
| 48 | + */ |
|
| 49 | + private $coffee_makers = array(); |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * where the instantiated "singleton" objects are stored |
|
| 53 | + * |
|
| 54 | + * @var CollectionInterface $carafe |
|
| 55 | + */ |
|
| 56 | + private $carafe; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * collection of Recipes that instruct us how to brew objects |
|
| 60 | + * |
|
| 61 | + * @var CollectionInterface $recipes |
|
| 62 | + */ |
|
| 63 | + private $recipes; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * collection of closures for brewing objects |
|
| 67 | + * |
|
| 68 | + * @var CollectionInterface $reservoir |
|
| 69 | + */ |
|
| 70 | + private $reservoir; |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * CoffeeShop constructor |
|
| 76 | + */ |
|
| 77 | + public function __construct() |
|
| 78 | + { |
|
| 79 | + // array for storing class aliases |
|
| 80 | + $this->filters = array(); |
|
| 81 | + // create collection for storing shared services |
|
| 82 | + $this->carafe = new LooseCollection( '' ); |
|
| 83 | + // create collection for storing recipes that tell how to build services and entities |
|
| 84 | + $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
|
| 85 | + // create collection for storing closures for constructing new entities |
|
| 86 | + $this->reservoir = new Collection('Closure'); |
|
| 87 | + // create collection for storing the generators that build our services and entity closures |
|
| 88 | + $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Returns true if the container can return an entry for the given identifier. |
|
| 95 | + * Returns false otherwise. |
|
| 96 | + * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
|
| 97 | + * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
|
| 98 | + * |
|
| 99 | + * @param string $identifier Identifier of the entry to look for. |
|
| 100 | + * Typically a Fully Qualified Class Name |
|
| 101 | + * @return boolean |
|
| 102 | + */ |
|
| 103 | + public function has($identifier) |
|
| 104 | + { |
|
| 105 | + $identifier = $this->filterIdentifier($identifier); |
|
| 106 | + return $this->carafe->has($identifier); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * finds a previously brewed (SHARED) service and returns it |
|
| 113 | + * |
|
| 114 | + * @param string $identifier Identifier for the entity class to be constructed. |
|
| 115 | + * Typically a Fully Qualified Class Name |
|
| 116 | + * @return mixed |
|
| 117 | + * @throws ServiceNotFoundException No service was found for this identifier. |
|
| 118 | + */ |
|
| 119 | + public function get($identifier) |
|
| 120 | + { |
|
| 121 | + $identifier = $this->filterIdentifier($identifier); |
|
| 122 | + if ($this->carafe->has($identifier)) { |
|
| 123 | + return $this->carafe->get($identifier); |
|
| 124 | + } |
|
| 125 | + throw new ServiceNotFoundException($identifier); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * returns an instance of the requested entity type using the supplied arguments. |
|
| 132 | + * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
|
| 133 | + * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
|
| 134 | + * If the request is for a new entity and a closure exists in the reservoir for creating it, |
|
| 135 | + * then a new entity will be instantiated from the closure and returned. |
|
| 136 | + * If a closure does not exist, then one will be built and added to the reservoir |
|
| 137 | + * before instantiating the requested entity. |
|
| 138 | + * |
|
| 139 | + * @param string $identifier Identifier for the entity class to be constructed. |
|
| 140 | + * Typically a Fully Qualified Class Name |
|
| 141 | + * @param array $arguments an array of arguments to be passed to the entity constructor |
|
| 142 | + * @param string $type |
|
| 143 | + * @return mixed |
|
| 144 | + * @throws ServiceNotFoundException No service was found for this identifier. |
|
| 145 | + */ |
|
| 146 | + public function brew($identifier, $arguments = array(), $type = '') |
|
| 147 | + { |
|
| 148 | + // resolve any class aliases that may exist |
|
| 149 | + $identifier = $this->filterIdentifier($identifier); |
|
| 150 | + try { |
|
| 151 | + // is a shared service being requested? |
|
| 152 | + if (empty($type) || $type === CoffeeMaker::BREW_SHARED) { |
|
| 153 | + // if a shared service was requested and an instance is in the carafe, then return it |
|
| 154 | + return $this->get($identifier); |
|
| 155 | + } |
|
| 156 | + } catch (ServiceNotFoundException $e) { |
|
| 157 | + // if not then we'll just catch the ServiceNotFoundException but not do anything just yet, |
|
| 158 | + // and instead, attempt to build whatever was requested |
|
| 159 | + } |
|
| 160 | + $brewed = false; |
|
| 161 | + // if the reservoir doesn't have a closure already for the requested identifier, |
|
| 162 | + // then neither a shared service nor a closure for making entities has been built yet |
|
| 163 | + if ( ! $this->reservoir->has($identifier)) { |
|
| 164 | + // so let's brew something up and add it to the proper collection |
|
| 165 | + $brewed = $this->makeCoffee($identifier, $arguments, $type); |
|
| 166 | + } |
|
| 167 | + // was the brewed item a callable factory function ? |
|
| 168 | + if (is_callable($brewed)) { |
|
| 169 | + // then instantiate a new entity from the cached closure |
|
| 170 | + $entity = $brewed($arguments); |
|
| 171 | + } else if ($brewed) { |
|
| 172 | + // requested object was a shared entity, so attempt to get it from the carafe again |
|
| 173 | + // because if it wasn't there before, then it should have just been brewed and added, |
|
| 174 | + // but if it still isn't there, then this time |
|
| 175 | + // the thrown ServiceNotFoundException will not be caught |
|
| 176 | + $entity = $this->get($identifier); |
|
| 177 | + } else { |
|
| 178 | + // if identifier is for a non-shared entity, |
|
| 179 | + // then either a cached closure already existed, or was just brewed |
|
| 180 | + $closure = $this->reservoir->get($identifier); |
|
| 181 | + $entity = $closure($arguments); |
|
| 182 | + } |
|
| 183 | + return $entity; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * @param CoffeeMakerInterface $coffee_maker |
|
| 190 | + * @param string $type |
|
| 191 | + * @return bool |
|
| 192 | + */ |
|
| 193 | + public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
|
| 194 | + { |
|
| 195 | + $type = CoffeeMaker::validateType($type); |
|
| 196 | + return $this->coffee_makers->add($coffee_maker, $type); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * @param string $identifier |
|
| 203 | + * @param callable $closure |
|
| 204 | + * @return callable|null |
|
| 205 | + */ |
|
| 206 | + public function addClosure($identifier, $closure) |
|
| 207 | + { |
|
| 208 | + if ( ! is_callable($closure)) { |
|
| 209 | + throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
|
| 210 | + } |
|
| 211 | + $identifier = $this->processIdentifier($identifier); |
|
| 212 | + if ($this->reservoir->add($closure, $identifier)) { |
|
| 213 | + return $closure; |
|
| 214 | + } |
|
| 215 | + return null; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @param string $identifier Identifier for the entity class that the service applies to |
|
| 222 | + * Typically a Fully Qualified Class Name |
|
| 223 | + * @param mixed $service |
|
| 224 | + * @return bool |
|
| 225 | + */ |
|
| 226 | + public function addService($identifier, $service) |
|
| 227 | + { |
|
| 228 | + $identifier = $this->processIdentifier($identifier); |
|
| 229 | + $service = $this->validateService($identifier, $service); |
|
| 230 | + return $this->carafe->add($service, $identifier); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Adds instructions on how to brew objects |
|
| 237 | + * |
|
| 238 | + * @param RecipeInterface $recipe |
|
| 239 | + * @return mixed |
|
| 240 | + */ |
|
| 241 | + public function addRecipe(RecipeInterface $recipe) |
|
| 242 | + { |
|
| 243 | + $this->addAliases($recipe->identifier(), $recipe->filters()); |
|
| 244 | + $identifier = $this->processIdentifier($recipe->identifier()); |
|
| 245 | + return $this->recipes->add($recipe, $identifier); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Get instructions on how to brew objects |
|
| 252 | + * |
|
| 253 | + * @param string $identifier Identifier for the entity class that the recipe applies to |
|
| 254 | + * Typically a Fully Qualified Class Name |
|
| 255 | + * @param string $type |
|
| 256 | + * @return RecipeInterface |
|
| 257 | + */ |
|
| 258 | + public function getRecipe($identifier, $type = '') |
|
| 259 | + { |
|
| 260 | + $identifier = $this->processIdentifier($identifier); |
|
| 261 | + if ($this->recipes->has($identifier)) { |
|
| 262 | + return $this->recipes->get($identifier); |
|
| 263 | + } |
|
| 264 | + $default_recipes = $this->getDefaultRecipes(); |
|
| 265 | + $matches = array(); |
|
| 266 | + foreach ($default_recipes as $wildcard => $default_recipe) { |
|
| 267 | + // is the wildcard recipe prefix in the identifier ? |
|
| 268 | + if (strpos($identifier, $wildcard) !== false) { |
|
| 269 | + // track matches and use the number of wildcard characters matched for the key |
|
| 270 | + $matches[strlen($wildcard)] = $default_recipe; |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + if (count($matches) > 0) { |
|
| 274 | + // sort our recipes by the number of wildcard characters matched |
|
| 275 | + ksort($matches); |
|
| 276 | + // then grab the last recipe form the list, since it had the most matching characters |
|
| 277 | + $match = array_pop($matches); |
|
| 278 | + // since we are using a default recipe, we need to set it's identifier and fqcn |
|
| 279 | + return $this->copyDefaultRecipe($match, $identifier, $type); |
|
| 280 | + } |
|
| 281 | + if ($this->recipes->has(Recipe::DEFAULT_ID)) { |
|
| 282 | + // since we are using a default recipe, we need to set it's identifier and fqcn |
|
| 283 | + return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type); |
|
| 284 | + } |
|
| 285 | + throw new OutOfBoundsException(__('Could not brew coffee because no recipes were found.', 'event_espresso')); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * adds class name aliases to list of filters |
|
| 292 | + * |
|
| 293 | + * @param string $identifier Identifier for the entity class that the alias applies to |
|
| 294 | + * Typically a Fully Qualified Class Name |
|
| 295 | + * @param array $aliases |
|
| 296 | + * @return void |
|
| 297 | + * @throws InvalidIdentifierException |
|
| 298 | + */ |
|
| 299 | + public function addAliases($identifier, $aliases) |
|
| 300 | + { |
|
| 301 | + if (empty($aliases)) { |
|
| 302 | + return; |
|
| 303 | + } |
|
| 304 | + $identifier = $this->processIdentifier($identifier); |
|
| 305 | + foreach ((array)$aliases as $alias) { |
|
| 306 | + $this->filters[$this->processIdentifier($alias)] = $identifier; |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Adds a service to one of the internal collections |
|
| 314 | + * |
|
| 315 | + * @param $identifier |
|
| 316 | + * @param array $arguments |
|
| 317 | + * @param string $type |
|
| 318 | + * @return mixed |
|
| 319 | + * @throws ServiceExistsException |
|
| 320 | + */ |
|
| 321 | + private function makeCoffee($identifier, $arguments = array(), $type = '') |
|
| 322 | + { |
|
| 323 | + if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) { |
|
| 324 | + throw new ServiceExistsException($identifier); |
|
| 325 | + } |
|
| 326 | + $identifier = $this->filterIdentifier($identifier); |
|
| 327 | + $recipe = $this->getRecipe($identifier, $type); |
|
| 328 | + $type = ! empty($type) ? $type : $recipe->type(); |
|
| 329 | + $coffee_maker = $this->getCoffeeMaker($type); |
|
| 330 | + return $coffee_maker->brew($recipe, $arguments); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * filters alias identifiers to find the real class name |
|
| 337 | + * |
|
| 338 | + * @param string $identifier Identifier for the entity class that the filter applies to |
|
| 339 | + * Typically a Fully Qualified Class Name |
|
| 340 | + * @return string |
|
| 341 | + * @throws InvalidIdentifierException |
|
| 342 | + */ |
|
| 343 | + private function filterIdentifier($identifier) |
|
| 344 | + { |
|
| 345 | + $identifier = $this->processIdentifier($identifier); |
|
| 346 | + return isset($this->filters[$identifier]) && ! empty($this->filters[$identifier]) |
|
| 347 | + ? $this->filters[$identifier] |
|
| 348 | + : $identifier; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * verifies and standardizes identifiers |
|
| 355 | + * |
|
| 356 | + * @param string $identifier Identifier for the entity class |
|
| 357 | + * Typically a Fully Qualified Class Name |
|
| 358 | + * @return string |
|
| 359 | + * @throws InvalidIdentifierException |
|
| 360 | + */ |
|
| 361 | + private function processIdentifier($identifier) |
|
| 362 | + { |
|
| 363 | + if ( ! is_string($identifier)) { |
|
| 364 | + throw new InvalidIdentifierException( |
|
| 365 | + is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
| 366 | + '\Fully\Qualified\ClassName' |
|
| 367 | + ); |
|
| 368 | + } |
|
| 369 | + return ltrim($identifier, '\\'); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * @param string $type |
|
| 376 | + * @return CoffeeMakerInterface |
|
| 377 | + * @throws InvalidDataTypeException |
|
| 378 | + * @throws InvalidClassException |
|
| 379 | + */ |
|
| 380 | + private function getCoffeeMaker($type) |
|
| 381 | + { |
|
| 382 | + if ( ! $this->coffee_makers->has($type)) { |
|
| 383 | + throw new OutOfBoundsException( |
|
| 384 | + __('The requested coffee maker is either missing or invalid.', 'event_espresso') |
|
| 385 | + ); |
|
| 386 | + } |
|
| 387 | + return $this->coffee_makers->get($type); |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Retrieves all recipes that use a wildcard "*" in their identifier |
|
| 394 | + * This allows recipes to be set up for handling |
|
| 395 | + * legacy classes that do not support PSR-4 autoloading. |
|
| 396 | + * for example: |
|
| 397 | + * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
|
| 398 | + * |
|
| 399 | + * @return array |
|
| 400 | + */ |
|
| 401 | + private function getDefaultRecipes() |
|
| 402 | + { |
|
| 403 | + $default_recipes = array(); |
|
| 404 | + $this->recipes->rewind(); |
|
| 405 | + while ($this->recipes->valid()) { |
|
| 406 | + $identifier = $this->recipes->getInfo(); |
|
| 407 | + // does this recipe use a wildcard ? (but is NOT the global default) |
|
| 408 | + if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
|
| 409 | + // strip the wildcard and use identifier as key |
|
| 410 | + $default_recipes[str_replace('*', '', $identifier)] = $this->recipes->current(); |
|
| 411 | + } |
|
| 412 | + $this->recipes->next(); |
|
| 413 | + } |
|
| 414 | + return $default_recipes; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * clones a default recipe and then copies details |
|
| 421 | + * from the incoming request to it so that it can be used |
|
| 422 | + * |
|
| 423 | + * @param RecipeInterface $default_recipe |
|
| 424 | + * @param string $identifier |
|
| 425 | + * @param string $type |
|
| 426 | + * @return RecipeInterface |
|
| 427 | + */ |
|
| 428 | + private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
|
| 429 | + { |
|
| 430 | + $recipe = clone($default_recipe); |
|
| 431 | + if ( ! empty($type)) { |
|
| 432 | + $recipe->setType($type); |
|
| 433 | + } |
|
| 434 | + // is this the base default recipe ? |
|
| 435 | + if ($default_recipe->identifier() === Recipe::DEFAULT_ID) { |
|
| 436 | + $recipe->setIdentifier($identifier); |
|
| 437 | + $recipe->setFqcn($identifier); |
|
| 438 | + return $recipe; |
|
| 439 | + } |
|
| 440 | + $recipe->setIdentifier($identifier); |
|
| 441 | + foreach ($default_recipe->paths() as $path) { |
|
| 442 | + $path = str_replace('*', $identifier, $path); |
|
| 443 | + if (is_readable($path)) { |
|
| 444 | + $recipe->setPaths($path); |
|
| 445 | + } |
|
| 446 | + } |
|
| 447 | + $recipe->setFqcn($identifier); |
|
| 448 | + return $recipe; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * @param string $identifier Identifier for the entity class that the service applies to |
|
| 455 | + * Typically a Fully Qualified Class Name |
|
| 456 | + * @param mixed $service |
|
| 457 | + * @return object |
|
| 458 | + * @throws InvalidServiceException |
|
| 459 | + */ |
|
| 460 | + private function validateService($identifier, $service) |
|
| 461 | + { |
|
| 462 | + if ( ! is_object($service)) { |
|
| 463 | + throw new InvalidServiceException( |
|
| 464 | + $identifier, |
|
| 465 | + $service |
|
| 466 | + ); |
|
| 467 | + } |
|
| 468 | + return $service; |
|
| 469 | + } |
|
| 470 | 470 | |
| 471 | 471 | } |
| 472 | 472 | // End of file CoffeeShop.php |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | namespace EventEspresso\core\services\container\exceptions; |
| 3 | 3 | |
| 4 | 4 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 5 | - exit('No direct script access allowed'); |
|
| 5 | + exit('No direct script access allowed'); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | |
@@ -18,28 +18,28 @@ discard block |
||
| 18 | 18 | class ServiceNotFoundException extends \RuntimeException |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * ServiceNotFoundException constructor |
|
| 23 | - * |
|
| 24 | - * @param string $service_name the name of the requested service |
|
| 25 | - * @param string $message |
|
| 26 | - * @param int $code |
|
| 27 | - * @param \Exception $previous |
|
| 28 | - */ |
|
| 29 | - public function __construct( |
|
| 30 | - $service_name, |
|
| 31 | - $message = '', |
|
| 32 | - $code = 0, |
|
| 33 | - \Exception $previous = null |
|
| 34 | - ) { |
|
| 35 | - if (empty($message)) { |
|
| 36 | - $message = sprintf( |
|
| 37 | - __('The requested service "%1$s" could not found be found in the CoffeeShop.', 'event_espresso'), |
|
| 38 | - $service_name |
|
| 39 | - ); |
|
| 40 | - } |
|
| 41 | - parent::__construct($message, $code, $previous); |
|
| 42 | - } |
|
| 21 | + /** |
|
| 22 | + * ServiceNotFoundException constructor |
|
| 23 | + * |
|
| 24 | + * @param string $service_name the name of the requested service |
|
| 25 | + * @param string $message |
|
| 26 | + * @param int $code |
|
| 27 | + * @param \Exception $previous |
|
| 28 | + */ |
|
| 29 | + public function __construct( |
|
| 30 | + $service_name, |
|
| 31 | + $message = '', |
|
| 32 | + $code = 0, |
|
| 33 | + \Exception $previous = null |
|
| 34 | + ) { |
|
| 35 | + if (empty($message)) { |
|
| 36 | + $message = sprintf( |
|
| 37 | + __('The requested service "%1$s" could not found be found in the CoffeeShop.', 'event_espresso'), |
|
| 38 | + $service_name |
|
| 39 | + ); |
|
| 40 | + } |
|
| 41 | + parent::__construct($message, $code, $previous); |
|
| 42 | + } |
|
| 43 | 43 | } |
| 44 | 44 | // End of file ServiceNotFoundException.php |
| 45 | 45 | // Location: /ServiceNotFoundException.php |
| 46 | 46 | \ No newline at end of file |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | namespace EventEspresso\core\services\container\exceptions; |
| 3 | 3 | |
| 4 | 4 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 5 | - exit('No direct script access allowed'); |
|
| 5 | + exit('No direct script access allowed'); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | |
@@ -19,28 +19,28 @@ discard block |
||
| 19 | 19 | { |
| 20 | 20 | |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * ServiceExistsException constructor |
|
| 24 | - * |
|
| 25 | - * @param string $service_name the name of the requested service |
|
| 26 | - * @param string $message |
|
| 27 | - * @param int $code |
|
| 28 | - * @param \Exception $previous |
|
| 29 | - */ |
|
| 30 | - public function __construct( |
|
| 31 | - $service_name, |
|
| 32 | - $message = '', |
|
| 33 | - $code = 0, |
|
| 34 | - \Exception $previous = null |
|
| 35 | - ) { |
|
| 36 | - if (empty($message)) { |
|
| 37 | - $message = sprintf( |
|
| 38 | - __('The "%1$s" service already exists in the CoffeeShop and can not be added again.', 'event_espresso'), |
|
| 39 | - $service_name |
|
| 40 | - ); |
|
| 41 | - } |
|
| 42 | - parent::__construct($message, $code, $previous); |
|
| 43 | - } |
|
| 22 | + /** |
|
| 23 | + * ServiceExistsException constructor |
|
| 24 | + * |
|
| 25 | + * @param string $service_name the name of the requested service |
|
| 26 | + * @param string $message |
|
| 27 | + * @param int $code |
|
| 28 | + * @param \Exception $previous |
|
| 29 | + */ |
|
| 30 | + public function __construct( |
|
| 31 | + $service_name, |
|
| 32 | + $message = '', |
|
| 33 | + $code = 0, |
|
| 34 | + \Exception $previous = null |
|
| 35 | + ) { |
|
| 36 | + if (empty($message)) { |
|
| 37 | + $message = sprintf( |
|
| 38 | + __('The "%1$s" service already exists in the CoffeeShop and can not be added again.', 'event_espresso'), |
|
| 39 | + $service_name |
|
| 40 | + ); |
|
| 41 | + } |
|
| 42 | + parent::__construct($message, $code, $previous); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | |
| 46 | 46 | } |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | namespace EventEspresso\core\services\container\exceptions; |
| 3 | 3 | |
| 4 | 4 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 5 | - exit('No direct script access allowed'); |
|
| 5 | + exit('No direct script access allowed'); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | |
@@ -18,29 +18,29 @@ discard block |
||
| 18 | 18 | class InvalidServiceException extends \UnexpectedValueException |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * InvalidServiceException constructor. |
|
| 23 | - * |
|
| 24 | - * @param string $service_name the name of the requested service |
|
| 25 | - * @param string $actual classname of what we got |
|
| 26 | - * @param string $message |
|
| 27 | - * @param int $code |
|
| 28 | - * @param \Exception $previous |
|
| 29 | - */ |
|
| 30 | - public function __construct($service_name, $actual, $message = '', $code = 0, \Exception $previous = null) |
|
| 31 | - { |
|
| 32 | - if (empty($message)) { |
|
| 33 | - $message = sprintf( |
|
| 34 | - __( |
|
| 35 | - 'The "%1$s" service could not be retrieved from the CoffeeShop, but "%2$s" was received.', |
|
| 36 | - 'event_espresso' |
|
| 37 | - ), |
|
| 38 | - $service_name, |
|
| 39 | - print_r($actual, true) |
|
| 40 | - ); |
|
| 41 | - } |
|
| 42 | - parent::__construct($message, $code, $previous); |
|
| 43 | - } |
|
| 21 | + /** |
|
| 22 | + * InvalidServiceException constructor. |
|
| 23 | + * |
|
| 24 | + * @param string $service_name the name of the requested service |
|
| 25 | + * @param string $actual classname of what we got |
|
| 26 | + * @param string $message |
|
| 27 | + * @param int $code |
|
| 28 | + * @param \Exception $previous |
|
| 29 | + */ |
|
| 30 | + public function __construct($service_name, $actual, $message = '', $code = 0, \Exception $previous = null) |
|
| 31 | + { |
|
| 32 | + if (empty($message)) { |
|
| 33 | + $message = sprintf( |
|
| 34 | + __( |
|
| 35 | + 'The "%1$s" service could not be retrieved from the CoffeeShop, but "%2$s" was received.', |
|
| 36 | + 'event_espresso' |
|
| 37 | + ), |
|
| 38 | + $service_name, |
|
| 39 | + print_r($actual, true) |
|
| 40 | + ); |
|
| 41 | + } |
|
| 42 | + parent::__construct($message, $code, $previous); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | } |
| 46 | 46 | // End of file InvalidServiceException.php |