Completed
Branch master (8b94c8)
by
unknown
04:54
created
core/services/container/CoffeeMaker.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
     public static function validateType($type)
69 69
     {
70 70
         $types = CoffeeMaker::getTypes();
71
-        if (! in_array($type, $types, true)) {
71
+        if ( ! in_array($type, $types, true)) {
72 72
             throw new InvalidIdentifierException(
73 73
                 is_object($type) ? get_class($type) : gettype($type),
74 74
                 esc_html__(
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
     protected function resolveClassAndFilepath(RecipeInterface $recipe)
152 152
     {
153 153
         $paths = $recipe->paths();
154
-        if (! empty($paths)) {
154
+        if ( ! empty($paths)) {
155 155
             foreach ($paths as $path) {
156 156
                 if (strpos($path, '*') === false && is_readable($path)) {
157 157
                     require_once($path);
Please login to merge, or discard this patch.
Indentation   +152 added lines, -152 removed lines patch added patch discarded remove patch
@@ -17,156 +17,156 @@
 block discarded – undo
17 17
  */
18 18
 abstract class CoffeeMaker implements CoffeeMakerInterface
19 19
 {
20
-    /**
21
-     * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given)
22
-     */
23
-    const BREW_NEW = 'new';
24
-
25
-    /**
26
-     * Indicates that CoffeeMaker should always return a SHARED instance
27
-     */
28
-    const BREW_SHARED = 'shared';
29
-
30
-    /**
31
-     * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate
32
-     */
33
-    const BREW_LOAD_ONLY = 'load_only';
34
-
35
-
36
-    /**
37
-     * @var CoffeePotInterface $coffee_pot
38
-     */
39
-    private $coffee_pot;
40
-
41
-    /**
42
-     * @var DependencyInjector $injector
43
-     */
44
-    private $injector;
45
-
46
-
47
-    /**
48
-     * @return array
49
-     */
50
-    public static function getTypes()
51
-    {
52
-        return (array) apply_filters(
53
-            'FHEE__EventEspresso\core\services\container\CoffeeMaker__getTypes',
54
-            array(
55
-                CoffeeMaker::BREW_NEW,
56
-                CoffeeMaker::BREW_SHARED,
57
-                CoffeeMaker::BREW_LOAD_ONLY,
58
-            )
59
-        );
60
-    }
61
-
62
-
63
-    /**
64
-     * @param $type
65
-     * @throws \EventEspresso\core\exceptions\InvalidIdentifierException
66
-     */
67
-    public static function validateType($type)
68
-    {
69
-        $types = CoffeeMaker::getTypes();
70
-        if (! in_array($type, $types, true)) {
71
-            throw new InvalidIdentifierException(
72
-                is_object($type) ? get_class($type) : gettype($type),
73
-                esc_html__(
74
-                    'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)',
75
-                    'event_espresso'
76
-                )
77
-            );
78
-        }
79
-        return $type;
80
-    }
81
-
82
-
83
-    /**
84
-     * CoffeeMaker constructor.
85
-     *
86
-     * @param CoffeePotInterface $coffee_pot
87
-     * @param InjectorInterface  $injector
88
-     */
89
-    public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector)
90
-    {
91
-        $this->coffee_pot = $coffee_pot;
92
-        $this->injector = $injector;
93
-    }
94
-
95
-
96
-    /**
97
-     * @return \EventEspresso\core\services\container\CoffeePotInterface
98
-     */
99
-    protected function coffeePot()
100
-    {
101
-        return $this->coffee_pot;
102
-    }
103
-
104
-
105
-    /**
106
-     * @return \EventEspresso\core\services\container\DependencyInjector
107
-     */
108
-    protected function injector()
109
-    {
110
-        return $this->injector;
111
-    }
112
-
113
-
114
-    /**
115
-     * Examines the constructor to determine which method should be used for instantiation
116
-     *
117
-     * @param \ReflectionClass $reflector
118
-     * @return mixed
119
-     * @throws InstantiationException
120
-     */
121
-    protected function resolveInstantiationMethod(\ReflectionClass $reflector)
122
-    {
123
-        if ($reflector->getConstructor() === null) {
124
-            return 'NewInstance';
125
-        }
126
-        if ($reflector->isInstantiable()) {
127
-            return 'NewInstanceArgs';
128
-        }
129
-        if (method_exists($reflector->getName(), 'instance')) {
130
-            return 'instance';
131
-        }
132
-        if (method_exists($reflector->getName(), 'new_instance')) {
133
-            return 'new_instance';
134
-        }
135
-        if (method_exists($reflector->getName(), 'new_instance_from_db')) {
136
-            return 'new_instance_from_db';
137
-        }
138
-        throw new InstantiationException($reflector->getName());
139
-    }
140
-
141
-
142
-    /**
143
-     * Ensures files for classes that are not PSR-4 compatible are loaded
144
-     * and then verifies that classes exist where applicable
145
-     *
146
-     * @param RecipeInterface $recipe
147
-     * @return bool
148
-     * @throws InvalidClassException
149
-     */
150
-    protected function resolveClassAndFilepath(RecipeInterface $recipe)
151
-    {
152
-        $paths = $recipe->paths();
153
-        if (! empty($paths)) {
154
-            foreach ($paths as $path) {
155
-                if (strpos($path, '*') === false && is_readable($path)) {
156
-                    require_once($path);
157
-                }
158
-            }
159
-        }
160
-        // re: using "false" for class_exists() second param:
161
-        // if a class name is not already known to PHP, then class_exists() will run through
162
-        // all of the registered spl_autoload functions until it either finds the class,
163
-        // or gets to the end of the registered spl_autoload functions.
164
-        // When the second parameter is true, it will also attempt to load the class file,
165
-        // but it will also trigger an error if the class can not be loaded.
166
-        // We don't want that extra error in the mix, so we have set the second param to "false"
167
-        if ($recipe->type() !== CoffeeMaker::BREW_LOAD_ONLY && ! class_exists($recipe->fqcn(), false)) {
168
-            throw new InvalidClassException($recipe->identifier());
169
-        }
170
-        return true;
171
-    }
20
+	/**
21
+	 * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given)
22
+	 */
23
+	const BREW_NEW = 'new';
24
+
25
+	/**
26
+	 * Indicates that CoffeeMaker should always return a SHARED instance
27
+	 */
28
+	const BREW_SHARED = 'shared';
29
+
30
+	/**
31
+	 * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate
32
+	 */
33
+	const BREW_LOAD_ONLY = 'load_only';
34
+
35
+
36
+	/**
37
+	 * @var CoffeePotInterface $coffee_pot
38
+	 */
39
+	private $coffee_pot;
40
+
41
+	/**
42
+	 * @var DependencyInjector $injector
43
+	 */
44
+	private $injector;
45
+
46
+
47
+	/**
48
+	 * @return array
49
+	 */
50
+	public static function getTypes()
51
+	{
52
+		return (array) apply_filters(
53
+			'FHEE__EventEspresso\core\services\container\CoffeeMaker__getTypes',
54
+			array(
55
+				CoffeeMaker::BREW_NEW,
56
+				CoffeeMaker::BREW_SHARED,
57
+				CoffeeMaker::BREW_LOAD_ONLY,
58
+			)
59
+		);
60
+	}
61
+
62
+
63
+	/**
64
+	 * @param $type
65
+	 * @throws \EventEspresso\core\exceptions\InvalidIdentifierException
66
+	 */
67
+	public static function validateType($type)
68
+	{
69
+		$types = CoffeeMaker::getTypes();
70
+		if (! in_array($type, $types, true)) {
71
+			throw new InvalidIdentifierException(
72
+				is_object($type) ? get_class($type) : gettype($type),
73
+				esc_html__(
74
+					'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)',
75
+					'event_espresso'
76
+				)
77
+			);
78
+		}
79
+		return $type;
80
+	}
81
+
82
+
83
+	/**
84
+	 * CoffeeMaker constructor.
85
+	 *
86
+	 * @param CoffeePotInterface $coffee_pot
87
+	 * @param InjectorInterface  $injector
88
+	 */
89
+	public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector)
90
+	{
91
+		$this->coffee_pot = $coffee_pot;
92
+		$this->injector = $injector;
93
+	}
94
+
95
+
96
+	/**
97
+	 * @return \EventEspresso\core\services\container\CoffeePotInterface
98
+	 */
99
+	protected function coffeePot()
100
+	{
101
+		return $this->coffee_pot;
102
+	}
103
+
104
+
105
+	/**
106
+	 * @return \EventEspresso\core\services\container\DependencyInjector
107
+	 */
108
+	protected function injector()
109
+	{
110
+		return $this->injector;
111
+	}
112
+
113
+
114
+	/**
115
+	 * Examines the constructor to determine which method should be used for instantiation
116
+	 *
117
+	 * @param \ReflectionClass $reflector
118
+	 * @return mixed
119
+	 * @throws InstantiationException
120
+	 */
121
+	protected function resolveInstantiationMethod(\ReflectionClass $reflector)
122
+	{
123
+		if ($reflector->getConstructor() === null) {
124
+			return 'NewInstance';
125
+		}
126
+		if ($reflector->isInstantiable()) {
127
+			return 'NewInstanceArgs';
128
+		}
129
+		if (method_exists($reflector->getName(), 'instance')) {
130
+			return 'instance';
131
+		}
132
+		if (method_exists($reflector->getName(), 'new_instance')) {
133
+			return 'new_instance';
134
+		}
135
+		if (method_exists($reflector->getName(), 'new_instance_from_db')) {
136
+			return 'new_instance_from_db';
137
+		}
138
+		throw new InstantiationException($reflector->getName());
139
+	}
140
+
141
+
142
+	/**
143
+	 * Ensures files for classes that are not PSR-4 compatible are loaded
144
+	 * and then verifies that classes exist where applicable
145
+	 *
146
+	 * @param RecipeInterface $recipe
147
+	 * @return bool
148
+	 * @throws InvalidClassException
149
+	 */
150
+	protected function resolveClassAndFilepath(RecipeInterface $recipe)
151
+	{
152
+		$paths = $recipe->paths();
153
+		if (! empty($paths)) {
154
+			foreach ($paths as $path) {
155
+				if (strpos($path, '*') === false && is_readable($path)) {
156
+					require_once($path);
157
+				}
158
+			}
159
+		}
160
+		// re: using "false" for class_exists() second param:
161
+		// if a class name is not already known to PHP, then class_exists() will run through
162
+		// all of the registered spl_autoload functions until it either finds the class,
163
+		// or gets to the end of the registered spl_autoload functions.
164
+		// When the second parameter is true, it will also attempt to load the class file,
165
+		// but it will also trigger an error if the class can not be loaded.
166
+		// We don't want that extra error in the mix, so we have set the second param to "false"
167
+		if ($recipe->type() !== CoffeeMaker::BREW_LOAD_ONLY && ! class_exists($recipe->fqcn(), false)) {
168
+			throw new InvalidClassException($recipe->identifier());
169
+		}
170
+		return true;
171
+	}
172 172
 }
Please login to merge, or discard this patch.
core/services/container/Recipe.php 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -189,7 +189,7 @@  discard block
 block discarded – undo
189 189
      */
190 190
     public function setIdentifier($identifier)
191 191
     {
192
-        if (! is_string($identifier) || empty($identifier)) {
192
+        if ( ! is_string($identifier) || empty($identifier)) {
193 193
             throw new InvalidIdentifierException(
194 194
                 is_object($identifier) ? get_class($identifier) : gettype($identifier),
195 195
                 esc_html__('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso')
@@ -216,7 +216,7 @@  discard block
 block discarded – undo
216 216
     public function setFqcn($fqcn)
217 217
     {
218 218
         $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier;
219
-        if (! is_string($fqcn)) {
219
+        if ( ! is_string($fqcn)) {
220 220
             throw new InvalidDataTypeException(
221 221
                 '$fqcn',
222 222
                 is_object($fqcn) ? get_class($fqcn) : gettype($fqcn),
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
         if (empty($ingredients)) {
248 248
             return;
249 249
         }
250
-        if (! is_array($ingredients)) {
250
+        if ( ! is_array($ingredients)) {
251 251
             throw new InvalidDataTypeException(
252 252
                 '$ingredients',
253 253
                 is_object($ingredients) ? get_class($ingredients) : gettype($ingredients),
@@ -279,7 +279,7 @@  discard block
 block discarded – undo
279 279
         if (empty($filters)) {
280 280
             return;
281 281
         }
282
-        if (! is_array($filters)) {
282
+        if ( ! is_array($filters)) {
283 283
             throw new InvalidDataTypeException(
284 284
                 '$filters',
285 285
                 is_object($filters) ? get_class($filters) : gettype($filters),
@@ -306,7 +306,7 @@  discard block
 block discarded – undo
306 306
         if (empty($paths)) {
307 307
             return;
308 308
         }
309
-        if (! (is_string($paths) || is_array($paths))) {
309
+        if ( ! (is_string($paths) || is_array($paths))) {
310 310
             throw new InvalidDataTypeException(
311 311
                 '$path',
312 312
                 is_object($paths) ? get_class($paths) : gettype($paths),
Please login to merge, or discard this patch.
Indentation   +307 added lines, -307 removed lines patch added patch discarded remove patch
@@ -18,311 +18,311 @@
 block discarded – undo
18 18
  */
19 19
 class Recipe implements RecipeInterface
20 20
 {
21
-    /**
22
-     * A default Recipe to use if none is specified for a class
23
-     */
24
-    const DEFAULT_ID = '*';
25
-
26
-    /**
27
-     * Identifier for the entity class to be constructed.
28
-     * Typically a Fully Qualified Class Name
29
-     *
30
-     * @var string $identifier
31
-     */
32
-    private $identifier;
33
-
34
-    /**
35
-     * Fully Qualified Class Name
36
-     *
37
-     * @var string $fqcn
38
-     */
39
-    private $fqcn;
40
-
41
-    /**
42
-     * a dependency class map array
43
-     * If a Recipe is for a single class (or group of classes that shares the EXACT SAME constructor arguments),
44
-     * and that class type hints for an interface, then this property allows you to configure what dependencies
45
-     * get used when instantiating the class.
46
-     * For example:
47
-     *  There's a class called Coffee, and one of its constructor arguments is BeanInterface
48
-     *  There are two implementations of BeanInterface: HonduranBean, and KenyanBean
49
-     *  We want one Coffee object to use HonduranBean for its BeanInterface,
50
-     *  and the 2nd Coffee object to use KenyanBean for its BeanInterface.
51
-     *  To do this, we need to create two Recipes:
52
-     *      one with an identifier of 'HonduranCoffee' using the following ingredients :
53
-     *          array('BeanInterface' => 'HonduranBean')
54
-     *      and the other with an identifier of 'KenyanCoffee' using the following ingredients :
55
-     *          array('BeanInterface' => 'KenyanBean')
56
-     *  Then, whenever the CoffeeShop brews an instance of HonduranCoffee,
57
-     *  an instance of HonduranBean will get injected for the BeanInterface dependency,
58
-     *  and whenever the CoffeeShop brews an instance of KenyanCoffee,
59
-     *  an instance of KenyanBean will get injected for the BeanInterface dependency
60
-     *
61
-     * @var array $ingredients
62
-     */
63
-    private $ingredients = array();
64
-
65
-    /**
66
-     * one of the class constants from CoffeeShop:
67
-     *  CoffeeMaker::BREW_NEW - creates a new instance
68
-     *  CoffeeMaker::BREW_SHARED - creates a shared instance
69
-     *  CoffeeMaker::BREW_LOAD_ONLY - loads but does not instantiate
70
-     *
71
-     * @var string $type
72
-     */
73
-    private $type;
74
-
75
-    /**
76
-     * class name aliases - typically a Fully Qualified Interface that the class implements
77
-     * identifiers passed to the CoffeeShop will be run through the filters to find the correct class name
78
-     *
79
-     * @var array $filters
80
-     */
81
-    private $filters = array();
82
-
83
-    /**
84
-     * array of full server filepaths to files that may contain the class
85
-     *
86
-     * @var array $paths
87
-     */
88
-    private $paths = array();
89
-
90
-
91
-    /**
92
-     * Recipe constructor.
93
-     *
94
-     * @param string $identifier    class identifier, can be an alias, or FQCN, or whatever
95
-     * @param string $fqcn          \Fully\Qualified\ClassName, optional if $identifier is FQCN
96
-     * @param array  $ingredients   array of dependencies that can not be resolved automatically,
97
-     *                              used for resolving concrete classes for type hinted interfaces
98
-     *                              for the dependencies of THIS class
99
-     * @param string $type          recipe type: one of the class constants on
100
-     *                              \EventEspresso\core\services\container\CoffeeMaker
101
-     * @param array  $filters       array of class aliases, or class interfaces
102
-     *                              this works somewhat opposite to the $ingredients array above,
103
-     *                              in that this array specifies interfaces or aliases
104
-     *                              that this Recipe can be used for when resolving OTHER class's dependencies
105
-     * @param array  $paths         if class can not be loaded via PSR-4 autoloading,
106
-     *                              then supply a filepath, or array of filepaths, so that it can be included
107
-     * @throws InvalidIdentifierException
108
-     * @throws RuntimeException
109
-     * @throws InvalidInterfaceException
110
-     * @throws InvalidClassException
111
-     * @throws InvalidDataTypeException
112
-     */
113
-    public function __construct(
114
-        $identifier,
115
-        $fqcn = '',
116
-        array $filters = array(),
117
-        array $ingredients = array(),
118
-        $type = CoffeeMaker::BREW_NEW,
119
-        array $paths = array()
120
-    ) {
121
-        $this->setIdentifier($identifier);
122
-        $this->setFilters($filters);
123
-        $this->setIngredients($ingredients);
124
-        $this->setType($type);
125
-        $this->setPaths($paths);
126
-        $this->setFqcn($fqcn);
127
-    }
128
-
129
-
130
-    /**
131
-     * @return string
132
-     */
133
-    public function identifier()
134
-    {
135
-        return $this->identifier;
136
-    }
137
-
138
-
139
-    /**
140
-     * @return string
141
-     */
142
-    public function fqcn()
143
-    {
144
-        return $this->fqcn;
145
-    }
146
-
147
-
148
-    /**
149
-     * @return array
150
-     */
151
-    public function filters()
152
-    {
153
-        return $this->filters;
154
-    }
155
-
156
-
157
-    /**
158
-     * @return array
159
-     */
160
-    public function ingredients()
161
-    {
162
-        return $this->ingredients;
163
-    }
164
-
165
-
166
-    /**
167
-     * @return string
168
-     */
169
-    public function type()
170
-    {
171
-        return $this->type;
172
-    }
173
-
174
-
175
-    /**
176
-     * @return array
177
-     */
178
-    public function paths()
179
-    {
180
-        return $this->paths;
181
-    }
182
-
183
-
184
-    /**
185
-     * @param  string $identifier Identifier for the entity class that the Recipe applies to
186
-     *                            Typically a Fully Qualified Class Name
187
-     * @throws InvalidIdentifierException
188
-     */
189
-    public function setIdentifier($identifier)
190
-    {
191
-        if (! is_string($identifier) || empty($identifier)) {
192
-            throw new InvalidIdentifierException(
193
-                is_object($identifier) ? get_class($identifier) : gettype($identifier),
194
-                esc_html__('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso')
195
-            );
196
-        }
197
-        $this->identifier = $identifier;
198
-    }
199
-
200
-
201
-    /**
202
-     * Ensures incoming string is a valid Fully Qualified Class Name,
203
-     * except if this is the default wildcard Recipe ( * ),
204
-     * or it's NOT an actual FQCN because the Recipe is using filepaths
205
-     * for classes that are not PSR-4 compatible
206
-     * PLZ NOTE:
207
-     *  Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not,
208
-     *  therefore you should always call Recipe::setPaths() before Recipe::setFqcn()
209
-     *
210
-     * @param string $fqcn
211
-     * @throws InvalidDataTypeException
212
-     * @throws InvalidClassException
213
-     * @throws InvalidInterfaceException
214
-     */
215
-    public function setFqcn($fqcn)
216
-    {
217
-        $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier;
218
-        if (! is_string($fqcn)) {
219
-            throw new InvalidDataTypeException(
220
-                '$fqcn',
221
-                is_object($fqcn) ? get_class($fqcn) : gettype($fqcn),
222
-                esc_html__('string (Fully\Qualified\ClassName)', 'event_espresso')
223
-            );
224
-        }
225
-        $fqcn = ltrim($fqcn, '\\');
226
-        if (
227
-            $fqcn !== Recipe::DEFAULT_ID
228
-            && ! empty($fqcn)
229
-            && empty($this->paths)
230
-            && ! (class_exists($fqcn) || interface_exists($fqcn))
231
-        ) {
232
-            throw new InvalidClassException($fqcn);
233
-        }
234
-        $this->fqcn = $fqcn;
235
-    }
236
-
237
-
238
-    /**
239
-     * @param array $ingredients    an array of dependencies where keys are the aliases and values are the FQCNs
240
-     *                              example:
241
-     *                              array( 'ClassInterface' => 'Fully\Qualified\ClassName' )
242
-     * @throws InvalidDataTypeException
243
-     */
244
-    public function setIngredients(array $ingredients)
245
-    {
246
-        if (empty($ingredients)) {
247
-            return;
248
-        }
249
-        if (! is_array($ingredients)) {
250
-            throw new InvalidDataTypeException(
251
-                '$ingredients',
252
-                is_object($ingredients) ? get_class($ingredients) : gettype($ingredients),
253
-                esc_html__('array of class dependencies', 'event_espresso')
254
-            );
255
-        }
256
-        $this->ingredients = array_merge($this->ingredients, $ingredients);
257
-    }
258
-
259
-
260
-    /**
261
-     * @param string $type one of the class constants returned from CoffeeMaker::getTypes()
262
-     * @throws InvalidIdentifierException
263
-     */
264
-    public function setType($type = CoffeeMaker::BREW_NEW)
265
-    {
266
-        $this->type = CoffeeMaker::validateType($type);
267
-    }
268
-
269
-
270
-    /**
271
-     * @param array $filters    an array of filters where keys are the aliases and values are the FQCNs
272
-     *                          example:
273
-     *                          array( 'ClassInterface' => 'Fully\Qualified\ClassName' )
274
-     * @throws InvalidDataTypeException
275
-     */
276
-    public function setFilters(array $filters)
277
-    {
278
-        if (empty($filters)) {
279
-            return;
280
-        }
281
-        if (! is_array($filters)) {
282
-            throw new InvalidDataTypeException(
283
-                '$filters',
284
-                is_object($filters) ? get_class($filters) : gettype($filters),
285
-                esc_html__('array of class aliases', 'event_espresso')
286
-            );
287
-        }
288
-        $this->filters = array_merge($this->filters, $filters);
289
-    }
290
-
291
-
292
-    /**
293
-     * Ensures incoming paths is a valid filepath, or array of valid filepaths,
294
-     * and merges them in with any existing filepaths
295
-     * PLZ NOTE:
296
-     *  Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not,
297
-     *  therefore you should always call Recipe::setPaths() before Recipe::setFqcn()
298
-     *
299
-     * @param string|array $paths
300
-     * @throws RuntimeException
301
-     * @throws InvalidDataTypeException
302
-     */
303
-    public function setPaths($paths = array())
304
-    {
305
-        if (empty($paths)) {
306
-            return;
307
-        }
308
-        if (! (is_string($paths) || is_array($paths))) {
309
-            throw new InvalidDataTypeException(
310
-                '$path',
311
-                is_object($paths) ? get_class($paths) : gettype($paths),
312
-                esc_html__('string or array of strings (full server filepath(s))', 'event_espresso')
313
-            );
314
-        }
315
-        $paths = (array) $paths;
316
-        foreach ($paths as $path) {
317
-            if (strpos($path, '*') === false && ! is_readable($path)) {
318
-                throw new RuntimeException(
319
-                    sprintf(
320
-                        esc_html__('The following filepath is not readable: "%1$s"', 'event_espresso'),
321
-                        $path
322
-                    )
323
-                );
324
-            }
325
-        }
326
-        $this->paths = array_merge($this->paths, $paths);
327
-    }
21
+	/**
22
+	 * A default Recipe to use if none is specified for a class
23
+	 */
24
+	const DEFAULT_ID = '*';
25
+
26
+	/**
27
+	 * Identifier for the entity class to be constructed.
28
+	 * Typically a Fully Qualified Class Name
29
+	 *
30
+	 * @var string $identifier
31
+	 */
32
+	private $identifier;
33
+
34
+	/**
35
+	 * Fully Qualified Class Name
36
+	 *
37
+	 * @var string $fqcn
38
+	 */
39
+	private $fqcn;
40
+
41
+	/**
42
+	 * a dependency class map array
43
+	 * If a Recipe is for a single class (or group of classes that shares the EXACT SAME constructor arguments),
44
+	 * and that class type hints for an interface, then this property allows you to configure what dependencies
45
+	 * get used when instantiating the class.
46
+	 * For example:
47
+	 *  There's a class called Coffee, and one of its constructor arguments is BeanInterface
48
+	 *  There are two implementations of BeanInterface: HonduranBean, and KenyanBean
49
+	 *  We want one Coffee object to use HonduranBean for its BeanInterface,
50
+	 *  and the 2nd Coffee object to use KenyanBean for its BeanInterface.
51
+	 *  To do this, we need to create two Recipes:
52
+	 *      one with an identifier of 'HonduranCoffee' using the following ingredients :
53
+	 *          array('BeanInterface' => 'HonduranBean')
54
+	 *      and the other with an identifier of 'KenyanCoffee' using the following ingredients :
55
+	 *          array('BeanInterface' => 'KenyanBean')
56
+	 *  Then, whenever the CoffeeShop brews an instance of HonduranCoffee,
57
+	 *  an instance of HonduranBean will get injected for the BeanInterface dependency,
58
+	 *  and whenever the CoffeeShop brews an instance of KenyanCoffee,
59
+	 *  an instance of KenyanBean will get injected for the BeanInterface dependency
60
+	 *
61
+	 * @var array $ingredients
62
+	 */
63
+	private $ingredients = array();
64
+
65
+	/**
66
+	 * one of the class constants from CoffeeShop:
67
+	 *  CoffeeMaker::BREW_NEW - creates a new instance
68
+	 *  CoffeeMaker::BREW_SHARED - creates a shared instance
69
+	 *  CoffeeMaker::BREW_LOAD_ONLY - loads but does not instantiate
70
+	 *
71
+	 * @var string $type
72
+	 */
73
+	private $type;
74
+
75
+	/**
76
+	 * class name aliases - typically a Fully Qualified Interface that the class implements
77
+	 * identifiers passed to the CoffeeShop will be run through the filters to find the correct class name
78
+	 *
79
+	 * @var array $filters
80
+	 */
81
+	private $filters = array();
82
+
83
+	/**
84
+	 * array of full server filepaths to files that may contain the class
85
+	 *
86
+	 * @var array $paths
87
+	 */
88
+	private $paths = array();
89
+
90
+
91
+	/**
92
+	 * Recipe constructor.
93
+	 *
94
+	 * @param string $identifier    class identifier, can be an alias, or FQCN, or whatever
95
+	 * @param string $fqcn          \Fully\Qualified\ClassName, optional if $identifier is FQCN
96
+	 * @param array  $ingredients   array of dependencies that can not be resolved automatically,
97
+	 *                              used for resolving concrete classes for type hinted interfaces
98
+	 *                              for the dependencies of THIS class
99
+	 * @param string $type          recipe type: one of the class constants on
100
+	 *                              \EventEspresso\core\services\container\CoffeeMaker
101
+	 * @param array  $filters       array of class aliases, or class interfaces
102
+	 *                              this works somewhat opposite to the $ingredients array above,
103
+	 *                              in that this array specifies interfaces or aliases
104
+	 *                              that this Recipe can be used for when resolving OTHER class's dependencies
105
+	 * @param array  $paths         if class can not be loaded via PSR-4 autoloading,
106
+	 *                              then supply a filepath, or array of filepaths, so that it can be included
107
+	 * @throws InvalidIdentifierException
108
+	 * @throws RuntimeException
109
+	 * @throws InvalidInterfaceException
110
+	 * @throws InvalidClassException
111
+	 * @throws InvalidDataTypeException
112
+	 */
113
+	public function __construct(
114
+		$identifier,
115
+		$fqcn = '',
116
+		array $filters = array(),
117
+		array $ingredients = array(),
118
+		$type = CoffeeMaker::BREW_NEW,
119
+		array $paths = array()
120
+	) {
121
+		$this->setIdentifier($identifier);
122
+		$this->setFilters($filters);
123
+		$this->setIngredients($ingredients);
124
+		$this->setType($type);
125
+		$this->setPaths($paths);
126
+		$this->setFqcn($fqcn);
127
+	}
128
+
129
+
130
+	/**
131
+	 * @return string
132
+	 */
133
+	public function identifier()
134
+	{
135
+		return $this->identifier;
136
+	}
137
+
138
+
139
+	/**
140
+	 * @return string
141
+	 */
142
+	public function fqcn()
143
+	{
144
+		return $this->fqcn;
145
+	}
146
+
147
+
148
+	/**
149
+	 * @return array
150
+	 */
151
+	public function filters()
152
+	{
153
+		return $this->filters;
154
+	}
155
+
156
+
157
+	/**
158
+	 * @return array
159
+	 */
160
+	public function ingredients()
161
+	{
162
+		return $this->ingredients;
163
+	}
164
+
165
+
166
+	/**
167
+	 * @return string
168
+	 */
169
+	public function type()
170
+	{
171
+		return $this->type;
172
+	}
173
+
174
+
175
+	/**
176
+	 * @return array
177
+	 */
178
+	public function paths()
179
+	{
180
+		return $this->paths;
181
+	}
182
+
183
+
184
+	/**
185
+	 * @param  string $identifier Identifier for the entity class that the Recipe applies to
186
+	 *                            Typically a Fully Qualified Class Name
187
+	 * @throws InvalidIdentifierException
188
+	 */
189
+	public function setIdentifier($identifier)
190
+	{
191
+		if (! is_string($identifier) || empty($identifier)) {
192
+			throw new InvalidIdentifierException(
193
+				is_object($identifier) ? get_class($identifier) : gettype($identifier),
194
+				esc_html__('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso')
195
+			);
196
+		}
197
+		$this->identifier = $identifier;
198
+	}
199
+
200
+
201
+	/**
202
+	 * Ensures incoming string is a valid Fully Qualified Class Name,
203
+	 * except if this is the default wildcard Recipe ( * ),
204
+	 * or it's NOT an actual FQCN because the Recipe is using filepaths
205
+	 * for classes that are not PSR-4 compatible
206
+	 * PLZ NOTE:
207
+	 *  Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not,
208
+	 *  therefore you should always call Recipe::setPaths() before Recipe::setFqcn()
209
+	 *
210
+	 * @param string $fqcn
211
+	 * @throws InvalidDataTypeException
212
+	 * @throws InvalidClassException
213
+	 * @throws InvalidInterfaceException
214
+	 */
215
+	public function setFqcn($fqcn)
216
+	{
217
+		$fqcn = ! empty($fqcn) ? $fqcn : $this->identifier;
218
+		if (! is_string($fqcn)) {
219
+			throw new InvalidDataTypeException(
220
+				'$fqcn',
221
+				is_object($fqcn) ? get_class($fqcn) : gettype($fqcn),
222
+				esc_html__('string (Fully\Qualified\ClassName)', 'event_espresso')
223
+			);
224
+		}
225
+		$fqcn = ltrim($fqcn, '\\');
226
+		if (
227
+			$fqcn !== Recipe::DEFAULT_ID
228
+			&& ! empty($fqcn)
229
+			&& empty($this->paths)
230
+			&& ! (class_exists($fqcn) || interface_exists($fqcn))
231
+		) {
232
+			throw new InvalidClassException($fqcn);
233
+		}
234
+		$this->fqcn = $fqcn;
235
+	}
236
+
237
+
238
+	/**
239
+	 * @param array $ingredients    an array of dependencies where keys are the aliases and values are the FQCNs
240
+	 *                              example:
241
+	 *                              array( 'ClassInterface' => 'Fully\Qualified\ClassName' )
242
+	 * @throws InvalidDataTypeException
243
+	 */
244
+	public function setIngredients(array $ingredients)
245
+	{
246
+		if (empty($ingredients)) {
247
+			return;
248
+		}
249
+		if (! is_array($ingredients)) {
250
+			throw new InvalidDataTypeException(
251
+				'$ingredients',
252
+				is_object($ingredients) ? get_class($ingredients) : gettype($ingredients),
253
+				esc_html__('array of class dependencies', 'event_espresso')
254
+			);
255
+		}
256
+		$this->ingredients = array_merge($this->ingredients, $ingredients);
257
+	}
258
+
259
+
260
+	/**
261
+	 * @param string $type one of the class constants returned from CoffeeMaker::getTypes()
262
+	 * @throws InvalidIdentifierException
263
+	 */
264
+	public function setType($type = CoffeeMaker::BREW_NEW)
265
+	{
266
+		$this->type = CoffeeMaker::validateType($type);
267
+	}
268
+
269
+
270
+	/**
271
+	 * @param array $filters    an array of filters where keys are the aliases and values are the FQCNs
272
+	 *                          example:
273
+	 *                          array( 'ClassInterface' => 'Fully\Qualified\ClassName' )
274
+	 * @throws InvalidDataTypeException
275
+	 */
276
+	public function setFilters(array $filters)
277
+	{
278
+		if (empty($filters)) {
279
+			return;
280
+		}
281
+		if (! is_array($filters)) {
282
+			throw new InvalidDataTypeException(
283
+				'$filters',
284
+				is_object($filters) ? get_class($filters) : gettype($filters),
285
+				esc_html__('array of class aliases', 'event_espresso')
286
+			);
287
+		}
288
+		$this->filters = array_merge($this->filters, $filters);
289
+	}
290
+
291
+
292
+	/**
293
+	 * Ensures incoming paths is a valid filepath, or array of valid filepaths,
294
+	 * and merges them in with any existing filepaths
295
+	 * PLZ NOTE:
296
+	 *  Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not,
297
+	 *  therefore you should always call Recipe::setPaths() before Recipe::setFqcn()
298
+	 *
299
+	 * @param string|array $paths
300
+	 * @throws RuntimeException
301
+	 * @throws InvalidDataTypeException
302
+	 */
303
+	public function setPaths($paths = array())
304
+	{
305
+		if (empty($paths)) {
306
+			return;
307
+		}
308
+		if (! (is_string($paths) || is_array($paths))) {
309
+			throw new InvalidDataTypeException(
310
+				'$path',
311
+				is_object($paths) ? get_class($paths) : gettype($paths),
312
+				esc_html__('string or array of strings (full server filepath(s))', 'event_espresso')
313
+			);
314
+		}
315
+		$paths = (array) $paths;
316
+		foreach ($paths as $path) {
317
+			if (strpos($path, '*') === false && ! is_readable($path)) {
318
+				throw new RuntimeException(
319
+					sprintf(
320
+						esc_html__('The following filepath is not readable: "%1$s"', 'event_espresso'),
321
+						$path
322
+					)
323
+				);
324
+			}
325
+		}
326
+		$this->paths = array_merge($this->paths, $paths);
327
+	}
328 328
 }
Please login to merge, or discard this patch.
core/services/container/CoffeeShop.php 2 patches
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -159,7 +159,7 @@  discard block
 block discarded – undo
159 159
         }
160 160
         // if the reservoir doesn't have a closure already for the requested identifier,
161 161
         // then neither a shared service nor a closure for making entities has been built yet
162
-        if (! $this->reservoir->has($identifier)) {
162
+        if ( ! $this->reservoir->has($identifier)) {
163 163
             // so let's brew something up and add it to the proper collection
164 164
             $brewed = $this->makeCoffee($identifier, $arguments, $type);
165 165
         }
@@ -284,7 +284,7 @@  discard block
 block discarded – undo
284 284
      */
285 285
     public function addClosure($identifier, $closure)
286 286
     {
287
-        if (! is_callable($closure)) {
287
+        if ( ! is_callable($closure)) {
288 288
             throw new InvalidDataTypeException('$closure', $closure, 'Closure');
289 289
         }
290 290
         $identifier = $this->processIdentifier($identifier);
@@ -393,7 +393,7 @@  discard block
 block discarded – undo
393 393
             // is the wildcard recipe prefix in the identifier ?
394 394
             if (strpos($identifier, $wildcard) !== false) {
395 395
                 // track matches and use the number of wildcard characters matched for the key
396
-                $matches[ strlen($wildcard) ] = $default_recipe;
396
+                $matches[strlen($wildcard)] = $default_recipe;
397 397
             }
398 398
         }
399 399
         if (count($matches) > 0) {
@@ -433,7 +433,7 @@  discard block
 block discarded – undo
433 433
         }
434 434
         $identifier = $this->processIdentifier($identifier);
435 435
         foreach ((array) $aliases as $alias) {
436
-            $this->filters[ $this->processIdentifier($alias) ] = $identifier;
436
+            $this->filters[$this->processIdentifier($alias)] = $identifier;
437 437
         }
438 438
     }
439 439
 
@@ -475,8 +475,8 @@  discard block
 block discarded – undo
475 475
     private function filterIdentifier($identifier)
476 476
     {
477 477
         $identifier = $this->processIdentifier($identifier);
478
-        return isset($this->filters[ $identifier ]) && ! empty($this->filters[ $identifier ])
479
-            ? $this->filters[ $identifier ]
478
+        return isset($this->filters[$identifier]) && ! empty($this->filters[$identifier])
479
+            ? $this->filters[$identifier]
480 480
             : $identifier;
481 481
     }
482 482
 
@@ -491,7 +491,7 @@  discard block
 block discarded – undo
491 491
      */
492 492
     private function processIdentifier($identifier)
493 493
     {
494
-        if (! is_string($identifier)) {
494
+        if ( ! is_string($identifier)) {
495 495
             throw new InvalidIdentifierException(
496 496
                 is_object($identifier) ? get_class($identifier) : gettype($identifier),
497 497
                 '\Fully\Qualified\ClassName'
@@ -510,7 +510,7 @@  discard block
 block discarded – undo
510 510
      */
511 511
     private function getCoffeeMaker($type)
512 512
     {
513
-        if (! $this->coffee_makers->has($type)) {
513
+        if ( ! $this->coffee_makers->has($type)) {
514 514
             throw new OutOfBoundsException(
515 515
                 esc_html__('The requested coffee maker is either missing or invalid.', 'event_espresso')
516 516
             );
@@ -537,7 +537,7 @@  discard block
 block discarded – undo
537 537
             // does this recipe use a wildcard ? (but is NOT the global default)
538 538
             if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) {
539 539
                 // strip the wildcard and use identifier as key
540
-                $default_recipes[ str_replace('*', '', $identifier) ] = $this->recipes->current();
540
+                $default_recipes[str_replace('*', '', $identifier)] = $this->recipes->current();
541 541
             }
542 542
             $this->recipes->next();
543 543
         }
@@ -557,7 +557,7 @@  discard block
 block discarded – undo
557 557
     private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '')
558 558
     {
559 559
         $recipe = clone $default_recipe;
560
-        if (! empty($type)) {
560
+        if ( ! empty($type)) {
561 561
             $recipe->setType($type);
562 562
         }
563 563
         // is this the base default recipe ?
@@ -587,7 +587,7 @@  discard block
 block discarded – undo
587 587
      */
588 588
     private function validateService($identifier, $service)
589 589
     {
590
-        if (! is_object($service)) {
590
+        if ( ! is_object($service)) {
591 591
             throw new InvalidServiceException(
592 592
                 $identifier,
593 593
                 $service
Please login to merge, or discard this patch.
Indentation   +565 added lines, -565 removed lines patch added patch discarded remove patch
@@ -28,569 +28,569 @@
 block discarded – undo
28 28
  */
29 29
 class CoffeeShop implements CoffeePotInterface
30 30
 {
31
-    /**
32
-     * This was the best coffee related name I could think of to represent class name "aliases"
33
-     * So classes can be found via an alias identifier,
34
-     * that is revealed when it is run through... the filters... eh? get it?
35
-     *
36
-     * @var array $filters
37
-     */
38
-    private $filters;
39
-
40
-    /**
41
-     * These are the classes that will actually build the objects (to order of course)
42
-     *
43
-     * @var array $coffee_makers
44
-     */
45
-    private $coffee_makers;
46
-
47
-    /**
48
-     * where the instantiated "singleton" objects are stored
49
-     *
50
-     * @var CollectionInterface $carafe
51
-     */
52
-    private $carafe;
53
-
54
-    /**
55
-     * collection of Recipes that instruct us how to brew objects
56
-     *
57
-     * @var CollectionInterface $recipes
58
-     */
59
-    private $recipes;
60
-
61
-    /**
62
-     * collection of closures for brewing objects
63
-     *
64
-     * @var CollectionInterface $reservoir
65
-     */
66
-    private $reservoir;
67
-
68
-
69
-    /**
70
-     * CoffeeShop constructor
71
-     *
72
-     * @throws InvalidInterfaceException
73
-     */
74
-    public function __construct()
75
-    {
76
-        // array for storing class aliases
77
-        $this->filters = array();
78
-        // create collection for storing shared services
79
-        $this->carafe = new LooseCollection('');
80
-        // create collection for storing recipes that tell us how to build services and entities
81
-        $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface');
82
-        // create collection for storing closures for constructing new entities
83
-        $this->reservoir = new Collection('Closure');
84
-        // create collection for storing the generators that build our services and entity closures
85
-        $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface');
86
-    }
87
-
88
-
89
-    /**
90
-     * Returns true if the container can return an entry for the given identifier.
91
-     * Returns false otherwise.
92
-     * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception.
93
-     * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`.
94
-     *
95
-     * @param string $identifier  Identifier of the entry to look for.
96
-     *                            Typically a Fully Qualified Class Name
97
-     * @return boolean
98
-     * @throws InvalidIdentifierException
99
-     */
100
-    public function has($identifier)
101
-    {
102
-        $identifier = $this->filterIdentifier($identifier);
103
-        return $this->carafe->has($identifier);
104
-    }
105
-
106
-
107
-    /**
108
-     * finds a previously brewed (SHARED) service and returns it
109
-     *
110
-     * @param  string $identifier Identifier for the entity class to be constructed.
111
-     *                            Typically a Fully Qualified Class Name
112
-     * @return mixed
113
-     * @throws InvalidIdentifierException
114
-     * @throws ServiceNotFoundException No service was found for this identifier.
115
-     */
116
-    public function get($identifier)
117
-    {
118
-        $identifier = $this->filterIdentifier($identifier);
119
-        if ($this->carafe->has($identifier)) {
120
-            return $this->carafe->get($identifier);
121
-        }
122
-        throw new ServiceNotFoundException($identifier);
123
-    }
124
-
125
-
126
-    /**
127
-     * returns an instance of the requested entity type using the supplied arguments.
128
-     * If a shared service is requested and an instance is already in the carafe, then it will be returned.
129
-     * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned
130
-     * If the request is for a new entity and a closure exists in the reservoir for creating it,
131
-     * then a new entity will be instantiated from the closure and returned.
132
-     * If a closure does not exist, then one will be built and added to the reservoir
133
-     * before instantiating the requested entity.
134
-     *
135
-     * @param  string $identifier Identifier for the entity class to be constructed.
136
-     *                            Typically a Fully Qualified Class Name
137
-     * @param array   $arguments  an array of arguments to be passed to the entity constructor
138
-     * @param string  $type
139
-     * @return mixed
140
-     * @throws OutOfBoundsException
141
-     * @throws InstantiationException
142
-     * @throws InvalidDataTypeException
143
-     * @throws InvalidClassException
144
-     * @throws InvalidIdentifierException
145
-     * @throws ServiceExistsException
146
-     * @throws ServiceNotFoundException No service was found for this identifier.
147
-     */
148
-    public function brew($identifier, $arguments = array(), $type = '')
149
-    {
150
-        // resolve any class aliases that may exist
151
-        $identifier = $this->filterIdentifier($identifier);
152
-        // is a shared service being requested and already exists in the carafe?
153
-        $brewed = $this->getShared($identifier, $type);
154
-        // then return whatever was found
155
-        if ($brewed !== false) {
156
-            return $brewed;
157
-        }
158
-        // if the reservoir doesn't have a closure already for the requested identifier,
159
-        // then neither a shared service nor a closure for making entities has been built yet
160
-        if (! $this->reservoir->has($identifier)) {
161
-            // so let's brew something up and add it to the proper collection
162
-            $brewed = $this->makeCoffee($identifier, $arguments, $type);
163
-        }
164
-        // did the requested class only require loading, and if so, was that successful?
165
-        if ($this->brewedLoadOnly($brewed, $identifier, $type) === true) {
166
-            return true;
167
-        }
168
-        // was the brewed item a callable factory function ?
169
-        if (is_callable($brewed)) {
170
-            // then instantiate a new entity from the cached closure
171
-            return $brewed($arguments);
172
-        }
173
-        if ($brewed) {
174
-            // requested object was a shared entity, so attempt to get it from the carafe again
175
-            // because if it wasn't there before, then it should have just been brewed and added,
176
-            // but if it still isn't there, then this time the thrown ServiceNotFoundException will not be caught
177
-            return $this->get($identifier);
178
-        }
179
-        // if identifier is for a non-shared entity,
180
-        // then either a cached closure already existed, or was just brewed
181
-        return $this->brewedClosure($identifier, $arguments);
182
-    }
183
-
184
-
185
-    /**
186
-     * @param string $identifier
187
-     * @param string $type
188
-     * @return bool|mixed
189
-     * @throws InvalidIdentifierException
190
-     */
191
-    protected function getShared($identifier, $type)
192
-    {
193
-        try {
194
-            if (empty($type) || $type === CoffeeMaker::BREW_SHARED) {
195
-                // if a shared service was requested and an instance is in the carafe, then return it
196
-                return $this->get($identifier);
197
-            }
198
-        } catch (ServiceNotFoundException $e) {
199
-            // if not then we'll just catch the ServiceNotFoundException but not do anything just yet,
200
-            // and instead, attempt to build whatever was requested
201
-        }
202
-        return false;
203
-    }
204
-
205
-
206
-    /**
207
-     * @param mixed  $brewed
208
-     * @param string $identifier
209
-     * @param string $type
210
-     * @return bool
211
-     * @throws InvalidClassException
212
-     * @throws InvalidDataTypeException
213
-     * @throws InvalidIdentifierException
214
-     * @throws OutOfBoundsException
215
-     * @throws ServiceExistsException
216
-     * @throws ServiceNotFoundException
217
-     */
218
-    protected function brewedLoadOnly($brewed, $identifier, $type)
219
-    {
220
-        if ($type === CoffeeMaker::BREW_LOAD_ONLY) {
221
-            if ($brewed !== true) {
222
-                throw new ServiceNotFoundException(
223
-                    sprintf(
224
-                        esc_html__(
225
-                            'The "%1$s" class could not be loaded.',
226
-                            'event_espresso'
227
-                        ),
228
-                        $identifier
229
-                    )
230
-                );
231
-            }
232
-            return true;
233
-        }
234
-        return false;
235
-    }
236
-
237
-
238
-    /**
239
-     * @param string $identifier
240
-     * @param array  $arguments
241
-     * @return mixed
242
-     * @throws InstantiationException
243
-     */
244
-    protected function brewedClosure($identifier, array $arguments)
245
-    {
246
-        $closure = $this->reservoir->get($identifier);
247
-        if (empty($closure)) {
248
-            throw new InstantiationException(
249
-                sprintf(
250
-                    esc_html__(
251
-                        'Could not brew an instance of "%1$s".',
252
-                        'event_espresso'
253
-                    ),
254
-                    $identifier
255
-                )
256
-            );
257
-        }
258
-        return $closure($arguments);
259
-    }
260
-
261
-
262
-    /**
263
-     * @param CoffeeMakerInterface $coffee_maker
264
-     * @param string               $type
265
-     * @return bool
266
-     * @throws InvalidIdentifierException
267
-     * @throws InvalidEntityException
268
-     */
269
-    public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type)
270
-    {
271
-        $type = CoffeeMaker::validateType($type);
272
-        return $this->coffee_makers->add($coffee_maker, $type);
273
-    }
274
-
275
-
276
-    /**
277
-     * @param string   $identifier
278
-     * @param callable $closure
279
-     * @return callable|null
280
-     * @throws InvalidIdentifierException
281
-     * @throws InvalidDataTypeException
282
-     */
283
-    public function addClosure($identifier, $closure)
284
-    {
285
-        if (! is_callable($closure)) {
286
-            throw new InvalidDataTypeException('$closure', $closure, 'Closure');
287
-        }
288
-        $identifier = $this->processIdentifier($identifier);
289
-        if ($this->reservoir->add($closure, $identifier)) {
290
-            return $closure;
291
-        }
292
-        return null;
293
-    }
294
-
295
-
296
-    /**
297
-     * @param string $identifier
298
-     * @return boolean
299
-     * @throws InvalidIdentifierException
300
-     */
301
-    public function removeClosure($identifier)
302
-    {
303
-        $identifier = $this->processIdentifier($identifier);
304
-        if ($this->reservoir->has($identifier)) {
305
-            return $this->reservoir->remove($this->reservoir->get($identifier));
306
-        }
307
-        return false;
308
-    }
309
-
310
-
311
-    /**
312
-     * @param  string $identifier Identifier for the entity class that the service applies to
313
-     *                            Typically a Fully Qualified Class Name
314
-     * @param mixed   $service
315
-     * @return bool
316
-     * @throws \EventEspresso\core\services\container\exceptions\InvalidServiceException
317
-     * @throws InvalidIdentifierException
318
-     */
319
-    public function addService($identifier, $service)
320
-    {
321
-        $identifier = $this->processIdentifier($identifier);
322
-        $service = $this->validateService($identifier, $service);
323
-        return $this->carafe->add($service, $identifier);
324
-    }
325
-
326
-
327
-    /**
328
-     * @param string $identifier
329
-     * @return boolean
330
-     * @throws InvalidIdentifierException
331
-     */
332
-    public function removeService($identifier)
333
-    {
334
-        $identifier = $this->processIdentifier($identifier);
335
-        if ($this->carafe->has($identifier)) {
336
-            return $this->carafe->remove($this->carafe->get($identifier));
337
-        }
338
-        return false;
339
-    }
340
-
341
-
342
-    /**
343
-     * Adds instructions on how to brew objects
344
-     *
345
-     * @param RecipeInterface $recipe
346
-     * @return mixed
347
-     * @throws InvalidIdentifierException
348
-     */
349
-    public function addRecipe(RecipeInterface $recipe)
350
-    {
351
-        $this->addAliases($recipe->identifier(), $recipe->filters());
352
-        $identifier = $this->processIdentifier($recipe->identifier());
353
-        return $this->recipes->add($recipe, $identifier);
354
-    }
355
-
356
-
357
-    /**
358
-     * @param string $identifier The Recipe's identifier
359
-     * @return boolean
360
-     * @throws InvalidIdentifierException
361
-     */
362
-    public function removeRecipe($identifier)
363
-    {
364
-        $identifier = $this->processIdentifier($identifier);
365
-        if ($this->recipes->has($identifier)) {
366
-            return $this->recipes->remove($this->recipes->get($identifier));
367
-        }
368
-        return false;
369
-    }
370
-
371
-
372
-    /**
373
-     * Get instructions on how to brew objects
374
-     *
375
-     * @param  string $identifier Identifier for the entity class that the recipe applies to
376
-     *                            Typically a Fully Qualified Class Name
377
-     * @param string  $type
378
-     * @return RecipeInterface
379
-     * @throws OutOfBoundsException
380
-     * @throws InvalidIdentifierException
381
-     */
382
-    public function getRecipe($identifier, $type = '')
383
-    {
384
-        $identifier = $this->processIdentifier($identifier);
385
-        if ($this->recipes->has($identifier)) {
386
-            return $this->recipes->get($identifier);
387
-        }
388
-        $default_recipes = $this->getDefaultRecipes();
389
-        $matches = array();
390
-        foreach ($default_recipes as $wildcard => $default_recipe) {
391
-            // is the wildcard recipe prefix in the identifier ?
392
-            if (strpos($identifier, $wildcard) !== false) {
393
-                // track matches and use the number of wildcard characters matched for the key
394
-                $matches[ strlen($wildcard) ] = $default_recipe;
395
-            }
396
-        }
397
-        if (count($matches) > 0) {
398
-            // sort our recipes by the number of wildcard characters matched
399
-            ksort($matches);
400
-            // then grab the last recipe form the list, since it had the most matching characters
401
-            $match = array_pop($matches);
402
-            // since we are using a default recipe, we need to set it's identifier and fqcn
403
-            return $this->copyDefaultRecipe($match, $identifier, $type);
404
-        }
405
-        if ($this->recipes->has(Recipe::DEFAULT_ID)) {
406
-            // since we are using a default recipe, we need to set it's identifier and fqcn
407
-            return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type);
408
-        }
409
-        throw new OutOfBoundsException(
410
-            sprintf(
411
-                esc_html__('Could not brew coffee because no recipes were found for class "%1$s".', 'event_espresso'),
412
-                $identifier
413
-            )
414
-        );
415
-    }
416
-
417
-
418
-    /**
419
-     * adds class name aliases to list of filters
420
-     *
421
-     * @param  string       $identifier Identifier for the entity class that the alias applies to
422
-     *                                  Typically a Fully Qualified Class Name
423
-     * @param  array|string $aliases
424
-     * @return void
425
-     * @throws InvalidIdentifierException
426
-     */
427
-    public function addAliases($identifier, $aliases)
428
-    {
429
-        if (empty($aliases)) {
430
-            return;
431
-        }
432
-        $identifier = $this->processIdentifier($identifier);
433
-        foreach ((array) $aliases as $alias) {
434
-            $this->filters[ $this->processIdentifier($alias) ] = $identifier;
435
-        }
436
-    }
437
-
438
-
439
-    /**
440
-     * Adds a service to one of the internal collections
441
-     *
442
-     * @param        $identifier
443
-     * @param array  $arguments
444
-     * @param string $type
445
-     * @return mixed
446
-     * @throws InvalidDataTypeException
447
-     * @throws InvalidClassException
448
-     * @throws OutOfBoundsException
449
-     * @throws InvalidIdentifierException
450
-     * @throws ServiceExistsException
451
-     */
452
-    private function makeCoffee($identifier, $arguments = array(), $type = '')
453
-    {
454
-        if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) {
455
-            throw new ServiceExistsException($identifier);
456
-        }
457
-        $identifier = $this->filterIdentifier($identifier);
458
-        $recipe = $this->getRecipe($identifier, $type);
459
-        $type = ! empty($type) ? $type : $recipe->type();
460
-        $coffee_maker = $this->getCoffeeMaker($type);
461
-        return $coffee_maker->brew($recipe, $arguments);
462
-    }
463
-
464
-
465
-    /**
466
-     * filters alias identifiers to find the real class name
467
-     *
468
-     * @param  string $identifier Identifier for the entity class that the filter applies to
469
-     *                            Typically a Fully Qualified Class Name
470
-     * @return string
471
-     * @throws InvalidIdentifierException
472
-     */
473
-    private function filterIdentifier($identifier)
474
-    {
475
-        $identifier = $this->processIdentifier($identifier);
476
-        return isset($this->filters[ $identifier ]) && ! empty($this->filters[ $identifier ])
477
-            ? $this->filters[ $identifier ]
478
-            : $identifier;
479
-    }
480
-
481
-
482
-    /**
483
-     * verifies and standardizes identifiers
484
-     *
485
-     * @param  string $identifier Identifier for the entity class
486
-     *                            Typically a Fully Qualified Class Name
487
-     * @return string
488
-     * @throws InvalidIdentifierException
489
-     */
490
-    private function processIdentifier($identifier)
491
-    {
492
-        if (! is_string($identifier)) {
493
-            throw new InvalidIdentifierException(
494
-                is_object($identifier) ? get_class($identifier) : gettype($identifier),
495
-                '\Fully\Qualified\ClassName'
496
-            );
497
-        }
498
-        return ltrim($identifier, '\\');
499
-    }
500
-
501
-
502
-    /**
503
-     * @param string $type
504
-     * @return CoffeeMakerInterface
505
-     * @throws OutOfBoundsException
506
-     * @throws InvalidDataTypeException
507
-     * @throws InvalidClassException
508
-     */
509
-    private function getCoffeeMaker($type)
510
-    {
511
-        if (! $this->coffee_makers->has($type)) {
512
-            throw new OutOfBoundsException(
513
-                esc_html__('The requested coffee maker is either missing or invalid.', 'event_espresso')
514
-            );
515
-        }
516
-        return $this->coffee_makers->get($type);
517
-    }
518
-
519
-
520
-    /**
521
-     * Retrieves all recipes that use a wildcard "*" in their identifier
522
-     * This allows recipes to be set up for handling
523
-     * legacy classes that do not support PSR-4 autoloading.
524
-     * for example:
525
-     * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee
526
-     *
527
-     * @return array
528
-     */
529
-    private function getDefaultRecipes()
530
-    {
531
-        $default_recipes = array();
532
-        $this->recipes->rewind();
533
-        while ($this->recipes->valid()) {
534
-            $identifier = $this->recipes->getInfo();
535
-            // does this recipe use a wildcard ? (but is NOT the global default)
536
-            if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) {
537
-                // strip the wildcard and use identifier as key
538
-                $default_recipes[ str_replace('*', '', $identifier) ] = $this->recipes->current();
539
-            }
540
-            $this->recipes->next();
541
-        }
542
-        return $default_recipes;
543
-    }
544
-
545
-
546
-    /**
547
-     * clones a default recipe and then copies details
548
-     * from the incoming request to it so that it can be used
549
-     *
550
-     * @param RecipeInterface $default_recipe
551
-     * @param string          $identifier
552
-     * @param string          $type
553
-     * @return RecipeInterface
554
-     */
555
-    private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '')
556
-    {
557
-        $recipe = clone $default_recipe;
558
-        if (! empty($type)) {
559
-            $recipe->setType($type);
560
-        }
561
-        // is this the base default recipe ?
562
-        if ($default_recipe->identifier() === Recipe::DEFAULT_ID) {
563
-            $recipe->setIdentifier($identifier);
564
-            $recipe->setFqcn($identifier);
565
-            return $recipe;
566
-        }
567
-        $recipe->setIdentifier($identifier);
568
-        foreach ($default_recipe->paths() as $path) {
569
-            $path = str_replace('*', $identifier, $path);
570
-            if (is_readable($path)) {
571
-                $recipe->setPaths($path);
572
-            }
573
-        }
574
-        $recipe->setFqcn($identifier);
575
-        return $recipe;
576
-    }
577
-
578
-
579
-    /**
580
-     * @param  string $identifier Identifier for the entity class that the service applies to
581
-     *                            Typically a Fully Qualified Class Name
582
-     * @param mixed   $service
583
-     * @return mixed
584
-     * @throws InvalidServiceException
585
-     */
586
-    private function validateService($identifier, $service)
587
-    {
588
-        if (! is_object($service)) {
589
-            throw new InvalidServiceException(
590
-                $identifier,
591
-                $service
592
-            );
593
-        }
594
-        return $service;
595
-    }
31
+	/**
32
+	 * This was the best coffee related name I could think of to represent class name "aliases"
33
+	 * So classes can be found via an alias identifier,
34
+	 * that is revealed when it is run through... the filters... eh? get it?
35
+	 *
36
+	 * @var array $filters
37
+	 */
38
+	private $filters;
39
+
40
+	/**
41
+	 * These are the classes that will actually build the objects (to order of course)
42
+	 *
43
+	 * @var array $coffee_makers
44
+	 */
45
+	private $coffee_makers;
46
+
47
+	/**
48
+	 * where the instantiated "singleton" objects are stored
49
+	 *
50
+	 * @var CollectionInterface $carafe
51
+	 */
52
+	private $carafe;
53
+
54
+	/**
55
+	 * collection of Recipes that instruct us how to brew objects
56
+	 *
57
+	 * @var CollectionInterface $recipes
58
+	 */
59
+	private $recipes;
60
+
61
+	/**
62
+	 * collection of closures for brewing objects
63
+	 *
64
+	 * @var CollectionInterface $reservoir
65
+	 */
66
+	private $reservoir;
67
+
68
+
69
+	/**
70
+	 * CoffeeShop constructor
71
+	 *
72
+	 * @throws InvalidInterfaceException
73
+	 */
74
+	public function __construct()
75
+	{
76
+		// array for storing class aliases
77
+		$this->filters = array();
78
+		// create collection for storing shared services
79
+		$this->carafe = new LooseCollection('');
80
+		// create collection for storing recipes that tell us how to build services and entities
81
+		$this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface');
82
+		// create collection for storing closures for constructing new entities
83
+		$this->reservoir = new Collection('Closure');
84
+		// create collection for storing the generators that build our services and entity closures
85
+		$this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface');
86
+	}
87
+
88
+
89
+	/**
90
+	 * Returns true if the container can return an entry for the given identifier.
91
+	 * Returns false otherwise.
92
+	 * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception.
93
+	 * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`.
94
+	 *
95
+	 * @param string $identifier  Identifier of the entry to look for.
96
+	 *                            Typically a Fully Qualified Class Name
97
+	 * @return boolean
98
+	 * @throws InvalidIdentifierException
99
+	 */
100
+	public function has($identifier)
101
+	{
102
+		$identifier = $this->filterIdentifier($identifier);
103
+		return $this->carafe->has($identifier);
104
+	}
105
+
106
+
107
+	/**
108
+	 * finds a previously brewed (SHARED) service and returns it
109
+	 *
110
+	 * @param  string $identifier Identifier for the entity class to be constructed.
111
+	 *                            Typically a Fully Qualified Class Name
112
+	 * @return mixed
113
+	 * @throws InvalidIdentifierException
114
+	 * @throws ServiceNotFoundException No service was found for this identifier.
115
+	 */
116
+	public function get($identifier)
117
+	{
118
+		$identifier = $this->filterIdentifier($identifier);
119
+		if ($this->carafe->has($identifier)) {
120
+			return $this->carafe->get($identifier);
121
+		}
122
+		throw new ServiceNotFoundException($identifier);
123
+	}
124
+
125
+
126
+	/**
127
+	 * returns an instance of the requested entity type using the supplied arguments.
128
+	 * If a shared service is requested and an instance is already in the carafe, then it will be returned.
129
+	 * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned
130
+	 * If the request is for a new entity and a closure exists in the reservoir for creating it,
131
+	 * then a new entity will be instantiated from the closure and returned.
132
+	 * If a closure does not exist, then one will be built and added to the reservoir
133
+	 * before instantiating the requested entity.
134
+	 *
135
+	 * @param  string $identifier Identifier for the entity class to be constructed.
136
+	 *                            Typically a Fully Qualified Class Name
137
+	 * @param array   $arguments  an array of arguments to be passed to the entity constructor
138
+	 * @param string  $type
139
+	 * @return mixed
140
+	 * @throws OutOfBoundsException
141
+	 * @throws InstantiationException
142
+	 * @throws InvalidDataTypeException
143
+	 * @throws InvalidClassException
144
+	 * @throws InvalidIdentifierException
145
+	 * @throws ServiceExistsException
146
+	 * @throws ServiceNotFoundException No service was found for this identifier.
147
+	 */
148
+	public function brew($identifier, $arguments = array(), $type = '')
149
+	{
150
+		// resolve any class aliases that may exist
151
+		$identifier = $this->filterIdentifier($identifier);
152
+		// is a shared service being requested and already exists in the carafe?
153
+		$brewed = $this->getShared($identifier, $type);
154
+		// then return whatever was found
155
+		if ($brewed !== false) {
156
+			return $brewed;
157
+		}
158
+		// if the reservoir doesn't have a closure already for the requested identifier,
159
+		// then neither a shared service nor a closure for making entities has been built yet
160
+		if (! $this->reservoir->has($identifier)) {
161
+			// so let's brew something up and add it to the proper collection
162
+			$brewed = $this->makeCoffee($identifier, $arguments, $type);
163
+		}
164
+		// did the requested class only require loading, and if so, was that successful?
165
+		if ($this->brewedLoadOnly($brewed, $identifier, $type) === true) {
166
+			return true;
167
+		}
168
+		// was the brewed item a callable factory function ?
169
+		if (is_callable($brewed)) {
170
+			// then instantiate a new entity from the cached closure
171
+			return $brewed($arguments);
172
+		}
173
+		if ($brewed) {
174
+			// requested object was a shared entity, so attempt to get it from the carafe again
175
+			// because if it wasn't there before, then it should have just been brewed and added,
176
+			// but if it still isn't there, then this time the thrown ServiceNotFoundException will not be caught
177
+			return $this->get($identifier);
178
+		}
179
+		// if identifier is for a non-shared entity,
180
+		// then either a cached closure already existed, or was just brewed
181
+		return $this->brewedClosure($identifier, $arguments);
182
+	}
183
+
184
+
185
+	/**
186
+	 * @param string $identifier
187
+	 * @param string $type
188
+	 * @return bool|mixed
189
+	 * @throws InvalidIdentifierException
190
+	 */
191
+	protected function getShared($identifier, $type)
192
+	{
193
+		try {
194
+			if (empty($type) || $type === CoffeeMaker::BREW_SHARED) {
195
+				// if a shared service was requested and an instance is in the carafe, then return it
196
+				return $this->get($identifier);
197
+			}
198
+		} catch (ServiceNotFoundException $e) {
199
+			// if not then we'll just catch the ServiceNotFoundException but not do anything just yet,
200
+			// and instead, attempt to build whatever was requested
201
+		}
202
+		return false;
203
+	}
204
+
205
+
206
+	/**
207
+	 * @param mixed  $brewed
208
+	 * @param string $identifier
209
+	 * @param string $type
210
+	 * @return bool
211
+	 * @throws InvalidClassException
212
+	 * @throws InvalidDataTypeException
213
+	 * @throws InvalidIdentifierException
214
+	 * @throws OutOfBoundsException
215
+	 * @throws ServiceExistsException
216
+	 * @throws ServiceNotFoundException
217
+	 */
218
+	protected function brewedLoadOnly($brewed, $identifier, $type)
219
+	{
220
+		if ($type === CoffeeMaker::BREW_LOAD_ONLY) {
221
+			if ($brewed !== true) {
222
+				throw new ServiceNotFoundException(
223
+					sprintf(
224
+						esc_html__(
225
+							'The "%1$s" class could not be loaded.',
226
+							'event_espresso'
227
+						),
228
+						$identifier
229
+					)
230
+				);
231
+			}
232
+			return true;
233
+		}
234
+		return false;
235
+	}
236
+
237
+
238
+	/**
239
+	 * @param string $identifier
240
+	 * @param array  $arguments
241
+	 * @return mixed
242
+	 * @throws InstantiationException
243
+	 */
244
+	protected function brewedClosure($identifier, array $arguments)
245
+	{
246
+		$closure = $this->reservoir->get($identifier);
247
+		if (empty($closure)) {
248
+			throw new InstantiationException(
249
+				sprintf(
250
+					esc_html__(
251
+						'Could not brew an instance of "%1$s".',
252
+						'event_espresso'
253
+					),
254
+					$identifier
255
+				)
256
+			);
257
+		}
258
+		return $closure($arguments);
259
+	}
260
+
261
+
262
+	/**
263
+	 * @param CoffeeMakerInterface $coffee_maker
264
+	 * @param string               $type
265
+	 * @return bool
266
+	 * @throws InvalidIdentifierException
267
+	 * @throws InvalidEntityException
268
+	 */
269
+	public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type)
270
+	{
271
+		$type = CoffeeMaker::validateType($type);
272
+		return $this->coffee_makers->add($coffee_maker, $type);
273
+	}
274
+
275
+
276
+	/**
277
+	 * @param string   $identifier
278
+	 * @param callable $closure
279
+	 * @return callable|null
280
+	 * @throws InvalidIdentifierException
281
+	 * @throws InvalidDataTypeException
282
+	 */
283
+	public function addClosure($identifier, $closure)
284
+	{
285
+		if (! is_callable($closure)) {
286
+			throw new InvalidDataTypeException('$closure', $closure, 'Closure');
287
+		}
288
+		$identifier = $this->processIdentifier($identifier);
289
+		if ($this->reservoir->add($closure, $identifier)) {
290
+			return $closure;
291
+		}
292
+		return null;
293
+	}
294
+
295
+
296
+	/**
297
+	 * @param string $identifier
298
+	 * @return boolean
299
+	 * @throws InvalidIdentifierException
300
+	 */
301
+	public function removeClosure($identifier)
302
+	{
303
+		$identifier = $this->processIdentifier($identifier);
304
+		if ($this->reservoir->has($identifier)) {
305
+			return $this->reservoir->remove($this->reservoir->get($identifier));
306
+		}
307
+		return false;
308
+	}
309
+
310
+
311
+	/**
312
+	 * @param  string $identifier Identifier for the entity class that the service applies to
313
+	 *                            Typically a Fully Qualified Class Name
314
+	 * @param mixed   $service
315
+	 * @return bool
316
+	 * @throws \EventEspresso\core\services\container\exceptions\InvalidServiceException
317
+	 * @throws InvalidIdentifierException
318
+	 */
319
+	public function addService($identifier, $service)
320
+	{
321
+		$identifier = $this->processIdentifier($identifier);
322
+		$service = $this->validateService($identifier, $service);
323
+		return $this->carafe->add($service, $identifier);
324
+	}
325
+
326
+
327
+	/**
328
+	 * @param string $identifier
329
+	 * @return boolean
330
+	 * @throws InvalidIdentifierException
331
+	 */
332
+	public function removeService($identifier)
333
+	{
334
+		$identifier = $this->processIdentifier($identifier);
335
+		if ($this->carafe->has($identifier)) {
336
+			return $this->carafe->remove($this->carafe->get($identifier));
337
+		}
338
+		return false;
339
+	}
340
+
341
+
342
+	/**
343
+	 * Adds instructions on how to brew objects
344
+	 *
345
+	 * @param RecipeInterface $recipe
346
+	 * @return mixed
347
+	 * @throws InvalidIdentifierException
348
+	 */
349
+	public function addRecipe(RecipeInterface $recipe)
350
+	{
351
+		$this->addAliases($recipe->identifier(), $recipe->filters());
352
+		$identifier = $this->processIdentifier($recipe->identifier());
353
+		return $this->recipes->add($recipe, $identifier);
354
+	}
355
+
356
+
357
+	/**
358
+	 * @param string $identifier The Recipe's identifier
359
+	 * @return boolean
360
+	 * @throws InvalidIdentifierException
361
+	 */
362
+	public function removeRecipe($identifier)
363
+	{
364
+		$identifier = $this->processIdentifier($identifier);
365
+		if ($this->recipes->has($identifier)) {
366
+			return $this->recipes->remove($this->recipes->get($identifier));
367
+		}
368
+		return false;
369
+	}
370
+
371
+
372
+	/**
373
+	 * Get instructions on how to brew objects
374
+	 *
375
+	 * @param  string $identifier Identifier for the entity class that the recipe applies to
376
+	 *                            Typically a Fully Qualified Class Name
377
+	 * @param string  $type
378
+	 * @return RecipeInterface
379
+	 * @throws OutOfBoundsException
380
+	 * @throws InvalidIdentifierException
381
+	 */
382
+	public function getRecipe($identifier, $type = '')
383
+	{
384
+		$identifier = $this->processIdentifier($identifier);
385
+		if ($this->recipes->has($identifier)) {
386
+			return $this->recipes->get($identifier);
387
+		}
388
+		$default_recipes = $this->getDefaultRecipes();
389
+		$matches = array();
390
+		foreach ($default_recipes as $wildcard => $default_recipe) {
391
+			// is the wildcard recipe prefix in the identifier ?
392
+			if (strpos($identifier, $wildcard) !== false) {
393
+				// track matches and use the number of wildcard characters matched for the key
394
+				$matches[ strlen($wildcard) ] = $default_recipe;
395
+			}
396
+		}
397
+		if (count($matches) > 0) {
398
+			// sort our recipes by the number of wildcard characters matched
399
+			ksort($matches);
400
+			// then grab the last recipe form the list, since it had the most matching characters
401
+			$match = array_pop($matches);
402
+			// since we are using a default recipe, we need to set it's identifier and fqcn
403
+			return $this->copyDefaultRecipe($match, $identifier, $type);
404
+		}
405
+		if ($this->recipes->has(Recipe::DEFAULT_ID)) {
406
+			// since we are using a default recipe, we need to set it's identifier and fqcn
407
+			return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type);
408
+		}
409
+		throw new OutOfBoundsException(
410
+			sprintf(
411
+				esc_html__('Could not brew coffee because no recipes were found for class "%1$s".', 'event_espresso'),
412
+				$identifier
413
+			)
414
+		);
415
+	}
416
+
417
+
418
+	/**
419
+	 * adds class name aliases to list of filters
420
+	 *
421
+	 * @param  string       $identifier Identifier for the entity class that the alias applies to
422
+	 *                                  Typically a Fully Qualified Class Name
423
+	 * @param  array|string $aliases
424
+	 * @return void
425
+	 * @throws InvalidIdentifierException
426
+	 */
427
+	public function addAliases($identifier, $aliases)
428
+	{
429
+		if (empty($aliases)) {
430
+			return;
431
+		}
432
+		$identifier = $this->processIdentifier($identifier);
433
+		foreach ((array) $aliases as $alias) {
434
+			$this->filters[ $this->processIdentifier($alias) ] = $identifier;
435
+		}
436
+	}
437
+
438
+
439
+	/**
440
+	 * Adds a service to one of the internal collections
441
+	 *
442
+	 * @param        $identifier
443
+	 * @param array  $arguments
444
+	 * @param string $type
445
+	 * @return mixed
446
+	 * @throws InvalidDataTypeException
447
+	 * @throws InvalidClassException
448
+	 * @throws OutOfBoundsException
449
+	 * @throws InvalidIdentifierException
450
+	 * @throws ServiceExistsException
451
+	 */
452
+	private function makeCoffee($identifier, $arguments = array(), $type = '')
453
+	{
454
+		if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) {
455
+			throw new ServiceExistsException($identifier);
456
+		}
457
+		$identifier = $this->filterIdentifier($identifier);
458
+		$recipe = $this->getRecipe($identifier, $type);
459
+		$type = ! empty($type) ? $type : $recipe->type();
460
+		$coffee_maker = $this->getCoffeeMaker($type);
461
+		return $coffee_maker->brew($recipe, $arguments);
462
+	}
463
+
464
+
465
+	/**
466
+	 * filters alias identifiers to find the real class name
467
+	 *
468
+	 * @param  string $identifier Identifier for the entity class that the filter applies to
469
+	 *                            Typically a Fully Qualified Class Name
470
+	 * @return string
471
+	 * @throws InvalidIdentifierException
472
+	 */
473
+	private function filterIdentifier($identifier)
474
+	{
475
+		$identifier = $this->processIdentifier($identifier);
476
+		return isset($this->filters[ $identifier ]) && ! empty($this->filters[ $identifier ])
477
+			? $this->filters[ $identifier ]
478
+			: $identifier;
479
+	}
480
+
481
+
482
+	/**
483
+	 * verifies and standardizes identifiers
484
+	 *
485
+	 * @param  string $identifier Identifier for the entity class
486
+	 *                            Typically a Fully Qualified Class Name
487
+	 * @return string
488
+	 * @throws InvalidIdentifierException
489
+	 */
490
+	private function processIdentifier($identifier)
491
+	{
492
+		if (! is_string($identifier)) {
493
+			throw new InvalidIdentifierException(
494
+				is_object($identifier) ? get_class($identifier) : gettype($identifier),
495
+				'\Fully\Qualified\ClassName'
496
+			);
497
+		}
498
+		return ltrim($identifier, '\\');
499
+	}
500
+
501
+
502
+	/**
503
+	 * @param string $type
504
+	 * @return CoffeeMakerInterface
505
+	 * @throws OutOfBoundsException
506
+	 * @throws InvalidDataTypeException
507
+	 * @throws InvalidClassException
508
+	 */
509
+	private function getCoffeeMaker($type)
510
+	{
511
+		if (! $this->coffee_makers->has($type)) {
512
+			throw new OutOfBoundsException(
513
+				esc_html__('The requested coffee maker is either missing or invalid.', 'event_espresso')
514
+			);
515
+		}
516
+		return $this->coffee_makers->get($type);
517
+	}
518
+
519
+
520
+	/**
521
+	 * Retrieves all recipes that use a wildcard "*" in their identifier
522
+	 * This allows recipes to be set up for handling
523
+	 * legacy classes that do not support PSR-4 autoloading.
524
+	 * for example:
525
+	 * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee
526
+	 *
527
+	 * @return array
528
+	 */
529
+	private function getDefaultRecipes()
530
+	{
531
+		$default_recipes = array();
532
+		$this->recipes->rewind();
533
+		while ($this->recipes->valid()) {
534
+			$identifier = $this->recipes->getInfo();
535
+			// does this recipe use a wildcard ? (but is NOT the global default)
536
+			if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) {
537
+				// strip the wildcard and use identifier as key
538
+				$default_recipes[ str_replace('*', '', $identifier) ] = $this->recipes->current();
539
+			}
540
+			$this->recipes->next();
541
+		}
542
+		return $default_recipes;
543
+	}
544
+
545
+
546
+	/**
547
+	 * clones a default recipe and then copies details
548
+	 * from the incoming request to it so that it can be used
549
+	 *
550
+	 * @param RecipeInterface $default_recipe
551
+	 * @param string          $identifier
552
+	 * @param string          $type
553
+	 * @return RecipeInterface
554
+	 */
555
+	private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '')
556
+	{
557
+		$recipe = clone $default_recipe;
558
+		if (! empty($type)) {
559
+			$recipe->setType($type);
560
+		}
561
+		// is this the base default recipe ?
562
+		if ($default_recipe->identifier() === Recipe::DEFAULT_ID) {
563
+			$recipe->setIdentifier($identifier);
564
+			$recipe->setFqcn($identifier);
565
+			return $recipe;
566
+		}
567
+		$recipe->setIdentifier($identifier);
568
+		foreach ($default_recipe->paths() as $path) {
569
+			$path = str_replace('*', $identifier, $path);
570
+			if (is_readable($path)) {
571
+				$recipe->setPaths($path);
572
+			}
573
+		}
574
+		$recipe->setFqcn($identifier);
575
+		return $recipe;
576
+	}
577
+
578
+
579
+	/**
580
+	 * @param  string $identifier Identifier for the entity class that the service applies to
581
+	 *                            Typically a Fully Qualified Class Name
582
+	 * @param mixed   $service
583
+	 * @return mixed
584
+	 * @throws InvalidServiceException
585
+	 */
586
+	private function validateService($identifier, $service)
587
+	{
588
+		if (! is_object($service)) {
589
+			throw new InvalidServiceException(
590
+				$identifier,
591
+				$service
592
+			);
593
+		}
594
+		return $service;
595
+	}
596 596
 }
Please login to merge, or discard this patch.
core/helpers/EEH_Template_Validator.helper.php 2 patches
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -40,10 +40,10 @@  discard block
 block discarded – undo
40 40
      */
41 41
     public static function verify_is_array_of($variable_to_test, $name_of_variable, $class_name, $allow_null = 'allow_null')
42 42
     {
43
-        if (!WP_DEBUG) {
43
+        if ( ! WP_DEBUG) {
44 44
             return;
45 45
         }
46
-        self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null'));
46
+        self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null', 'do_not_allow_null'));
47 47
         if ('allow_null' == $allow_null && is_null($variable_to_test)) {
48 48
             return;
49 49
         }
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
      */
67 67
     public static function verify_isnt_null($variable_to_test, $name_of_variable)
68 68
     {
69
-        if (!WP_DEBUG) {
69
+        if ( ! WP_DEBUG) {
70 70
             return;
71 71
         }
72 72
         if ($variable_to_test == null && $variable_to_test != 0 && $variable_to_test != false) {
@@ -86,10 +86,10 @@  discard block
 block discarded – undo
86 86
      */
87 87
     public static function verify_is_true($expression_to_test, $expression_string_representation)
88 88
     {
89
-        if (!WP_DEBUG) {
89
+        if ( ! WP_DEBUG) {
90 90
             return;
91 91
         }
92
-        if (!$expression_to_test) {
92
+        if ( ! $expression_to_test) {
93 93
             $error[] = esc_html__('Template error.', 'event_espresso');
94 94
             $error[] = esc_html__("%s evaluated to false, but it must be true!", 'event_espresso');
95 95
             throw new EE_Error(sprintf(implode(",", $error), $expression_string_representation));
@@ -110,14 +110,14 @@  discard block
 block discarded – undo
110 110
      */
111 111
     public static function verify_instanceof($variable_to_test, $name_of_variable, $class_name, $allow_null = 'do_not_allow_null')
112 112
     {
113
-        if (!WP_DEBUG) {
113
+        if ( ! WP_DEBUG) {
114 114
             return;
115 115
         }
116
-        self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null'));
116
+        self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null', 'do_not_allow_null'));
117 117
         if ($allow_null == 'allow_null' && is_null($variable_to_test)) {
118 118
             return;
119 119
         }
120
-        if ($variable_to_test == null ||  ! ( $variable_to_test instanceof $class_name )) {
120
+        if ($variable_to_test == null || ! ($variable_to_test instanceof $class_name)) {
121 121
             $msg[] = esc_html__('Variable %s is not of the correct type.', 'event_espresso');
122 122
             $msg[] = esc_html__("It should be of type %s", 'event_espresso');
123 123
             throw new EE_Error(sprintf(implode(",", $msg), $name_of_variable, $name_of_variable, $class_name));
@@ -138,14 +138,14 @@  discard block
 block discarded – undo
138 138
      */
139 139
     public static function verify_is_array($variable_to_test, $variable_name, $allow_empty = 'allow_empty')
140 140
     {
141
-        if (!WP_DEBUG) {
141
+        if ( ! WP_DEBUG) {
142 142
             return;
143 143
         }
144
-        self::verify_argument_is_one_of($allow_empty, $variable_name, array('allow_empty','do_not_allow_empty'));
144
+        self::verify_argument_is_one_of($allow_empty, $variable_name, array('allow_empty', 'do_not_allow_empty'));
145 145
         if (empty($variable_to_test) && 'allow_empty' == $allow_empty) {
146 146
             return;
147 147
         }
148
-        if (!is_array($variable_to_test)) {
148
+        if ( ! is_array($variable_to_test)) {
149 149
             $error[] = esc_html__('Variable %s should be an array, but it is not.', 'event_espresso');
150 150
             $error[] = esc_html__("Its value is, instead '%s'", 'event_espresso');
151 151
             throw new EE_Error(sprintf(implode(",", $error), $variable_name, $variable_name, $variable_to_test));
@@ -168,10 +168,10 @@  discard block
 block discarded – undo
168 168
      */
169 169
     public static function verify_argument_is_one_of($variable_to_test, $variable_name, $string_options)
170 170
     {
171
-        if (!WP_DEBUG) {
171
+        if ( ! WP_DEBUG) {
172 172
             return;
173 173
         }
174
-        if (!in_array($variable_to_test, $string_options)) {
174
+        if ( ! in_array($variable_to_test, $string_options)) {
175 175
             $msg[0] = esc_html__('Malconfigured template.', 'event_espresso');
176 176
             $msg[1] = esc_html__("Variable named '%s' was set to '%s'. It can only be one of '%s'", 'event_espresso');
177 177
             throw new EE_Error(sprintf(implode("||", $msg), $variable_name, $variable_to_test, implode("', '", $string_options)));
Please login to merge, or discard this patch.
Indentation   +148 added lines, -148 removed lines patch added patch discarded remove patch
@@ -24,152 +24,152 @@
 block discarded – undo
24 24
  */
25 25
 class EEH_Template_Validator
26 26
 {
27
-    /**
28
-     * Throws an EE_Error if $variabel_to_test isn't an array of objects of class $class_name
29
-     * @param mixed $variable_to_test
30
-     * @param string $name_of_variable helpful in throwing intelligent errors
31
-     * @param string $class_name eg EE_Answer, EE_Transaction, etc.
32
-     * @param string $allow_null one of 'allow_null', or 'do_not_allow_null'
33
-     * @return void
34
-     * @throws EE_Error (indirectly)
35
-     */
36
-    public static function verify_is_array_of($variable_to_test, $name_of_variable, $class_name, $allow_null = 'allow_null')
37
-    {
38
-        if (!WP_DEBUG) {
39
-            return;
40
-        }
41
-        self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null'));
42
-        if ('allow_null' == $allow_null && is_null($variable_to_test)) {
43
-            return;
44
-        }
45
-        self::verify_is_array($variable_to_test, $name_of_variable);
46
-        foreach ($variable_to_test as $key => $array_element) {
47
-            self::verify_instanceof($array_element, $key, $class_name);
48
-        }
49
-    }
50
-
51
-
52
-
53
-
54
-
55
-    /**
56
-     * throws an EE_Error if $variable_to_test is null
57
-     * @param mixed $variable_to_test
58
-     * @param string $name_of_variable helpful for throwing intelligent errors
59
-     * @return void
60
-     * @throws EE_Error
61
-     */
62
-    public static function verify_isnt_null($variable_to_test, $name_of_variable)
63
-    {
64
-        if (!WP_DEBUG) {
65
-            return;
66
-        }
67
-        if ($variable_to_test == null && $variable_to_test != 0 && $variable_to_test != false) {
68
-            $error[] = esc_html__('Variable named %s is null.', 'event_espresso');
69
-            $error[] = esc_html__("Consider looking at the stack trace to see why it wasn't set.", 'event_espresso');
70
-            throw new EE_Error(sprintf(implode(",", $error), $name_of_variable, $name_of_variable));
71
-        }
72
-    }
73
-
74
-    /**
75
-     * When WP_DEBUG is activted, throws an error if $expression_to_test is false.
76
-     * @param boolean $expression_to_test
77
-     * @param string $expression_string_representation a string representation of your expression
78
-     * for example, if your expression were $var1==23, then this should be '$var1==23'
79
-     * @return void
80
-     * @throws EE_Error
81
-     */
82
-    public static function verify_is_true($expression_to_test, $expression_string_representation)
83
-    {
84
-        if (!WP_DEBUG) {
85
-            return;
86
-        }
87
-        if (!$expression_to_test) {
88
-            $error[] = esc_html__('Template error.', 'event_espresso');
89
-            $error[] = esc_html__("%s evaluated to false, but it must be true!", 'event_espresso');
90
-            throw new EE_Error(sprintf(implode(",", $error), $expression_string_representation));
91
-        }
92
-    }
93
-
94
-
95
-
96
-
97
-
98
-    /**
99
-     * For verifying that a variable is indeed an object of class $class_name
100
-     * @param mixed $variable_to_test
101
-     * @param string $name_of_variable helpful when throwing errors
102
-     * @param string $class_name eg, EE_Answer,
103
-     * @return void
104
-     * @throws EE_Error
105
-     */
106
-    public static function verify_instanceof($variable_to_test, $name_of_variable, $class_name, $allow_null = 'do_not_allow_null')
107
-    {
108
-        if (!WP_DEBUG) {
109
-            return;
110
-        }
111
-        self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null'));
112
-        if ($allow_null == 'allow_null' && is_null($variable_to_test)) {
113
-            return;
114
-        }
115
-        if ($variable_to_test == null ||  ! ( $variable_to_test instanceof $class_name )) {
116
-            $msg[] = esc_html__('Variable %s is not of the correct type.', 'event_espresso');
117
-            $msg[] = esc_html__("It should be of type %s", 'event_espresso');
118
-            throw new EE_Error(sprintf(implode(",", $msg), $name_of_variable, $name_of_variable, $class_name));
119
-        }
120
-    }
121
-
122
-
123
-
124
-
125
-
126
-    /**
127
-     * For verifying that a variable is indeed an array, else throw an EE_Error
128
-     * @param type $variable_to_test
129
-     * @param type $variable_name
130
-     * @param type $allow_empty one of 'allow_empty' or 'do_not_allow_empty'
131
-     * @return void
132
-     * @throws EE_Error
133
-     */
134
-    public static function verify_is_array($variable_to_test, $variable_name, $allow_empty = 'allow_empty')
135
-    {
136
-        if (!WP_DEBUG) {
137
-            return;
138
-        }
139
-        self::verify_argument_is_one_of($allow_empty, $variable_name, array('allow_empty','do_not_allow_empty'));
140
-        if (empty($variable_to_test) && 'allow_empty' == $allow_empty) {
141
-            return;
142
-        }
143
-        if (!is_array($variable_to_test)) {
144
-            $error[] = esc_html__('Variable %s should be an array, but it is not.', 'event_espresso');
145
-            $error[] = esc_html__("Its value is, instead '%s'", 'event_espresso');
146
-            throw new EE_Error(sprintf(implode(",", $error), $variable_name, $variable_name, $variable_to_test));
147
-        }
148
-    }
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-    /**
157
-     * for verifying that a variable is one of the string optiosn supplied
158
-     * @param mixed $variable_to_test
159
-     * @param mixed $variable_name the name you've given the variable. Eg, '$foo'. THis helps in producing better error messages
160
-     * @param array $string_options an array of acceptable values
161
-     * @return void
162
-     * @throws EE_Error
163
-     */
164
-    public static function verify_argument_is_one_of($variable_to_test, $variable_name, $string_options)
165
-    {
166
-        if (!WP_DEBUG) {
167
-            return;
168
-        }
169
-        if (!in_array($variable_to_test, $string_options)) {
170
-            $msg[0] = esc_html__('Malconfigured template.', 'event_espresso');
171
-            $msg[1] = esc_html__("Variable named '%s' was set to '%s'. It can only be one of '%s'", 'event_espresso');
172
-            throw new EE_Error(sprintf(implode("||", $msg), $variable_name, $variable_to_test, implode("', '", $string_options)));
173
-        }
174
-    }
27
+	/**
28
+	 * Throws an EE_Error if $variabel_to_test isn't an array of objects of class $class_name
29
+	 * @param mixed $variable_to_test
30
+	 * @param string $name_of_variable helpful in throwing intelligent errors
31
+	 * @param string $class_name eg EE_Answer, EE_Transaction, etc.
32
+	 * @param string $allow_null one of 'allow_null', or 'do_not_allow_null'
33
+	 * @return void
34
+	 * @throws EE_Error (indirectly)
35
+	 */
36
+	public static function verify_is_array_of($variable_to_test, $name_of_variable, $class_name, $allow_null = 'allow_null')
37
+	{
38
+		if (!WP_DEBUG) {
39
+			return;
40
+		}
41
+		self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null'));
42
+		if ('allow_null' == $allow_null && is_null($variable_to_test)) {
43
+			return;
44
+		}
45
+		self::verify_is_array($variable_to_test, $name_of_variable);
46
+		foreach ($variable_to_test as $key => $array_element) {
47
+			self::verify_instanceof($array_element, $key, $class_name);
48
+		}
49
+	}
50
+
51
+
52
+
53
+
54
+
55
+	/**
56
+	 * throws an EE_Error if $variable_to_test is null
57
+	 * @param mixed $variable_to_test
58
+	 * @param string $name_of_variable helpful for throwing intelligent errors
59
+	 * @return void
60
+	 * @throws EE_Error
61
+	 */
62
+	public static function verify_isnt_null($variable_to_test, $name_of_variable)
63
+	{
64
+		if (!WP_DEBUG) {
65
+			return;
66
+		}
67
+		if ($variable_to_test == null && $variable_to_test != 0 && $variable_to_test != false) {
68
+			$error[] = esc_html__('Variable named %s is null.', 'event_espresso');
69
+			$error[] = esc_html__("Consider looking at the stack trace to see why it wasn't set.", 'event_espresso');
70
+			throw new EE_Error(sprintf(implode(",", $error), $name_of_variable, $name_of_variable));
71
+		}
72
+	}
73
+
74
+	/**
75
+	 * When WP_DEBUG is activted, throws an error if $expression_to_test is false.
76
+	 * @param boolean $expression_to_test
77
+	 * @param string $expression_string_representation a string representation of your expression
78
+	 * for example, if your expression were $var1==23, then this should be '$var1==23'
79
+	 * @return void
80
+	 * @throws EE_Error
81
+	 */
82
+	public static function verify_is_true($expression_to_test, $expression_string_representation)
83
+	{
84
+		if (!WP_DEBUG) {
85
+			return;
86
+		}
87
+		if (!$expression_to_test) {
88
+			$error[] = esc_html__('Template error.', 'event_espresso');
89
+			$error[] = esc_html__("%s evaluated to false, but it must be true!", 'event_espresso');
90
+			throw new EE_Error(sprintf(implode(",", $error), $expression_string_representation));
91
+		}
92
+	}
93
+
94
+
95
+
96
+
97
+
98
+	/**
99
+	 * For verifying that a variable is indeed an object of class $class_name
100
+	 * @param mixed $variable_to_test
101
+	 * @param string $name_of_variable helpful when throwing errors
102
+	 * @param string $class_name eg, EE_Answer,
103
+	 * @return void
104
+	 * @throws EE_Error
105
+	 */
106
+	public static function verify_instanceof($variable_to_test, $name_of_variable, $class_name, $allow_null = 'do_not_allow_null')
107
+	{
108
+		if (!WP_DEBUG) {
109
+			return;
110
+		}
111
+		self::verify_argument_is_one_of($allow_null, 'allow_null', array('allow_null','do_not_allow_null'));
112
+		if ($allow_null == 'allow_null' && is_null($variable_to_test)) {
113
+			return;
114
+		}
115
+		if ($variable_to_test == null ||  ! ( $variable_to_test instanceof $class_name )) {
116
+			$msg[] = esc_html__('Variable %s is not of the correct type.', 'event_espresso');
117
+			$msg[] = esc_html__("It should be of type %s", 'event_espresso');
118
+			throw new EE_Error(sprintf(implode(",", $msg), $name_of_variable, $name_of_variable, $class_name));
119
+		}
120
+	}
121
+
122
+
123
+
124
+
125
+
126
+	/**
127
+	 * For verifying that a variable is indeed an array, else throw an EE_Error
128
+	 * @param type $variable_to_test
129
+	 * @param type $variable_name
130
+	 * @param type $allow_empty one of 'allow_empty' or 'do_not_allow_empty'
131
+	 * @return void
132
+	 * @throws EE_Error
133
+	 */
134
+	public static function verify_is_array($variable_to_test, $variable_name, $allow_empty = 'allow_empty')
135
+	{
136
+		if (!WP_DEBUG) {
137
+			return;
138
+		}
139
+		self::verify_argument_is_one_of($allow_empty, $variable_name, array('allow_empty','do_not_allow_empty'));
140
+		if (empty($variable_to_test) && 'allow_empty' == $allow_empty) {
141
+			return;
142
+		}
143
+		if (!is_array($variable_to_test)) {
144
+			$error[] = esc_html__('Variable %s should be an array, but it is not.', 'event_espresso');
145
+			$error[] = esc_html__("Its value is, instead '%s'", 'event_espresso');
146
+			throw new EE_Error(sprintf(implode(",", $error), $variable_name, $variable_name, $variable_to_test));
147
+		}
148
+	}
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+	/**
157
+	 * for verifying that a variable is one of the string optiosn supplied
158
+	 * @param mixed $variable_to_test
159
+	 * @param mixed $variable_name the name you've given the variable. Eg, '$foo'. THis helps in producing better error messages
160
+	 * @param array $string_options an array of acceptable values
161
+	 * @return void
162
+	 * @throws EE_Error
163
+	 */
164
+	public static function verify_argument_is_one_of($variable_to_test, $variable_name, $string_options)
165
+	{
166
+		if (!WP_DEBUG) {
167
+			return;
168
+		}
169
+		if (!in_array($variable_to_test, $string_options)) {
170
+			$msg[0] = esc_html__('Malconfigured template.', 'event_espresso');
171
+			$msg[1] = esc_html__("Variable named '%s' was set to '%s'. It can only be one of '%s'", 'event_espresso');
172
+			throw new EE_Error(sprintf(implode("||", $msg), $variable_name, $variable_to_test, implode("', '", $string_options)));
173
+		}
174
+	}
175 175
 }
Please login to merge, or discard this patch.
core/exceptions/InvalidStatusException.php 1 patch
Indentation   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -7,26 +7,26 @@
 block discarded – undo
7 7
 
8 8
 class InvalidStatusException extends InvalidArgumentException
9 9
 {
10
-    /**
11
-     * InvalidStatusException constructor.
12
-     * @param string $status the invalid status id that was supplied
13
-     * @param string $domain the name of the domain, model, or class that the status belongs to
14
-     * @param string $message custom message
15
-     * @param int $code
16
-     * @param Exception|null $previous
17
-     */
18
-    public function __construct($status, $domain, $message = '', $code = 0, Exception $previous = null)
19
-    {
20
-        if (empty($message)) {
21
-            $message = sprintf(
22
-                esc_html__(
23
-                    '"%1$s" is not a valid %2$s status',
24
-                    'event_espresso'
25
-                ),
26
-                $status,
27
-                $domain
28
-            );
29
-        }
30
-        parent::__construct($message, $code, $previous);
31
-    }
10
+	/**
11
+	 * InvalidStatusException constructor.
12
+	 * @param string $status the invalid status id that was supplied
13
+	 * @param string $domain the name of the domain, model, or class that the status belongs to
14
+	 * @param string $message custom message
15
+	 * @param int $code
16
+	 * @param Exception|null $previous
17
+	 */
18
+	public function __construct($status, $domain, $message = '', $code = 0, Exception $previous = null)
19
+	{
20
+		if (empty($message)) {
21
+			$message = sprintf(
22
+				esc_html__(
23
+					'"%1$s" is not a valid %2$s status',
24
+					'event_espresso'
25
+				),
26
+				$status,
27
+				$domain
28
+			);
29
+		}
30
+		parent::__construct($message, $code, $previous);
31
+	}
32 32
 }
Please login to merge, or discard this patch.
core/exceptions/InvalidDataTypeException.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -31,7 +31,7 @@  discard block
 block discarded – undo
31 31
     {
32 32
         if (empty($message)) {
33 33
             $expected = strpos(' was expected.', $expected) === false
34
-                ? $this->addIndefiniteArticle($expected) . ' was expected.'
34
+                ? $this->addIndefiniteArticle($expected).' was expected.'
35 35
                 : $expected;
36 36
             $message = sprintf(
37 37
                 esc_html__(
@@ -56,6 +56,6 @@  discard block
 block discarded – undo
56 56
         if (strtolower($string) === 'null') {
57 57
             return $string;
58 58
         }
59
-        return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ') . $string;
59
+        return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ').$string;
60 60
     }
61 61
 }
Please login to merge, or discard this patch.
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -15,46 +15,46 @@
 block discarded – undo
15 15
  */
16 16
 class InvalidDataTypeException extends InvalidArgumentException
17 17
 {
18
-    /**
19
-     * InvalidDataTypeException constructor
20
-     *
21
-     * @param string    $var_name  name of the variable that was of the wrong type ie: "$my_var"
22
-     * @param mixed     $variable  the actual variable that was of the wrong data type, ie: $my_var
23
-     * @param string    $expected  data type we wanted ie: "integer", "string", "array", etc.
24
-     *                             or an entire rewrite of: "{something something} was expected."
25
-     * @param string    $message
26
-     * @param int       $code
27
-     * @param Exception $previous
28
-     */
29
-    public function __construct($var_name, $variable, $expected, $message = '', $code = 0, Exception $previous = null)
30
-    {
31
-        if (empty($message)) {
32
-            $expected = strpos(' was expected.', $expected) === false
33
-                ? $this->addIndefiniteArticle($expected) . ' was expected.'
34
-                : $expected;
35
-            $message = sprintf(
36
-                esc_html__(
37
-                    'The supplied data for "%1$s" was %2$s, but %3$s',
38
-                    'event_espresso'
39
-                ),
40
-                $var_name,
41
-                $this->addIndefiniteArticle(gettype($variable)),
42
-                $expected
43
-            );
44
-        }
45
-        parent::__construct($message, $code, $previous);
46
-    }
18
+	/**
19
+	 * InvalidDataTypeException constructor
20
+	 *
21
+	 * @param string    $var_name  name of the variable that was of the wrong type ie: "$my_var"
22
+	 * @param mixed     $variable  the actual variable that was of the wrong data type, ie: $my_var
23
+	 * @param string    $expected  data type we wanted ie: "integer", "string", "array", etc.
24
+	 *                             or an entire rewrite of: "{something something} was expected."
25
+	 * @param string    $message
26
+	 * @param int       $code
27
+	 * @param Exception $previous
28
+	 */
29
+	public function __construct($var_name, $variable, $expected, $message = '', $code = 0, Exception $previous = null)
30
+	{
31
+		if (empty($message)) {
32
+			$expected = strpos(' was expected.', $expected) === false
33
+				? $this->addIndefiniteArticle($expected) . ' was expected.'
34
+				: $expected;
35
+			$message = sprintf(
36
+				esc_html__(
37
+					'The supplied data for "%1$s" was %2$s, but %3$s',
38
+					'event_espresso'
39
+				),
40
+				$var_name,
41
+				$this->addIndefiniteArticle(gettype($variable)),
42
+				$expected
43
+			);
44
+		}
45
+		parent::__construct($message, $code, $previous);
46
+	}
47 47
 
48 48
 
49
-    /**
50
-     * @param $string
51
-     * @return string
52
-     */
53
-    protected function addIndefiniteArticle($string)
54
-    {
55
-        if (strtolower($string) === 'null') {
56
-            return $string;
57
-        }
58
-        return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ') . $string;
59
-    }
49
+	/**
50
+	 * @param $string
51
+	 * @return string
52
+	 */
53
+	protected function addIndefiniteArticle($string)
54
+	{
55
+		if (strtolower($string) === 'null') {
56
+			return $string;
57
+		}
58
+		return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ') . $string;
59
+	}
60 60
 }
Please login to merge, or discard this patch.
payment_methods/Paypal_Express/templates/paypal_express_intro.template.php 1 patch
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -1,10 +1,10 @@
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 printf(
4
-    esc_html__(
5
-        'PayPal Express (Express Checkout) is an off-site payment method for accepting payments via PayPal and is available to event organizers in many countries. A PayPal premier or business account is needed to accept payments. Need a PayPal account? Call 1-855-456-1338 or %1$sclick here to sign up for a merchant account%2$s.',
6
-        'event_espresso'
7
-    ),
8
-    '<a href="https://eventespresso.com/go/paypalexpress/" target="_blank">',
9
-    '</a>'
4
+	esc_html__(
5
+		'PayPal Express (Express Checkout) is an off-site payment method for accepting payments via PayPal and is available to event organizers in many countries. A PayPal premier or business account is needed to accept payments. Need a PayPal account? Call 1-855-456-1338 or %1$sclick here to sign up for a merchant account%2$s.',
6
+		'event_espresso'
7
+	),
8
+	'<a href="https://eventespresso.com/go/paypalexpress/" target="_blank">',
9
+	'</a>'
10 10
 );
Please login to merge, or discard this patch.
payment_methods/Paypal_Standard/EE_Paypal_Standard_Form.form.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
         $paypal_calculates_taxes = $this->get_input_value('paypal_taxes');
68 68
         $paypal_requests_address_info = $this->get_input_value('shipping_details');
69 69
         if (
70
-            ( $paypal_calculates_shipping || $paypal_calculates_taxes ) &&
70
+            ($paypal_calculates_shipping || $paypal_calculates_taxes) &&
71 71
             $paypal_requests_address_info == EE_PMT_Paypal_Standard::shipping_info_none
72 72
         ) {
73 73
             // they want paypal to calculate taxes or shipping. They need to ask for
@@ -80,8 +80,8 @@  discard block
 block discarded – undo
80 80
                 sprintf(
81 81
                     esc_html__('Automatically set "%s" to "%s" because Paypal requires address info in order to calculate shipping or taxes.', 'event_espresso'),
82 82
                     strip_tags($shipping_details_input->html_label_text()),
83
-                    isset($shipping_details_input_options[ EE_PMT_Paypal_Standard::shipping_info_optional ])
84
-                        ? $shipping_details_input_options[ EE_PMT_Paypal_Standard::shipping_info_optional ]
83
+                    isset($shipping_details_input_options[EE_PMT_Paypal_Standard::shipping_info_optional])
84
+                        ? $shipping_details_input_options[EE_PMT_Paypal_Standard::shipping_info_optional]
85 85
                         : esc_html__('Unknown', 'event_espresso')
86 86
                 ),
87 87
                 __FILE__,
Please login to merge, or discard this patch.
Indentation   +71 added lines, -71 removed lines patch added patch discarded remove patch
@@ -13,78 +13,78 @@
 block discarded – undo
13 13
  */
14 14
 class EE_Paypal_Standard_Form extends EE_Payment_Method_Form
15 15
 {
16
-    /**
17
-     * @param EE_PMT_Paypal_Standard $payment_method_type
18
-     */
19
-    public function __construct($payment_method_type)
20
-    {
21
-        parent::__construct(
22
-            array(
23
-                'payment_method_type'          => $payment_method_type,
24
-                'extra_meta_inputs'            => array(
25
-                    'paypal_id'        => new EE_Text_Input(array(
26
-                        'html_label_text' => sprintf(esc_html__("Paypal Email %s", 'event_espresso'), $payment_method_type->get_help_tab_link()),
27
-                        'html_help_text'  => esc_html__("Typically [email protected]", 'event_espresso'),
28
-                        'required'        => true
29
-                    )),
30
-                    'image_url'        => new EE_Admin_File_Uploader_Input(array(
31
-                        'html_help_text'  => esc_html__("Used for your business/personal logo on the PayPal page", 'event_espresso'),
32
-                        'html_label_text' => esc_html__('Image URL', 'event_espresso')
33
-                    )),
34
-                    'paypal_taxes'     => new EE_Yes_No_Input(array(
35
-                        'html_label_text' => sprintf(esc_html__('Paypal Calculates Taxes %s', 'event_espresso'), $payment_method_type->get_help_tab_link()),
36
-                        'html_help_text'  => esc_html__('Whether Paypal should add taxes to the order', 'event_espresso'),
37
-                        'default'         => false
38
-                    )),
39
-                    'paypal_shipping'  => new EE_Yes_No_Input(array(
40
-                        'html_label_text' => sprintf(esc_html__('Paypal Calculates Shipping %s', 'event_espresso'), $payment_method_type->get_help_tab_link()),
41
-                        'html_help_text'  => esc_html__('Whether Paypal should add shipping surcharges', 'event_espresso'),
42
-                        'default'         => false
43
-                    )),
44
-                    'shipping_details' => new EE_Select_Input(array(
45
-                        EE_PMT_Paypal_Standard::shipping_info_none     => esc_html__("Do not prompt for an address", 'event_espresso'),
46
-                        EE_PMT_Paypal_Standard::shipping_info_optional => esc_html__("Prompt for an address, but do not require it", 'event_espresso'),
47
-                        EE_PMT_Paypal_Standard::shipping_info_required => esc_html__("Prompt for an address, and require it", 'event_espresso')
48
-                    ))
49
-                )
50
-            )
51
-        );
52
-    }
16
+	/**
17
+	 * @param EE_PMT_Paypal_Standard $payment_method_type
18
+	 */
19
+	public function __construct($payment_method_type)
20
+	{
21
+		parent::__construct(
22
+			array(
23
+				'payment_method_type'          => $payment_method_type,
24
+				'extra_meta_inputs'            => array(
25
+					'paypal_id'        => new EE_Text_Input(array(
26
+						'html_label_text' => sprintf(esc_html__("Paypal Email %s", 'event_espresso'), $payment_method_type->get_help_tab_link()),
27
+						'html_help_text'  => esc_html__("Typically [email protected]", 'event_espresso'),
28
+						'required'        => true
29
+					)),
30
+					'image_url'        => new EE_Admin_File_Uploader_Input(array(
31
+						'html_help_text'  => esc_html__("Used for your business/personal logo on the PayPal page", 'event_espresso'),
32
+						'html_label_text' => esc_html__('Image URL', 'event_espresso')
33
+					)),
34
+					'paypal_taxes'     => new EE_Yes_No_Input(array(
35
+						'html_label_text' => sprintf(esc_html__('Paypal Calculates Taxes %s', 'event_espresso'), $payment_method_type->get_help_tab_link()),
36
+						'html_help_text'  => esc_html__('Whether Paypal should add taxes to the order', 'event_espresso'),
37
+						'default'         => false
38
+					)),
39
+					'paypal_shipping'  => new EE_Yes_No_Input(array(
40
+						'html_label_text' => sprintf(esc_html__('Paypal Calculates Shipping %s', 'event_espresso'), $payment_method_type->get_help_tab_link()),
41
+						'html_help_text'  => esc_html__('Whether Paypal should add shipping surcharges', 'event_espresso'),
42
+						'default'         => false
43
+					)),
44
+					'shipping_details' => new EE_Select_Input(array(
45
+						EE_PMT_Paypal_Standard::shipping_info_none     => esc_html__("Do not prompt for an address", 'event_espresso'),
46
+						EE_PMT_Paypal_Standard::shipping_info_optional => esc_html__("Prompt for an address, but do not require it", 'event_espresso'),
47
+						EE_PMT_Paypal_Standard::shipping_info_required => esc_html__("Prompt for an address, and require it", 'event_espresso')
48
+					))
49
+				)
50
+			)
51
+		);
52
+	}
53 53
 
54 54
 
55 55
 
56
-    /**
57
-     * @param array $req_data
58
-     * @throws EE_Error
59
-     */
60
-    public function _normalize($req_data)
61
-    {
62
-        parent::_normalize($req_data);
63
-        $paypal_calculates_shipping = $this->get_input_value('paypal_shipping');
64
-        $paypal_calculates_taxes = $this->get_input_value('paypal_taxes');
65
-        $paypal_requests_address_info = $this->get_input_value('shipping_details');
66
-        if (
67
-            ( $paypal_calculates_shipping || $paypal_calculates_taxes ) &&
68
-            $paypal_requests_address_info == EE_PMT_Paypal_Standard::shipping_info_none
69
-        ) {
70
-            // they want paypal to calculate taxes or shipping. They need to ask for
71
-            // address info, otherwise paypal can't calculate taxes or shipping
72
-            /** @type EE_Select_Input $shipping_details_input */
73
-            $shipping_details_input = $this->get_input('shipping_details');
74
-            $shipping_details_input->set_default(EE_PMT_Paypal_Standard::shipping_info_optional);
75
-            $shipping_details_input_options = $shipping_details_input->options();
76
-            EE_Error::add_attention(
77
-                sprintf(
78
-                    esc_html__('Automatically set "%s" to "%s" because Paypal requires address info in order to calculate shipping or taxes.', 'event_espresso'),
79
-                    strip_tags($shipping_details_input->html_label_text()),
80
-                    isset($shipping_details_input_options[ EE_PMT_Paypal_Standard::shipping_info_optional ])
81
-                        ? $shipping_details_input_options[ EE_PMT_Paypal_Standard::shipping_info_optional ]
82
-                        : esc_html__('Unknown', 'event_espresso')
83
-                ),
84
-                __FILE__,
85
-                __FUNCTION__,
86
-                __LINE__
87
-            );
88
-        }
89
-    }
56
+	/**
57
+	 * @param array $req_data
58
+	 * @throws EE_Error
59
+	 */
60
+	public function _normalize($req_data)
61
+	{
62
+		parent::_normalize($req_data);
63
+		$paypal_calculates_shipping = $this->get_input_value('paypal_shipping');
64
+		$paypal_calculates_taxes = $this->get_input_value('paypal_taxes');
65
+		$paypal_requests_address_info = $this->get_input_value('shipping_details');
66
+		if (
67
+			( $paypal_calculates_shipping || $paypal_calculates_taxes ) &&
68
+			$paypal_requests_address_info == EE_PMT_Paypal_Standard::shipping_info_none
69
+		) {
70
+			// they want paypal to calculate taxes or shipping. They need to ask for
71
+			// address info, otherwise paypal can't calculate taxes or shipping
72
+			/** @type EE_Select_Input $shipping_details_input */
73
+			$shipping_details_input = $this->get_input('shipping_details');
74
+			$shipping_details_input->set_default(EE_PMT_Paypal_Standard::shipping_info_optional);
75
+			$shipping_details_input_options = $shipping_details_input->options();
76
+			EE_Error::add_attention(
77
+				sprintf(
78
+					esc_html__('Automatically set "%s" to "%s" because Paypal requires address info in order to calculate shipping or taxes.', 'event_espresso'),
79
+					strip_tags($shipping_details_input->html_label_text()),
80
+					isset($shipping_details_input_options[ EE_PMT_Paypal_Standard::shipping_info_optional ])
81
+						? $shipping_details_input_options[ EE_PMT_Paypal_Standard::shipping_info_optional ]
82
+						: esc_html__('Unknown', 'event_espresso')
83
+				),
84
+				__FILE__,
85
+				__FUNCTION__,
86
+				__LINE__
87
+			);
88
+		}
89
+	}
90 90
 }
Please login to merge, or discard this patch.
admin_pages/registrations/qtips/Registration_List_Table_Tips.lib.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -84,7 +84,7 @@
 block discarded – undo
84 84
                . esc_html__(
85 85
                    'This lock-icon means that this registration cannot be trashed.  Registrations that belong to a transaction that has payments cannot be trashed.  If you wish to trash this registration then you must delete all payments attached to the related transaction first.',
86 86
                    'event_espresso'
87
-               ) . '</p>';
87
+               ).'</p>';
88 88
     }
89 89
 
90 90
 
Please login to merge, or discard this patch.
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -15,16 +15,16 @@  discard block
 block discarded – undo
15 15
  */
16 16
 class Registration_List_Table_Tips extends EE_Qtip_Config
17 17
 {
18
-    protected function _set_tips_array()
19
-    {
20
-        $this->_qtipsa = array(
21
-            0 => array(
22
-                'content_id' => 'registration-trash-lock',
23
-                'target'     => '.ee-lock-icon',
24
-                'content'    => $this->_registration_trash_message(),
25
-            ),
26
-            /** removing status strips for now because they are triggered anywhere on the row. */
27
-            /**1 => array(
18
+	protected function _set_tips_array()
19
+	{
20
+		$this->_qtipsa = array(
21
+			0 => array(
22
+				'content_id' => 'registration-trash-lock',
23
+				'target'     => '.ee-lock-icon',
24
+				'content'    => $this->_registration_trash_message(),
25
+			),
26
+			/** removing status strips for now because they are triggered anywhere on the row. */
27
+			/**1 => array(
28 28
              * 'content_id' => 'registration-status-' . RegStatus::APPROVED,
29 29
              * 'target' => '.reg-status-' . RegStatus::APPROVED,
30 30
              * 'content' => $this->_registration_status_legend(RegStatus::APPROVED),
@@ -74,37 +74,37 @@  discard block
 block discarded – undo
74 74
              * )
75 75
              * )
76 76
              * )/**/
77
-        );
78
-    }
77
+		);
78
+	}
79 79
 
80 80
 
81
-    private function _registration_trash_message()
82
-    {
83
-        return '<p>'
84
-               . esc_html__(
85
-                   'This lock-icon means that this registration cannot be trashed.  Registrations that belong to a transaction that has payments cannot be trashed.  If you wish to trash this registration then you must delete all payments attached to the related transaction first.',
86
-                   'event_espresso'
87
-               ) . '</p>';
88
-    }
81
+	private function _registration_trash_message()
82
+	{
83
+		return '<p>'
84
+			   . esc_html__(
85
+				   'This lock-icon means that this registration cannot be trashed.  Registrations that belong to a transaction that has payments cannot be trashed.  If you wish to trash this registration then you must delete all payments attached to the related transaction first.',
86
+				   'event_espresso'
87
+			   ) . '</p>';
88
+	}
89 89
 
90 90
 
91
-    /**
92
-     * output the relevant ee-status-legend with the designated status highlighted.
93
-     *
94
-     * @param  EEM_Registration constant $status What status is set (by class)
95
-     * @return string         The status legend with the related status highlighted
96
-     */
97
-    private function _registration_status_legend($status)
98
-    {
91
+	/**
92
+	 * output the relevant ee-status-legend with the designated status highlighted.
93
+	 *
94
+	 * @param  EEM_Registration constant $status What status is set (by class)
95
+	 * @return string         The status legend with the related status highlighted
96
+	 */
97
+	private function _registration_status_legend($status)
98
+	{
99 99
 
100
-        $status_array = array(
101
-            'approved_status' => RegStatus::APPROVED,
102
-            'pending_status' => RegStatus::PENDING_PAYMENT,
103
-            'not_approved' => RegStatus::AWAITING_REVIEW,
104
-            'declined_status' => RegStatus::DECLINED,
105
-            'cancelled_status' => RegStatus::CANCELLED,
106
-        );
100
+		$status_array = array(
101
+			'approved_status' => RegStatus::APPROVED,
102
+			'pending_status' => RegStatus::PENDING_PAYMENT,
103
+			'not_approved' => RegStatus::AWAITING_REVIEW,
104
+			'declined_status' => RegStatus::DECLINED,
105
+			'cancelled_status' => RegStatus::CANCELLED,
106
+		);
107 107
 
108
-        return EEH_Template::status_legend($status_array, $status);
109
-    }
108
+		return EEH_Template::status_legend($status_array, $status);
109
+	}
110 110
 }
Please login to merge, or discard this patch.