@@ -430,7 +430,7 @@ |
||
430 | 430 | * Gets the order description that should generlly be sent to gateways |
431 | 431 | * @deprecated since 4.9.31 instead use $this->_get_gateway_formatter()->formatOrderDescription($payment) |
432 | 432 | * @param EEI_Payment $payment |
433 | - * @return type |
|
433 | + * @return string |
|
434 | 434 | */ |
435 | 435 | protected function _format_order_description( EEI_Payment $payment ) { |
436 | 436 | return $this->_get_gateway_formatter()->formatOrderDescription($payment); |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | |
70 | 70 | |
71 | 71 | /** |
72 | - * @param $type |
|
72 | + * @param string $type |
|
73 | 73 | * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
74 | 74 | */ |
75 | 75 | public static function validateType($type) |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | * Examines the constructor to determine which method should be used for instantiation |
128 | 128 | * |
129 | 129 | * @param \ReflectionClass $reflector |
130 | - * @return mixed |
|
130 | + * @return string |
|
131 | 131 | * @throws InstantiationException |
132 | 132 | */ |
133 | 133 | protected function resolveInstantiationMethod(\ReflectionClass $reflector) |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | use EventEspresso\core\services\container\exceptions\InstantiationException; |
7 | 7 | |
8 | 8 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
9 | - exit('No direct script access allowed'); |
|
9 | + exit('No direct script access allowed'); |
|
10 | 10 | } |
11 | 11 | |
12 | 12 | |
@@ -23,162 +23,162 @@ discard block |
||
23 | 23 | abstract class CoffeeMaker implements CoffeeMakerInterface |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given) |
|
28 | - */ |
|
29 | - const BREW_NEW = 'new'; |
|
30 | - |
|
31 | - /** |
|
32 | - * Indicates that CoffeeMaker should always return a SHARED instance |
|
33 | - */ |
|
34 | - const BREW_SHARED = 'shared'; |
|
35 | - |
|
36 | - /** |
|
37 | - * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate |
|
38 | - */ |
|
39 | - const BREW_LOAD_ONLY = 'load_only'; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * @var CoffeePotInterface $coffee_pot |
|
44 | - */ |
|
45 | - private $coffee_pot; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var DependencyInjector $injector |
|
49 | - */ |
|
50 | - private $injector; |
|
51 | - |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - public static function getTypes() |
|
58 | - { |
|
59 | - return (array)apply_filters( |
|
60 | - 'FHEE__EventEspresso\core\services\container\CoffeeMaker__getTypes', |
|
61 | - array( |
|
62 | - CoffeeMaker::BREW_NEW, |
|
63 | - CoffeeMaker::BREW_SHARED, |
|
64 | - CoffeeMaker::BREW_LOAD_ONLY, |
|
65 | - ) |
|
66 | - ); |
|
67 | - } |
|
68 | - |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * @param $type |
|
73 | - * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
74 | - */ |
|
75 | - public static function validateType($type) |
|
76 | - { |
|
77 | - $types = CoffeeMaker::getTypes(); |
|
78 | - if ( ! in_array($type, $types, true)) { |
|
79 | - throw new InvalidIdentifierException( |
|
80 | - is_object($type) ? get_class($type) : gettype($type), |
|
81 | - __( |
|
82 | - 'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)', |
|
83 | - 'event_espresso' |
|
84 | - ) |
|
85 | - ); |
|
86 | - } |
|
87 | - return $type; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * CoffeeMaker constructor. |
|
94 | - * |
|
95 | - * @param CoffeePotInterface $coffee_pot |
|
96 | - * @param InjectorInterface $injector |
|
97 | - */ |
|
98 | - public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector) |
|
99 | - { |
|
100 | - $this->coffee_pot = $coffee_pot; |
|
101 | - $this->injector = $injector; |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * @return \EventEspresso\core\services\container\CoffeePotInterface |
|
108 | - */ |
|
109 | - protected function coffeePot() |
|
110 | - { |
|
111 | - return $this->coffee_pot; |
|
112 | - } |
|
113 | - |
|
114 | - |
|
115 | - |
|
116 | - /** |
|
117 | - * @return \EventEspresso\core\services\container\DependencyInjector |
|
118 | - */ |
|
119 | - protected function injector() |
|
120 | - { |
|
121 | - return $this->injector; |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * Examines the constructor to determine which method should be used for instantiation |
|
128 | - * |
|
129 | - * @param \ReflectionClass $reflector |
|
130 | - * @return mixed |
|
131 | - * @throws InstantiationException |
|
132 | - */ |
|
133 | - protected function resolveInstantiationMethod(\ReflectionClass $reflector) |
|
134 | - { |
|
135 | - if ($reflector->getConstructor() === null) { |
|
136 | - return 'NewInstance'; |
|
137 | - } else if ($reflector->isInstantiable()) { |
|
138 | - return 'NewInstanceArgs'; |
|
139 | - } else if (method_exists($reflector->getName(), 'instance')) { |
|
140 | - return 'instance'; |
|
141 | - } else if (method_exists($reflector->getName(), 'new_instance')) { |
|
142 | - return 'new_instance'; |
|
143 | - } else if (method_exists($reflector->getName(), 'new_instance_from_db')) { |
|
144 | - return 'new_instance_from_db'; |
|
145 | - } else { |
|
146 | - throw new InstantiationException($reflector->getName()); |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * Ensures files for classes that are not PSR-4 compatible are loaded |
|
154 | - * and then verifies that classes exist where applicable |
|
155 | - * |
|
156 | - * @param RecipeInterface $recipe |
|
157 | - * @return bool |
|
158 | - * @throws InvalidClassException |
|
159 | - */ |
|
160 | - protected function resolveClassAndFilepath(RecipeInterface $recipe) |
|
161 | - { |
|
162 | - $paths = $recipe->paths(); |
|
163 | - if ( ! empty($paths)) { |
|
164 | - foreach ($paths as $path) { |
|
165 | - if (strpos($path, '*') === false && is_readable($path)) { |
|
166 | - require_once($path); |
|
167 | - } |
|
168 | - } |
|
169 | - } |
|
170 | - // re: using "false" for class_exists() second param: |
|
171 | - // if a class name is not already known to PHP, then class_exists() will run through |
|
172 | - // all of the registered spl_autoload functions until it either finds the class, |
|
173 | - // or gets to the end of the registered spl_autoload functions. |
|
174 | - // When the second parameter is true, it will also attempt to load the class file, |
|
175 | - // but it will also trigger an error if the class can not be loaded. |
|
176 | - // We don't want that extra error in the mix, so we have set the second param to "false" |
|
177 | - if ($recipe->type() !== CoffeeMaker::BREW_LOAD_ONLY && ! class_exists($recipe->fqcn(), false)) { |
|
178 | - throw new InvalidClassException($recipe->identifier()); |
|
179 | - } |
|
180 | - return true; |
|
181 | - } |
|
26 | + /** |
|
27 | + * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given) |
|
28 | + */ |
|
29 | + const BREW_NEW = 'new'; |
|
30 | + |
|
31 | + /** |
|
32 | + * Indicates that CoffeeMaker should always return a SHARED instance |
|
33 | + */ |
|
34 | + const BREW_SHARED = 'shared'; |
|
35 | + |
|
36 | + /** |
|
37 | + * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate |
|
38 | + */ |
|
39 | + const BREW_LOAD_ONLY = 'load_only'; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * @var CoffeePotInterface $coffee_pot |
|
44 | + */ |
|
45 | + private $coffee_pot; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var DependencyInjector $injector |
|
49 | + */ |
|
50 | + private $injector; |
|
51 | + |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + public static function getTypes() |
|
58 | + { |
|
59 | + return (array)apply_filters( |
|
60 | + 'FHEE__EventEspresso\core\services\container\CoffeeMaker__getTypes', |
|
61 | + array( |
|
62 | + CoffeeMaker::BREW_NEW, |
|
63 | + CoffeeMaker::BREW_SHARED, |
|
64 | + CoffeeMaker::BREW_LOAD_ONLY, |
|
65 | + ) |
|
66 | + ); |
|
67 | + } |
|
68 | + |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * @param $type |
|
73 | + * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
74 | + */ |
|
75 | + public static function validateType($type) |
|
76 | + { |
|
77 | + $types = CoffeeMaker::getTypes(); |
|
78 | + if ( ! in_array($type, $types, true)) { |
|
79 | + throw new InvalidIdentifierException( |
|
80 | + is_object($type) ? get_class($type) : gettype($type), |
|
81 | + __( |
|
82 | + 'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)', |
|
83 | + 'event_espresso' |
|
84 | + ) |
|
85 | + ); |
|
86 | + } |
|
87 | + return $type; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * CoffeeMaker constructor. |
|
94 | + * |
|
95 | + * @param CoffeePotInterface $coffee_pot |
|
96 | + * @param InjectorInterface $injector |
|
97 | + */ |
|
98 | + public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector) |
|
99 | + { |
|
100 | + $this->coffee_pot = $coffee_pot; |
|
101 | + $this->injector = $injector; |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * @return \EventEspresso\core\services\container\CoffeePotInterface |
|
108 | + */ |
|
109 | + protected function coffeePot() |
|
110 | + { |
|
111 | + return $this->coffee_pot; |
|
112 | + } |
|
113 | + |
|
114 | + |
|
115 | + |
|
116 | + /** |
|
117 | + * @return \EventEspresso\core\services\container\DependencyInjector |
|
118 | + */ |
|
119 | + protected function injector() |
|
120 | + { |
|
121 | + return $this->injector; |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * Examines the constructor to determine which method should be used for instantiation |
|
128 | + * |
|
129 | + * @param \ReflectionClass $reflector |
|
130 | + * @return mixed |
|
131 | + * @throws InstantiationException |
|
132 | + */ |
|
133 | + protected function resolveInstantiationMethod(\ReflectionClass $reflector) |
|
134 | + { |
|
135 | + if ($reflector->getConstructor() === null) { |
|
136 | + return 'NewInstance'; |
|
137 | + } else if ($reflector->isInstantiable()) { |
|
138 | + return 'NewInstanceArgs'; |
|
139 | + } else if (method_exists($reflector->getName(), 'instance')) { |
|
140 | + return 'instance'; |
|
141 | + } else if (method_exists($reflector->getName(), 'new_instance')) { |
|
142 | + return 'new_instance'; |
|
143 | + } else if (method_exists($reflector->getName(), 'new_instance_from_db')) { |
|
144 | + return 'new_instance_from_db'; |
|
145 | + } else { |
|
146 | + throw new InstantiationException($reflector->getName()); |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * Ensures files for classes that are not PSR-4 compatible are loaded |
|
154 | + * and then verifies that classes exist where applicable |
|
155 | + * |
|
156 | + * @param RecipeInterface $recipe |
|
157 | + * @return bool |
|
158 | + * @throws InvalidClassException |
|
159 | + */ |
|
160 | + protected function resolveClassAndFilepath(RecipeInterface $recipe) |
|
161 | + { |
|
162 | + $paths = $recipe->paths(); |
|
163 | + if ( ! empty($paths)) { |
|
164 | + foreach ($paths as $path) { |
|
165 | + if (strpos($path, '*') === false && is_readable($path)) { |
|
166 | + require_once($path); |
|
167 | + } |
|
168 | + } |
|
169 | + } |
|
170 | + // re: using "false" for class_exists() second param: |
|
171 | + // if a class name is not already known to PHP, then class_exists() will run through |
|
172 | + // all of the registered spl_autoload functions until it either finds the class, |
|
173 | + // or gets to the end of the registered spl_autoload functions. |
|
174 | + // When the second parameter is true, it will also attempt to load the class file, |
|
175 | + // but it will also trigger an error if the class can not be loaded. |
|
176 | + // We don't want that extra error in the mix, so we have set the second param to "false" |
|
177 | + if ($recipe->type() !== CoffeeMaker::BREW_LOAD_ONLY && ! class_exists($recipe->fqcn(), false)) { |
|
178 | + throw new InvalidClassException($recipe->identifier()); |
|
179 | + } |
|
180 | + return true; |
|
181 | + } |
|
182 | 182 | |
183 | 183 | |
184 | 184 |
@@ -361,7 +361,7 @@ discard block |
||
361 | 361 | * Adds instructions on how to brew objects |
362 | 362 | * |
363 | 363 | * @param RecipeInterface $recipe |
364 | - * @return mixed |
|
364 | + * @return boolean |
|
365 | 365 | * @throws InvalidIdentifierException |
366 | 366 | */ |
367 | 367 | public function addRecipe(RecipeInterface $recipe) |
@@ -461,7 +461,7 @@ discard block |
||
461 | 461 | /** |
462 | 462 | * Adds a service to one of the internal collections |
463 | 463 | * |
464 | - * @param $identifier |
|
464 | + * @param string $identifier |
|
465 | 465 | * @param array $arguments |
466 | 466 | * @param string $type |
467 | 467 | * @return mixed |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | // array for storing class aliases |
83 | 83 | $this->filters = array(); |
84 | 84 | // create collection for storing shared services |
85 | - $this->carafe = new LooseCollection( '' ); |
|
85 | + $this->carafe = new LooseCollection(''); |
|
86 | 86 | // create collection for storing recipes that tell us how to build services and entities |
87 | 87 | $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
88 | 88 | // create collection for storing closures for constructing new entities |
@@ -161,17 +161,17 @@ discard block |
||
161 | 161 | // is a shared service being requested and already exists in the carafe? |
162 | 162 | $brewed = $this->getShared($identifier, $type); |
163 | 163 | // then return whatever was found |
164 | - if($brewed !== false) { |
|
164 | + if ($brewed !== false) { |
|
165 | 165 | return $brewed; |
166 | 166 | } |
167 | 167 | // if the reservoir doesn't have a closure already for the requested identifier, |
168 | 168 | // then neither a shared service nor a closure for making entities has been built yet |
169 | - if (! $this->reservoir->has($identifier)) { |
|
169 | + if ( ! $this->reservoir->has($identifier)) { |
|
170 | 170 | // so let's brew something up and add it to the proper collection |
171 | 171 | $brewed = $this->makeCoffee($identifier, $arguments, $type); |
172 | 172 | } |
173 | 173 | // did the requested class only require loading, and if so, was that successful? |
174 | - if($this->brewedLoadOnly($brewed, $identifier, $type) === true) { |
|
174 | + if ($this->brewedLoadOnly($brewed, $identifier, $type) === true) { |
|
175 | 175 | return true; |
176 | 176 | } |
177 | 177 | // was the brewed item a callable factory function ? |
@@ -451,7 +451,7 @@ discard block |
||
451 | 451 | return; |
452 | 452 | } |
453 | 453 | $identifier = $this->processIdentifier($identifier); |
454 | - foreach ((array)$aliases as $alias) { |
|
454 | + foreach ((array) $aliases as $alias) { |
|
455 | 455 | $this->filters[$this->processIdentifier($alias)] = $identifier; |
456 | 456 | } |
457 | 457 | } |
@@ -33,594 +33,594 @@ |
||
33 | 33 | { |
34 | 34 | |
35 | 35 | |
36 | - /** |
|
37 | - * This was the best coffee related name I could think of to represent class name "aliases" |
|
38 | - * So classes can be found via an alias identifier, |
|
39 | - * that is revealed when it is run through... the filters... eh? get it? |
|
40 | - * |
|
41 | - * @var array $filters |
|
42 | - */ |
|
43 | - private $filters; |
|
44 | - |
|
45 | - /** |
|
46 | - * These are the classes that will actually build the objects (to order of course) |
|
47 | - * |
|
48 | - * @var array $coffee_makers |
|
49 | - */ |
|
50 | - private $coffee_makers; |
|
51 | - |
|
52 | - /** |
|
53 | - * where the instantiated "singleton" objects are stored |
|
54 | - * |
|
55 | - * @var CollectionInterface $carafe |
|
56 | - */ |
|
57 | - private $carafe; |
|
58 | - |
|
59 | - /** |
|
60 | - * collection of Recipes that instruct us how to brew objects |
|
61 | - * |
|
62 | - * @var CollectionInterface $recipes |
|
63 | - */ |
|
64 | - private $recipes; |
|
65 | - |
|
66 | - /** |
|
67 | - * collection of closures for brewing objects |
|
68 | - * |
|
69 | - * @var CollectionInterface $reservoir |
|
70 | - */ |
|
71 | - private $reservoir; |
|
72 | - |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * CoffeeShop constructor |
|
77 | - * |
|
78 | - * @throws InvalidInterfaceException |
|
79 | - */ |
|
80 | - public function __construct() |
|
81 | - { |
|
82 | - // array for storing class aliases |
|
83 | - $this->filters = array(); |
|
84 | - // create collection for storing shared services |
|
85 | - $this->carafe = new LooseCollection( '' ); |
|
86 | - // create collection for storing recipes that tell us how to build services and entities |
|
87 | - $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
|
88 | - // create collection for storing closures for constructing new entities |
|
89 | - $this->reservoir = new Collection('Closure'); |
|
90 | - // create collection for storing the generators that build our services and entity closures |
|
91 | - $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface'); |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * Returns true if the container can return an entry for the given identifier. |
|
98 | - * Returns false otherwise. |
|
99 | - * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
|
100 | - * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
|
101 | - * |
|
102 | - * @param string $identifier Identifier of the entry to look for. |
|
103 | - * Typically a Fully Qualified Class Name |
|
104 | - * @return boolean |
|
105 | - * @throws InvalidIdentifierException |
|
106 | - */ |
|
107 | - public function has($identifier) |
|
108 | - { |
|
109 | - $identifier = $this->filterIdentifier($identifier); |
|
110 | - return $this->carafe->has($identifier); |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - |
|
115 | - /** |
|
116 | - * finds a previously brewed (SHARED) service and returns it |
|
117 | - * |
|
118 | - * @param string $identifier Identifier for the entity class to be constructed. |
|
119 | - * Typically a Fully Qualified Class Name |
|
120 | - * @return mixed |
|
121 | - * @throws InvalidIdentifierException |
|
122 | - * @throws ServiceNotFoundException No service was found for this identifier. |
|
123 | - */ |
|
124 | - public function get($identifier) |
|
125 | - { |
|
126 | - $identifier = $this->filterIdentifier($identifier); |
|
127 | - if ($this->carafe->has($identifier)) { |
|
128 | - return $this->carafe->get($identifier); |
|
129 | - } |
|
130 | - throw new ServiceNotFoundException($identifier); |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * returns an instance of the requested entity type using the supplied arguments. |
|
137 | - * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
|
138 | - * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
|
139 | - * If the request is for a new entity and a closure exists in the reservoir for creating it, |
|
140 | - * then a new entity will be instantiated from the closure and returned. |
|
141 | - * If a closure does not exist, then one will be built and added to the reservoir |
|
142 | - * before instantiating the requested entity. |
|
143 | - * |
|
144 | - * @param string $identifier Identifier for the entity class to be constructed. |
|
145 | - * Typically a Fully Qualified Class Name |
|
146 | - * @param array $arguments an array of arguments to be passed to the entity constructor |
|
147 | - * @param string $type |
|
148 | - * @return mixed |
|
149 | - * @throws OutOfBoundsException |
|
150 | - * @throws InstantiationException |
|
151 | - * @throws InvalidDataTypeException |
|
152 | - * @throws InvalidClassException |
|
153 | - * @throws InvalidIdentifierException |
|
154 | - * @throws ServiceExistsException |
|
155 | - * @throws ServiceNotFoundException No service was found for this identifier. |
|
156 | - */ |
|
157 | - public function brew($identifier, $arguments = array(), $type = '') |
|
158 | - { |
|
159 | - // resolve any class aliases that may exist |
|
160 | - $identifier = $this->filterIdentifier($identifier); |
|
161 | - // is a shared service being requested and already exists in the carafe? |
|
162 | - $brewed = $this->getShared($identifier, $type); |
|
163 | - // then return whatever was found |
|
164 | - if($brewed !== false) { |
|
165 | - return $brewed; |
|
166 | - } |
|
167 | - // if the reservoir doesn't have a closure already for the requested identifier, |
|
168 | - // then neither a shared service nor a closure for making entities has been built yet |
|
169 | - if (! $this->reservoir->has($identifier)) { |
|
170 | - // so let's brew something up and add it to the proper collection |
|
171 | - $brewed = $this->makeCoffee($identifier, $arguments, $type); |
|
172 | - } |
|
173 | - // did the requested class only require loading, and if so, was that successful? |
|
174 | - if($this->brewedLoadOnly($brewed, $identifier, $type) === true) { |
|
175 | - return true; |
|
176 | - } |
|
177 | - // was the brewed item a callable factory function ? |
|
178 | - if (is_callable($brewed)) { |
|
179 | - // then instantiate a new entity from the cached closure |
|
180 | - return $brewed($arguments); |
|
181 | - } |
|
182 | - if ($brewed) { |
|
183 | - // requested object was a shared entity, so attempt to get it from the carafe again |
|
184 | - // because if it wasn't there before, then it should have just been brewed and added, |
|
185 | - // but if it still isn't there, then this time the thrown ServiceNotFoundException will not be caught |
|
186 | - return $this->get($identifier); |
|
187 | - } |
|
188 | - // if identifier is for a non-shared entity, |
|
189 | - // then either a cached closure already existed, or was just brewed |
|
190 | - return $this->brewedClosure($identifier, $arguments); |
|
191 | - } |
|
192 | - |
|
193 | - |
|
194 | - |
|
195 | - /** |
|
196 | - * @param string $identifier |
|
197 | - * @param string $type |
|
198 | - * @return bool|mixed |
|
199 | - * @throws InvalidIdentifierException |
|
200 | - */ |
|
201 | - protected function getShared($identifier, $type) |
|
202 | - { |
|
203 | - try { |
|
204 | - if (empty($type) || $type === CoffeeMaker::BREW_SHARED) { |
|
205 | - // if a shared service was requested and an instance is in the carafe, then return it |
|
206 | - return $this->get($identifier); |
|
207 | - } |
|
208 | - } catch (ServiceNotFoundException $e) { |
|
209 | - // if not then we'll just catch the ServiceNotFoundException but not do anything just yet, |
|
210 | - // and instead, attempt to build whatever was requested |
|
211 | - } |
|
212 | - return false; |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - |
|
217 | - /** |
|
218 | - * @param mixed $brewed |
|
219 | - * @param string $identifier |
|
220 | - * @param string $type |
|
221 | - * @return bool |
|
222 | - * @throws InvalidClassException |
|
223 | - * @throws InvalidDataTypeException |
|
224 | - * @throws InvalidIdentifierException |
|
225 | - * @throws OutOfBoundsException |
|
226 | - * @throws ServiceExistsException |
|
227 | - * @throws ServiceNotFoundException |
|
228 | - */ |
|
229 | - protected function brewedLoadOnly($brewed, $identifier, $type) |
|
230 | - { |
|
231 | - if ($type === CoffeeMaker::BREW_LOAD_ONLY) { |
|
232 | - if ($brewed !== true) { |
|
233 | - throw new ServiceNotFoundException( |
|
234 | - sprintf( |
|
235 | - esc_html__( |
|
236 | - 'The "%1$s" class could not be loaded.', |
|
237 | - 'event_espresso' |
|
238 | - ), |
|
239 | - $identifier |
|
240 | - ) |
|
241 | - ); |
|
242 | - } |
|
243 | - return true; |
|
244 | - } |
|
245 | - return false; |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * @param string $identifier |
|
252 | - * @param array $arguments |
|
253 | - * @return mixed |
|
254 | - * @throws InstantiationException |
|
255 | - */ |
|
256 | - protected function brewedClosure($identifier, array $arguments) |
|
257 | - { |
|
258 | - $closure = $this->reservoir->get($identifier); |
|
259 | - if (empty($closure)) { |
|
260 | - throw new InstantiationException( |
|
261 | - sprintf( |
|
262 | - esc_html__( |
|
263 | - 'Could not brew an instance of "%1$s".', |
|
264 | - 'event_espresso' |
|
265 | - ), |
|
266 | - $identifier |
|
267 | - ) |
|
268 | - ); |
|
269 | - } |
|
270 | - return $closure($arguments); |
|
271 | - } |
|
272 | - |
|
273 | - |
|
274 | - |
|
275 | - /** |
|
276 | - * @param CoffeeMakerInterface $coffee_maker |
|
277 | - * @param string $type |
|
278 | - * @return bool |
|
279 | - * @throws InvalidIdentifierException |
|
280 | - * @throws InvalidEntityException |
|
281 | - */ |
|
282 | - public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
|
283 | - { |
|
284 | - $type = CoffeeMaker::validateType($type); |
|
285 | - return $this->coffee_makers->add($coffee_maker, $type); |
|
286 | - } |
|
287 | - |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * @param string $identifier |
|
292 | - * @param callable $closure |
|
293 | - * @return callable|null |
|
294 | - * @throws InvalidIdentifierException |
|
295 | - * @throws InvalidDataTypeException |
|
296 | - */ |
|
297 | - public function addClosure($identifier, $closure) |
|
298 | - { |
|
299 | - if ( ! is_callable($closure)) { |
|
300 | - throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
|
301 | - } |
|
302 | - $identifier = $this->processIdentifier($identifier); |
|
303 | - if ($this->reservoir->add($closure, $identifier)) { |
|
304 | - return $closure; |
|
305 | - } |
|
306 | - return null; |
|
307 | - } |
|
308 | - |
|
309 | - |
|
310 | - |
|
311 | - /** |
|
312 | - * @param string $identifier |
|
313 | - * @return boolean |
|
314 | - * @throws InvalidIdentifierException |
|
315 | - */ |
|
316 | - public function removeClosure($identifier) |
|
317 | - { |
|
318 | - $identifier = $this->processIdentifier($identifier); |
|
319 | - if ($this->reservoir->has($identifier)) { |
|
320 | - return $this->reservoir->remove($this->reservoir->get($identifier)); |
|
321 | - } |
|
322 | - return false; |
|
323 | - } |
|
324 | - |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * @param string $identifier Identifier for the entity class that the service applies to |
|
329 | - * Typically a Fully Qualified Class Name |
|
330 | - * @param mixed $service |
|
331 | - * @return bool |
|
332 | - * @throws \EventEspresso\core\services\container\exceptions\InvalidServiceException |
|
333 | - * @throws InvalidIdentifierException |
|
334 | - */ |
|
335 | - public function addService($identifier, $service) |
|
336 | - { |
|
337 | - $identifier = $this->processIdentifier($identifier); |
|
338 | - $service = $this->validateService($identifier, $service); |
|
339 | - return $this->carafe->add($service, $identifier); |
|
340 | - } |
|
341 | - |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * @param string $identifier |
|
346 | - * @return boolean |
|
347 | - * @throws InvalidIdentifierException |
|
348 | - */ |
|
349 | - public function removeService($identifier) |
|
350 | - { |
|
351 | - $identifier = $this->processIdentifier($identifier); |
|
352 | - if ($this->carafe->has($identifier)) { |
|
353 | - return $this->carafe->remove($this->carafe->get($identifier)); |
|
354 | - } |
|
355 | - return false; |
|
356 | - } |
|
357 | - |
|
358 | - |
|
359 | - |
|
360 | - /** |
|
361 | - * Adds instructions on how to brew objects |
|
362 | - * |
|
363 | - * @param RecipeInterface $recipe |
|
364 | - * @return mixed |
|
365 | - * @throws InvalidIdentifierException |
|
366 | - */ |
|
367 | - public function addRecipe(RecipeInterface $recipe) |
|
368 | - { |
|
369 | - $this->addAliases($recipe->identifier(), $recipe->filters()); |
|
370 | - $identifier = $this->processIdentifier($recipe->identifier()); |
|
371 | - return $this->recipes->add($recipe, $identifier); |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - |
|
376 | - /** |
|
377 | - * @param string $identifier The Recipe's identifier |
|
378 | - * @return boolean |
|
379 | - * @throws InvalidIdentifierException |
|
380 | - */ |
|
381 | - public function removeRecipe($identifier) |
|
382 | - { |
|
383 | - $identifier = $this->processIdentifier($identifier); |
|
384 | - if ($this->recipes->has($identifier)) { |
|
385 | - return $this->recipes->remove($this->recipes->get($identifier)); |
|
386 | - } |
|
387 | - return false; |
|
388 | - } |
|
389 | - |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * Get instructions on how to brew objects |
|
394 | - * |
|
395 | - * @param string $identifier Identifier for the entity class that the recipe applies to |
|
396 | - * Typically a Fully Qualified Class Name |
|
397 | - * @param string $type |
|
398 | - * @return RecipeInterface |
|
399 | - * @throws OutOfBoundsException |
|
400 | - * @throws InvalidIdentifierException |
|
401 | - */ |
|
402 | - public function getRecipe($identifier, $type = '') |
|
403 | - { |
|
404 | - $identifier = $this->processIdentifier($identifier); |
|
405 | - if ($this->recipes->has($identifier)) { |
|
406 | - return $this->recipes->get($identifier); |
|
407 | - } |
|
408 | - $default_recipes = $this->getDefaultRecipes(); |
|
409 | - $matches = array(); |
|
410 | - foreach ($default_recipes as $wildcard => $default_recipe) { |
|
411 | - // is the wildcard recipe prefix in the identifier ? |
|
412 | - if (strpos($identifier, $wildcard) !== false) { |
|
413 | - // track matches and use the number of wildcard characters matched for the key |
|
414 | - $matches[strlen($wildcard)] = $default_recipe; |
|
415 | - } |
|
416 | - } |
|
417 | - if (count($matches) > 0) { |
|
418 | - // sort our recipes by the number of wildcard characters matched |
|
419 | - ksort($matches); |
|
420 | - // then grab the last recipe form the list, since it had the most matching characters |
|
421 | - $match = array_pop($matches); |
|
422 | - // since we are using a default recipe, we need to set it's identifier and fqcn |
|
423 | - return $this->copyDefaultRecipe($match, $identifier, $type); |
|
424 | - } |
|
425 | - if ($this->recipes->has(Recipe::DEFAULT_ID)) { |
|
426 | - // since we are using a default recipe, we need to set it's identifier and fqcn |
|
427 | - return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type); |
|
428 | - } |
|
429 | - throw new OutOfBoundsException( |
|
430 | - sprintf( |
|
431 | - __('Could not brew coffee because no recipes were found for class "%1$s".', 'event_espresso'), |
|
432 | - $identifier |
|
433 | - ) |
|
434 | - ); |
|
435 | - } |
|
436 | - |
|
437 | - |
|
438 | - |
|
439 | - /** |
|
440 | - * adds class name aliases to list of filters |
|
441 | - * |
|
442 | - * @param string $identifier Identifier for the entity class that the alias applies to |
|
443 | - * Typically a Fully Qualified Class Name |
|
444 | - * @param array|string $aliases |
|
445 | - * @return void |
|
446 | - * @throws InvalidIdentifierException |
|
447 | - */ |
|
448 | - public function addAliases($identifier, $aliases) |
|
449 | - { |
|
450 | - if (empty($aliases)) { |
|
451 | - return; |
|
452 | - } |
|
453 | - $identifier = $this->processIdentifier($identifier); |
|
454 | - foreach ((array)$aliases as $alias) { |
|
455 | - $this->filters[$this->processIdentifier($alias)] = $identifier; |
|
456 | - } |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - |
|
461 | - /** |
|
462 | - * Adds a service to one of the internal collections |
|
463 | - * |
|
464 | - * @param $identifier |
|
465 | - * @param array $arguments |
|
466 | - * @param string $type |
|
467 | - * @return mixed |
|
468 | - * @throws InvalidDataTypeException |
|
469 | - * @throws InvalidClassException |
|
470 | - * @throws OutOfBoundsException |
|
471 | - * @throws InvalidIdentifierException |
|
472 | - * @throws ServiceExistsException |
|
473 | - */ |
|
474 | - private function makeCoffee($identifier, $arguments = array(), $type = '') |
|
475 | - { |
|
476 | - if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) { |
|
477 | - throw new ServiceExistsException($identifier); |
|
478 | - } |
|
479 | - $identifier = $this->filterIdentifier($identifier); |
|
480 | - $recipe = $this->getRecipe($identifier, $type); |
|
481 | - $type = ! empty($type) ? $type : $recipe->type(); |
|
482 | - $coffee_maker = $this->getCoffeeMaker($type); |
|
483 | - return $coffee_maker->brew($recipe, $arguments); |
|
484 | - } |
|
485 | - |
|
486 | - |
|
487 | - |
|
488 | - /** |
|
489 | - * filters alias identifiers to find the real class name |
|
490 | - * |
|
491 | - * @param string $identifier Identifier for the entity class that the filter applies to |
|
492 | - * Typically a Fully Qualified Class Name |
|
493 | - * @return string |
|
494 | - * @throws InvalidIdentifierException |
|
495 | - */ |
|
496 | - private function filterIdentifier($identifier) |
|
497 | - { |
|
498 | - $identifier = $this->processIdentifier($identifier); |
|
499 | - return isset($this->filters[$identifier]) && ! empty($this->filters[$identifier]) |
|
500 | - ? $this->filters[$identifier] |
|
501 | - : $identifier; |
|
502 | - } |
|
503 | - |
|
504 | - |
|
505 | - |
|
506 | - /** |
|
507 | - * verifies and standardizes identifiers |
|
508 | - * |
|
509 | - * @param string $identifier Identifier for the entity class |
|
510 | - * Typically a Fully Qualified Class Name |
|
511 | - * @return string |
|
512 | - * @throws InvalidIdentifierException |
|
513 | - */ |
|
514 | - private function processIdentifier($identifier) |
|
515 | - { |
|
516 | - if ( ! is_string($identifier)) { |
|
517 | - throw new InvalidIdentifierException( |
|
518 | - is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
519 | - '\Fully\Qualified\ClassName' |
|
520 | - ); |
|
521 | - } |
|
522 | - return ltrim($identifier, '\\'); |
|
523 | - } |
|
524 | - |
|
525 | - |
|
526 | - |
|
527 | - /** |
|
528 | - * @param string $type |
|
529 | - * @return CoffeeMakerInterface |
|
530 | - * @throws OutOfBoundsException |
|
531 | - * @throws InvalidDataTypeException |
|
532 | - * @throws InvalidClassException |
|
533 | - */ |
|
534 | - private function getCoffeeMaker($type) |
|
535 | - { |
|
536 | - if ( ! $this->coffee_makers->has($type)) { |
|
537 | - throw new OutOfBoundsException( |
|
538 | - __('The requested coffee maker is either missing or invalid.', 'event_espresso') |
|
539 | - ); |
|
540 | - } |
|
541 | - return $this->coffee_makers->get($type); |
|
542 | - } |
|
543 | - |
|
544 | - |
|
545 | - |
|
546 | - /** |
|
547 | - * Retrieves all recipes that use a wildcard "*" in their identifier |
|
548 | - * This allows recipes to be set up for handling |
|
549 | - * legacy classes that do not support PSR-4 autoloading. |
|
550 | - * for example: |
|
551 | - * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
|
552 | - * |
|
553 | - * @return array |
|
554 | - */ |
|
555 | - private function getDefaultRecipes() |
|
556 | - { |
|
557 | - $default_recipes = array(); |
|
558 | - $this->recipes->rewind(); |
|
559 | - while ($this->recipes->valid()) { |
|
560 | - $identifier = $this->recipes->getInfo(); |
|
561 | - // does this recipe use a wildcard ? (but is NOT the global default) |
|
562 | - if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
|
563 | - // strip the wildcard and use identifier as key |
|
564 | - $default_recipes[str_replace('*', '', $identifier)] = $this->recipes->current(); |
|
565 | - } |
|
566 | - $this->recipes->next(); |
|
567 | - } |
|
568 | - return $default_recipes; |
|
569 | - } |
|
570 | - |
|
571 | - |
|
572 | - |
|
573 | - /** |
|
574 | - * clones a default recipe and then copies details |
|
575 | - * from the incoming request to it so that it can be used |
|
576 | - * |
|
577 | - * @param RecipeInterface $default_recipe |
|
578 | - * @param string $identifier |
|
579 | - * @param string $type |
|
580 | - * @return RecipeInterface |
|
581 | - */ |
|
582 | - private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
|
583 | - { |
|
584 | - $recipe = clone $default_recipe; |
|
585 | - if ( ! empty($type)) { |
|
586 | - $recipe->setType($type); |
|
587 | - } |
|
588 | - // is this the base default recipe ? |
|
589 | - if ($default_recipe->identifier() === Recipe::DEFAULT_ID) { |
|
590 | - $recipe->setIdentifier($identifier); |
|
591 | - $recipe->setFqcn($identifier); |
|
592 | - return $recipe; |
|
593 | - } |
|
594 | - $recipe->setIdentifier($identifier); |
|
595 | - foreach ($default_recipe->paths() as $path) { |
|
596 | - $path = str_replace('*', $identifier, $path); |
|
597 | - if (is_readable($path)) { |
|
598 | - $recipe->setPaths($path); |
|
599 | - } |
|
600 | - } |
|
601 | - $recipe->setFqcn($identifier); |
|
602 | - return $recipe; |
|
603 | - } |
|
604 | - |
|
605 | - |
|
606 | - |
|
607 | - /** |
|
608 | - * @param string $identifier Identifier for the entity class that the service applies to |
|
609 | - * Typically a Fully Qualified Class Name |
|
610 | - * @param mixed $service |
|
611 | - * @return mixed |
|
612 | - * @throws InvalidServiceException |
|
613 | - */ |
|
614 | - private function validateService($identifier, $service) |
|
615 | - { |
|
616 | - if ( ! is_object($service)) { |
|
617 | - throw new InvalidServiceException( |
|
618 | - $identifier, |
|
619 | - $service |
|
620 | - ); |
|
621 | - } |
|
622 | - return $service; |
|
623 | - } |
|
36 | + /** |
|
37 | + * This was the best coffee related name I could think of to represent class name "aliases" |
|
38 | + * So classes can be found via an alias identifier, |
|
39 | + * that is revealed when it is run through... the filters... eh? get it? |
|
40 | + * |
|
41 | + * @var array $filters |
|
42 | + */ |
|
43 | + private $filters; |
|
44 | + |
|
45 | + /** |
|
46 | + * These are the classes that will actually build the objects (to order of course) |
|
47 | + * |
|
48 | + * @var array $coffee_makers |
|
49 | + */ |
|
50 | + private $coffee_makers; |
|
51 | + |
|
52 | + /** |
|
53 | + * where the instantiated "singleton" objects are stored |
|
54 | + * |
|
55 | + * @var CollectionInterface $carafe |
|
56 | + */ |
|
57 | + private $carafe; |
|
58 | + |
|
59 | + /** |
|
60 | + * collection of Recipes that instruct us how to brew objects |
|
61 | + * |
|
62 | + * @var CollectionInterface $recipes |
|
63 | + */ |
|
64 | + private $recipes; |
|
65 | + |
|
66 | + /** |
|
67 | + * collection of closures for brewing objects |
|
68 | + * |
|
69 | + * @var CollectionInterface $reservoir |
|
70 | + */ |
|
71 | + private $reservoir; |
|
72 | + |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * CoffeeShop constructor |
|
77 | + * |
|
78 | + * @throws InvalidInterfaceException |
|
79 | + */ |
|
80 | + public function __construct() |
|
81 | + { |
|
82 | + // array for storing class aliases |
|
83 | + $this->filters = array(); |
|
84 | + // create collection for storing shared services |
|
85 | + $this->carafe = new LooseCollection( '' ); |
|
86 | + // create collection for storing recipes that tell us how to build services and entities |
|
87 | + $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
|
88 | + // create collection for storing closures for constructing new entities |
|
89 | + $this->reservoir = new Collection('Closure'); |
|
90 | + // create collection for storing the generators that build our services and entity closures |
|
91 | + $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface'); |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * Returns true if the container can return an entry for the given identifier. |
|
98 | + * Returns false otherwise. |
|
99 | + * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
|
100 | + * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
|
101 | + * |
|
102 | + * @param string $identifier Identifier of the entry to look for. |
|
103 | + * Typically a Fully Qualified Class Name |
|
104 | + * @return boolean |
|
105 | + * @throws InvalidIdentifierException |
|
106 | + */ |
|
107 | + public function has($identifier) |
|
108 | + { |
|
109 | + $identifier = $this->filterIdentifier($identifier); |
|
110 | + return $this->carafe->has($identifier); |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + |
|
115 | + /** |
|
116 | + * finds a previously brewed (SHARED) service and returns it |
|
117 | + * |
|
118 | + * @param string $identifier Identifier for the entity class to be constructed. |
|
119 | + * Typically a Fully Qualified Class Name |
|
120 | + * @return mixed |
|
121 | + * @throws InvalidIdentifierException |
|
122 | + * @throws ServiceNotFoundException No service was found for this identifier. |
|
123 | + */ |
|
124 | + public function get($identifier) |
|
125 | + { |
|
126 | + $identifier = $this->filterIdentifier($identifier); |
|
127 | + if ($this->carafe->has($identifier)) { |
|
128 | + return $this->carafe->get($identifier); |
|
129 | + } |
|
130 | + throw new ServiceNotFoundException($identifier); |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * returns an instance of the requested entity type using the supplied arguments. |
|
137 | + * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
|
138 | + * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
|
139 | + * If the request is for a new entity and a closure exists in the reservoir for creating it, |
|
140 | + * then a new entity will be instantiated from the closure and returned. |
|
141 | + * If a closure does not exist, then one will be built and added to the reservoir |
|
142 | + * before instantiating the requested entity. |
|
143 | + * |
|
144 | + * @param string $identifier Identifier for the entity class to be constructed. |
|
145 | + * Typically a Fully Qualified Class Name |
|
146 | + * @param array $arguments an array of arguments to be passed to the entity constructor |
|
147 | + * @param string $type |
|
148 | + * @return mixed |
|
149 | + * @throws OutOfBoundsException |
|
150 | + * @throws InstantiationException |
|
151 | + * @throws InvalidDataTypeException |
|
152 | + * @throws InvalidClassException |
|
153 | + * @throws InvalidIdentifierException |
|
154 | + * @throws ServiceExistsException |
|
155 | + * @throws ServiceNotFoundException No service was found for this identifier. |
|
156 | + */ |
|
157 | + public function brew($identifier, $arguments = array(), $type = '') |
|
158 | + { |
|
159 | + // resolve any class aliases that may exist |
|
160 | + $identifier = $this->filterIdentifier($identifier); |
|
161 | + // is a shared service being requested and already exists in the carafe? |
|
162 | + $brewed = $this->getShared($identifier, $type); |
|
163 | + // then return whatever was found |
|
164 | + if($brewed !== false) { |
|
165 | + return $brewed; |
|
166 | + } |
|
167 | + // if the reservoir doesn't have a closure already for the requested identifier, |
|
168 | + // then neither a shared service nor a closure for making entities has been built yet |
|
169 | + if (! $this->reservoir->has($identifier)) { |
|
170 | + // so let's brew something up and add it to the proper collection |
|
171 | + $brewed = $this->makeCoffee($identifier, $arguments, $type); |
|
172 | + } |
|
173 | + // did the requested class only require loading, and if so, was that successful? |
|
174 | + if($this->brewedLoadOnly($brewed, $identifier, $type) === true) { |
|
175 | + return true; |
|
176 | + } |
|
177 | + // was the brewed item a callable factory function ? |
|
178 | + if (is_callable($brewed)) { |
|
179 | + // then instantiate a new entity from the cached closure |
|
180 | + return $brewed($arguments); |
|
181 | + } |
|
182 | + if ($brewed) { |
|
183 | + // requested object was a shared entity, so attempt to get it from the carafe again |
|
184 | + // because if it wasn't there before, then it should have just been brewed and added, |
|
185 | + // but if it still isn't there, then this time the thrown ServiceNotFoundException will not be caught |
|
186 | + return $this->get($identifier); |
|
187 | + } |
|
188 | + // if identifier is for a non-shared entity, |
|
189 | + // then either a cached closure already existed, or was just brewed |
|
190 | + return $this->brewedClosure($identifier, $arguments); |
|
191 | + } |
|
192 | + |
|
193 | + |
|
194 | + |
|
195 | + /** |
|
196 | + * @param string $identifier |
|
197 | + * @param string $type |
|
198 | + * @return bool|mixed |
|
199 | + * @throws InvalidIdentifierException |
|
200 | + */ |
|
201 | + protected function getShared($identifier, $type) |
|
202 | + { |
|
203 | + try { |
|
204 | + if (empty($type) || $type === CoffeeMaker::BREW_SHARED) { |
|
205 | + // if a shared service was requested and an instance is in the carafe, then return it |
|
206 | + return $this->get($identifier); |
|
207 | + } |
|
208 | + } catch (ServiceNotFoundException $e) { |
|
209 | + // if not then we'll just catch the ServiceNotFoundException but not do anything just yet, |
|
210 | + // and instead, attempt to build whatever was requested |
|
211 | + } |
|
212 | + return false; |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + |
|
217 | + /** |
|
218 | + * @param mixed $brewed |
|
219 | + * @param string $identifier |
|
220 | + * @param string $type |
|
221 | + * @return bool |
|
222 | + * @throws InvalidClassException |
|
223 | + * @throws InvalidDataTypeException |
|
224 | + * @throws InvalidIdentifierException |
|
225 | + * @throws OutOfBoundsException |
|
226 | + * @throws ServiceExistsException |
|
227 | + * @throws ServiceNotFoundException |
|
228 | + */ |
|
229 | + protected function brewedLoadOnly($brewed, $identifier, $type) |
|
230 | + { |
|
231 | + if ($type === CoffeeMaker::BREW_LOAD_ONLY) { |
|
232 | + if ($brewed !== true) { |
|
233 | + throw new ServiceNotFoundException( |
|
234 | + sprintf( |
|
235 | + esc_html__( |
|
236 | + 'The "%1$s" class could not be loaded.', |
|
237 | + 'event_espresso' |
|
238 | + ), |
|
239 | + $identifier |
|
240 | + ) |
|
241 | + ); |
|
242 | + } |
|
243 | + return true; |
|
244 | + } |
|
245 | + return false; |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * @param string $identifier |
|
252 | + * @param array $arguments |
|
253 | + * @return mixed |
|
254 | + * @throws InstantiationException |
|
255 | + */ |
|
256 | + protected function brewedClosure($identifier, array $arguments) |
|
257 | + { |
|
258 | + $closure = $this->reservoir->get($identifier); |
|
259 | + if (empty($closure)) { |
|
260 | + throw new InstantiationException( |
|
261 | + sprintf( |
|
262 | + esc_html__( |
|
263 | + 'Could not brew an instance of "%1$s".', |
|
264 | + 'event_espresso' |
|
265 | + ), |
|
266 | + $identifier |
|
267 | + ) |
|
268 | + ); |
|
269 | + } |
|
270 | + return $closure($arguments); |
|
271 | + } |
|
272 | + |
|
273 | + |
|
274 | + |
|
275 | + /** |
|
276 | + * @param CoffeeMakerInterface $coffee_maker |
|
277 | + * @param string $type |
|
278 | + * @return bool |
|
279 | + * @throws InvalidIdentifierException |
|
280 | + * @throws InvalidEntityException |
|
281 | + */ |
|
282 | + public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
|
283 | + { |
|
284 | + $type = CoffeeMaker::validateType($type); |
|
285 | + return $this->coffee_makers->add($coffee_maker, $type); |
|
286 | + } |
|
287 | + |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * @param string $identifier |
|
292 | + * @param callable $closure |
|
293 | + * @return callable|null |
|
294 | + * @throws InvalidIdentifierException |
|
295 | + * @throws InvalidDataTypeException |
|
296 | + */ |
|
297 | + public function addClosure($identifier, $closure) |
|
298 | + { |
|
299 | + if ( ! is_callable($closure)) { |
|
300 | + throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
|
301 | + } |
|
302 | + $identifier = $this->processIdentifier($identifier); |
|
303 | + if ($this->reservoir->add($closure, $identifier)) { |
|
304 | + return $closure; |
|
305 | + } |
|
306 | + return null; |
|
307 | + } |
|
308 | + |
|
309 | + |
|
310 | + |
|
311 | + /** |
|
312 | + * @param string $identifier |
|
313 | + * @return boolean |
|
314 | + * @throws InvalidIdentifierException |
|
315 | + */ |
|
316 | + public function removeClosure($identifier) |
|
317 | + { |
|
318 | + $identifier = $this->processIdentifier($identifier); |
|
319 | + if ($this->reservoir->has($identifier)) { |
|
320 | + return $this->reservoir->remove($this->reservoir->get($identifier)); |
|
321 | + } |
|
322 | + return false; |
|
323 | + } |
|
324 | + |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * @param string $identifier Identifier for the entity class that the service applies to |
|
329 | + * Typically a Fully Qualified Class Name |
|
330 | + * @param mixed $service |
|
331 | + * @return bool |
|
332 | + * @throws \EventEspresso\core\services\container\exceptions\InvalidServiceException |
|
333 | + * @throws InvalidIdentifierException |
|
334 | + */ |
|
335 | + public function addService($identifier, $service) |
|
336 | + { |
|
337 | + $identifier = $this->processIdentifier($identifier); |
|
338 | + $service = $this->validateService($identifier, $service); |
|
339 | + return $this->carafe->add($service, $identifier); |
|
340 | + } |
|
341 | + |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * @param string $identifier |
|
346 | + * @return boolean |
|
347 | + * @throws InvalidIdentifierException |
|
348 | + */ |
|
349 | + public function removeService($identifier) |
|
350 | + { |
|
351 | + $identifier = $this->processIdentifier($identifier); |
|
352 | + if ($this->carafe->has($identifier)) { |
|
353 | + return $this->carafe->remove($this->carafe->get($identifier)); |
|
354 | + } |
|
355 | + return false; |
|
356 | + } |
|
357 | + |
|
358 | + |
|
359 | + |
|
360 | + /** |
|
361 | + * Adds instructions on how to brew objects |
|
362 | + * |
|
363 | + * @param RecipeInterface $recipe |
|
364 | + * @return mixed |
|
365 | + * @throws InvalidIdentifierException |
|
366 | + */ |
|
367 | + public function addRecipe(RecipeInterface $recipe) |
|
368 | + { |
|
369 | + $this->addAliases($recipe->identifier(), $recipe->filters()); |
|
370 | + $identifier = $this->processIdentifier($recipe->identifier()); |
|
371 | + return $this->recipes->add($recipe, $identifier); |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + |
|
376 | + /** |
|
377 | + * @param string $identifier The Recipe's identifier |
|
378 | + * @return boolean |
|
379 | + * @throws InvalidIdentifierException |
|
380 | + */ |
|
381 | + public function removeRecipe($identifier) |
|
382 | + { |
|
383 | + $identifier = $this->processIdentifier($identifier); |
|
384 | + if ($this->recipes->has($identifier)) { |
|
385 | + return $this->recipes->remove($this->recipes->get($identifier)); |
|
386 | + } |
|
387 | + return false; |
|
388 | + } |
|
389 | + |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * Get instructions on how to brew objects |
|
394 | + * |
|
395 | + * @param string $identifier Identifier for the entity class that the recipe applies to |
|
396 | + * Typically a Fully Qualified Class Name |
|
397 | + * @param string $type |
|
398 | + * @return RecipeInterface |
|
399 | + * @throws OutOfBoundsException |
|
400 | + * @throws InvalidIdentifierException |
|
401 | + */ |
|
402 | + public function getRecipe($identifier, $type = '') |
|
403 | + { |
|
404 | + $identifier = $this->processIdentifier($identifier); |
|
405 | + if ($this->recipes->has($identifier)) { |
|
406 | + return $this->recipes->get($identifier); |
|
407 | + } |
|
408 | + $default_recipes = $this->getDefaultRecipes(); |
|
409 | + $matches = array(); |
|
410 | + foreach ($default_recipes as $wildcard => $default_recipe) { |
|
411 | + // is the wildcard recipe prefix in the identifier ? |
|
412 | + if (strpos($identifier, $wildcard) !== false) { |
|
413 | + // track matches and use the number of wildcard characters matched for the key |
|
414 | + $matches[strlen($wildcard)] = $default_recipe; |
|
415 | + } |
|
416 | + } |
|
417 | + if (count($matches) > 0) { |
|
418 | + // sort our recipes by the number of wildcard characters matched |
|
419 | + ksort($matches); |
|
420 | + // then grab the last recipe form the list, since it had the most matching characters |
|
421 | + $match = array_pop($matches); |
|
422 | + // since we are using a default recipe, we need to set it's identifier and fqcn |
|
423 | + return $this->copyDefaultRecipe($match, $identifier, $type); |
|
424 | + } |
|
425 | + if ($this->recipes->has(Recipe::DEFAULT_ID)) { |
|
426 | + // since we are using a default recipe, we need to set it's identifier and fqcn |
|
427 | + return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type); |
|
428 | + } |
|
429 | + throw new OutOfBoundsException( |
|
430 | + sprintf( |
|
431 | + __('Could not brew coffee because no recipes were found for class "%1$s".', 'event_espresso'), |
|
432 | + $identifier |
|
433 | + ) |
|
434 | + ); |
|
435 | + } |
|
436 | + |
|
437 | + |
|
438 | + |
|
439 | + /** |
|
440 | + * adds class name aliases to list of filters |
|
441 | + * |
|
442 | + * @param string $identifier Identifier for the entity class that the alias applies to |
|
443 | + * Typically a Fully Qualified Class Name |
|
444 | + * @param array|string $aliases |
|
445 | + * @return void |
|
446 | + * @throws InvalidIdentifierException |
|
447 | + */ |
|
448 | + public function addAliases($identifier, $aliases) |
|
449 | + { |
|
450 | + if (empty($aliases)) { |
|
451 | + return; |
|
452 | + } |
|
453 | + $identifier = $this->processIdentifier($identifier); |
|
454 | + foreach ((array)$aliases as $alias) { |
|
455 | + $this->filters[$this->processIdentifier($alias)] = $identifier; |
|
456 | + } |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + |
|
461 | + /** |
|
462 | + * Adds a service to one of the internal collections |
|
463 | + * |
|
464 | + * @param $identifier |
|
465 | + * @param array $arguments |
|
466 | + * @param string $type |
|
467 | + * @return mixed |
|
468 | + * @throws InvalidDataTypeException |
|
469 | + * @throws InvalidClassException |
|
470 | + * @throws OutOfBoundsException |
|
471 | + * @throws InvalidIdentifierException |
|
472 | + * @throws ServiceExistsException |
|
473 | + */ |
|
474 | + private function makeCoffee($identifier, $arguments = array(), $type = '') |
|
475 | + { |
|
476 | + if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) { |
|
477 | + throw new ServiceExistsException($identifier); |
|
478 | + } |
|
479 | + $identifier = $this->filterIdentifier($identifier); |
|
480 | + $recipe = $this->getRecipe($identifier, $type); |
|
481 | + $type = ! empty($type) ? $type : $recipe->type(); |
|
482 | + $coffee_maker = $this->getCoffeeMaker($type); |
|
483 | + return $coffee_maker->brew($recipe, $arguments); |
|
484 | + } |
|
485 | + |
|
486 | + |
|
487 | + |
|
488 | + /** |
|
489 | + * filters alias identifiers to find the real class name |
|
490 | + * |
|
491 | + * @param string $identifier Identifier for the entity class that the filter applies to |
|
492 | + * Typically a Fully Qualified Class Name |
|
493 | + * @return string |
|
494 | + * @throws InvalidIdentifierException |
|
495 | + */ |
|
496 | + private function filterIdentifier($identifier) |
|
497 | + { |
|
498 | + $identifier = $this->processIdentifier($identifier); |
|
499 | + return isset($this->filters[$identifier]) && ! empty($this->filters[$identifier]) |
|
500 | + ? $this->filters[$identifier] |
|
501 | + : $identifier; |
|
502 | + } |
|
503 | + |
|
504 | + |
|
505 | + |
|
506 | + /** |
|
507 | + * verifies and standardizes identifiers |
|
508 | + * |
|
509 | + * @param string $identifier Identifier for the entity class |
|
510 | + * Typically a Fully Qualified Class Name |
|
511 | + * @return string |
|
512 | + * @throws InvalidIdentifierException |
|
513 | + */ |
|
514 | + private function processIdentifier($identifier) |
|
515 | + { |
|
516 | + if ( ! is_string($identifier)) { |
|
517 | + throw new InvalidIdentifierException( |
|
518 | + is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
519 | + '\Fully\Qualified\ClassName' |
|
520 | + ); |
|
521 | + } |
|
522 | + return ltrim($identifier, '\\'); |
|
523 | + } |
|
524 | + |
|
525 | + |
|
526 | + |
|
527 | + /** |
|
528 | + * @param string $type |
|
529 | + * @return CoffeeMakerInterface |
|
530 | + * @throws OutOfBoundsException |
|
531 | + * @throws InvalidDataTypeException |
|
532 | + * @throws InvalidClassException |
|
533 | + */ |
|
534 | + private function getCoffeeMaker($type) |
|
535 | + { |
|
536 | + if ( ! $this->coffee_makers->has($type)) { |
|
537 | + throw new OutOfBoundsException( |
|
538 | + __('The requested coffee maker is either missing or invalid.', 'event_espresso') |
|
539 | + ); |
|
540 | + } |
|
541 | + return $this->coffee_makers->get($type); |
|
542 | + } |
|
543 | + |
|
544 | + |
|
545 | + |
|
546 | + /** |
|
547 | + * Retrieves all recipes that use a wildcard "*" in their identifier |
|
548 | + * This allows recipes to be set up for handling |
|
549 | + * legacy classes that do not support PSR-4 autoloading. |
|
550 | + * for example: |
|
551 | + * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
|
552 | + * |
|
553 | + * @return array |
|
554 | + */ |
|
555 | + private function getDefaultRecipes() |
|
556 | + { |
|
557 | + $default_recipes = array(); |
|
558 | + $this->recipes->rewind(); |
|
559 | + while ($this->recipes->valid()) { |
|
560 | + $identifier = $this->recipes->getInfo(); |
|
561 | + // does this recipe use a wildcard ? (but is NOT the global default) |
|
562 | + if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
|
563 | + // strip the wildcard and use identifier as key |
|
564 | + $default_recipes[str_replace('*', '', $identifier)] = $this->recipes->current(); |
|
565 | + } |
|
566 | + $this->recipes->next(); |
|
567 | + } |
|
568 | + return $default_recipes; |
|
569 | + } |
|
570 | + |
|
571 | + |
|
572 | + |
|
573 | + /** |
|
574 | + * clones a default recipe and then copies details |
|
575 | + * from the incoming request to it so that it can be used |
|
576 | + * |
|
577 | + * @param RecipeInterface $default_recipe |
|
578 | + * @param string $identifier |
|
579 | + * @param string $type |
|
580 | + * @return RecipeInterface |
|
581 | + */ |
|
582 | + private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
|
583 | + { |
|
584 | + $recipe = clone $default_recipe; |
|
585 | + if ( ! empty($type)) { |
|
586 | + $recipe->setType($type); |
|
587 | + } |
|
588 | + // is this the base default recipe ? |
|
589 | + if ($default_recipe->identifier() === Recipe::DEFAULT_ID) { |
|
590 | + $recipe->setIdentifier($identifier); |
|
591 | + $recipe->setFqcn($identifier); |
|
592 | + return $recipe; |
|
593 | + } |
|
594 | + $recipe->setIdentifier($identifier); |
|
595 | + foreach ($default_recipe->paths() as $path) { |
|
596 | + $path = str_replace('*', $identifier, $path); |
|
597 | + if (is_readable($path)) { |
|
598 | + $recipe->setPaths($path); |
|
599 | + } |
|
600 | + } |
|
601 | + $recipe->setFqcn($identifier); |
|
602 | + return $recipe; |
|
603 | + } |
|
604 | + |
|
605 | + |
|
606 | + |
|
607 | + /** |
|
608 | + * @param string $identifier Identifier for the entity class that the service applies to |
|
609 | + * Typically a Fully Qualified Class Name |
|
610 | + * @param mixed $service |
|
611 | + * @return mixed |
|
612 | + * @throws InvalidServiceException |
|
613 | + */ |
|
614 | + private function validateService($identifier, $service) |
|
615 | + { |
|
616 | + if ( ! is_object($service)) { |
|
617 | + throw new InvalidServiceException( |
|
618 | + $identifier, |
|
619 | + $service |
|
620 | + ); |
|
621 | + } |
|
622 | + return $service; |
|
623 | + } |
|
624 | 624 | |
625 | 625 | } |
626 | 626 | // End of file CoffeeShop.php |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | use EventEspresso\core\exceptions\InvalidClassException; |
5 | 5 | |
6 | 6 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
7 | - exit('No direct script access allowed'); |
|
7 | + exit('No direct script access allowed'); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | |
@@ -26,26 +26,26 @@ discard block |
||
26 | 26 | class LoadOnlyCoffeeMaker extends CoffeeMaker |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - public function type() |
|
33 | - { |
|
34 | - return CoffeeMaker::BREW_LOAD_ONLY; |
|
35 | - } |
|
36 | - |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * @param RecipeInterface $recipe |
|
41 | - * @param array $arguments |
|
42 | - * @return mixed |
|
43 | - * @throws InvalidClassException |
|
44 | - */ |
|
45 | - public function brew(RecipeInterface $recipe, $arguments = array()) |
|
46 | - { |
|
47 | - return $this->resolveClassAndFilepath($recipe); |
|
48 | - } |
|
29 | + /** |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + public function type() |
|
33 | + { |
|
34 | + return CoffeeMaker::BREW_LOAD_ONLY; |
|
35 | + } |
|
36 | + |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * @param RecipeInterface $recipe |
|
41 | + * @param array $arguments |
|
42 | + * @return mixed |
|
43 | + * @throws InvalidClassException |
|
44 | + */ |
|
45 | + public function brew(RecipeInterface $recipe, $arguments = array()) |
|
46 | + { |
|
47 | + return $this->resolveClassAndFilepath($recipe); |
|
48 | + } |
|
49 | 49 | |
50 | 50 | |
51 | 51 |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | use RuntimeException; |
9 | 9 | |
10 | 10 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
11 | - exit('No direct script access allowed'); |
|
11 | + exit('No direct script access allowed'); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | |
@@ -24,316 +24,316 @@ discard block |
||
24 | 24 | class Recipe implements RecipeInterface |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * A default Recipe to use if none is specified for a class |
|
29 | - */ |
|
30 | - const DEFAULT_ID = '*'; |
|
31 | - |
|
32 | - /** |
|
33 | - * Identifier for the entity class to be constructed. |
|
34 | - * Typically a Fully Qualified Class Name |
|
35 | - * |
|
36 | - * @var string $identifier |
|
37 | - */ |
|
38 | - private $identifier; |
|
39 | - |
|
40 | - /** |
|
41 | - * Fully Qualified Class Name |
|
42 | - * |
|
43 | - * @var string $fqcn |
|
44 | - */ |
|
45 | - private $fqcn; |
|
46 | - |
|
47 | - /** |
|
48 | - * a dependency class map array |
|
49 | - * If a Recipe is for a single class (or group of classes that shares the EXACT SAME constructor arguments), |
|
50 | - * and that class type hints for an interface, then this property allows you to configure what dependencies |
|
51 | - * get used when instantiating the class. |
|
52 | - * For example: |
|
53 | - * There's a class called Coffee, and one of its constructor arguments is BeanInterface |
|
54 | - * There are two implementations of BeanInterface: HonduranBean, and KenyanBean |
|
55 | - * We want one Coffee object to use HonduranBean for its BeanInterface, |
|
56 | - * and the 2nd Coffee object to use KenyanBean for its BeanInterface. |
|
57 | - * To do this, we need to create two Recipes: |
|
58 | - * one with an identifier of 'HonduranCoffee' using the following ingredients : |
|
59 | - * array('BeanInterface' => 'HonduranBean') |
|
60 | - * and the other with an identifier of 'KenyanCoffee' using the following ingredients : |
|
61 | - * array('BeanInterface' => 'KenyanBean') |
|
62 | - * Then, whenever the CoffeeShop brews an instance of HonduranCoffee, |
|
63 | - * an instance of HonduranBean will get injected for the BeanInterface dependency, |
|
64 | - * and whenever the CoffeeShop brews an instance of KenyanCoffee, |
|
65 | - * an instance of KenyanBean will get injected for the BeanInterface dependency |
|
66 | - * |
|
67 | - * @var array $ingredients |
|
68 | - */ |
|
69 | - private $ingredients = array(); |
|
70 | - |
|
71 | - /** |
|
72 | - * one of the class constants from CoffeeShop: |
|
73 | - * CoffeeMaker::BREW_NEW - creates a new instance |
|
74 | - * CoffeeMaker::BREW_SHARED - creates a shared instance |
|
75 | - * CoffeeMaker::BREW_LOAD_ONLY - loads but does not instantiate |
|
76 | - * |
|
77 | - * @var string $type |
|
78 | - */ |
|
79 | - private $type; |
|
80 | - |
|
81 | - /** |
|
82 | - * class name aliases - typically a Fully Qualified Interface that the class implements |
|
83 | - * identifiers passed to the CoffeeShop will be run through the filters to find the correct class name |
|
84 | - * |
|
85 | - * @var array $filters |
|
86 | - */ |
|
87 | - private $filters = array(); |
|
88 | - |
|
89 | - /** |
|
90 | - * array of full server filepaths to files that may contain the class |
|
91 | - * |
|
92 | - * @var array $paths |
|
93 | - */ |
|
94 | - private $paths = array(); |
|
95 | - |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * Recipe constructor. |
|
100 | - * |
|
101 | - * @param string $identifier class identifier, can be an alias, or FQCN, or whatever |
|
102 | - * @param string $fqcn \Fully\Qualified\ClassName, optional if $identifier is FQCN |
|
103 | - * @param array $ingredients array of dependencies that can not be resolved automatically, |
|
104 | - * used for resolving concrete classes for type hinted interfaces |
|
105 | - * for the dependencies of THIS class |
|
106 | - * @param string $type recipe type: one of the class constants on |
|
107 | - * \EventEspresso\core\services\container\CoffeeMaker |
|
108 | - * @param array $filters array of class aliases, or class interfaces |
|
109 | - * this works somewhat opposite to the $ingredients array above, |
|
110 | - * in that this array specifies interfaces or aliases |
|
111 | - * that this Recipe can be used for when resolving OTHER class's dependencies |
|
112 | - * @param array $paths if class can not be loaded via PSR-4 autoloading, |
|
113 | - * then supply a filepath, or array of filepaths, so that it can be included |
|
114 | - */ |
|
115 | - public function __construct( |
|
116 | - $identifier, |
|
117 | - $fqcn = '', |
|
118 | - $filters = array(), |
|
119 | - $ingredients = array(), |
|
120 | - $type = CoffeeMaker::BREW_NEW, |
|
121 | - $paths = array() |
|
122 | - ) |
|
123 | - { |
|
124 | - $this->setIdentifier($identifier); |
|
125 | - $this->setFilters((array)$filters); |
|
126 | - $this->setIngredients((array)$ingredients); |
|
127 | - $this->setType($type); |
|
128 | - $this->setPaths($paths); |
|
129 | - $this->setFqcn($fqcn); |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - public function identifier() |
|
138 | - { |
|
139 | - return $this->identifier; |
|
140 | - } |
|
141 | - |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * @return string |
|
146 | - */ |
|
147 | - public function fqcn() |
|
148 | - { |
|
149 | - return $this->fqcn; |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - |
|
154 | - /** |
|
155 | - * @return array |
|
156 | - */ |
|
157 | - public function filters() |
|
158 | - { |
|
159 | - return (array)$this->filters; |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * @return array |
|
166 | - */ |
|
167 | - public function ingredients() |
|
168 | - { |
|
169 | - return $this->ingredients; |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * @return string |
|
176 | - */ |
|
177 | - public function type() |
|
178 | - { |
|
179 | - return $this->type; |
|
180 | - } |
|
181 | - |
|
182 | - |
|
183 | - |
|
184 | - /** |
|
185 | - * @return array |
|
186 | - */ |
|
187 | - public function paths() |
|
188 | - { |
|
189 | - return (array)$this->paths; |
|
190 | - } |
|
191 | - |
|
192 | - |
|
193 | - |
|
194 | - /** |
|
195 | - * @param string $identifier Identifier for the entity class that the Recipe applies to |
|
196 | - * Typically a Fully Qualified Class Name |
|
197 | - */ |
|
198 | - public function setIdentifier($identifier) |
|
199 | - { |
|
200 | - if ( ! is_string($identifier) || empty($identifier)) { |
|
201 | - throw new InvalidIdentifierException( |
|
202 | - is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
203 | - __('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso') |
|
204 | - ); |
|
205 | - } |
|
206 | - $this->identifier = $identifier; |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - |
|
211 | - /** |
|
212 | - * Ensures incoming string is a valid Fully Qualified Class Name, |
|
213 | - * except if this is the default wildcard Recipe ( * ), |
|
214 | - * or it's NOT an actual FQCN because the Recipe is using filepaths |
|
215 | - * for classes that are not PSR-4 compatible |
|
216 | - * PLZ NOTE: |
|
217 | - * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
218 | - * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
219 | - * |
|
220 | - * @param string $fqcn |
|
221 | - * @throws InvalidDataTypeException |
|
222 | - * @throws InvalidClassException |
|
223 | - * @throws InvalidInterfaceException |
|
224 | - */ |
|
225 | - public function setFqcn($fqcn) |
|
226 | - { |
|
227 | - $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier; |
|
228 | - if ( ! is_string($fqcn)) { |
|
229 | - throw new InvalidDataTypeException( |
|
230 | - '$fqcn', |
|
231 | - is_object($fqcn) ? get_class($fqcn) : gettype($fqcn), |
|
232 | - __('string (Fully\Qualified\ClassName)', 'event_espresso') |
|
233 | - ); |
|
234 | - } |
|
235 | - $fqcn = ltrim($fqcn, '\\'); |
|
236 | - if ( |
|
237 | - $fqcn !== Recipe::DEFAULT_ID |
|
238 | - && ! empty($fqcn) |
|
239 | - && empty($this->paths) |
|
240 | - && ! (class_exists($fqcn) || interface_exists($fqcn)) |
|
241 | - ) { |
|
242 | - throw new InvalidClassException($fqcn); |
|
243 | - } |
|
244 | - $this->fqcn = $fqcn; |
|
245 | - } |
|
246 | - |
|
247 | - |
|
248 | - |
|
249 | - /** |
|
250 | - * @param array $ingredients an array of dependencies where keys are the aliases and values are the FQCNs |
|
251 | - * example: |
|
252 | - * array( 'ClassInterface' => 'Fully\Qualified\ClassName' ) |
|
253 | - */ |
|
254 | - public function setIngredients(array $ingredients) |
|
255 | - { |
|
256 | - if (empty($ingredients)) { |
|
257 | - return; |
|
258 | - } |
|
259 | - if ( ! is_array($ingredients)) { |
|
260 | - throw new InvalidDataTypeException( |
|
261 | - '$ingredients', |
|
262 | - is_object($ingredients) ? get_class($ingredients) : gettype($ingredients), |
|
263 | - __('array of class dependencies', 'event_espresso') |
|
264 | - ); |
|
265 | - } |
|
266 | - $this->ingredients = array_merge($this->ingredients, $ingredients); |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * @param string $type one of the class constants returned from CoffeeMaker::getTypes() |
|
272 | - */ |
|
273 | - public function setType($type = CoffeeMaker::BREW_NEW) |
|
274 | - { |
|
275 | - $this->type = CoffeeMaker::validateType($type); |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - |
|
280 | - /** |
|
281 | - * @param array $filters an array of filters where keys are the aliases and values are the FQCNs |
|
282 | - * example: |
|
283 | - * array( 'ClassInterface' => 'Fully\Qualified\ClassName' ) |
|
284 | - */ |
|
285 | - public function setFilters(array $filters) |
|
286 | - { |
|
287 | - if (empty($filters)) { |
|
288 | - return; |
|
289 | - } |
|
290 | - if ( ! is_array($filters)) { |
|
291 | - throw new InvalidDataTypeException( |
|
292 | - '$filters', |
|
293 | - is_object($filters) ? get_class($filters) : gettype($filters), |
|
294 | - __('array of class aliases', 'event_espresso') |
|
295 | - ); |
|
296 | - } |
|
297 | - $this->filters = array_merge($this->filters, $filters); |
|
298 | - } |
|
299 | - |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * Ensures incoming paths is a valid filepath, or array of valid filepaths, |
|
304 | - * and merges them in with any existing filepaths |
|
305 | - * |
|
306 | - * PLZ NOTE: |
|
307 | - * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
308 | - * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
309 | - * |
|
310 | - * @param string|array $paths |
|
311 | - */ |
|
312 | - public function setPaths($paths = array()) |
|
313 | - { |
|
314 | - if (empty($paths)) { |
|
315 | - return; |
|
316 | - } |
|
317 | - if ( ! (is_string($paths) || is_array($paths))) { |
|
318 | - throw new InvalidDataTypeException( |
|
319 | - '$path', |
|
320 | - is_object($paths) ? get_class($paths) : gettype($paths), |
|
321 | - __('string or array of strings (full server filepath(s))', 'event_espresso') |
|
322 | - ); |
|
323 | - } |
|
324 | - $paths = (array)$paths; |
|
325 | - foreach ($paths as $path) { |
|
326 | - if (strpos($path, '*') === false && ! is_readable($path)) { |
|
327 | - throw new RuntimeException( |
|
328 | - sprintf( |
|
329 | - __('The following filepath is not readable: "%1$s"', 'event_espresso'), |
|
330 | - $path |
|
331 | - ) |
|
332 | - ); |
|
333 | - } |
|
334 | - } |
|
335 | - $this->paths = array_merge($this->paths, $paths); |
|
336 | - } |
|
27 | + /** |
|
28 | + * A default Recipe to use if none is specified for a class |
|
29 | + */ |
|
30 | + const DEFAULT_ID = '*'; |
|
31 | + |
|
32 | + /** |
|
33 | + * Identifier for the entity class to be constructed. |
|
34 | + * Typically a Fully Qualified Class Name |
|
35 | + * |
|
36 | + * @var string $identifier |
|
37 | + */ |
|
38 | + private $identifier; |
|
39 | + |
|
40 | + /** |
|
41 | + * Fully Qualified Class Name |
|
42 | + * |
|
43 | + * @var string $fqcn |
|
44 | + */ |
|
45 | + private $fqcn; |
|
46 | + |
|
47 | + /** |
|
48 | + * a dependency class map array |
|
49 | + * If a Recipe is for a single class (or group of classes that shares the EXACT SAME constructor arguments), |
|
50 | + * and that class type hints for an interface, then this property allows you to configure what dependencies |
|
51 | + * get used when instantiating the class. |
|
52 | + * For example: |
|
53 | + * There's a class called Coffee, and one of its constructor arguments is BeanInterface |
|
54 | + * There are two implementations of BeanInterface: HonduranBean, and KenyanBean |
|
55 | + * We want one Coffee object to use HonduranBean for its BeanInterface, |
|
56 | + * and the 2nd Coffee object to use KenyanBean for its BeanInterface. |
|
57 | + * To do this, we need to create two Recipes: |
|
58 | + * one with an identifier of 'HonduranCoffee' using the following ingredients : |
|
59 | + * array('BeanInterface' => 'HonduranBean') |
|
60 | + * and the other with an identifier of 'KenyanCoffee' using the following ingredients : |
|
61 | + * array('BeanInterface' => 'KenyanBean') |
|
62 | + * Then, whenever the CoffeeShop brews an instance of HonduranCoffee, |
|
63 | + * an instance of HonduranBean will get injected for the BeanInterface dependency, |
|
64 | + * and whenever the CoffeeShop brews an instance of KenyanCoffee, |
|
65 | + * an instance of KenyanBean will get injected for the BeanInterface dependency |
|
66 | + * |
|
67 | + * @var array $ingredients |
|
68 | + */ |
|
69 | + private $ingredients = array(); |
|
70 | + |
|
71 | + /** |
|
72 | + * one of the class constants from CoffeeShop: |
|
73 | + * CoffeeMaker::BREW_NEW - creates a new instance |
|
74 | + * CoffeeMaker::BREW_SHARED - creates a shared instance |
|
75 | + * CoffeeMaker::BREW_LOAD_ONLY - loads but does not instantiate |
|
76 | + * |
|
77 | + * @var string $type |
|
78 | + */ |
|
79 | + private $type; |
|
80 | + |
|
81 | + /** |
|
82 | + * class name aliases - typically a Fully Qualified Interface that the class implements |
|
83 | + * identifiers passed to the CoffeeShop will be run through the filters to find the correct class name |
|
84 | + * |
|
85 | + * @var array $filters |
|
86 | + */ |
|
87 | + private $filters = array(); |
|
88 | + |
|
89 | + /** |
|
90 | + * array of full server filepaths to files that may contain the class |
|
91 | + * |
|
92 | + * @var array $paths |
|
93 | + */ |
|
94 | + private $paths = array(); |
|
95 | + |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * Recipe constructor. |
|
100 | + * |
|
101 | + * @param string $identifier class identifier, can be an alias, or FQCN, or whatever |
|
102 | + * @param string $fqcn \Fully\Qualified\ClassName, optional if $identifier is FQCN |
|
103 | + * @param array $ingredients array of dependencies that can not be resolved automatically, |
|
104 | + * used for resolving concrete classes for type hinted interfaces |
|
105 | + * for the dependencies of THIS class |
|
106 | + * @param string $type recipe type: one of the class constants on |
|
107 | + * \EventEspresso\core\services\container\CoffeeMaker |
|
108 | + * @param array $filters array of class aliases, or class interfaces |
|
109 | + * this works somewhat opposite to the $ingredients array above, |
|
110 | + * in that this array specifies interfaces or aliases |
|
111 | + * that this Recipe can be used for when resolving OTHER class's dependencies |
|
112 | + * @param array $paths if class can not be loaded via PSR-4 autoloading, |
|
113 | + * then supply a filepath, or array of filepaths, so that it can be included |
|
114 | + */ |
|
115 | + public function __construct( |
|
116 | + $identifier, |
|
117 | + $fqcn = '', |
|
118 | + $filters = array(), |
|
119 | + $ingredients = array(), |
|
120 | + $type = CoffeeMaker::BREW_NEW, |
|
121 | + $paths = array() |
|
122 | + ) |
|
123 | + { |
|
124 | + $this->setIdentifier($identifier); |
|
125 | + $this->setFilters((array)$filters); |
|
126 | + $this->setIngredients((array)$ingredients); |
|
127 | + $this->setType($type); |
|
128 | + $this->setPaths($paths); |
|
129 | + $this->setFqcn($fqcn); |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + public function identifier() |
|
138 | + { |
|
139 | + return $this->identifier; |
|
140 | + } |
|
141 | + |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * @return string |
|
146 | + */ |
|
147 | + public function fqcn() |
|
148 | + { |
|
149 | + return $this->fqcn; |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + |
|
154 | + /** |
|
155 | + * @return array |
|
156 | + */ |
|
157 | + public function filters() |
|
158 | + { |
|
159 | + return (array)$this->filters; |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * @return array |
|
166 | + */ |
|
167 | + public function ingredients() |
|
168 | + { |
|
169 | + return $this->ingredients; |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * @return string |
|
176 | + */ |
|
177 | + public function type() |
|
178 | + { |
|
179 | + return $this->type; |
|
180 | + } |
|
181 | + |
|
182 | + |
|
183 | + |
|
184 | + /** |
|
185 | + * @return array |
|
186 | + */ |
|
187 | + public function paths() |
|
188 | + { |
|
189 | + return (array)$this->paths; |
|
190 | + } |
|
191 | + |
|
192 | + |
|
193 | + |
|
194 | + /** |
|
195 | + * @param string $identifier Identifier for the entity class that the Recipe applies to |
|
196 | + * Typically a Fully Qualified Class Name |
|
197 | + */ |
|
198 | + public function setIdentifier($identifier) |
|
199 | + { |
|
200 | + if ( ! is_string($identifier) || empty($identifier)) { |
|
201 | + throw new InvalidIdentifierException( |
|
202 | + is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
203 | + __('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso') |
|
204 | + ); |
|
205 | + } |
|
206 | + $this->identifier = $identifier; |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + |
|
211 | + /** |
|
212 | + * Ensures incoming string is a valid Fully Qualified Class Name, |
|
213 | + * except if this is the default wildcard Recipe ( * ), |
|
214 | + * or it's NOT an actual FQCN because the Recipe is using filepaths |
|
215 | + * for classes that are not PSR-4 compatible |
|
216 | + * PLZ NOTE: |
|
217 | + * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
218 | + * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
219 | + * |
|
220 | + * @param string $fqcn |
|
221 | + * @throws InvalidDataTypeException |
|
222 | + * @throws InvalidClassException |
|
223 | + * @throws InvalidInterfaceException |
|
224 | + */ |
|
225 | + public function setFqcn($fqcn) |
|
226 | + { |
|
227 | + $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier; |
|
228 | + if ( ! is_string($fqcn)) { |
|
229 | + throw new InvalidDataTypeException( |
|
230 | + '$fqcn', |
|
231 | + is_object($fqcn) ? get_class($fqcn) : gettype($fqcn), |
|
232 | + __('string (Fully\Qualified\ClassName)', 'event_espresso') |
|
233 | + ); |
|
234 | + } |
|
235 | + $fqcn = ltrim($fqcn, '\\'); |
|
236 | + if ( |
|
237 | + $fqcn !== Recipe::DEFAULT_ID |
|
238 | + && ! empty($fqcn) |
|
239 | + && empty($this->paths) |
|
240 | + && ! (class_exists($fqcn) || interface_exists($fqcn)) |
|
241 | + ) { |
|
242 | + throw new InvalidClassException($fqcn); |
|
243 | + } |
|
244 | + $this->fqcn = $fqcn; |
|
245 | + } |
|
246 | + |
|
247 | + |
|
248 | + |
|
249 | + /** |
|
250 | + * @param array $ingredients an array of dependencies where keys are the aliases and values are the FQCNs |
|
251 | + * example: |
|
252 | + * array( 'ClassInterface' => 'Fully\Qualified\ClassName' ) |
|
253 | + */ |
|
254 | + public function setIngredients(array $ingredients) |
|
255 | + { |
|
256 | + if (empty($ingredients)) { |
|
257 | + return; |
|
258 | + } |
|
259 | + if ( ! is_array($ingredients)) { |
|
260 | + throw new InvalidDataTypeException( |
|
261 | + '$ingredients', |
|
262 | + is_object($ingredients) ? get_class($ingredients) : gettype($ingredients), |
|
263 | + __('array of class dependencies', 'event_espresso') |
|
264 | + ); |
|
265 | + } |
|
266 | + $this->ingredients = array_merge($this->ingredients, $ingredients); |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * @param string $type one of the class constants returned from CoffeeMaker::getTypes() |
|
272 | + */ |
|
273 | + public function setType($type = CoffeeMaker::BREW_NEW) |
|
274 | + { |
|
275 | + $this->type = CoffeeMaker::validateType($type); |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * @param array $filters an array of filters where keys are the aliases and values are the FQCNs |
|
282 | + * example: |
|
283 | + * array( 'ClassInterface' => 'Fully\Qualified\ClassName' ) |
|
284 | + */ |
|
285 | + public function setFilters(array $filters) |
|
286 | + { |
|
287 | + if (empty($filters)) { |
|
288 | + return; |
|
289 | + } |
|
290 | + if ( ! is_array($filters)) { |
|
291 | + throw new InvalidDataTypeException( |
|
292 | + '$filters', |
|
293 | + is_object($filters) ? get_class($filters) : gettype($filters), |
|
294 | + __('array of class aliases', 'event_espresso') |
|
295 | + ); |
|
296 | + } |
|
297 | + $this->filters = array_merge($this->filters, $filters); |
|
298 | + } |
|
299 | + |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * Ensures incoming paths is a valid filepath, or array of valid filepaths, |
|
304 | + * and merges them in with any existing filepaths |
|
305 | + * |
|
306 | + * PLZ NOTE: |
|
307 | + * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
308 | + * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
309 | + * |
|
310 | + * @param string|array $paths |
|
311 | + */ |
|
312 | + public function setPaths($paths = array()) |
|
313 | + { |
|
314 | + if (empty($paths)) { |
|
315 | + return; |
|
316 | + } |
|
317 | + if ( ! (is_string($paths) || is_array($paths))) { |
|
318 | + throw new InvalidDataTypeException( |
|
319 | + '$path', |
|
320 | + is_object($paths) ? get_class($paths) : gettype($paths), |
|
321 | + __('string or array of strings (full server filepath(s))', 'event_espresso') |
|
322 | + ); |
|
323 | + } |
|
324 | + $paths = (array)$paths; |
|
325 | + foreach ($paths as $path) { |
|
326 | + if (strpos($path, '*') === false && ! is_readable($path)) { |
|
327 | + throw new RuntimeException( |
|
328 | + sprintf( |
|
329 | + __('The following filepath is not readable: "%1$s"', 'event_espresso'), |
|
330 | + $path |
|
331 | + ) |
|
332 | + ); |
|
333 | + } |
|
334 | + } |
|
335 | + $this->paths = array_merge($this->paths, $paths); |
|
336 | + } |
|
337 | 337 | |
338 | 338 | |
339 | 339 |
@@ -30,16 +30,16 @@ discard block |
||
30 | 30 | |
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * OpenCoffeeShop constructor |
|
35 | - * |
|
36 | - * @throws InvalidInterfaceException |
|
37 | - */ |
|
33 | + /** |
|
34 | + * OpenCoffeeShop constructor |
|
35 | + * |
|
36 | + * @throws InvalidInterfaceException |
|
37 | + */ |
|
38 | 38 | public function __construct() |
39 | - { |
|
40 | - // instantiate the DI container |
|
39 | + { |
|
40 | + // instantiate the DI container |
|
41 | 41 | $this->CoffeeShop = new CoffeeShop(); |
42 | - } |
|
42 | + } |
|
43 | 43 | |
44 | 44 | |
45 | 45 | |
@@ -52,60 +52,60 @@ discard block |
||
52 | 52 | |
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * configure coffee makers which control the different kinds of brews |
|
57 | - * ( shared services, new factory objects, etc ) |
|
58 | - * |
|
59 | - * @throws InvalidEntityException |
|
60 | - */ |
|
55 | + /** |
|
56 | + * configure coffee makers which control the different kinds of brews |
|
57 | + * ( shared services, new factory objects, etc ) |
|
58 | + * |
|
59 | + * @throws InvalidEntityException |
|
60 | + */ |
|
61 | 61 | public function setupCoffeeMakers() { |
62 | - // create a dependency injector class for resolving class constructor arguments |
|
63 | - $DependencyInjector = new DependencyInjector( |
|
64 | - $this->CoffeeShop, |
|
65 | - new \EEH_Array() |
|
66 | - ); |
|
67 | - // and some coffeemakers, one for creating new instances |
|
68 | - $this->CoffeeShop->addCoffeeMaker( |
|
69 | - new NewCoffeeMaker($this->CoffeeShop, $DependencyInjector), |
|
70 | - CoffeeMaker::BREW_NEW |
|
71 | - ); |
|
72 | - // one for shared services |
|
73 | - $this->CoffeeShop->addCoffeeMaker( |
|
74 | - new SharedCoffeeMaker($this->CoffeeShop, $DependencyInjector), |
|
75 | - CoffeeMaker::BREW_SHARED |
|
76 | - ); |
|
77 | - // and one for classes that only get loaded |
|
78 | - $this->CoffeeShop->addCoffeeMaker( |
|
79 | - new LoadOnlyCoffeeMaker($this->CoffeeShop, $DependencyInjector), |
|
80 | - CoffeeMaker::BREW_LOAD_ONLY |
|
81 | - ); |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * Recipes define how to load legacy classes |
|
88 | - * |
|
89 | - * @throws InvalidIdentifierException |
|
90 | - */ |
|
91 | - public function addRecipes() { |
|
92 | - // add default recipe, which should handle loading for most PSR-4 compatible classes |
|
93 | - // as long as they are not type hinting for interfaces |
|
94 | - $this->CoffeeShop->addRecipe( |
|
95 | - new Recipe( |
|
96 | - Recipe::DEFAULT_ID |
|
97 | - ) |
|
98 | - ); |
|
99 | - // PSR-4 compatible class with aliases |
|
62 | + // create a dependency injector class for resolving class constructor arguments |
|
63 | + $DependencyInjector = new DependencyInjector( |
|
64 | + $this->CoffeeShop, |
|
65 | + new \EEH_Array() |
|
66 | + ); |
|
67 | + // and some coffeemakers, one for creating new instances |
|
68 | + $this->CoffeeShop->addCoffeeMaker( |
|
69 | + new NewCoffeeMaker($this->CoffeeShop, $DependencyInjector), |
|
70 | + CoffeeMaker::BREW_NEW |
|
71 | + ); |
|
72 | + // one for shared services |
|
73 | + $this->CoffeeShop->addCoffeeMaker( |
|
74 | + new SharedCoffeeMaker($this->CoffeeShop, $DependencyInjector), |
|
75 | + CoffeeMaker::BREW_SHARED |
|
76 | + ); |
|
77 | + // and one for classes that only get loaded |
|
78 | + $this->CoffeeShop->addCoffeeMaker( |
|
79 | + new LoadOnlyCoffeeMaker($this->CoffeeShop, $DependencyInjector), |
|
80 | + CoffeeMaker::BREW_LOAD_ONLY |
|
81 | + ); |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * Recipes define how to load legacy classes |
|
88 | + * |
|
89 | + * @throws InvalidIdentifierException |
|
90 | + */ |
|
91 | + public function addRecipes() { |
|
92 | + // add default recipe, which should handle loading for most PSR-4 compatible classes |
|
93 | + // as long as they are not type hinting for interfaces |
|
94 | + $this->CoffeeShop->addRecipe( |
|
95 | + new Recipe( |
|
96 | + Recipe::DEFAULT_ID |
|
97 | + ) |
|
98 | + ); |
|
99 | + // PSR-4 compatible class with aliases |
|
100 | 100 | $this->CoffeeShop->addRecipe( |
101 | 101 | new Recipe( |
102 | 102 | 'CommandHandlerManager', |
103 | 103 | 'EventEspresso\core\services\commands\CommandHandlerManager', |
104 | - array( |
|
105 | - 'CommandHandlerManagerInterface', |
|
106 | - 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
107 | - ), |
|
108 | - array(), |
|
104 | + array( |
|
105 | + 'CommandHandlerManagerInterface', |
|
106 | + 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
107 | + ), |
|
108 | + array(), |
|
109 | 109 | CoffeeMaker::BREW_SHARED |
110 | 110 | ) |
111 | 111 | ); |
@@ -114,11 +114,11 @@ discard block |
||
114 | 114 | new Recipe( |
115 | 115 | 'CommandBus', |
116 | 116 | 'EventEspresso\core\services\commands\CommandBus', |
117 | - array( |
|
118 | - 'CommandBusInterface', |
|
119 | - 'EventEspresso\core\services\commands\CommandBusInterface', |
|
120 | - ), |
|
121 | - array(), |
|
117 | + array( |
|
118 | + 'CommandBusInterface', |
|
119 | + 'EventEspresso\core\services\commands\CommandBusInterface', |
|
120 | + ), |
|
121 | + array(), |
|
122 | 122 | CoffeeMaker::BREW_SHARED |
123 | 123 | ) |
124 | 124 | ); |
@@ -128,22 +128,22 @@ discard block |
||
128 | 128 | new Recipe( |
129 | 129 | 'EEI_*', |
130 | 130 | '', |
131 | - array(), |
|
132 | - array(), |
|
131 | + array(), |
|
132 | + array(), |
|
133 | 133 | CoffeeMaker::BREW_LOAD_ONLY, |
134 | - array( |
|
135 | - EE_INTERFACES . '*.php', |
|
136 | - EE_INTERFACES . '*.interfaces.php', |
|
137 | - ) |
|
134 | + array( |
|
135 | + EE_INTERFACES . '*.php', |
|
136 | + EE_INTERFACES . '*.interfaces.php', |
|
137 | + ) |
|
138 | 138 | ) |
139 | 139 | ); |
140 | 140 | // add a wildcard recipe for loading models |
141 | 141 | $this->CoffeeShop->addRecipe( |
142 | 142 | new Recipe( |
143 | 143 | 'EEM_*', |
144 | - '', |
|
145 | - array(), |
|
146 | - array(), |
|
144 | + '', |
|
145 | + array(), |
|
146 | + array(), |
|
147 | 147 | CoffeeMaker::BREW_SHARED, |
148 | 148 | EE_MODELS . '*.model.php' |
149 | 149 | ) |
@@ -152,10 +152,10 @@ discard block |
||
152 | 152 | $this->CoffeeShop->addRecipe( |
153 | 153 | new Recipe( |
154 | 154 | 'EE_*', |
155 | - '', |
|
156 | - array(), |
|
157 | - array(), |
|
158 | - CoffeeMaker::BREW_SHARED, |
|
155 | + '', |
|
156 | + array(), |
|
157 | + array(), |
|
158 | + CoffeeMaker::BREW_SHARED, |
|
159 | 159 | array( |
160 | 160 | EE_CORE . '*.core.php', |
161 | 161 | EE_ADMIN . '*.core.php', |
@@ -170,10 +170,10 @@ discard block |
||
170 | 170 | $this->CoffeeShop->addRecipe( |
171 | 171 | new Recipe( |
172 | 172 | 'EE_Admin_Page*', |
173 | - '', |
|
174 | - array(), |
|
175 | - array(), |
|
176 | - CoffeeMaker::BREW_LOAD_ONLY, |
|
173 | + '', |
|
174 | + array(), |
|
175 | + array(), |
|
176 | + CoffeeMaker::BREW_LOAD_ONLY, |
|
177 | 177 | array( EE_ADMIN . '*.core.php' ) |
178 | 178 | ) |
179 | 179 | ); |
@@ -181,10 +181,10 @@ discard block |
||
181 | 181 | // $this->CoffeeShop->addRecipe( |
182 | 182 | // new Recipe( |
183 | 183 | // '*_Admin_Page', |
184 | - // '', |
|
185 | - // array(), |
|
186 | - // array(), |
|
187 | - // CoffeeMaker::BREW_SHARED, |
|
184 | + // '', |
|
185 | + // array(), |
|
186 | + // array(), |
|
187 | + // CoffeeMaker::BREW_SHARED, |
|
188 | 188 | // array( |
189 | 189 | // EE_ADMIN_PAGES . 'transactions' . DS . '*.core.php', |
190 | 190 | // ) |
@@ -194,23 +194,23 @@ discard block |
||
194 | 194 | |
195 | 195 | |
196 | 196 | |
197 | - /** |
|
198 | - * bootstrap EE and the request stack |
|
199 | - * |
|
200 | - * @throws ServiceNotFoundException |
|
201 | - * @throws InvalidClassException |
|
202 | - * @throws InvalidDataTypeException |
|
203 | - * @throws InvalidIdentifierException |
|
204 | - * @throws exceptions\ServiceExistsException |
|
205 | - * @throws OutOfBoundsException |
|
206 | - * @throws exceptions\InstantiationException |
|
207 | - */ |
|
208 | - public function firstBrew() |
|
209 | - { |
|
210 | - $this->CoffeeShop->brew('EE_Request', array($_GET, $_POST, $_COOKIE)); |
|
211 | - $this->CoffeeShop->brew('EE_Response'); |
|
212 | - $this->CoffeeShop->brew('EE_Bootstrap'); |
|
213 | - } |
|
197 | + /** |
|
198 | + * bootstrap EE and the request stack |
|
199 | + * |
|
200 | + * @throws ServiceNotFoundException |
|
201 | + * @throws InvalidClassException |
|
202 | + * @throws InvalidDataTypeException |
|
203 | + * @throws InvalidIdentifierException |
|
204 | + * @throws exceptions\ServiceExistsException |
|
205 | + * @throws OutOfBoundsException |
|
206 | + * @throws exceptions\InstantiationException |
|
207 | + */ |
|
208 | + public function firstBrew() |
|
209 | + { |
|
210 | + $this->CoffeeShop->brew('EE_Request', array($_GET, $_POST, $_COOKIE)); |
|
211 | + $this->CoffeeShop->brew('EE_Response'); |
|
212 | + $this->CoffeeShop->brew('EE_Bootstrap'); |
|
213 | + } |
|
214 | 214 | |
215 | 215 | |
216 | 216 | } |
@@ -132,8 +132,8 @@ discard block |
||
132 | 132 | array(), |
133 | 133 | CoffeeMaker::BREW_LOAD_ONLY, |
134 | 134 | array( |
135 | - EE_INTERFACES . '*.php', |
|
136 | - EE_INTERFACES . '*.interfaces.php', |
|
135 | + EE_INTERFACES.'*.php', |
|
136 | + EE_INTERFACES.'*.interfaces.php', |
|
137 | 137 | ) |
138 | 138 | ) |
139 | 139 | ); |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | array(), |
146 | 146 | array(), |
147 | 147 | CoffeeMaker::BREW_SHARED, |
148 | - EE_MODELS . '*.model.php' |
|
148 | + EE_MODELS.'*.model.php' |
|
149 | 149 | ) |
150 | 150 | ); |
151 | 151 | // add a wildcard recipe for loading core classes |
@@ -157,12 +157,12 @@ discard block |
||
157 | 157 | array(), |
158 | 158 | CoffeeMaker::BREW_SHARED, |
159 | 159 | array( |
160 | - EE_CORE . '*.core.php', |
|
161 | - EE_ADMIN . '*.core.php', |
|
162 | - EE_CPTS . '*.core.php', |
|
163 | - EE_CORE . 'data_migration_scripts' . DS . '*.core.php', |
|
164 | - EE_CORE . 'request_stack' . DS . '*.core.php', |
|
165 | - EE_CORE . 'middleware' . DS . '*.core.php', |
|
160 | + EE_CORE.'*.core.php', |
|
161 | + EE_ADMIN.'*.core.php', |
|
162 | + EE_CPTS.'*.core.php', |
|
163 | + EE_CORE.'data_migration_scripts'.DS.'*.core.php', |
|
164 | + EE_CORE.'request_stack'.DS.'*.core.php', |
|
165 | + EE_CORE.'middleware'.DS.'*.core.php', |
|
166 | 166 | ) |
167 | 167 | ) |
168 | 168 | ); |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | array(), |
175 | 175 | array(), |
176 | 176 | CoffeeMaker::BREW_LOAD_ONLY, |
177 | - array( EE_ADMIN . '*.core.php' ) |
|
177 | + array(EE_ADMIN.'*.core.php') |
|
178 | 178 | ) |
179 | 179 | ); |
180 | 180 | // add a wildcard recipe for loading core classes |
@@ -9,69 +9,69 @@ |
||
9 | 9 | interface EEI_Attendee |
10 | 10 | { |
11 | 11 | |
12 | - public function fname(); |
|
12 | + public function fname(); |
|
13 | 13 | |
14 | 14 | |
15 | 15 | |
16 | - public function lname(); |
|
16 | + public function lname(); |
|
17 | 17 | |
18 | 18 | |
19 | 19 | |
20 | - public function full_name(); |
|
20 | + public function full_name(); |
|
21 | 21 | |
22 | 22 | |
23 | 23 | |
24 | - public function email(); |
|
24 | + public function email(); |
|
25 | 25 | |
26 | 26 | |
27 | 27 | |
28 | - public function phone(); |
|
28 | + public function phone(); |
|
29 | 29 | |
30 | 30 | |
31 | 31 | |
32 | - public function address(); |
|
32 | + public function address(); |
|
33 | 33 | |
34 | 34 | |
35 | 35 | |
36 | - public function address2(); |
|
36 | + public function address2(); |
|
37 | 37 | |
38 | 38 | |
39 | 39 | |
40 | - public function city(); |
|
40 | + public function city(); |
|
41 | 41 | |
42 | 42 | |
43 | 43 | |
44 | - public function state_ID(); |
|
44 | + public function state_ID(); |
|
45 | 45 | |
46 | 46 | |
47 | 47 | |
48 | - public function state_name(); |
|
48 | + public function state_name(); |
|
49 | 49 | |
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * @return EE_State |
|
54 | - */ |
|
55 | - public function state_obj(); |
|
52 | + /** |
|
53 | + * @return EE_State |
|
54 | + */ |
|
55 | + public function state_obj(); |
|
56 | 56 | |
57 | 57 | |
58 | 58 | |
59 | - public function country_ID(); |
|
59 | + public function country_ID(); |
|
60 | 60 | |
61 | 61 | |
62 | 62 | |
63 | - public function country_name(); |
|
63 | + public function country_name(); |
|
64 | 64 | |
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * @return EE_Country |
|
69 | - */ |
|
70 | - public function country_obj(); |
|
67 | + /** |
|
68 | + * @return EE_Country |
|
69 | + */ |
|
70 | + public function country_obj(); |
|
71 | 71 | |
72 | 72 | |
73 | 73 | |
74 | - public function zip(); |
|
74 | + public function zip(); |
|
75 | 75 | |
76 | 76 | } |
77 | 77 | // End of file EEI_Attendee.interface.php |
@@ -9,90 +9,90 @@ |
||
9 | 9 | interface EEHI_Line_Item |
10 | 10 | { |
11 | 11 | |
12 | - /** |
|
13 | - * Adds an item to the purchase in the right spot |
|
14 | - * |
|
15 | - * @param EE_Line_Item $total_line_item |
|
16 | - * @param EE_Line_Item $line_item |
|
17 | - */ |
|
18 | - public function add_item(EE_Line_Item $total_line_item, EE_Line_Item $line_item); |
|
19 | - |
|
20 | - |
|
21 | - |
|
22 | - /** |
|
23 | - * Overwrites the previous tax by clearing out the old taxes, and creates a new |
|
24 | - * tax and updates the total line item accordingly |
|
25 | - * |
|
26 | - * @param EE_Line_Item $total_line_item |
|
27 | - * @param float $amount |
|
28 | - * @param string $name |
|
29 | - * @param string $description |
|
30 | - * @param string $code |
|
31 | - * @param boolean $add_to_existing_line_item if true and a duplicate line item with |
|
32 | - * the same code is found, $amount will be added onto it; otherwise will simply |
|
33 | - * set the taxes to match $amount |
|
34 | - * @return EE_Line_Item the new tax created |
|
35 | - */ |
|
36 | - public function set_total_tax_to( |
|
37 | - EE_Line_Item $total_line_item, |
|
38 | - $amount, |
|
39 | - $name = null, |
|
40 | - $description = null, |
|
41 | - $code = null, |
|
42 | - $add_to_existing_line_item = false |
|
43 | - ); |
|
44 | - |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * Makes all the line items which are children of $line_item taxable (or not). |
|
49 | - * Does NOT save the line items |
|
50 | - * |
|
51 | - * @param EE_Line_Item $line_item |
|
52 | - * @param boolean $taxable |
|
53 | - * @param string $code_substring_for_whitelist if this string is part of the line item's code |
|
54 | - * it will be whitelisted (ie, except from becoming taxable) |
|
55 | - */ |
|
56 | - public static function set_line_items_taxable( |
|
57 | - EE_Line_Item $line_item, |
|
58 | - $taxable = true, |
|
59 | - $code_substring_for_whitelist = null |
|
60 | - ); |
|
61 | - |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Adds a simple item ( unrelated to any other model object) to the total line item, |
|
66 | - * in the correct spot in the line item tree. |
|
67 | - * |
|
68 | - * @param EE_Line_Item $total_line_item |
|
69 | - * @param string $name |
|
70 | - * @param float $unit_price |
|
71 | - * @param string $description |
|
72 | - * @param int $quantity |
|
73 | - * @param boolean $taxable |
|
74 | - * @param boolean $code if set to a value, ensures there is only one line item with that code |
|
75 | - * @return boolean success |
|
76 | - */ |
|
77 | - public function add_unrelated_item( |
|
78 | - EE_Line_Item $total_line_item, |
|
79 | - $name, |
|
80 | - $unit_price, |
|
81 | - $description = '', |
|
82 | - $quantity = 1, |
|
83 | - $taxable = false, |
|
84 | - $code = null |
|
85 | - ); |
|
86 | - |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Gets the line item for the taxes subtotal |
|
91 | - * |
|
92 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
93 | - * @return \EE_Line_Item |
|
94 | - */ |
|
95 | - public static function get_taxes_subtotal(EE_Line_Item $total_line_item); |
|
12 | + /** |
|
13 | + * Adds an item to the purchase in the right spot |
|
14 | + * |
|
15 | + * @param EE_Line_Item $total_line_item |
|
16 | + * @param EE_Line_Item $line_item |
|
17 | + */ |
|
18 | + public function add_item(EE_Line_Item $total_line_item, EE_Line_Item $line_item); |
|
19 | + |
|
20 | + |
|
21 | + |
|
22 | + /** |
|
23 | + * Overwrites the previous tax by clearing out the old taxes, and creates a new |
|
24 | + * tax and updates the total line item accordingly |
|
25 | + * |
|
26 | + * @param EE_Line_Item $total_line_item |
|
27 | + * @param float $amount |
|
28 | + * @param string $name |
|
29 | + * @param string $description |
|
30 | + * @param string $code |
|
31 | + * @param boolean $add_to_existing_line_item if true and a duplicate line item with |
|
32 | + * the same code is found, $amount will be added onto it; otherwise will simply |
|
33 | + * set the taxes to match $amount |
|
34 | + * @return EE_Line_Item the new tax created |
|
35 | + */ |
|
36 | + public function set_total_tax_to( |
|
37 | + EE_Line_Item $total_line_item, |
|
38 | + $amount, |
|
39 | + $name = null, |
|
40 | + $description = null, |
|
41 | + $code = null, |
|
42 | + $add_to_existing_line_item = false |
|
43 | + ); |
|
44 | + |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * Makes all the line items which are children of $line_item taxable (or not). |
|
49 | + * Does NOT save the line items |
|
50 | + * |
|
51 | + * @param EE_Line_Item $line_item |
|
52 | + * @param boolean $taxable |
|
53 | + * @param string $code_substring_for_whitelist if this string is part of the line item's code |
|
54 | + * it will be whitelisted (ie, except from becoming taxable) |
|
55 | + */ |
|
56 | + public static function set_line_items_taxable( |
|
57 | + EE_Line_Item $line_item, |
|
58 | + $taxable = true, |
|
59 | + $code_substring_for_whitelist = null |
|
60 | + ); |
|
61 | + |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Adds a simple item ( unrelated to any other model object) to the total line item, |
|
66 | + * in the correct spot in the line item tree. |
|
67 | + * |
|
68 | + * @param EE_Line_Item $total_line_item |
|
69 | + * @param string $name |
|
70 | + * @param float $unit_price |
|
71 | + * @param string $description |
|
72 | + * @param int $quantity |
|
73 | + * @param boolean $taxable |
|
74 | + * @param boolean $code if set to a value, ensures there is only one line item with that code |
|
75 | + * @return boolean success |
|
76 | + */ |
|
77 | + public function add_unrelated_item( |
|
78 | + EE_Line_Item $total_line_item, |
|
79 | + $name, |
|
80 | + $unit_price, |
|
81 | + $description = '', |
|
82 | + $quantity = 1, |
|
83 | + $taxable = false, |
|
84 | + $code = null |
|
85 | + ); |
|
86 | + |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Gets the line item for the taxes subtotal |
|
91 | + * |
|
92 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
93 | + * @return \EE_Line_Item |
|
94 | + */ |
|
95 | + public static function get_taxes_subtotal(EE_Line_Item $total_line_item); |
|
96 | 96 | } |
97 | 97 | // End of file EEHI_Line_Item.interface.php |
98 | 98 | // Location: core/interfaces/line_items/EEHI_Line_Item.interface.php |
99 | 99 | \ No newline at end of file |
@@ -9,12 +9,12 @@ |
||
9 | 9 | interface EEI_Line_Item_Display |
10 | 10 | { |
11 | 11 | |
12 | - /** |
|
13 | - * @param EE_Line_Item $line_item |
|
14 | - * @param array $options |
|
15 | - * @return mixed |
|
16 | - */ |
|
17 | - public function display_line_item(EE_Line_Item $line_item, $options = array()); |
|
12 | + /** |
|
13 | + * @param EE_Line_Item $line_item |
|
14 | + * @param array $options |
|
15 | + * @return mixed |
|
16 | + */ |
|
17 | + public function display_line_item(EE_Line_Item $line_item, $options = array()); |
|
18 | 18 | |
19 | 19 | } |
20 | 20 |