@@ -67,9 +67,9 @@ discard block |
||
67 | 67 | */ |
68 | 68 | public function prefixes($prefix = '') |
69 | 69 | { |
70 | - if (! empty($prefix)) { |
|
70 | + if ( ! empty($prefix)) { |
|
71 | 71 | // are there any base directories for this namespace prefix? |
72 | - return isset($this->prefixes[ $prefix ]) ? $this->prefixes[ $prefix ] : array(); |
|
72 | + return isset($this->prefixes[$prefix]) ? $this->prefixes[$prefix] : array(); |
|
73 | 73 | } |
74 | 74 | return $this->prefixes; |
75 | 75 | } |
@@ -100,18 +100,18 @@ discard block |
||
100 | 100 | public function addNamespace($prefix, $base_dir, $prepend = false) |
101 | 101 | { |
102 | 102 | // normalize namespace prefix |
103 | - $prefix = trim($prefix, Psr4Autoloader::NS) . Psr4Autoloader::NS; |
|
103 | + $prefix = trim($prefix, Psr4Autoloader::NS).Psr4Autoloader::NS; |
|
104 | 104 | // normalize the base directory with a trailing separator |
105 | 105 | $base_dir = \EEH_File::standardise_and_end_with_directory_separator($base_dir); |
106 | 106 | // initialize the namespace prefix array |
107 | - if (isset($this->prefixes[ $prefix ]) === false) { |
|
108 | - $this->prefixes[ $prefix ] = array(); |
|
107 | + if (isset($this->prefixes[$prefix]) === false) { |
|
108 | + $this->prefixes[$prefix] = array(); |
|
109 | 109 | } |
110 | 110 | // retain the base directory for the namespace prefix |
111 | 111 | if ($prepend) { |
112 | - array_unshift($this->prefixes[ $prefix ], $base_dir); |
|
112 | + array_unshift($this->prefixes[$prefix], $base_dir); |
|
113 | 113 | } else { |
114 | - $this->prefixes[ $prefix ][] = $base_dir; |
|
114 | + $this->prefixes[$prefix][] = $base_dir; |
|
115 | 115 | } |
116 | 116 | } |
117 | 117 |
@@ -45,150 +45,150 @@ |
||
45 | 45 | class Psr4Autoloader |
46 | 46 | { |
47 | 47 | |
48 | - /** |
|
49 | - * namespace separator |
|
50 | - */ |
|
51 | - const NS = '\\'; |
|
52 | - |
|
53 | - /** |
|
54 | - * An associative array where the key is a namespace prefix and the value |
|
55 | - * is an array of base directories for classes in that namespace. |
|
56 | - * |
|
57 | - * @var array |
|
58 | - */ |
|
59 | - protected $prefixes = array(); |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * returns an array of registered namespace prefixes |
|
64 | - * |
|
65 | - * @param string $prefix |
|
66 | - * @return array |
|
67 | - */ |
|
68 | - public function prefixes($prefix = '') |
|
69 | - { |
|
70 | - if (! empty($prefix)) { |
|
71 | - // are there any base directories for this namespace prefix? |
|
72 | - return isset($this->prefixes[ $prefix ]) ? $this->prefixes[ $prefix ] : array(); |
|
73 | - } |
|
74 | - return $this->prefixes; |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * Register loader with SPL autoloader stack. |
|
80 | - * |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - public function register() |
|
84 | - { |
|
85 | - spl_autoload_register(array($this, 'loadClass')); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Adds a base directory for a namespace prefix. |
|
91 | - * |
|
92 | - * @param string $prefix The namespace prefix. |
|
93 | - * @param string $base_dir A base directory for class files in the |
|
94 | - * namespace. |
|
95 | - * @param bool $prepend If true, prepend the base directory to the stack |
|
96 | - * instead of appending it; this causes it to be searched first rather |
|
97 | - * than last. |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - public function addNamespace($prefix, $base_dir, $prepend = false) |
|
101 | - { |
|
102 | - // normalize namespace prefix |
|
103 | - $prefix = trim($prefix, Psr4Autoloader::NS) . Psr4Autoloader::NS; |
|
104 | - // normalize the base directory with a trailing separator |
|
105 | - $base_dir = \EEH_File::standardise_and_end_with_directory_separator($base_dir); |
|
106 | - // initialize the namespace prefix array |
|
107 | - if (isset($this->prefixes[ $prefix ]) === false) { |
|
108 | - $this->prefixes[ $prefix ] = array(); |
|
109 | - } |
|
110 | - // retain the base directory for the namespace prefix |
|
111 | - if ($prepend) { |
|
112 | - array_unshift($this->prefixes[ $prefix ], $base_dir); |
|
113 | - } else { |
|
114 | - $this->prefixes[ $prefix ][] = $base_dir; |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * Loads the class file for a given class name. |
|
121 | - * |
|
122 | - * @param string $class The fully-qualified class name. |
|
123 | - * @return mixed The mapped file name on success, or boolean false on |
|
124 | - * failure. |
|
125 | - */ |
|
126 | - public function loadClass($class) |
|
127 | - { |
|
128 | - // the current namespace prefix |
|
129 | - $prefix = $class; |
|
130 | - // work backwards through the namespace names of the fully-qualified |
|
131 | - // class name to find a mapped file name |
|
132 | - while (false !== $pos = strrpos($prefix, Psr4Autoloader::NS)) { |
|
133 | - // retain the trailing namespace separator in the prefix |
|
134 | - $prefix = substr($class, 0, $pos + 1); |
|
135 | - // the rest is the relative class name |
|
136 | - $relative_class = substr($class, $pos + 1); |
|
137 | - // try to load a mapped file for the prefix and relative class |
|
138 | - $mapped_file = $this->loadMappedFile($prefix, $relative_class); |
|
139 | - if ($mapped_file) { |
|
140 | - return $mapped_file; |
|
141 | - } |
|
142 | - // remove the trailing namespace separator for the next iteration |
|
143 | - // of strrpos() |
|
144 | - $prefix = rtrim($prefix, Psr4Autoloader::NS); |
|
145 | - } |
|
146 | - // never found a mapped file |
|
147 | - return false; |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * Load the mapped file for a namespace prefix and relative class. |
|
153 | - * |
|
154 | - * @param string $prefix The namespace prefix. |
|
155 | - * @param string $relative_class The relative class name. |
|
156 | - * @return mixed Boolean false if no mapped file can be loaded, or the |
|
157 | - * name of the mapped file that was loaded. |
|
158 | - */ |
|
159 | - protected function loadMappedFile($prefix, $relative_class) |
|
160 | - { |
|
161 | - // look through base directories for this namespace prefix |
|
162 | - foreach ($this->prefixes($prefix) as $base_dir) { |
|
163 | - // replace the namespace prefix with the base directory, |
|
164 | - // replace namespace separators with directory separators |
|
165 | - // in the relative class name, append with .php |
|
166 | - $file = $base_dir |
|
167 | - . str_replace(Psr4Autoloader::NS, '/', $relative_class) |
|
168 | - . '.php'; |
|
169 | - // if the mapped file exists, require it |
|
170 | - if ($this->requireFile($file)) { |
|
171 | - // yes, we're done |
|
172 | - return $file; |
|
173 | - } |
|
174 | - } |
|
175 | - // never found it |
|
176 | - return false; |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * If a file exists, require it from the file system. |
|
182 | - * |
|
183 | - * @param string $file The file to require. |
|
184 | - * @return bool True if the file exists, false if not. |
|
185 | - */ |
|
186 | - protected function requireFile($file) |
|
187 | - { |
|
188 | - if (file_exists($file)) { |
|
189 | - require $file; |
|
190 | - return true; |
|
191 | - } |
|
192 | - return false; |
|
193 | - } |
|
48 | + /** |
|
49 | + * namespace separator |
|
50 | + */ |
|
51 | + const NS = '\\'; |
|
52 | + |
|
53 | + /** |
|
54 | + * An associative array where the key is a namespace prefix and the value |
|
55 | + * is an array of base directories for classes in that namespace. |
|
56 | + * |
|
57 | + * @var array |
|
58 | + */ |
|
59 | + protected $prefixes = array(); |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * returns an array of registered namespace prefixes |
|
64 | + * |
|
65 | + * @param string $prefix |
|
66 | + * @return array |
|
67 | + */ |
|
68 | + public function prefixes($prefix = '') |
|
69 | + { |
|
70 | + if (! empty($prefix)) { |
|
71 | + // are there any base directories for this namespace prefix? |
|
72 | + return isset($this->prefixes[ $prefix ]) ? $this->prefixes[ $prefix ] : array(); |
|
73 | + } |
|
74 | + return $this->prefixes; |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * Register loader with SPL autoloader stack. |
|
80 | + * |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function register() |
|
84 | + { |
|
85 | + spl_autoload_register(array($this, 'loadClass')); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Adds a base directory for a namespace prefix. |
|
91 | + * |
|
92 | + * @param string $prefix The namespace prefix. |
|
93 | + * @param string $base_dir A base directory for class files in the |
|
94 | + * namespace. |
|
95 | + * @param bool $prepend If true, prepend the base directory to the stack |
|
96 | + * instead of appending it; this causes it to be searched first rather |
|
97 | + * than last. |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + public function addNamespace($prefix, $base_dir, $prepend = false) |
|
101 | + { |
|
102 | + // normalize namespace prefix |
|
103 | + $prefix = trim($prefix, Psr4Autoloader::NS) . Psr4Autoloader::NS; |
|
104 | + // normalize the base directory with a trailing separator |
|
105 | + $base_dir = \EEH_File::standardise_and_end_with_directory_separator($base_dir); |
|
106 | + // initialize the namespace prefix array |
|
107 | + if (isset($this->prefixes[ $prefix ]) === false) { |
|
108 | + $this->prefixes[ $prefix ] = array(); |
|
109 | + } |
|
110 | + // retain the base directory for the namespace prefix |
|
111 | + if ($prepend) { |
|
112 | + array_unshift($this->prefixes[ $prefix ], $base_dir); |
|
113 | + } else { |
|
114 | + $this->prefixes[ $prefix ][] = $base_dir; |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * Loads the class file for a given class name. |
|
121 | + * |
|
122 | + * @param string $class The fully-qualified class name. |
|
123 | + * @return mixed The mapped file name on success, or boolean false on |
|
124 | + * failure. |
|
125 | + */ |
|
126 | + public function loadClass($class) |
|
127 | + { |
|
128 | + // the current namespace prefix |
|
129 | + $prefix = $class; |
|
130 | + // work backwards through the namespace names of the fully-qualified |
|
131 | + // class name to find a mapped file name |
|
132 | + while (false !== $pos = strrpos($prefix, Psr4Autoloader::NS)) { |
|
133 | + // retain the trailing namespace separator in the prefix |
|
134 | + $prefix = substr($class, 0, $pos + 1); |
|
135 | + // the rest is the relative class name |
|
136 | + $relative_class = substr($class, $pos + 1); |
|
137 | + // try to load a mapped file for the prefix and relative class |
|
138 | + $mapped_file = $this->loadMappedFile($prefix, $relative_class); |
|
139 | + if ($mapped_file) { |
|
140 | + return $mapped_file; |
|
141 | + } |
|
142 | + // remove the trailing namespace separator for the next iteration |
|
143 | + // of strrpos() |
|
144 | + $prefix = rtrim($prefix, Psr4Autoloader::NS); |
|
145 | + } |
|
146 | + // never found a mapped file |
|
147 | + return false; |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * Load the mapped file for a namespace prefix and relative class. |
|
153 | + * |
|
154 | + * @param string $prefix The namespace prefix. |
|
155 | + * @param string $relative_class The relative class name. |
|
156 | + * @return mixed Boolean false if no mapped file can be loaded, or the |
|
157 | + * name of the mapped file that was loaded. |
|
158 | + */ |
|
159 | + protected function loadMappedFile($prefix, $relative_class) |
|
160 | + { |
|
161 | + // look through base directories for this namespace prefix |
|
162 | + foreach ($this->prefixes($prefix) as $base_dir) { |
|
163 | + // replace the namespace prefix with the base directory, |
|
164 | + // replace namespace separators with directory separators |
|
165 | + // in the relative class name, append with .php |
|
166 | + $file = $base_dir |
|
167 | + . str_replace(Psr4Autoloader::NS, '/', $relative_class) |
|
168 | + . '.php'; |
|
169 | + // if the mapped file exists, require it |
|
170 | + if ($this->requireFile($file)) { |
|
171 | + // yes, we're done |
|
172 | + return $file; |
|
173 | + } |
|
174 | + } |
|
175 | + // never found it |
|
176 | + return false; |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * If a file exists, require it from the file system. |
|
182 | + * |
|
183 | + * @param string $file The file to require. |
|
184 | + * @return bool True if the file exists, false if not. |
|
185 | + */ |
|
186 | + protected function requireFile($file) |
|
187 | + { |
|
188 | + if (file_exists($file)) { |
|
189 | + require $file; |
|
190 | + return true; |
|
191 | + } |
|
192 | + return false; |
|
193 | + } |
|
194 | 194 | } |
@@ -13,121 +13,121 @@ |
||
13 | 13 | class PostRelatedCacheManager extends BasicCacheManager |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * @type string |
|
18 | - */ |
|
19 | - const POST_CACHE_PREFIX = 'ee_cache_post_'; |
|
20 | - |
|
21 | - /** |
|
22 | - * wp-option option_name for tracking post related cache |
|
23 | - * |
|
24 | - * @type string |
|
25 | - */ |
|
26 | - const POST_CACHE_OPTIONS_KEY = 'ee_post_cache'; |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * PostRelatedCacheManager constructor. |
|
31 | - * |
|
32 | - * @param CacheStorageInterface $cache_storage |
|
33 | - */ |
|
34 | - public function __construct(CacheStorageInterface $cache_storage) |
|
35 | - { |
|
36 | - parent::__construct($cache_storage); |
|
37 | - add_action('save_post', array($this, 'clearPostRelatedCache')); |
|
38 | - } |
|
39 | - |
|
40 | - |
|
41 | - /** |
|
42 | - * returns a string that will be prepended to all cache identifiers |
|
43 | - * |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - public function cachePrefix() |
|
47 | - { |
|
48 | - return PostRelatedCacheManager::POST_CACHE_PREFIX; |
|
49 | - } |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * @return array |
|
54 | - */ |
|
55 | - protected function getPostRelatedCache() |
|
56 | - { |
|
57 | - $post_related_cache = get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
58 | - // verify that cached data was not truncated or corrupted and no longer an array |
|
59 | - if (! is_array($post_related_cache)) { |
|
60 | - // uh-oh... let's get rid of any transients using our cache prefix |
|
61 | - $this->clear(PostRelatedCacheManager::CACHE_PREFIX); |
|
62 | - // then update the post related cache tracking option |
|
63 | - $post_related_cache = array(); |
|
64 | - $this->updatePostRelatedCache($post_related_cache); |
|
65 | - } |
|
66 | - return $post_related_cache; |
|
67 | - } |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * @param array $post_related_cache |
|
72 | - */ |
|
73 | - protected function updatePostRelatedCache(array $post_related_cache = array()) |
|
74 | - { |
|
75 | - update_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, $post_related_cache); |
|
76 | - } |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * If you are caching content that pertains to a Post of any type, |
|
81 | - * then it is recommended to pass the post id and cache id prefix to this method |
|
82 | - * so that it can be added to the post related cache tracking. |
|
83 | - * Then, whenever that post is updated, the cache will automatically be deleted, |
|
84 | - * which helps to ensure that outdated cache content will not be served |
|
85 | - * |
|
86 | - * @param int $post_ID [required] |
|
87 | - * @param string $id_prefix [required] Appended to all cache IDs. Can be helpful in finding specific cache types. |
|
88 | - * May also be helpful to include an additional specific identifier, |
|
89 | - * such as a post ID as part of the $id_prefix so that individual caches |
|
90 | - * can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
91 | - * BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
92 | - */ |
|
93 | - public function clearPostRelatedCacheOnUpdate($post_ID, $id_prefix) |
|
94 | - { |
|
95 | - $post_related_cache = $this->getPostRelatedCache(); |
|
96 | - // if post is not already being tracked |
|
97 | - if (! isset($post_related_cache[ $post_ID ])) { |
|
98 | - // add array to add cache ids to |
|
99 | - $post_related_cache[ $post_ID ] = array(); |
|
100 | - } |
|
101 | - if (! in_array($id_prefix, $post_related_cache[ $post_ID ], true)) { |
|
102 | - // add cache id to be tracked |
|
103 | - $post_related_cache[ $post_ID ][] = $id_prefix; |
|
104 | - $this->updatePostRelatedCache($post_related_cache); |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * callback hooked into the WordPress "save_post" action |
|
111 | - * deletes any cache content associated with the post |
|
112 | - * |
|
113 | - * @param int $post_ID [required] |
|
114 | - */ |
|
115 | - public function clearPostRelatedCache($post_ID) |
|
116 | - { |
|
117 | - $post_related_cache = $this->getPostRelatedCache(); |
|
118 | - // if post is not being tracked |
|
119 | - if (! isset($post_related_cache[ $post_ID ])) { |
|
120 | - // let's clean up some of the duplicate IDs that were getting added |
|
121 | - foreach ($post_related_cache as $other_post_ID => $cache_IDs) { |
|
122 | - // remove duplicates |
|
123 | - $post_related_cache[ $other_post_ID ] = array_unique($post_related_cache[ $other_post_ID ]); |
|
124 | - } |
|
125 | - $this->updatePostRelatedCache($post_related_cache); |
|
126 | - return; |
|
127 | - } |
|
128 | - // get cache id prefixes for post, and delete their corresponding transients |
|
129 | - $this->clear($post_related_cache[ $post_ID ]); |
|
130 | - unset($post_related_cache[ $post_ID ]); |
|
131 | - $this->updatePostRelatedCache($post_related_cache); |
|
132 | - } |
|
16 | + /** |
|
17 | + * @type string |
|
18 | + */ |
|
19 | + const POST_CACHE_PREFIX = 'ee_cache_post_'; |
|
20 | + |
|
21 | + /** |
|
22 | + * wp-option option_name for tracking post related cache |
|
23 | + * |
|
24 | + * @type string |
|
25 | + */ |
|
26 | + const POST_CACHE_OPTIONS_KEY = 'ee_post_cache'; |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * PostRelatedCacheManager constructor. |
|
31 | + * |
|
32 | + * @param CacheStorageInterface $cache_storage |
|
33 | + */ |
|
34 | + public function __construct(CacheStorageInterface $cache_storage) |
|
35 | + { |
|
36 | + parent::__construct($cache_storage); |
|
37 | + add_action('save_post', array($this, 'clearPostRelatedCache')); |
|
38 | + } |
|
39 | + |
|
40 | + |
|
41 | + /** |
|
42 | + * returns a string that will be prepended to all cache identifiers |
|
43 | + * |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + public function cachePrefix() |
|
47 | + { |
|
48 | + return PostRelatedCacheManager::POST_CACHE_PREFIX; |
|
49 | + } |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * @return array |
|
54 | + */ |
|
55 | + protected function getPostRelatedCache() |
|
56 | + { |
|
57 | + $post_related_cache = get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
58 | + // verify that cached data was not truncated or corrupted and no longer an array |
|
59 | + if (! is_array($post_related_cache)) { |
|
60 | + // uh-oh... let's get rid of any transients using our cache prefix |
|
61 | + $this->clear(PostRelatedCacheManager::CACHE_PREFIX); |
|
62 | + // then update the post related cache tracking option |
|
63 | + $post_related_cache = array(); |
|
64 | + $this->updatePostRelatedCache($post_related_cache); |
|
65 | + } |
|
66 | + return $post_related_cache; |
|
67 | + } |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * @param array $post_related_cache |
|
72 | + */ |
|
73 | + protected function updatePostRelatedCache(array $post_related_cache = array()) |
|
74 | + { |
|
75 | + update_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, $post_related_cache); |
|
76 | + } |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * If you are caching content that pertains to a Post of any type, |
|
81 | + * then it is recommended to pass the post id and cache id prefix to this method |
|
82 | + * so that it can be added to the post related cache tracking. |
|
83 | + * Then, whenever that post is updated, the cache will automatically be deleted, |
|
84 | + * which helps to ensure that outdated cache content will not be served |
|
85 | + * |
|
86 | + * @param int $post_ID [required] |
|
87 | + * @param string $id_prefix [required] Appended to all cache IDs. Can be helpful in finding specific cache types. |
|
88 | + * May also be helpful to include an additional specific identifier, |
|
89 | + * such as a post ID as part of the $id_prefix so that individual caches |
|
90 | + * can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
91 | + * BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
92 | + */ |
|
93 | + public function clearPostRelatedCacheOnUpdate($post_ID, $id_prefix) |
|
94 | + { |
|
95 | + $post_related_cache = $this->getPostRelatedCache(); |
|
96 | + // if post is not already being tracked |
|
97 | + if (! isset($post_related_cache[ $post_ID ])) { |
|
98 | + // add array to add cache ids to |
|
99 | + $post_related_cache[ $post_ID ] = array(); |
|
100 | + } |
|
101 | + if (! in_array($id_prefix, $post_related_cache[ $post_ID ], true)) { |
|
102 | + // add cache id to be tracked |
|
103 | + $post_related_cache[ $post_ID ][] = $id_prefix; |
|
104 | + $this->updatePostRelatedCache($post_related_cache); |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * callback hooked into the WordPress "save_post" action |
|
111 | + * deletes any cache content associated with the post |
|
112 | + * |
|
113 | + * @param int $post_ID [required] |
|
114 | + */ |
|
115 | + public function clearPostRelatedCache($post_ID) |
|
116 | + { |
|
117 | + $post_related_cache = $this->getPostRelatedCache(); |
|
118 | + // if post is not being tracked |
|
119 | + if (! isset($post_related_cache[ $post_ID ])) { |
|
120 | + // let's clean up some of the duplicate IDs that were getting added |
|
121 | + foreach ($post_related_cache as $other_post_ID => $cache_IDs) { |
|
122 | + // remove duplicates |
|
123 | + $post_related_cache[ $other_post_ID ] = array_unique($post_related_cache[ $other_post_ID ]); |
|
124 | + } |
|
125 | + $this->updatePostRelatedCache($post_related_cache); |
|
126 | + return; |
|
127 | + } |
|
128 | + // get cache id prefixes for post, and delete their corresponding transients |
|
129 | + $this->clear($post_related_cache[ $post_ID ]); |
|
130 | + unset($post_related_cache[ $post_ID ]); |
|
131 | + $this->updatePostRelatedCache($post_related_cache); |
|
132 | + } |
|
133 | 133 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | { |
57 | 57 | $post_related_cache = get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
58 | 58 | // verify that cached data was not truncated or corrupted and no longer an array |
59 | - if (! is_array($post_related_cache)) { |
|
59 | + if ( ! is_array($post_related_cache)) { |
|
60 | 60 | // uh-oh... let's get rid of any transients using our cache prefix |
61 | 61 | $this->clear(PostRelatedCacheManager::CACHE_PREFIX); |
62 | 62 | // then update the post related cache tracking option |
@@ -94,13 +94,13 @@ discard block |
||
94 | 94 | { |
95 | 95 | $post_related_cache = $this->getPostRelatedCache(); |
96 | 96 | // if post is not already being tracked |
97 | - if (! isset($post_related_cache[ $post_ID ])) { |
|
97 | + if ( ! isset($post_related_cache[$post_ID])) { |
|
98 | 98 | // add array to add cache ids to |
99 | - $post_related_cache[ $post_ID ] = array(); |
|
99 | + $post_related_cache[$post_ID] = array(); |
|
100 | 100 | } |
101 | - if (! in_array($id_prefix, $post_related_cache[ $post_ID ], true)) { |
|
101 | + if ( ! in_array($id_prefix, $post_related_cache[$post_ID], true)) { |
|
102 | 102 | // add cache id to be tracked |
103 | - $post_related_cache[ $post_ID ][] = $id_prefix; |
|
103 | + $post_related_cache[$post_ID][] = $id_prefix; |
|
104 | 104 | $this->updatePostRelatedCache($post_related_cache); |
105 | 105 | } |
106 | 106 | } |
@@ -116,18 +116,18 @@ discard block |
||
116 | 116 | { |
117 | 117 | $post_related_cache = $this->getPostRelatedCache(); |
118 | 118 | // if post is not being tracked |
119 | - if (! isset($post_related_cache[ $post_ID ])) { |
|
119 | + if ( ! isset($post_related_cache[$post_ID])) { |
|
120 | 120 | // let's clean up some of the duplicate IDs that were getting added |
121 | 121 | foreach ($post_related_cache as $other_post_ID => $cache_IDs) { |
122 | 122 | // remove duplicates |
123 | - $post_related_cache[ $other_post_ID ] = array_unique($post_related_cache[ $other_post_ID ]); |
|
123 | + $post_related_cache[$other_post_ID] = array_unique($post_related_cache[$other_post_ID]); |
|
124 | 124 | } |
125 | 125 | $this->updatePostRelatedCache($post_related_cache); |
126 | 126 | return; |
127 | 127 | } |
128 | 128 | // get cache id prefixes for post, and delete their corresponding transients |
129 | - $this->clear($post_related_cache[ $post_ID ]); |
|
130 | - unset($post_related_cache[ $post_ID ]); |
|
129 | + $this->clear($post_related_cache[$post_ID]); |
|
130 | + unset($post_related_cache[$post_ID]); |
|
131 | 131 | $this->updatePostRelatedCache($post_related_cache); |
132 | 132 | } |
133 | 133 | } |
@@ -16,366 +16,366 @@ |
||
16 | 16 | class TransientCacheStorage implements CacheStorageInterface |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * wp-option option_name for tracking transients |
|
21 | - * |
|
22 | - * @type string |
|
23 | - */ |
|
24 | - const TRANSIENT_SCHEDULE_OPTIONS_KEY = 'ee_transient_schedule'; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var int $current_time |
|
28 | - */ |
|
29 | - private $current_time; |
|
30 | - |
|
31 | - /** |
|
32 | - * how often to perform transient cleanup |
|
33 | - * |
|
34 | - * @var string $transient_cleanup_frequency |
|
35 | - */ |
|
36 | - private $transient_cleanup_frequency; |
|
37 | - |
|
38 | - /** |
|
39 | - * options for how often to perform transient cleanup |
|
40 | - * |
|
41 | - * @var array $transient_cleanup_frequency_options |
|
42 | - */ |
|
43 | - private $transient_cleanup_frequency_options = array(); |
|
44 | - |
|
45 | - /** |
|
46 | - * @var array $transients |
|
47 | - */ |
|
48 | - private $transients; |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * TransientCacheStorage constructor. |
|
53 | - */ |
|
54 | - public function __construct() |
|
55 | - { |
|
56 | - $this->transient_cleanup_frequency = $this->setTransientCleanupFrequency(); |
|
57 | - // round current time down to closest 5 minutes to simplify scheduling |
|
58 | - $this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
|
59 | - $this->transients = (array) get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
|
60 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
61 | - add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * Sets how often transient cleanup occurs |
|
68 | - * |
|
69 | - * @return string |
|
70 | - */ |
|
71 | - private function setTransientCleanupFrequency() |
|
72 | - { |
|
73 | - // sets how often transients are cleaned up |
|
74 | - $this->transient_cleanup_frequency_options = apply_filters( |
|
75 | - 'FHEE__TransientCacheStorage__transient_cleanup_schedule_options', |
|
76 | - array( |
|
77 | - 'off', |
|
78 | - '15-minutes', |
|
79 | - 'hour', |
|
80 | - '12-hours', |
|
81 | - 'day', |
|
82 | - ) |
|
83 | - ); |
|
84 | - $transient_cleanup_frequency = apply_filters( |
|
85 | - 'FHEE__TransientCacheStorage__transient_cleanup_schedule', |
|
86 | - 'hour' |
|
87 | - ); |
|
88 | - return in_array( |
|
89 | - $transient_cleanup_frequency, |
|
90 | - $this->transient_cleanup_frequency_options, |
|
91 | - true |
|
92 | - ) |
|
93 | - ? $transient_cleanup_frequency |
|
94 | - : 'hour'; |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * we need to be able to round timestamps off to match the set transient cleanup frequency |
|
100 | - * so if a transient is set to expire at 1:17 pm for example, and our cleanup schedule is every hour, |
|
101 | - * then that timestamp needs to be rounded up to 2:00 pm so that it is removed |
|
102 | - * during the next scheduled cleanup after its expiration. |
|
103 | - * We also round off the current time timestamp to the closest 5 minutes |
|
104 | - * just to make the timestamps a little easier to round which helps with debugging. |
|
105 | - * |
|
106 | - * @param int $timestamp [required] |
|
107 | - * @param string $cleanup_frequency |
|
108 | - * @param bool $round_up |
|
109 | - * @return int |
|
110 | - */ |
|
111 | - private function roundTimestamp($timestamp, $cleanup_frequency = 'hour', $round_up = true) |
|
112 | - { |
|
113 | - $cleanup_frequency = $cleanup_frequency ? $cleanup_frequency : $this->transient_cleanup_frequency; |
|
114 | - // in order to round the time to the closest xx minutes (or hours), |
|
115 | - // we take the minutes (or hours) portion of the timestamp and divide it by xx, |
|
116 | - // round down to a whole number, then multiply by xx to bring us almost back up to where we were |
|
117 | - // why round down ? so the minutes (or hours) don't go over 60 (or 24) |
|
118 | - // and bump the hour, which could bump the day, which could bump the month, etc, |
|
119 | - // which would be bad because we don't always want to round up, |
|
120 | - // but when we do we can easily achieve that by simply adding the desired offset, |
|
121 | - $minutes = '00'; |
|
122 | - $hours = 'H'; |
|
123 | - switch ($cleanup_frequency) { |
|
124 | - case '5-minutes': |
|
125 | - $minutes = floor((int) date('i', $timestamp) / 5) * 5; |
|
126 | - $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
127 | - $offset = MINUTE_IN_SECONDS * 5; |
|
128 | - break; |
|
129 | - case '15-minutes': |
|
130 | - $minutes = floor((int) date('i', $timestamp) / 15) * 15; |
|
131 | - $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
132 | - $offset = MINUTE_IN_SECONDS * 15; |
|
133 | - break; |
|
134 | - case '12-hours': |
|
135 | - $hours = floor((int) date('H', $timestamp) / 12) * 12; |
|
136 | - $hours = str_pad($hours, 2, '0', STR_PAD_LEFT); |
|
137 | - $offset = HOUR_IN_SECONDS * 12; |
|
138 | - break; |
|
139 | - case 'day': |
|
140 | - $hours = '03'; // run cleanup at 3:00 am (or first site hit after that) |
|
141 | - $offset = DAY_IN_SECONDS; |
|
142 | - break; |
|
143 | - case 'hour': |
|
144 | - default: |
|
145 | - $offset = HOUR_IN_SECONDS; |
|
146 | - break; |
|
147 | - } |
|
148 | - $rounded_timestamp = (int) strtotime(date("Y-m-d {$hours}:{$minutes}:00", $timestamp)); |
|
149 | - $rounded_timestamp += $round_up ? $offset : 0; |
|
150 | - return apply_filters( |
|
151 | - 'FHEE__TransientCacheStorage__roundTimestamp__timestamp', |
|
152 | - $rounded_timestamp, |
|
153 | - $timestamp, |
|
154 | - $cleanup_frequency, |
|
155 | - $round_up |
|
156 | - ); |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * Saves supplied data to a transient |
|
162 | - * if an expiration is set, then it automatically schedules the transient for cleanup |
|
163 | - * |
|
164 | - * @param string $transient_key [required] |
|
165 | - * @param string $data [required] |
|
166 | - * @param int $expiration number of seconds until the cache expires |
|
167 | - * @return bool |
|
168 | - */ |
|
169 | - public function add($transient_key, $data, $expiration = 0) |
|
170 | - { |
|
171 | - $expiration = (int) abs($expiration); |
|
172 | - $saved = set_transient($transient_key, $data, $expiration); |
|
173 | - if ($saved && $expiration) { |
|
174 | - $this->scheduleTransientCleanup($transient_key, $expiration); |
|
175 | - } |
|
176 | - return $saved; |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * retrieves transient data |
|
182 | - * automatically triggers early cache refresh for standard cache items |
|
183 | - * in order to avoid cache stampedes on busy sites. |
|
184 | - * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
|
185 | - * the $standard_cache parameter should be set to false when retrieving data |
|
186 | - * |
|
187 | - * @param string $transient_key [required] |
|
188 | - * @param bool $standard_cache |
|
189 | - * @return mixed|null |
|
190 | - */ |
|
191 | - public function get($transient_key, $standard_cache = true) |
|
192 | - { |
|
193 | - if (isset($this->transients[ $transient_key ])) { |
|
194 | - // to avoid cache stampedes (AKA:dogpiles) for standard cache items, |
|
195 | - // check if known cache expires within the next minute, |
|
196 | - // and if so, remove it from our tracking and and return nothing. |
|
197 | - // this should trigger the cache content to be regenerated during this request, |
|
198 | - // while allowing any following requests to still access the existing cache |
|
199 | - // until it gets replaced with the refreshed content |
|
200 | - if ($standard_cache |
|
201 | - && $this->transients[ $transient_key ] - time() <= MINUTE_IN_SECONDS |
|
202 | - ) { |
|
203 | - unset($this->transients[ $transient_key ]); |
|
204 | - $this->updateTransients(); |
|
205 | - return null; |
|
206 | - } |
|
207 | - |
|
208 | - // for non standard cache items, remove the key from our tracking, |
|
209 | - // but proceed to retrieve the transient so that it also gets removed from the db |
|
210 | - if ($this->transients[ $transient_key ] <= time()) { |
|
211 | - unset($this->transients[ $transient_key ]); |
|
212 | - $this->updateTransients(); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - $content = get_transient($transient_key); |
|
217 | - return $content !== false ? $content : null; |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * delete a single transient and remove tracking |
|
223 | - * |
|
224 | - * @param string $transient_key [required] full or partial transient key to be deleted |
|
225 | - */ |
|
226 | - public function delete($transient_key) |
|
227 | - { |
|
228 | - $this->deleteMany(array($transient_key)); |
|
229 | - } |
|
230 | - |
|
231 | - |
|
232 | - /** |
|
233 | - * delete multiple transients and remove tracking |
|
234 | - * |
|
235 | - * @param array $transient_keys [required] array of full or partial transient keys to be deleted |
|
236 | - * @param bool $force_delete [optional] if true, then will not check incoming keys against those being tracked |
|
237 | - * and proceed directly to deleting those entries from the cache storage |
|
238 | - */ |
|
239 | - public function deleteMany(array $transient_keys, $force_delete = false) |
|
240 | - { |
|
241 | - $full_transient_keys = $force_delete ? $transient_keys : array(); |
|
242 | - if (empty($full_transient_keys)) { |
|
243 | - foreach ($this->transients as $transient_key => $expiration) { |
|
244 | - foreach ($transient_keys as $transient_key_to_delete) { |
|
245 | - if (strpos($transient_key, $transient_key_to_delete) !== false) { |
|
246 | - $full_transient_keys[] = $transient_key; |
|
247 | - } |
|
248 | - } |
|
249 | - } |
|
250 | - } |
|
251 | - if ($this->deleteTransientKeys($full_transient_keys)) { |
|
252 | - $this->updateTransients(); |
|
253 | - } |
|
254 | - } |
|
255 | - |
|
256 | - |
|
257 | - /** |
|
258 | - * sorts transients numerically by timestamp |
|
259 | - * then saves the transient schedule to a WP option |
|
260 | - */ |
|
261 | - private function updateTransients() |
|
262 | - { |
|
263 | - asort($this->transients, SORT_NUMERIC); |
|
264 | - update_option( |
|
265 | - TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, |
|
266 | - $this->transients |
|
267 | - ); |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - /** |
|
272 | - * schedules a transient for cleanup by adding it to the transient tracking |
|
273 | - * |
|
274 | - * @param string $transient_key [required] |
|
275 | - * @param int $expiration [required] |
|
276 | - */ |
|
277 | - private function scheduleTransientCleanup($transient_key, $expiration) |
|
278 | - { |
|
279 | - // make sure a valid future timestamp is set |
|
280 | - $expiration += $expiration < time() ? time() : 0; |
|
281 | - // and round to the closest 15 minutes |
|
282 | - $expiration = $this->roundTimestamp($expiration); |
|
283 | - // save transients to clear using their ID as the key to avoid duplicates |
|
284 | - $this->transients[ $transient_key ] = $expiration; |
|
285 | - $this->updateTransients(); |
|
286 | - } |
|
287 | - |
|
288 | - |
|
289 | - /** |
|
290 | - * Since our tracked transients are sorted by their timestamps |
|
291 | - * we can grab the first transient and see when it is scheduled for cleanup. |
|
292 | - * If that timestamp is less than or equal to the current time, |
|
293 | - * then cleanup is triggered |
|
294 | - */ |
|
295 | - public function checkTransientCleanupSchedule() |
|
296 | - { |
|
297 | - if (empty($this->transients)) { |
|
298 | - return; |
|
299 | - } |
|
300 | - // when do we run the next cleanup job? |
|
301 | - reset($this->transients); |
|
302 | - $next_scheduled_cleanup = current($this->transients); |
|
303 | - // if the next cleanup job is scheduled for the current hour |
|
304 | - if ($next_scheduled_cleanup <= $this->current_time) { |
|
305 | - if ($this->cleanupExpiredTransients()) { |
|
306 | - $this->updateTransients(); |
|
307 | - } |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - |
|
312 | - /** |
|
313 | - * loops through the array of tracked transients, |
|
314 | - * compiles a list of those that have expired, and sends that list off for deletion. |
|
315 | - * Also removes any bad records from the transients array |
|
316 | - * |
|
317 | - * @return bool |
|
318 | - */ |
|
319 | - private function cleanupExpiredTransients() |
|
320 | - { |
|
321 | - $update = false; |
|
322 | - // filter the query limit. Set to 0 to turn off garbage collection |
|
323 | - $limit = (int) abs( |
|
324 | - apply_filters( |
|
325 | - 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
326 | - 50 |
|
327 | - ) |
|
328 | - ); |
|
329 | - // non-zero LIMIT means take out the trash |
|
330 | - if ($limit) { |
|
331 | - $transient_keys = array(); |
|
332 | - foreach ($this->transients as $transient_key => $expiration) { |
|
333 | - if ($expiration > $this->current_time) { |
|
334 | - continue; |
|
335 | - } |
|
336 | - if (! $expiration || ! $transient_key) { |
|
337 | - unset($this->transients[ $transient_key ]); |
|
338 | - $update = true; |
|
339 | - continue; |
|
340 | - } |
|
341 | - $transient_keys[] = $transient_key; |
|
342 | - } |
|
343 | - // delete expired keys, but maintain value of $update if nothing is deleted |
|
344 | - $update = $this->deleteTransientKeys($transient_keys, $limit) ? true : $update; |
|
345 | - do_action('FHEE__TransientCacheStorage__clearExpiredTransients__end', $this); |
|
346 | - } |
|
347 | - return $update; |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * calls delete_transient() on each transient key provided, up to the specified limit |
|
353 | - * |
|
354 | - * @param array $transient_keys [required] |
|
355 | - * @param int $limit |
|
356 | - * @return bool |
|
357 | - */ |
|
358 | - private function deleteTransientKeys(array $transient_keys, $limit = 50) |
|
359 | - { |
|
360 | - if (empty($transient_keys)) { |
|
361 | - return false; |
|
362 | - } |
|
363 | - $counter = 0; |
|
364 | - foreach ($transient_keys as $transient_key) { |
|
365 | - if ($counter === $limit) { |
|
366 | - break; |
|
367 | - } |
|
368 | - // remove any transient prefixes |
|
369 | - $transient_key = strpos($transient_key, '_transient_timeout_') === 0 |
|
370 | - ? str_replace('_transient_timeout_', '', $transient_key) |
|
371 | - : $transient_key; |
|
372 | - $transient_key = strpos($transient_key, '_transient_') === 0 |
|
373 | - ? str_replace('_transient_', '', $transient_key) |
|
374 | - : $transient_key; |
|
375 | - delete_transient($transient_key); |
|
376 | - unset($this->transients[ $transient_key ]); |
|
377 | - $counter++; |
|
378 | - } |
|
379 | - return $counter > 0; |
|
380 | - } |
|
19 | + /** |
|
20 | + * wp-option option_name for tracking transients |
|
21 | + * |
|
22 | + * @type string |
|
23 | + */ |
|
24 | + const TRANSIENT_SCHEDULE_OPTIONS_KEY = 'ee_transient_schedule'; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var int $current_time |
|
28 | + */ |
|
29 | + private $current_time; |
|
30 | + |
|
31 | + /** |
|
32 | + * how often to perform transient cleanup |
|
33 | + * |
|
34 | + * @var string $transient_cleanup_frequency |
|
35 | + */ |
|
36 | + private $transient_cleanup_frequency; |
|
37 | + |
|
38 | + /** |
|
39 | + * options for how often to perform transient cleanup |
|
40 | + * |
|
41 | + * @var array $transient_cleanup_frequency_options |
|
42 | + */ |
|
43 | + private $transient_cleanup_frequency_options = array(); |
|
44 | + |
|
45 | + /** |
|
46 | + * @var array $transients |
|
47 | + */ |
|
48 | + private $transients; |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * TransientCacheStorage constructor. |
|
53 | + */ |
|
54 | + public function __construct() |
|
55 | + { |
|
56 | + $this->transient_cleanup_frequency = $this->setTransientCleanupFrequency(); |
|
57 | + // round current time down to closest 5 minutes to simplify scheduling |
|
58 | + $this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
|
59 | + $this->transients = (array) get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
|
60 | + if (! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
61 | + add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * Sets how often transient cleanup occurs |
|
68 | + * |
|
69 | + * @return string |
|
70 | + */ |
|
71 | + private function setTransientCleanupFrequency() |
|
72 | + { |
|
73 | + // sets how often transients are cleaned up |
|
74 | + $this->transient_cleanup_frequency_options = apply_filters( |
|
75 | + 'FHEE__TransientCacheStorage__transient_cleanup_schedule_options', |
|
76 | + array( |
|
77 | + 'off', |
|
78 | + '15-minutes', |
|
79 | + 'hour', |
|
80 | + '12-hours', |
|
81 | + 'day', |
|
82 | + ) |
|
83 | + ); |
|
84 | + $transient_cleanup_frequency = apply_filters( |
|
85 | + 'FHEE__TransientCacheStorage__transient_cleanup_schedule', |
|
86 | + 'hour' |
|
87 | + ); |
|
88 | + return in_array( |
|
89 | + $transient_cleanup_frequency, |
|
90 | + $this->transient_cleanup_frequency_options, |
|
91 | + true |
|
92 | + ) |
|
93 | + ? $transient_cleanup_frequency |
|
94 | + : 'hour'; |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * we need to be able to round timestamps off to match the set transient cleanup frequency |
|
100 | + * so if a transient is set to expire at 1:17 pm for example, and our cleanup schedule is every hour, |
|
101 | + * then that timestamp needs to be rounded up to 2:00 pm so that it is removed |
|
102 | + * during the next scheduled cleanup after its expiration. |
|
103 | + * We also round off the current time timestamp to the closest 5 minutes |
|
104 | + * just to make the timestamps a little easier to round which helps with debugging. |
|
105 | + * |
|
106 | + * @param int $timestamp [required] |
|
107 | + * @param string $cleanup_frequency |
|
108 | + * @param bool $round_up |
|
109 | + * @return int |
|
110 | + */ |
|
111 | + private function roundTimestamp($timestamp, $cleanup_frequency = 'hour', $round_up = true) |
|
112 | + { |
|
113 | + $cleanup_frequency = $cleanup_frequency ? $cleanup_frequency : $this->transient_cleanup_frequency; |
|
114 | + // in order to round the time to the closest xx minutes (or hours), |
|
115 | + // we take the minutes (or hours) portion of the timestamp and divide it by xx, |
|
116 | + // round down to a whole number, then multiply by xx to bring us almost back up to where we were |
|
117 | + // why round down ? so the minutes (or hours) don't go over 60 (or 24) |
|
118 | + // and bump the hour, which could bump the day, which could bump the month, etc, |
|
119 | + // which would be bad because we don't always want to round up, |
|
120 | + // but when we do we can easily achieve that by simply adding the desired offset, |
|
121 | + $minutes = '00'; |
|
122 | + $hours = 'H'; |
|
123 | + switch ($cleanup_frequency) { |
|
124 | + case '5-minutes': |
|
125 | + $minutes = floor((int) date('i', $timestamp) / 5) * 5; |
|
126 | + $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
127 | + $offset = MINUTE_IN_SECONDS * 5; |
|
128 | + break; |
|
129 | + case '15-minutes': |
|
130 | + $minutes = floor((int) date('i', $timestamp) / 15) * 15; |
|
131 | + $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
132 | + $offset = MINUTE_IN_SECONDS * 15; |
|
133 | + break; |
|
134 | + case '12-hours': |
|
135 | + $hours = floor((int) date('H', $timestamp) / 12) * 12; |
|
136 | + $hours = str_pad($hours, 2, '0', STR_PAD_LEFT); |
|
137 | + $offset = HOUR_IN_SECONDS * 12; |
|
138 | + break; |
|
139 | + case 'day': |
|
140 | + $hours = '03'; // run cleanup at 3:00 am (or first site hit after that) |
|
141 | + $offset = DAY_IN_SECONDS; |
|
142 | + break; |
|
143 | + case 'hour': |
|
144 | + default: |
|
145 | + $offset = HOUR_IN_SECONDS; |
|
146 | + break; |
|
147 | + } |
|
148 | + $rounded_timestamp = (int) strtotime(date("Y-m-d {$hours}:{$minutes}:00", $timestamp)); |
|
149 | + $rounded_timestamp += $round_up ? $offset : 0; |
|
150 | + return apply_filters( |
|
151 | + 'FHEE__TransientCacheStorage__roundTimestamp__timestamp', |
|
152 | + $rounded_timestamp, |
|
153 | + $timestamp, |
|
154 | + $cleanup_frequency, |
|
155 | + $round_up |
|
156 | + ); |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * Saves supplied data to a transient |
|
162 | + * if an expiration is set, then it automatically schedules the transient for cleanup |
|
163 | + * |
|
164 | + * @param string $transient_key [required] |
|
165 | + * @param string $data [required] |
|
166 | + * @param int $expiration number of seconds until the cache expires |
|
167 | + * @return bool |
|
168 | + */ |
|
169 | + public function add($transient_key, $data, $expiration = 0) |
|
170 | + { |
|
171 | + $expiration = (int) abs($expiration); |
|
172 | + $saved = set_transient($transient_key, $data, $expiration); |
|
173 | + if ($saved && $expiration) { |
|
174 | + $this->scheduleTransientCleanup($transient_key, $expiration); |
|
175 | + } |
|
176 | + return $saved; |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * retrieves transient data |
|
182 | + * automatically triggers early cache refresh for standard cache items |
|
183 | + * in order to avoid cache stampedes on busy sites. |
|
184 | + * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
|
185 | + * the $standard_cache parameter should be set to false when retrieving data |
|
186 | + * |
|
187 | + * @param string $transient_key [required] |
|
188 | + * @param bool $standard_cache |
|
189 | + * @return mixed|null |
|
190 | + */ |
|
191 | + public function get($transient_key, $standard_cache = true) |
|
192 | + { |
|
193 | + if (isset($this->transients[ $transient_key ])) { |
|
194 | + // to avoid cache stampedes (AKA:dogpiles) for standard cache items, |
|
195 | + // check if known cache expires within the next minute, |
|
196 | + // and if so, remove it from our tracking and and return nothing. |
|
197 | + // this should trigger the cache content to be regenerated during this request, |
|
198 | + // while allowing any following requests to still access the existing cache |
|
199 | + // until it gets replaced with the refreshed content |
|
200 | + if ($standard_cache |
|
201 | + && $this->transients[ $transient_key ] - time() <= MINUTE_IN_SECONDS |
|
202 | + ) { |
|
203 | + unset($this->transients[ $transient_key ]); |
|
204 | + $this->updateTransients(); |
|
205 | + return null; |
|
206 | + } |
|
207 | + |
|
208 | + // for non standard cache items, remove the key from our tracking, |
|
209 | + // but proceed to retrieve the transient so that it also gets removed from the db |
|
210 | + if ($this->transients[ $transient_key ] <= time()) { |
|
211 | + unset($this->transients[ $transient_key ]); |
|
212 | + $this->updateTransients(); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + $content = get_transient($transient_key); |
|
217 | + return $content !== false ? $content : null; |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * delete a single transient and remove tracking |
|
223 | + * |
|
224 | + * @param string $transient_key [required] full or partial transient key to be deleted |
|
225 | + */ |
|
226 | + public function delete($transient_key) |
|
227 | + { |
|
228 | + $this->deleteMany(array($transient_key)); |
|
229 | + } |
|
230 | + |
|
231 | + |
|
232 | + /** |
|
233 | + * delete multiple transients and remove tracking |
|
234 | + * |
|
235 | + * @param array $transient_keys [required] array of full or partial transient keys to be deleted |
|
236 | + * @param bool $force_delete [optional] if true, then will not check incoming keys against those being tracked |
|
237 | + * and proceed directly to deleting those entries from the cache storage |
|
238 | + */ |
|
239 | + public function deleteMany(array $transient_keys, $force_delete = false) |
|
240 | + { |
|
241 | + $full_transient_keys = $force_delete ? $transient_keys : array(); |
|
242 | + if (empty($full_transient_keys)) { |
|
243 | + foreach ($this->transients as $transient_key => $expiration) { |
|
244 | + foreach ($transient_keys as $transient_key_to_delete) { |
|
245 | + if (strpos($transient_key, $transient_key_to_delete) !== false) { |
|
246 | + $full_transient_keys[] = $transient_key; |
|
247 | + } |
|
248 | + } |
|
249 | + } |
|
250 | + } |
|
251 | + if ($this->deleteTransientKeys($full_transient_keys)) { |
|
252 | + $this->updateTransients(); |
|
253 | + } |
|
254 | + } |
|
255 | + |
|
256 | + |
|
257 | + /** |
|
258 | + * sorts transients numerically by timestamp |
|
259 | + * then saves the transient schedule to a WP option |
|
260 | + */ |
|
261 | + private function updateTransients() |
|
262 | + { |
|
263 | + asort($this->transients, SORT_NUMERIC); |
|
264 | + update_option( |
|
265 | + TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, |
|
266 | + $this->transients |
|
267 | + ); |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + /** |
|
272 | + * schedules a transient for cleanup by adding it to the transient tracking |
|
273 | + * |
|
274 | + * @param string $transient_key [required] |
|
275 | + * @param int $expiration [required] |
|
276 | + */ |
|
277 | + private function scheduleTransientCleanup($transient_key, $expiration) |
|
278 | + { |
|
279 | + // make sure a valid future timestamp is set |
|
280 | + $expiration += $expiration < time() ? time() : 0; |
|
281 | + // and round to the closest 15 minutes |
|
282 | + $expiration = $this->roundTimestamp($expiration); |
|
283 | + // save transients to clear using their ID as the key to avoid duplicates |
|
284 | + $this->transients[ $transient_key ] = $expiration; |
|
285 | + $this->updateTransients(); |
|
286 | + } |
|
287 | + |
|
288 | + |
|
289 | + /** |
|
290 | + * Since our tracked transients are sorted by their timestamps |
|
291 | + * we can grab the first transient and see when it is scheduled for cleanup. |
|
292 | + * If that timestamp is less than or equal to the current time, |
|
293 | + * then cleanup is triggered |
|
294 | + */ |
|
295 | + public function checkTransientCleanupSchedule() |
|
296 | + { |
|
297 | + if (empty($this->transients)) { |
|
298 | + return; |
|
299 | + } |
|
300 | + // when do we run the next cleanup job? |
|
301 | + reset($this->transients); |
|
302 | + $next_scheduled_cleanup = current($this->transients); |
|
303 | + // if the next cleanup job is scheduled for the current hour |
|
304 | + if ($next_scheduled_cleanup <= $this->current_time) { |
|
305 | + if ($this->cleanupExpiredTransients()) { |
|
306 | + $this->updateTransients(); |
|
307 | + } |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + |
|
312 | + /** |
|
313 | + * loops through the array of tracked transients, |
|
314 | + * compiles a list of those that have expired, and sends that list off for deletion. |
|
315 | + * Also removes any bad records from the transients array |
|
316 | + * |
|
317 | + * @return bool |
|
318 | + */ |
|
319 | + private function cleanupExpiredTransients() |
|
320 | + { |
|
321 | + $update = false; |
|
322 | + // filter the query limit. Set to 0 to turn off garbage collection |
|
323 | + $limit = (int) abs( |
|
324 | + apply_filters( |
|
325 | + 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
326 | + 50 |
|
327 | + ) |
|
328 | + ); |
|
329 | + // non-zero LIMIT means take out the trash |
|
330 | + if ($limit) { |
|
331 | + $transient_keys = array(); |
|
332 | + foreach ($this->transients as $transient_key => $expiration) { |
|
333 | + if ($expiration > $this->current_time) { |
|
334 | + continue; |
|
335 | + } |
|
336 | + if (! $expiration || ! $transient_key) { |
|
337 | + unset($this->transients[ $transient_key ]); |
|
338 | + $update = true; |
|
339 | + continue; |
|
340 | + } |
|
341 | + $transient_keys[] = $transient_key; |
|
342 | + } |
|
343 | + // delete expired keys, but maintain value of $update if nothing is deleted |
|
344 | + $update = $this->deleteTransientKeys($transient_keys, $limit) ? true : $update; |
|
345 | + do_action('FHEE__TransientCacheStorage__clearExpiredTransients__end', $this); |
|
346 | + } |
|
347 | + return $update; |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * calls delete_transient() on each transient key provided, up to the specified limit |
|
353 | + * |
|
354 | + * @param array $transient_keys [required] |
|
355 | + * @param int $limit |
|
356 | + * @return bool |
|
357 | + */ |
|
358 | + private function deleteTransientKeys(array $transient_keys, $limit = 50) |
|
359 | + { |
|
360 | + if (empty($transient_keys)) { |
|
361 | + return false; |
|
362 | + } |
|
363 | + $counter = 0; |
|
364 | + foreach ($transient_keys as $transient_key) { |
|
365 | + if ($counter === $limit) { |
|
366 | + break; |
|
367 | + } |
|
368 | + // remove any transient prefixes |
|
369 | + $transient_key = strpos($transient_key, '_transient_timeout_') === 0 |
|
370 | + ? str_replace('_transient_timeout_', '', $transient_key) |
|
371 | + : $transient_key; |
|
372 | + $transient_key = strpos($transient_key, '_transient_') === 0 |
|
373 | + ? str_replace('_transient_', '', $transient_key) |
|
374 | + : $transient_key; |
|
375 | + delete_transient($transient_key); |
|
376 | + unset($this->transients[ $transient_key ]); |
|
377 | + $counter++; |
|
378 | + } |
|
379 | + return $counter > 0; |
|
380 | + } |
|
381 | 381 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | // round current time down to closest 5 minutes to simplify scheduling |
58 | 58 | $this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
59 | 59 | $this->transients = (array) get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
60 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
60 | + if ( ! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
61 | 61 | add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
62 | 62 | } |
63 | 63 | } |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | */ |
191 | 191 | public function get($transient_key, $standard_cache = true) |
192 | 192 | { |
193 | - if (isset($this->transients[ $transient_key ])) { |
|
193 | + if (isset($this->transients[$transient_key])) { |
|
194 | 194 | // to avoid cache stampedes (AKA:dogpiles) for standard cache items, |
195 | 195 | // check if known cache expires within the next minute, |
196 | 196 | // and if so, remove it from our tracking and and return nothing. |
@@ -198,17 +198,17 @@ discard block |
||
198 | 198 | // while allowing any following requests to still access the existing cache |
199 | 199 | // until it gets replaced with the refreshed content |
200 | 200 | if ($standard_cache |
201 | - && $this->transients[ $transient_key ] - time() <= MINUTE_IN_SECONDS |
|
201 | + && $this->transients[$transient_key] - time() <= MINUTE_IN_SECONDS |
|
202 | 202 | ) { |
203 | - unset($this->transients[ $transient_key ]); |
|
203 | + unset($this->transients[$transient_key]); |
|
204 | 204 | $this->updateTransients(); |
205 | 205 | return null; |
206 | 206 | } |
207 | 207 | |
208 | 208 | // for non standard cache items, remove the key from our tracking, |
209 | 209 | // but proceed to retrieve the transient so that it also gets removed from the db |
210 | - if ($this->transients[ $transient_key ] <= time()) { |
|
211 | - unset($this->transients[ $transient_key ]); |
|
210 | + if ($this->transients[$transient_key] <= time()) { |
|
211 | + unset($this->transients[$transient_key]); |
|
212 | 212 | $this->updateTransients(); |
213 | 213 | } |
214 | 214 | } |
@@ -281,7 +281,7 @@ discard block |
||
281 | 281 | // and round to the closest 15 minutes |
282 | 282 | $expiration = $this->roundTimestamp($expiration); |
283 | 283 | // save transients to clear using their ID as the key to avoid duplicates |
284 | - $this->transients[ $transient_key ] = $expiration; |
|
284 | + $this->transients[$transient_key] = $expiration; |
|
285 | 285 | $this->updateTransients(); |
286 | 286 | } |
287 | 287 | |
@@ -333,8 +333,8 @@ discard block |
||
333 | 333 | if ($expiration > $this->current_time) { |
334 | 334 | continue; |
335 | 335 | } |
336 | - if (! $expiration || ! $transient_key) { |
|
337 | - unset($this->transients[ $transient_key ]); |
|
336 | + if ( ! $expiration || ! $transient_key) { |
|
337 | + unset($this->transients[$transient_key]); |
|
338 | 338 | $update = true; |
339 | 339 | continue; |
340 | 340 | } |
@@ -373,7 +373,7 @@ discard block |
||
373 | 373 | ? str_replace('_transient_', '', $transient_key) |
374 | 374 | : $transient_key; |
375 | 375 | delete_transient($transient_key); |
376 | - unset($this->transients[ $transient_key ]); |
|
376 | + unset($this->transients[$transient_key]); |
|
377 | 377 | $counter++; |
378 | 378 | } |
379 | 379 | return $counter > 0; |
@@ -15,324 +15,324 @@ |
||
15 | 15 | class Benchmark |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var string $output |
|
20 | - */ |
|
21 | - private static $output; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var array $start_times array containing the start time for the timers |
|
25 | - */ |
|
26 | - private static $start_times; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var array $times array containing all the timer'd times, which can be outputted via show_times() |
|
30 | - */ |
|
31 | - private static $times = array(); |
|
32 | - |
|
33 | - /** |
|
34 | - * @var array $memory_usage |
|
35 | - */ |
|
36 | - protected static $memory_usage = array(); |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * @param string $output |
|
41 | - * @param bool $formatted |
|
42 | - */ |
|
43 | - public static function addOutput($output, $formatted = true) |
|
44 | - { |
|
45 | - Benchmark::$output .= $formatted |
|
46 | - ? "<br />{$output}" |
|
47 | - : "\n{$output}"; |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public static function resetOutput() |
|
55 | - { |
|
56 | - Benchmark::$output = ''; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * whether to benchmark code or not |
|
61 | - */ |
|
62 | - public static function doNotRun() |
|
63 | - { |
|
64 | - return ! WP_DEBUG || (defined('DOING_AJAX') && DOING_AJAX); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * resetTimes |
|
70 | - */ |
|
71 | - public static function resetTimes() |
|
72 | - { |
|
73 | - Benchmark::$times = array(); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Add Benchmark::startTimer() before a block of code you want to measure the performance of |
|
79 | - * |
|
80 | - * @param null $timer_name |
|
81 | - */ |
|
82 | - public static function startTimer($timer_name = null) |
|
83 | - { |
|
84 | - if (Benchmark::doNotRun()) { |
|
85 | - return; |
|
86 | - } |
|
87 | - $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
88 | - Benchmark::$start_times[ $timer_name ] = microtime(true); |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * Add Benchmark::stopTimer() after a block of code you want to measure the performance of |
|
94 | - * |
|
95 | - * @param string $timer_name |
|
96 | - */ |
|
97 | - public static function stopTimer($timer_name = '') |
|
98 | - { |
|
99 | - if (Benchmark::doNotRun()) { |
|
100 | - return; |
|
101 | - } |
|
102 | - $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
103 | - if (isset(Benchmark::$start_times[ $timer_name ])) { |
|
104 | - $start_time = Benchmark::$start_times[ $timer_name ]; |
|
105 | - unset(Benchmark::$start_times[ $timer_name ]); |
|
106 | - } else { |
|
107 | - $start_time = array_pop(Benchmark::$start_times); |
|
108 | - } |
|
109 | - Benchmark::$times[ $timer_name ] = number_format(microtime(true) - $start_time, 8); |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * Measure the memory usage by PHP so far. |
|
115 | - * |
|
116 | - * @param string $label The label to show for this time eg "Start of calling Some_Class::some_function" |
|
117 | - * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called |
|
118 | - * @param bool $formatted |
|
119 | - * @return void |
|
120 | - */ |
|
121 | - public static function measureMemory($label = 'memory usage', $output_now = false, $formatted = true) |
|
122 | - { |
|
123 | - if (Benchmark::doNotRun()) { |
|
124 | - return; |
|
125 | - } |
|
126 | - $memory_used = Benchmark::convert(memory_get_usage(true)); |
|
127 | - Benchmark::$memory_usage[ $label ] = $memory_used; |
|
128 | - if ($output_now) { |
|
129 | - echo $formatted |
|
130 | - ? "<br>{$label} : {$memory_used}" |
|
131 | - : "\n {$label} : {$memory_used}"; |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * will display the benchmarking results at shutdown |
|
138 | - * |
|
139 | - * @param bool $formatted |
|
140 | - * @return void |
|
141 | - */ |
|
142 | - public static function displayResultsAtShutdown($formatted = true) |
|
143 | - { |
|
144 | - Benchmark::resetOutput(); |
|
145 | - add_action( |
|
146 | - 'shutdown', |
|
147 | - function () use ($formatted) { |
|
148 | - Benchmark::displayResults(true, $formatted); |
|
149 | - }, |
|
150 | - 999999 |
|
151 | - ); |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * will display the benchmarking results at shutdown |
|
157 | - * |
|
158 | - * @param string $filepath |
|
159 | - * @param bool $formatted |
|
160 | - * @param bool $append |
|
161 | - * @return void |
|
162 | - */ |
|
163 | - public static function writeResultsAtShutdown($filepath = '', $formatted = true, $append = true) |
|
164 | - { |
|
165 | - Benchmark::resetOutput(); |
|
166 | - add_action( |
|
167 | - 'shutdown', |
|
168 | - function () use ($filepath, $formatted, $append) { |
|
169 | - Benchmark::writeResultsToFile($filepath, $formatted, $append); |
|
170 | - }, |
|
171 | - 999999 |
|
172 | - ); |
|
173 | - } |
|
174 | - |
|
175 | - |
|
176 | - /** |
|
177 | - * @param bool $formatted |
|
178 | - * @return string |
|
179 | - */ |
|
180 | - private static function generateResults($formatted = true) |
|
181 | - { |
|
182 | - if (Benchmark::doNotRun()) { |
|
183 | - return ''; |
|
184 | - } |
|
185 | - if (! empty(Benchmark::$times)) { |
|
186 | - $total = 0; |
|
187 | - Benchmark::$output .= $formatted |
|
188 | - ? '<span style="color:#999999; font-size:.8em;">( time in milliseconds )</span><br />' |
|
189 | - : ''; |
|
190 | - foreach (Benchmark::$times as $timer_name => $total_time) { |
|
191 | - Benchmark::$output .= Benchmark::formatTime($timer_name, $total_time, $formatted); |
|
192 | - Benchmark::$output .= $formatted ? '<br />' : "\n"; |
|
193 | - $total += $total_time; |
|
194 | - } |
|
195 | - if ($formatted) { |
|
196 | - Benchmark::$output .= '<br />'; |
|
197 | - Benchmark::$output .= '<h4>TOTAL TIME</h4>'; |
|
198 | - Benchmark::$output .= Benchmark::formatTime('', $total, $formatted); |
|
199 | - Benchmark::$output .= '<span style="color:#999999; font-size:.8em;"> milliseconds</span><br />'; |
|
200 | - Benchmark::$output .= '<br />'; |
|
201 | - Benchmark::$output .= '<h5>Performance scale (from best to worse)</h5>'; |
|
202 | - Benchmark::$output .= '<span style="color:mediumpurple">Like wow! How about a Scooby snack?</span><br />'; |
|
203 | - Benchmark::$output .= '<span style="color:deepskyblue">Like...no way man!</span><br />'; |
|
204 | - Benchmark::$output .= '<span style="color:limegreen">Like...groovy!</span><br />'; |
|
205 | - Benchmark::$output .= '<span style="color:gold">Ruh Oh</span><br />'; |
|
206 | - Benchmark::$output .= '<span style="color:darkorange">Zoinks!</span><br />'; |
|
207 | - Benchmark::$output .= '<span style="color:red">Like...HEEELLLP</span><br />'; |
|
208 | - } |
|
209 | - } |
|
210 | - if (! empty(Benchmark::$memory_usage)) { |
|
211 | - Benchmark::$output .= $formatted |
|
212 | - ? '<h5>Memory</h5>' |
|
213 | - : "\nMemory"; |
|
214 | - foreach (Benchmark::$memory_usage as $label => $memory_usage) { |
|
215 | - Benchmark::$output .= $formatted |
|
216 | - ? "<br />{$memory_usage} : {$label}" |
|
217 | - : "\n{$memory_usage} : {$label}"; |
|
218 | - } |
|
219 | - } |
|
220 | - if (empty(Benchmark::$output)) { |
|
221 | - return ''; |
|
222 | - } |
|
223 | - Benchmark::$output = $formatted |
|
224 | - ? '<div style="border:1px solid #dddddd; background-color:#ffffff;' |
|
225 | - . (is_admin() |
|
226 | - ? ' margin:2em 2em 2em 180px;' |
|
227 | - : ' margin:2em;') |
|
228 | - . ' padding:2em;">' |
|
229 | - . '<h4>BENCHMARKING</h4>' |
|
230 | - . Benchmark::$output |
|
231 | - . '</div>' |
|
232 | - : Benchmark::$output; |
|
233 | - return Benchmark::$output; |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * @param bool $echo |
|
239 | - * @param bool $formatted |
|
240 | - * @return string |
|
241 | - */ |
|
242 | - public static function displayResults($echo = true, $formatted = true) |
|
243 | - { |
|
244 | - $results = Benchmark::generateResults($formatted); |
|
245 | - if ($echo) { |
|
246 | - echo $results; |
|
247 | - $results = ''; |
|
248 | - } |
|
249 | - return $results; |
|
250 | - } |
|
251 | - |
|
252 | - |
|
253 | - /** |
|
254 | - * @param string $filepath |
|
255 | - * @param bool $formatted |
|
256 | - * @param bool $append |
|
257 | - * @throws EE_Error |
|
258 | - */ |
|
259 | - public static function writeResultsToFile($filepath = '', $formatted = true, $append = true) |
|
260 | - { |
|
261 | - $filepath = ! empty($filepath) && is_readable(dirname($filepath)) |
|
262 | - ? $filepath |
|
263 | - : ''; |
|
264 | - if (empty($filepath)) { |
|
265 | - $filepath = EVENT_ESPRESSO_UPLOAD_DIR . 'logs/benchmarking-' . date('Y-m-d') . '.html'; |
|
266 | - } |
|
267 | - EEH_File::ensure_file_exists_and_is_writable($filepath); |
|
268 | - file_put_contents( |
|
269 | - $filepath, |
|
270 | - "\n" . date('Y-m-d H:i:s') . Benchmark::generateResults($formatted), |
|
271 | - $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
|
272 | - ); |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * Converts a measure of memory bytes into the most logical units (eg kb, mb, etc) |
|
278 | - * |
|
279 | - * @param int $size |
|
280 | - * @return string |
|
281 | - */ |
|
282 | - public static function convert($size) |
|
283 | - { |
|
284 | - $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); |
|
285 | - return round( |
|
286 | - $size / pow(1024, $i = floor(log($size, 1024))), |
|
287 | - 2 |
|
288 | - ) . ' ' . $unit[ absint($i) ]; |
|
289 | - } |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * @param string $timer_name |
|
294 | - * @param float $total_time |
|
295 | - * @param bool $formatted |
|
296 | - * @return string |
|
297 | - */ |
|
298 | - public static function formatTime($timer_name, $total_time, $formatted = true) |
|
299 | - { |
|
300 | - $total_time *= 1000; |
|
301 | - switch ($total_time) { |
|
302 | - case $total_time > 12500: |
|
303 | - $color = 'red'; |
|
304 | - $bold = 'bold'; |
|
305 | - break; |
|
306 | - case $total_time > 2500: |
|
307 | - $color = 'darkorange'; |
|
308 | - $bold = 'bold'; |
|
309 | - break; |
|
310 | - case $total_time > 500: |
|
311 | - $color = 'gold'; |
|
312 | - $bold = 'bold'; |
|
313 | - break; |
|
314 | - case $total_time > 100: |
|
315 | - $color = 'limegreen'; |
|
316 | - $bold = 'normal'; |
|
317 | - break; |
|
318 | - case $total_time > 20: |
|
319 | - $color = 'deepskyblue'; |
|
320 | - $bold = 'normal'; |
|
321 | - break; |
|
322 | - default: |
|
323 | - $color = 'mediumpurple'; |
|
324 | - $bold = 'normal'; |
|
325 | - break; |
|
326 | - } |
|
327 | - return $formatted |
|
328 | - ? '<span style="min-width: 10px; margin:0 1em; color:' |
|
329 | - . $color |
|
330 | - . '; font-weight:' |
|
331 | - . $bold |
|
332 | - . '; font-size:1.2em;">' |
|
333 | - . str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT) |
|
334 | - . '</span> ' |
|
335 | - . $timer_name |
|
336 | - : str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT); |
|
337 | - } |
|
18 | + /** |
|
19 | + * @var string $output |
|
20 | + */ |
|
21 | + private static $output; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var array $start_times array containing the start time for the timers |
|
25 | + */ |
|
26 | + private static $start_times; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var array $times array containing all the timer'd times, which can be outputted via show_times() |
|
30 | + */ |
|
31 | + private static $times = array(); |
|
32 | + |
|
33 | + /** |
|
34 | + * @var array $memory_usage |
|
35 | + */ |
|
36 | + protected static $memory_usage = array(); |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * @param string $output |
|
41 | + * @param bool $formatted |
|
42 | + */ |
|
43 | + public static function addOutput($output, $formatted = true) |
|
44 | + { |
|
45 | + Benchmark::$output .= $formatted |
|
46 | + ? "<br />{$output}" |
|
47 | + : "\n{$output}"; |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public static function resetOutput() |
|
55 | + { |
|
56 | + Benchmark::$output = ''; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * whether to benchmark code or not |
|
61 | + */ |
|
62 | + public static function doNotRun() |
|
63 | + { |
|
64 | + return ! WP_DEBUG || (defined('DOING_AJAX') && DOING_AJAX); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * resetTimes |
|
70 | + */ |
|
71 | + public static function resetTimes() |
|
72 | + { |
|
73 | + Benchmark::$times = array(); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Add Benchmark::startTimer() before a block of code you want to measure the performance of |
|
79 | + * |
|
80 | + * @param null $timer_name |
|
81 | + */ |
|
82 | + public static function startTimer($timer_name = null) |
|
83 | + { |
|
84 | + if (Benchmark::doNotRun()) { |
|
85 | + return; |
|
86 | + } |
|
87 | + $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
88 | + Benchmark::$start_times[ $timer_name ] = microtime(true); |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * Add Benchmark::stopTimer() after a block of code you want to measure the performance of |
|
94 | + * |
|
95 | + * @param string $timer_name |
|
96 | + */ |
|
97 | + public static function stopTimer($timer_name = '') |
|
98 | + { |
|
99 | + if (Benchmark::doNotRun()) { |
|
100 | + return; |
|
101 | + } |
|
102 | + $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
103 | + if (isset(Benchmark::$start_times[ $timer_name ])) { |
|
104 | + $start_time = Benchmark::$start_times[ $timer_name ]; |
|
105 | + unset(Benchmark::$start_times[ $timer_name ]); |
|
106 | + } else { |
|
107 | + $start_time = array_pop(Benchmark::$start_times); |
|
108 | + } |
|
109 | + Benchmark::$times[ $timer_name ] = number_format(microtime(true) - $start_time, 8); |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * Measure the memory usage by PHP so far. |
|
115 | + * |
|
116 | + * @param string $label The label to show for this time eg "Start of calling Some_Class::some_function" |
|
117 | + * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called |
|
118 | + * @param bool $formatted |
|
119 | + * @return void |
|
120 | + */ |
|
121 | + public static function measureMemory($label = 'memory usage', $output_now = false, $formatted = true) |
|
122 | + { |
|
123 | + if (Benchmark::doNotRun()) { |
|
124 | + return; |
|
125 | + } |
|
126 | + $memory_used = Benchmark::convert(memory_get_usage(true)); |
|
127 | + Benchmark::$memory_usage[ $label ] = $memory_used; |
|
128 | + if ($output_now) { |
|
129 | + echo $formatted |
|
130 | + ? "<br>{$label} : {$memory_used}" |
|
131 | + : "\n {$label} : {$memory_used}"; |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + /** |
|
137 | + * will display the benchmarking results at shutdown |
|
138 | + * |
|
139 | + * @param bool $formatted |
|
140 | + * @return void |
|
141 | + */ |
|
142 | + public static function displayResultsAtShutdown($formatted = true) |
|
143 | + { |
|
144 | + Benchmark::resetOutput(); |
|
145 | + add_action( |
|
146 | + 'shutdown', |
|
147 | + function () use ($formatted) { |
|
148 | + Benchmark::displayResults(true, $formatted); |
|
149 | + }, |
|
150 | + 999999 |
|
151 | + ); |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * will display the benchmarking results at shutdown |
|
157 | + * |
|
158 | + * @param string $filepath |
|
159 | + * @param bool $formatted |
|
160 | + * @param bool $append |
|
161 | + * @return void |
|
162 | + */ |
|
163 | + public static function writeResultsAtShutdown($filepath = '', $formatted = true, $append = true) |
|
164 | + { |
|
165 | + Benchmark::resetOutput(); |
|
166 | + add_action( |
|
167 | + 'shutdown', |
|
168 | + function () use ($filepath, $formatted, $append) { |
|
169 | + Benchmark::writeResultsToFile($filepath, $formatted, $append); |
|
170 | + }, |
|
171 | + 999999 |
|
172 | + ); |
|
173 | + } |
|
174 | + |
|
175 | + |
|
176 | + /** |
|
177 | + * @param bool $formatted |
|
178 | + * @return string |
|
179 | + */ |
|
180 | + private static function generateResults($formatted = true) |
|
181 | + { |
|
182 | + if (Benchmark::doNotRun()) { |
|
183 | + return ''; |
|
184 | + } |
|
185 | + if (! empty(Benchmark::$times)) { |
|
186 | + $total = 0; |
|
187 | + Benchmark::$output .= $formatted |
|
188 | + ? '<span style="color:#999999; font-size:.8em;">( time in milliseconds )</span><br />' |
|
189 | + : ''; |
|
190 | + foreach (Benchmark::$times as $timer_name => $total_time) { |
|
191 | + Benchmark::$output .= Benchmark::formatTime($timer_name, $total_time, $formatted); |
|
192 | + Benchmark::$output .= $formatted ? '<br />' : "\n"; |
|
193 | + $total += $total_time; |
|
194 | + } |
|
195 | + if ($formatted) { |
|
196 | + Benchmark::$output .= '<br />'; |
|
197 | + Benchmark::$output .= '<h4>TOTAL TIME</h4>'; |
|
198 | + Benchmark::$output .= Benchmark::formatTime('', $total, $formatted); |
|
199 | + Benchmark::$output .= '<span style="color:#999999; font-size:.8em;"> milliseconds</span><br />'; |
|
200 | + Benchmark::$output .= '<br />'; |
|
201 | + Benchmark::$output .= '<h5>Performance scale (from best to worse)</h5>'; |
|
202 | + Benchmark::$output .= '<span style="color:mediumpurple">Like wow! How about a Scooby snack?</span><br />'; |
|
203 | + Benchmark::$output .= '<span style="color:deepskyblue">Like...no way man!</span><br />'; |
|
204 | + Benchmark::$output .= '<span style="color:limegreen">Like...groovy!</span><br />'; |
|
205 | + Benchmark::$output .= '<span style="color:gold">Ruh Oh</span><br />'; |
|
206 | + Benchmark::$output .= '<span style="color:darkorange">Zoinks!</span><br />'; |
|
207 | + Benchmark::$output .= '<span style="color:red">Like...HEEELLLP</span><br />'; |
|
208 | + } |
|
209 | + } |
|
210 | + if (! empty(Benchmark::$memory_usage)) { |
|
211 | + Benchmark::$output .= $formatted |
|
212 | + ? '<h5>Memory</h5>' |
|
213 | + : "\nMemory"; |
|
214 | + foreach (Benchmark::$memory_usage as $label => $memory_usage) { |
|
215 | + Benchmark::$output .= $formatted |
|
216 | + ? "<br />{$memory_usage} : {$label}" |
|
217 | + : "\n{$memory_usage} : {$label}"; |
|
218 | + } |
|
219 | + } |
|
220 | + if (empty(Benchmark::$output)) { |
|
221 | + return ''; |
|
222 | + } |
|
223 | + Benchmark::$output = $formatted |
|
224 | + ? '<div style="border:1px solid #dddddd; background-color:#ffffff;' |
|
225 | + . (is_admin() |
|
226 | + ? ' margin:2em 2em 2em 180px;' |
|
227 | + : ' margin:2em;') |
|
228 | + . ' padding:2em;">' |
|
229 | + . '<h4>BENCHMARKING</h4>' |
|
230 | + . Benchmark::$output |
|
231 | + . '</div>' |
|
232 | + : Benchmark::$output; |
|
233 | + return Benchmark::$output; |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * @param bool $echo |
|
239 | + * @param bool $formatted |
|
240 | + * @return string |
|
241 | + */ |
|
242 | + public static function displayResults($echo = true, $formatted = true) |
|
243 | + { |
|
244 | + $results = Benchmark::generateResults($formatted); |
|
245 | + if ($echo) { |
|
246 | + echo $results; |
|
247 | + $results = ''; |
|
248 | + } |
|
249 | + return $results; |
|
250 | + } |
|
251 | + |
|
252 | + |
|
253 | + /** |
|
254 | + * @param string $filepath |
|
255 | + * @param bool $formatted |
|
256 | + * @param bool $append |
|
257 | + * @throws EE_Error |
|
258 | + */ |
|
259 | + public static function writeResultsToFile($filepath = '', $formatted = true, $append = true) |
|
260 | + { |
|
261 | + $filepath = ! empty($filepath) && is_readable(dirname($filepath)) |
|
262 | + ? $filepath |
|
263 | + : ''; |
|
264 | + if (empty($filepath)) { |
|
265 | + $filepath = EVENT_ESPRESSO_UPLOAD_DIR . 'logs/benchmarking-' . date('Y-m-d') . '.html'; |
|
266 | + } |
|
267 | + EEH_File::ensure_file_exists_and_is_writable($filepath); |
|
268 | + file_put_contents( |
|
269 | + $filepath, |
|
270 | + "\n" . date('Y-m-d H:i:s') . Benchmark::generateResults($formatted), |
|
271 | + $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
|
272 | + ); |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * Converts a measure of memory bytes into the most logical units (eg kb, mb, etc) |
|
278 | + * |
|
279 | + * @param int $size |
|
280 | + * @return string |
|
281 | + */ |
|
282 | + public static function convert($size) |
|
283 | + { |
|
284 | + $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); |
|
285 | + return round( |
|
286 | + $size / pow(1024, $i = floor(log($size, 1024))), |
|
287 | + 2 |
|
288 | + ) . ' ' . $unit[ absint($i) ]; |
|
289 | + } |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * @param string $timer_name |
|
294 | + * @param float $total_time |
|
295 | + * @param bool $formatted |
|
296 | + * @return string |
|
297 | + */ |
|
298 | + public static function formatTime($timer_name, $total_time, $formatted = true) |
|
299 | + { |
|
300 | + $total_time *= 1000; |
|
301 | + switch ($total_time) { |
|
302 | + case $total_time > 12500: |
|
303 | + $color = 'red'; |
|
304 | + $bold = 'bold'; |
|
305 | + break; |
|
306 | + case $total_time > 2500: |
|
307 | + $color = 'darkorange'; |
|
308 | + $bold = 'bold'; |
|
309 | + break; |
|
310 | + case $total_time > 500: |
|
311 | + $color = 'gold'; |
|
312 | + $bold = 'bold'; |
|
313 | + break; |
|
314 | + case $total_time > 100: |
|
315 | + $color = 'limegreen'; |
|
316 | + $bold = 'normal'; |
|
317 | + break; |
|
318 | + case $total_time > 20: |
|
319 | + $color = 'deepskyblue'; |
|
320 | + $bold = 'normal'; |
|
321 | + break; |
|
322 | + default: |
|
323 | + $color = 'mediumpurple'; |
|
324 | + $bold = 'normal'; |
|
325 | + break; |
|
326 | + } |
|
327 | + return $formatted |
|
328 | + ? '<span style="min-width: 10px; margin:0 1em; color:' |
|
329 | + . $color |
|
330 | + . '; font-weight:' |
|
331 | + . $bold |
|
332 | + . '; font-size:1.2em;">' |
|
333 | + . str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT) |
|
334 | + . '</span> ' |
|
335 | + . $timer_name |
|
336 | + : str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT); |
|
337 | + } |
|
338 | 338 | } |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | return; |
86 | 86 | } |
87 | 87 | $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
88 | - Benchmark::$start_times[ $timer_name ] = microtime(true); |
|
88 | + Benchmark::$start_times[$timer_name] = microtime(true); |
|
89 | 89 | } |
90 | 90 | |
91 | 91 | |
@@ -100,13 +100,13 @@ discard block |
||
100 | 100 | return; |
101 | 101 | } |
102 | 102 | $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
103 | - if (isset(Benchmark::$start_times[ $timer_name ])) { |
|
104 | - $start_time = Benchmark::$start_times[ $timer_name ]; |
|
105 | - unset(Benchmark::$start_times[ $timer_name ]); |
|
103 | + if (isset(Benchmark::$start_times[$timer_name])) { |
|
104 | + $start_time = Benchmark::$start_times[$timer_name]; |
|
105 | + unset(Benchmark::$start_times[$timer_name]); |
|
106 | 106 | } else { |
107 | 107 | $start_time = array_pop(Benchmark::$start_times); |
108 | 108 | } |
109 | - Benchmark::$times[ $timer_name ] = number_format(microtime(true) - $start_time, 8); |
|
109 | + Benchmark::$times[$timer_name] = number_format(microtime(true) - $start_time, 8); |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | return; |
125 | 125 | } |
126 | 126 | $memory_used = Benchmark::convert(memory_get_usage(true)); |
127 | - Benchmark::$memory_usage[ $label ] = $memory_used; |
|
127 | + Benchmark::$memory_usage[$label] = $memory_used; |
|
128 | 128 | if ($output_now) { |
129 | 129 | echo $formatted |
130 | 130 | ? "<br>{$label} : {$memory_used}" |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | Benchmark::resetOutput(); |
145 | 145 | add_action( |
146 | 146 | 'shutdown', |
147 | - function () use ($formatted) { |
|
147 | + function() use ($formatted) { |
|
148 | 148 | Benchmark::displayResults(true, $formatted); |
149 | 149 | }, |
150 | 150 | 999999 |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | Benchmark::resetOutput(); |
166 | 166 | add_action( |
167 | 167 | 'shutdown', |
168 | - function () use ($filepath, $formatted, $append) { |
|
168 | + function() use ($filepath, $formatted, $append) { |
|
169 | 169 | Benchmark::writeResultsToFile($filepath, $formatted, $append); |
170 | 170 | }, |
171 | 171 | 999999 |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | if (Benchmark::doNotRun()) { |
183 | 183 | return ''; |
184 | 184 | } |
185 | - if (! empty(Benchmark::$times)) { |
|
185 | + if ( ! empty(Benchmark::$times)) { |
|
186 | 186 | $total = 0; |
187 | 187 | Benchmark::$output .= $formatted |
188 | 188 | ? '<span style="color:#999999; font-size:.8em;">( time in milliseconds )</span><br />' |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | Benchmark::$output .= '<span style="color:red">Like...HEEELLLP</span><br />'; |
208 | 208 | } |
209 | 209 | } |
210 | - if (! empty(Benchmark::$memory_usage)) { |
|
210 | + if ( ! empty(Benchmark::$memory_usage)) { |
|
211 | 211 | Benchmark::$output .= $formatted |
212 | 212 | ? '<h5>Memory</h5>' |
213 | 213 | : "\nMemory"; |
@@ -262,12 +262,12 @@ discard block |
||
262 | 262 | ? $filepath |
263 | 263 | : ''; |
264 | 264 | if (empty($filepath)) { |
265 | - $filepath = EVENT_ESPRESSO_UPLOAD_DIR . 'logs/benchmarking-' . date('Y-m-d') . '.html'; |
|
265 | + $filepath = EVENT_ESPRESSO_UPLOAD_DIR.'logs/benchmarking-'.date('Y-m-d').'.html'; |
|
266 | 266 | } |
267 | 267 | EEH_File::ensure_file_exists_and_is_writable($filepath); |
268 | 268 | file_put_contents( |
269 | 269 | $filepath, |
270 | - "\n" . date('Y-m-d H:i:s') . Benchmark::generateResults($formatted), |
|
270 | + "\n".date('Y-m-d H:i:s').Benchmark::generateResults($formatted), |
|
271 | 271 | $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
272 | 272 | ); |
273 | 273 | } |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | return round( |
286 | 286 | $size / pow(1024, $i = floor(log($size, 1024))), |
287 | 287 | 2 |
288 | - ) . ' ' . $unit[ absint($i) ]; |
|
288 | + ).' '.$unit[absint($i)]; |
|
289 | 289 | } |
290 | 290 | |
291 | 291 |
@@ -30,569 +30,569 @@ |
||
30 | 30 | { |
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * This was the best coffee related name I could think of to represent class name "aliases" |
|
35 | - * So classes can be found via an alias identifier, |
|
36 | - * that is revealed when it is run through... the filters... eh? get it? |
|
37 | - * |
|
38 | - * @var array $filters |
|
39 | - */ |
|
40 | - private $filters; |
|
41 | - |
|
42 | - /** |
|
43 | - * These are the classes that will actually build the objects (to order of course) |
|
44 | - * |
|
45 | - * @var array $coffee_makers |
|
46 | - */ |
|
47 | - private $coffee_makers; |
|
48 | - |
|
49 | - /** |
|
50 | - * where the instantiated "singleton" objects are stored |
|
51 | - * |
|
52 | - * @var CollectionInterface $carafe |
|
53 | - */ |
|
54 | - private $carafe; |
|
55 | - |
|
56 | - /** |
|
57 | - * collection of Recipes that instruct us how to brew objects |
|
58 | - * |
|
59 | - * @var CollectionInterface $recipes |
|
60 | - */ |
|
61 | - private $recipes; |
|
62 | - |
|
63 | - /** |
|
64 | - * collection of closures for brewing objects |
|
65 | - * |
|
66 | - * @var CollectionInterface $reservoir |
|
67 | - */ |
|
68 | - private $reservoir; |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * CoffeeShop constructor |
|
73 | - * |
|
74 | - * @throws InvalidInterfaceException |
|
75 | - */ |
|
76 | - public function __construct() |
|
77 | - { |
|
78 | - // array for storing class aliases |
|
79 | - $this->filters = array(); |
|
80 | - // create collection for storing shared services |
|
81 | - $this->carafe = new LooseCollection(''); |
|
82 | - // create collection for storing recipes that tell us how to build services and entities |
|
83 | - $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
|
84 | - // create collection for storing closures for constructing new entities |
|
85 | - $this->reservoir = new Collection('Closure'); |
|
86 | - // create collection for storing the generators that build our services and entity closures |
|
87 | - $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface'); |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Returns true if the container can return an entry for the given identifier. |
|
93 | - * Returns false otherwise. |
|
94 | - * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
|
95 | - * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
|
96 | - * |
|
97 | - * @param string $identifier Identifier of the entry to look for. |
|
98 | - * Typically a Fully Qualified Class Name |
|
99 | - * @return boolean |
|
100 | - * @throws InvalidIdentifierException |
|
101 | - */ |
|
102 | - public function has($identifier) |
|
103 | - { |
|
104 | - $identifier = $this->filterIdentifier($identifier); |
|
105 | - return $this->carafe->has($identifier); |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * finds a previously brewed (SHARED) service and returns it |
|
111 | - * |
|
112 | - * @param string $identifier Identifier for the entity class to be constructed. |
|
113 | - * Typically a Fully Qualified Class Name |
|
114 | - * @return mixed |
|
115 | - * @throws InvalidIdentifierException |
|
116 | - * @throws ServiceNotFoundException No service was found for this identifier. |
|
117 | - */ |
|
118 | - public function get($identifier) |
|
119 | - { |
|
120 | - $identifier = $this->filterIdentifier($identifier); |
|
121 | - if ($this->carafe->has($identifier)) { |
|
122 | - return $this->carafe->get($identifier); |
|
123 | - } |
|
124 | - throw new ServiceNotFoundException($identifier); |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * returns an instance of the requested entity type using the supplied arguments. |
|
130 | - * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
|
131 | - * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
|
132 | - * If the request is for a new entity and a closure exists in the reservoir for creating it, |
|
133 | - * then a new entity will be instantiated from the closure and returned. |
|
134 | - * If a closure does not exist, then one will be built and added to the reservoir |
|
135 | - * before instantiating the requested entity. |
|
136 | - * |
|
137 | - * @param string $identifier Identifier for the entity class to be constructed. |
|
138 | - * Typically a Fully Qualified Class Name |
|
139 | - * @param array $arguments an array of arguments to be passed to the entity constructor |
|
140 | - * @param string $type |
|
141 | - * @return mixed |
|
142 | - * @throws OutOfBoundsException |
|
143 | - * @throws InstantiationException |
|
144 | - * @throws InvalidDataTypeException |
|
145 | - * @throws InvalidClassException |
|
146 | - * @throws InvalidIdentifierException |
|
147 | - * @throws ServiceExistsException |
|
148 | - * @throws ServiceNotFoundException No service was found for this identifier. |
|
149 | - */ |
|
150 | - public function brew($identifier, $arguments = array(), $type = '') |
|
151 | - { |
|
152 | - // resolve any class aliases that may exist |
|
153 | - $identifier = $this->filterIdentifier($identifier); |
|
154 | - // is a shared service being requested and already exists in the carafe? |
|
155 | - $brewed = $this->getShared($identifier, $type); |
|
156 | - // then return whatever was found |
|
157 | - if ($brewed !== false) { |
|
158 | - return $brewed; |
|
159 | - } |
|
160 | - // if the reservoir doesn't have a closure already for the requested identifier, |
|
161 | - // then neither a shared service nor a closure for making entities has been built yet |
|
162 | - if (! $this->reservoir->has($identifier)) { |
|
163 | - // so let's brew something up and add it to the proper collection |
|
164 | - $brewed = $this->makeCoffee($identifier, $arguments, $type); |
|
165 | - } |
|
166 | - // did the requested class only require loading, and if so, was that successful? |
|
167 | - if ($this->brewedLoadOnly($brewed, $identifier, $type) === true) { |
|
168 | - return true; |
|
169 | - } |
|
170 | - // was the brewed item a callable factory function ? |
|
171 | - if (is_callable($brewed)) { |
|
172 | - // then instantiate a new entity from the cached closure |
|
173 | - return $brewed($arguments); |
|
174 | - } |
|
175 | - if ($brewed) { |
|
176 | - // requested object was a shared entity, so attempt to get it from the carafe again |
|
177 | - // because if it wasn't there before, then it should have just been brewed and added, |
|
178 | - // but if it still isn't there, then this time the thrown ServiceNotFoundException will not be caught |
|
179 | - return $this->get($identifier); |
|
180 | - } |
|
181 | - // if identifier is for a non-shared entity, |
|
182 | - // then either a cached closure already existed, or was just brewed |
|
183 | - return $this->brewedClosure($identifier, $arguments); |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * @param string $identifier |
|
189 | - * @param string $type |
|
190 | - * @return bool|mixed |
|
191 | - * @throws InvalidIdentifierException |
|
192 | - */ |
|
193 | - protected function getShared($identifier, $type) |
|
194 | - { |
|
195 | - try { |
|
196 | - if (empty($type) || $type === CoffeeMaker::BREW_SHARED) { |
|
197 | - // if a shared service was requested and an instance is in the carafe, then return it |
|
198 | - return $this->get($identifier); |
|
199 | - } |
|
200 | - } catch (ServiceNotFoundException $e) { |
|
201 | - // if not then we'll just catch the ServiceNotFoundException but not do anything just yet, |
|
202 | - // and instead, attempt to build whatever was requested |
|
203 | - } |
|
204 | - return false; |
|
205 | - } |
|
206 | - |
|
207 | - |
|
208 | - /** |
|
209 | - * @param mixed $brewed |
|
210 | - * @param string $identifier |
|
211 | - * @param string $type |
|
212 | - * @return bool |
|
213 | - * @throws InvalidClassException |
|
214 | - * @throws InvalidDataTypeException |
|
215 | - * @throws InvalidIdentifierException |
|
216 | - * @throws OutOfBoundsException |
|
217 | - * @throws ServiceExistsException |
|
218 | - * @throws ServiceNotFoundException |
|
219 | - */ |
|
220 | - protected function brewedLoadOnly($brewed, $identifier, $type) |
|
221 | - { |
|
222 | - if ($type === CoffeeMaker::BREW_LOAD_ONLY) { |
|
223 | - if ($brewed !== true) { |
|
224 | - throw new ServiceNotFoundException( |
|
225 | - sprintf( |
|
226 | - esc_html__( |
|
227 | - 'The "%1$s" class could not be loaded.', |
|
228 | - 'event_espresso' |
|
229 | - ), |
|
230 | - $identifier |
|
231 | - ) |
|
232 | - ); |
|
233 | - } |
|
234 | - return true; |
|
235 | - } |
|
236 | - return false; |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - /** |
|
241 | - * @param string $identifier |
|
242 | - * @param array $arguments |
|
243 | - * @return mixed |
|
244 | - * @throws InstantiationException |
|
245 | - */ |
|
246 | - protected function brewedClosure($identifier, array $arguments) |
|
247 | - { |
|
248 | - $closure = $this->reservoir->get($identifier); |
|
249 | - if (empty($closure)) { |
|
250 | - throw new InstantiationException( |
|
251 | - sprintf( |
|
252 | - esc_html__( |
|
253 | - 'Could not brew an instance of "%1$s".', |
|
254 | - 'event_espresso' |
|
255 | - ), |
|
256 | - $identifier |
|
257 | - ) |
|
258 | - ); |
|
259 | - } |
|
260 | - return $closure($arguments); |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - /** |
|
265 | - * @param CoffeeMakerInterface $coffee_maker |
|
266 | - * @param string $type |
|
267 | - * @return bool |
|
268 | - * @throws InvalidIdentifierException |
|
269 | - * @throws InvalidEntityException |
|
270 | - */ |
|
271 | - public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
|
272 | - { |
|
273 | - $type = CoffeeMaker::validateType($type); |
|
274 | - return $this->coffee_makers->add($coffee_maker, $type); |
|
275 | - } |
|
276 | - |
|
277 | - |
|
278 | - /** |
|
279 | - * @param string $identifier |
|
280 | - * @param callable $closure |
|
281 | - * @return callable|null |
|
282 | - * @throws InvalidIdentifierException |
|
283 | - * @throws InvalidDataTypeException |
|
284 | - */ |
|
285 | - public function addClosure($identifier, $closure) |
|
286 | - { |
|
287 | - if (! is_callable($closure)) { |
|
288 | - throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
|
289 | - } |
|
290 | - $identifier = $this->processIdentifier($identifier); |
|
291 | - if ($this->reservoir->add($closure, $identifier)) { |
|
292 | - return $closure; |
|
293 | - } |
|
294 | - return null; |
|
295 | - } |
|
296 | - |
|
297 | - |
|
298 | - /** |
|
299 | - * @param string $identifier |
|
300 | - * @return boolean |
|
301 | - * @throws InvalidIdentifierException |
|
302 | - */ |
|
303 | - public function removeClosure($identifier) |
|
304 | - { |
|
305 | - $identifier = $this->processIdentifier($identifier); |
|
306 | - if ($this->reservoir->has($identifier)) { |
|
307 | - return $this->reservoir->remove($this->reservoir->get($identifier)); |
|
308 | - } |
|
309 | - return false; |
|
310 | - } |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * @param string $identifier Identifier for the entity class that the service applies to |
|
315 | - * Typically a Fully Qualified Class Name |
|
316 | - * @param mixed $service |
|
317 | - * @return bool |
|
318 | - * @throws \EventEspresso\core\services\container\exceptions\InvalidServiceException |
|
319 | - * @throws InvalidIdentifierException |
|
320 | - */ |
|
321 | - public function addService($identifier, $service) |
|
322 | - { |
|
323 | - $identifier = $this->processIdentifier($identifier); |
|
324 | - $service = $this->validateService($identifier, $service); |
|
325 | - return $this->carafe->add($service, $identifier); |
|
326 | - } |
|
327 | - |
|
328 | - |
|
329 | - /** |
|
330 | - * @param string $identifier |
|
331 | - * @return boolean |
|
332 | - * @throws InvalidIdentifierException |
|
333 | - */ |
|
334 | - public function removeService($identifier) |
|
335 | - { |
|
336 | - $identifier = $this->processIdentifier($identifier); |
|
337 | - if ($this->carafe->has($identifier)) { |
|
338 | - return $this->carafe->remove($this->carafe->get($identifier)); |
|
339 | - } |
|
340 | - return false; |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * Adds instructions on how to brew objects |
|
346 | - * |
|
347 | - * @param RecipeInterface $recipe |
|
348 | - * @return mixed |
|
349 | - * @throws InvalidIdentifierException |
|
350 | - */ |
|
351 | - public function addRecipe(RecipeInterface $recipe) |
|
352 | - { |
|
353 | - $this->addAliases($recipe->identifier(), $recipe->filters()); |
|
354 | - $identifier = $this->processIdentifier($recipe->identifier()); |
|
355 | - return $this->recipes->add($recipe, $identifier); |
|
356 | - } |
|
357 | - |
|
358 | - |
|
359 | - /** |
|
360 | - * @param string $identifier The Recipe's identifier |
|
361 | - * @return boolean |
|
362 | - * @throws InvalidIdentifierException |
|
363 | - */ |
|
364 | - public function removeRecipe($identifier) |
|
365 | - { |
|
366 | - $identifier = $this->processIdentifier($identifier); |
|
367 | - if ($this->recipes->has($identifier)) { |
|
368 | - return $this->recipes->remove($this->recipes->get($identifier)); |
|
369 | - } |
|
370 | - return false; |
|
371 | - } |
|
372 | - |
|
373 | - |
|
374 | - /** |
|
375 | - * Get instructions on how to brew objects |
|
376 | - * |
|
377 | - * @param string $identifier Identifier for the entity class that the recipe applies to |
|
378 | - * Typically a Fully Qualified Class Name |
|
379 | - * @param string $type |
|
380 | - * @return RecipeInterface |
|
381 | - * @throws OutOfBoundsException |
|
382 | - * @throws InvalidIdentifierException |
|
383 | - */ |
|
384 | - public function getRecipe($identifier, $type = '') |
|
385 | - { |
|
386 | - $identifier = $this->processIdentifier($identifier); |
|
387 | - if ($this->recipes->has($identifier)) { |
|
388 | - return $this->recipes->get($identifier); |
|
389 | - } |
|
390 | - $default_recipes = $this->getDefaultRecipes(); |
|
391 | - $matches = array(); |
|
392 | - foreach ($default_recipes as $wildcard => $default_recipe) { |
|
393 | - // is the wildcard recipe prefix in the identifier ? |
|
394 | - if (strpos($identifier, $wildcard) !== false) { |
|
395 | - // track matches and use the number of wildcard characters matched for the key |
|
396 | - $matches[ strlen($wildcard) ] = $default_recipe; |
|
397 | - } |
|
398 | - } |
|
399 | - if (count($matches) > 0) { |
|
400 | - // sort our recipes by the number of wildcard characters matched |
|
401 | - ksort($matches); |
|
402 | - // then grab the last recipe form the list, since it had the most matching characters |
|
403 | - $match = array_pop($matches); |
|
404 | - // since we are using a default recipe, we need to set it's identifier and fqcn |
|
405 | - return $this->copyDefaultRecipe($match, $identifier, $type); |
|
406 | - } |
|
407 | - if ($this->recipes->has(Recipe::DEFAULT_ID)) { |
|
408 | - // since we are using a default recipe, we need to set it's identifier and fqcn |
|
409 | - return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type); |
|
410 | - } |
|
411 | - throw new OutOfBoundsException( |
|
412 | - sprintf( |
|
413 | - __('Could not brew coffee because no recipes were found for class "%1$s".', 'event_espresso'), |
|
414 | - $identifier |
|
415 | - ) |
|
416 | - ); |
|
417 | - } |
|
418 | - |
|
419 | - |
|
420 | - /** |
|
421 | - * adds class name aliases to list of filters |
|
422 | - * |
|
423 | - * @param string $identifier Identifier for the entity class that the alias applies to |
|
424 | - * Typically a Fully Qualified Class Name |
|
425 | - * @param array|string $aliases |
|
426 | - * @return void |
|
427 | - * @throws InvalidIdentifierException |
|
428 | - */ |
|
429 | - public function addAliases($identifier, $aliases) |
|
430 | - { |
|
431 | - if (empty($aliases)) { |
|
432 | - return; |
|
433 | - } |
|
434 | - $identifier = $this->processIdentifier($identifier); |
|
435 | - foreach ((array) $aliases as $alias) { |
|
436 | - $this->filters[ $this->processIdentifier($alias) ] = $identifier; |
|
437 | - } |
|
438 | - } |
|
439 | - |
|
440 | - |
|
441 | - /** |
|
442 | - * Adds a service to one of the internal collections |
|
443 | - * |
|
444 | - * @param $identifier |
|
445 | - * @param array $arguments |
|
446 | - * @param string $type |
|
447 | - * @return mixed |
|
448 | - * @throws InvalidDataTypeException |
|
449 | - * @throws InvalidClassException |
|
450 | - * @throws OutOfBoundsException |
|
451 | - * @throws InvalidIdentifierException |
|
452 | - * @throws ServiceExistsException |
|
453 | - */ |
|
454 | - private function makeCoffee($identifier, $arguments = array(), $type = '') |
|
455 | - { |
|
456 | - if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) { |
|
457 | - throw new ServiceExistsException($identifier); |
|
458 | - } |
|
459 | - $identifier = $this->filterIdentifier($identifier); |
|
460 | - $recipe = $this->getRecipe($identifier, $type); |
|
461 | - $type = ! empty($type) ? $type : $recipe->type(); |
|
462 | - $coffee_maker = $this->getCoffeeMaker($type); |
|
463 | - return $coffee_maker->brew($recipe, $arguments); |
|
464 | - } |
|
465 | - |
|
466 | - |
|
467 | - /** |
|
468 | - * filters alias identifiers to find the real class name |
|
469 | - * |
|
470 | - * @param string $identifier Identifier for the entity class that the filter applies to |
|
471 | - * Typically a Fully Qualified Class Name |
|
472 | - * @return string |
|
473 | - * @throws InvalidIdentifierException |
|
474 | - */ |
|
475 | - private function filterIdentifier($identifier) |
|
476 | - { |
|
477 | - $identifier = $this->processIdentifier($identifier); |
|
478 | - return isset($this->filters[ $identifier ]) && ! empty($this->filters[ $identifier ]) |
|
479 | - ? $this->filters[ $identifier ] |
|
480 | - : $identifier; |
|
481 | - } |
|
482 | - |
|
483 | - |
|
484 | - /** |
|
485 | - * verifies and standardizes identifiers |
|
486 | - * |
|
487 | - * @param string $identifier Identifier for the entity class |
|
488 | - * Typically a Fully Qualified Class Name |
|
489 | - * @return string |
|
490 | - * @throws InvalidIdentifierException |
|
491 | - */ |
|
492 | - private function processIdentifier($identifier) |
|
493 | - { |
|
494 | - if (! is_string($identifier)) { |
|
495 | - throw new InvalidIdentifierException( |
|
496 | - is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
497 | - '\Fully\Qualified\ClassName' |
|
498 | - ); |
|
499 | - } |
|
500 | - return ltrim($identifier, '\\'); |
|
501 | - } |
|
502 | - |
|
503 | - |
|
504 | - /** |
|
505 | - * @param string $type |
|
506 | - * @return CoffeeMakerInterface |
|
507 | - * @throws OutOfBoundsException |
|
508 | - * @throws InvalidDataTypeException |
|
509 | - * @throws InvalidClassException |
|
510 | - */ |
|
511 | - private function getCoffeeMaker($type) |
|
512 | - { |
|
513 | - if (! $this->coffee_makers->has($type)) { |
|
514 | - throw new OutOfBoundsException( |
|
515 | - __('The requested coffee maker is either missing or invalid.', 'event_espresso') |
|
516 | - ); |
|
517 | - } |
|
518 | - return $this->coffee_makers->get($type); |
|
519 | - } |
|
520 | - |
|
521 | - |
|
522 | - /** |
|
523 | - * Retrieves all recipes that use a wildcard "*" in their identifier |
|
524 | - * This allows recipes to be set up for handling |
|
525 | - * legacy classes that do not support PSR-4 autoloading. |
|
526 | - * for example: |
|
527 | - * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
|
528 | - * |
|
529 | - * @return array |
|
530 | - */ |
|
531 | - private function getDefaultRecipes() |
|
532 | - { |
|
533 | - $default_recipes = array(); |
|
534 | - $this->recipes->rewind(); |
|
535 | - while ($this->recipes->valid()) { |
|
536 | - $identifier = $this->recipes->getInfo(); |
|
537 | - // does this recipe use a wildcard ? (but is NOT the global default) |
|
538 | - if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
|
539 | - // strip the wildcard and use identifier as key |
|
540 | - $default_recipes[ str_replace('*', '', $identifier) ] = $this->recipes->current(); |
|
541 | - } |
|
542 | - $this->recipes->next(); |
|
543 | - } |
|
544 | - return $default_recipes; |
|
545 | - } |
|
546 | - |
|
547 | - |
|
548 | - /** |
|
549 | - * clones a default recipe and then copies details |
|
550 | - * from the incoming request to it so that it can be used |
|
551 | - * |
|
552 | - * @param RecipeInterface $default_recipe |
|
553 | - * @param string $identifier |
|
554 | - * @param string $type |
|
555 | - * @return RecipeInterface |
|
556 | - */ |
|
557 | - private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
|
558 | - { |
|
559 | - $recipe = clone $default_recipe; |
|
560 | - if (! empty($type)) { |
|
561 | - $recipe->setType($type); |
|
562 | - } |
|
563 | - // is this the base default recipe ? |
|
564 | - if ($default_recipe->identifier() === Recipe::DEFAULT_ID) { |
|
565 | - $recipe->setIdentifier($identifier); |
|
566 | - $recipe->setFqcn($identifier); |
|
567 | - return $recipe; |
|
568 | - } |
|
569 | - $recipe->setIdentifier($identifier); |
|
570 | - foreach ($default_recipe->paths() as $path) { |
|
571 | - $path = str_replace('*', $identifier, $path); |
|
572 | - if (is_readable($path)) { |
|
573 | - $recipe->setPaths($path); |
|
574 | - } |
|
575 | - } |
|
576 | - $recipe->setFqcn($identifier); |
|
577 | - return $recipe; |
|
578 | - } |
|
579 | - |
|
580 | - |
|
581 | - /** |
|
582 | - * @param string $identifier Identifier for the entity class that the service applies to |
|
583 | - * Typically a Fully Qualified Class Name |
|
584 | - * @param mixed $service |
|
585 | - * @return mixed |
|
586 | - * @throws InvalidServiceException |
|
587 | - */ |
|
588 | - private function validateService($identifier, $service) |
|
589 | - { |
|
590 | - if (! is_object($service)) { |
|
591 | - throw new InvalidServiceException( |
|
592 | - $identifier, |
|
593 | - $service |
|
594 | - ); |
|
595 | - } |
|
596 | - return $service; |
|
597 | - } |
|
33 | + /** |
|
34 | + * This was the best coffee related name I could think of to represent class name "aliases" |
|
35 | + * So classes can be found via an alias identifier, |
|
36 | + * that is revealed when it is run through... the filters... eh? get it? |
|
37 | + * |
|
38 | + * @var array $filters |
|
39 | + */ |
|
40 | + private $filters; |
|
41 | + |
|
42 | + /** |
|
43 | + * These are the classes that will actually build the objects (to order of course) |
|
44 | + * |
|
45 | + * @var array $coffee_makers |
|
46 | + */ |
|
47 | + private $coffee_makers; |
|
48 | + |
|
49 | + /** |
|
50 | + * where the instantiated "singleton" objects are stored |
|
51 | + * |
|
52 | + * @var CollectionInterface $carafe |
|
53 | + */ |
|
54 | + private $carafe; |
|
55 | + |
|
56 | + /** |
|
57 | + * collection of Recipes that instruct us how to brew objects |
|
58 | + * |
|
59 | + * @var CollectionInterface $recipes |
|
60 | + */ |
|
61 | + private $recipes; |
|
62 | + |
|
63 | + /** |
|
64 | + * collection of closures for brewing objects |
|
65 | + * |
|
66 | + * @var CollectionInterface $reservoir |
|
67 | + */ |
|
68 | + private $reservoir; |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * CoffeeShop constructor |
|
73 | + * |
|
74 | + * @throws InvalidInterfaceException |
|
75 | + */ |
|
76 | + public function __construct() |
|
77 | + { |
|
78 | + // array for storing class aliases |
|
79 | + $this->filters = array(); |
|
80 | + // create collection for storing shared services |
|
81 | + $this->carafe = new LooseCollection(''); |
|
82 | + // create collection for storing recipes that tell us how to build services and entities |
|
83 | + $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
|
84 | + // create collection for storing closures for constructing new entities |
|
85 | + $this->reservoir = new Collection('Closure'); |
|
86 | + // create collection for storing the generators that build our services and entity closures |
|
87 | + $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface'); |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Returns true if the container can return an entry for the given identifier. |
|
93 | + * Returns false otherwise. |
|
94 | + * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
|
95 | + * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
|
96 | + * |
|
97 | + * @param string $identifier Identifier of the entry to look for. |
|
98 | + * Typically a Fully Qualified Class Name |
|
99 | + * @return boolean |
|
100 | + * @throws InvalidIdentifierException |
|
101 | + */ |
|
102 | + public function has($identifier) |
|
103 | + { |
|
104 | + $identifier = $this->filterIdentifier($identifier); |
|
105 | + return $this->carafe->has($identifier); |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * finds a previously brewed (SHARED) service and returns it |
|
111 | + * |
|
112 | + * @param string $identifier Identifier for the entity class to be constructed. |
|
113 | + * Typically a Fully Qualified Class Name |
|
114 | + * @return mixed |
|
115 | + * @throws InvalidIdentifierException |
|
116 | + * @throws ServiceNotFoundException No service was found for this identifier. |
|
117 | + */ |
|
118 | + public function get($identifier) |
|
119 | + { |
|
120 | + $identifier = $this->filterIdentifier($identifier); |
|
121 | + if ($this->carafe->has($identifier)) { |
|
122 | + return $this->carafe->get($identifier); |
|
123 | + } |
|
124 | + throw new ServiceNotFoundException($identifier); |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * returns an instance of the requested entity type using the supplied arguments. |
|
130 | + * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
|
131 | + * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
|
132 | + * If the request is for a new entity and a closure exists in the reservoir for creating it, |
|
133 | + * then a new entity will be instantiated from the closure and returned. |
|
134 | + * If a closure does not exist, then one will be built and added to the reservoir |
|
135 | + * before instantiating the requested entity. |
|
136 | + * |
|
137 | + * @param string $identifier Identifier for the entity class to be constructed. |
|
138 | + * Typically a Fully Qualified Class Name |
|
139 | + * @param array $arguments an array of arguments to be passed to the entity constructor |
|
140 | + * @param string $type |
|
141 | + * @return mixed |
|
142 | + * @throws OutOfBoundsException |
|
143 | + * @throws InstantiationException |
|
144 | + * @throws InvalidDataTypeException |
|
145 | + * @throws InvalidClassException |
|
146 | + * @throws InvalidIdentifierException |
|
147 | + * @throws ServiceExistsException |
|
148 | + * @throws ServiceNotFoundException No service was found for this identifier. |
|
149 | + */ |
|
150 | + public function brew($identifier, $arguments = array(), $type = '') |
|
151 | + { |
|
152 | + // resolve any class aliases that may exist |
|
153 | + $identifier = $this->filterIdentifier($identifier); |
|
154 | + // is a shared service being requested and already exists in the carafe? |
|
155 | + $brewed = $this->getShared($identifier, $type); |
|
156 | + // then return whatever was found |
|
157 | + if ($brewed !== false) { |
|
158 | + return $brewed; |
|
159 | + } |
|
160 | + // if the reservoir doesn't have a closure already for the requested identifier, |
|
161 | + // then neither a shared service nor a closure for making entities has been built yet |
|
162 | + if (! $this->reservoir->has($identifier)) { |
|
163 | + // so let's brew something up and add it to the proper collection |
|
164 | + $brewed = $this->makeCoffee($identifier, $arguments, $type); |
|
165 | + } |
|
166 | + // did the requested class only require loading, and if so, was that successful? |
|
167 | + if ($this->brewedLoadOnly($brewed, $identifier, $type) === true) { |
|
168 | + return true; |
|
169 | + } |
|
170 | + // was the brewed item a callable factory function ? |
|
171 | + if (is_callable($brewed)) { |
|
172 | + // then instantiate a new entity from the cached closure |
|
173 | + return $brewed($arguments); |
|
174 | + } |
|
175 | + if ($brewed) { |
|
176 | + // requested object was a shared entity, so attempt to get it from the carafe again |
|
177 | + // because if it wasn't there before, then it should have just been brewed and added, |
|
178 | + // but if it still isn't there, then this time the thrown ServiceNotFoundException will not be caught |
|
179 | + return $this->get($identifier); |
|
180 | + } |
|
181 | + // if identifier is for a non-shared entity, |
|
182 | + // then either a cached closure already existed, or was just brewed |
|
183 | + return $this->brewedClosure($identifier, $arguments); |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + /** |
|
188 | + * @param string $identifier |
|
189 | + * @param string $type |
|
190 | + * @return bool|mixed |
|
191 | + * @throws InvalidIdentifierException |
|
192 | + */ |
|
193 | + protected function getShared($identifier, $type) |
|
194 | + { |
|
195 | + try { |
|
196 | + if (empty($type) || $type === CoffeeMaker::BREW_SHARED) { |
|
197 | + // if a shared service was requested and an instance is in the carafe, then return it |
|
198 | + return $this->get($identifier); |
|
199 | + } |
|
200 | + } catch (ServiceNotFoundException $e) { |
|
201 | + // if not then we'll just catch the ServiceNotFoundException but not do anything just yet, |
|
202 | + // and instead, attempt to build whatever was requested |
|
203 | + } |
|
204 | + return false; |
|
205 | + } |
|
206 | + |
|
207 | + |
|
208 | + /** |
|
209 | + * @param mixed $brewed |
|
210 | + * @param string $identifier |
|
211 | + * @param string $type |
|
212 | + * @return bool |
|
213 | + * @throws InvalidClassException |
|
214 | + * @throws InvalidDataTypeException |
|
215 | + * @throws InvalidIdentifierException |
|
216 | + * @throws OutOfBoundsException |
|
217 | + * @throws ServiceExistsException |
|
218 | + * @throws ServiceNotFoundException |
|
219 | + */ |
|
220 | + protected function brewedLoadOnly($brewed, $identifier, $type) |
|
221 | + { |
|
222 | + if ($type === CoffeeMaker::BREW_LOAD_ONLY) { |
|
223 | + if ($brewed !== true) { |
|
224 | + throw new ServiceNotFoundException( |
|
225 | + sprintf( |
|
226 | + esc_html__( |
|
227 | + 'The "%1$s" class could not be loaded.', |
|
228 | + 'event_espresso' |
|
229 | + ), |
|
230 | + $identifier |
|
231 | + ) |
|
232 | + ); |
|
233 | + } |
|
234 | + return true; |
|
235 | + } |
|
236 | + return false; |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * @param string $identifier |
|
242 | + * @param array $arguments |
|
243 | + * @return mixed |
|
244 | + * @throws InstantiationException |
|
245 | + */ |
|
246 | + protected function brewedClosure($identifier, array $arguments) |
|
247 | + { |
|
248 | + $closure = $this->reservoir->get($identifier); |
|
249 | + if (empty($closure)) { |
|
250 | + throw new InstantiationException( |
|
251 | + sprintf( |
|
252 | + esc_html__( |
|
253 | + 'Could not brew an instance of "%1$s".', |
|
254 | + 'event_espresso' |
|
255 | + ), |
|
256 | + $identifier |
|
257 | + ) |
|
258 | + ); |
|
259 | + } |
|
260 | + return $closure($arguments); |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + /** |
|
265 | + * @param CoffeeMakerInterface $coffee_maker |
|
266 | + * @param string $type |
|
267 | + * @return bool |
|
268 | + * @throws InvalidIdentifierException |
|
269 | + * @throws InvalidEntityException |
|
270 | + */ |
|
271 | + public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
|
272 | + { |
|
273 | + $type = CoffeeMaker::validateType($type); |
|
274 | + return $this->coffee_makers->add($coffee_maker, $type); |
|
275 | + } |
|
276 | + |
|
277 | + |
|
278 | + /** |
|
279 | + * @param string $identifier |
|
280 | + * @param callable $closure |
|
281 | + * @return callable|null |
|
282 | + * @throws InvalidIdentifierException |
|
283 | + * @throws InvalidDataTypeException |
|
284 | + */ |
|
285 | + public function addClosure($identifier, $closure) |
|
286 | + { |
|
287 | + if (! is_callable($closure)) { |
|
288 | + throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
|
289 | + } |
|
290 | + $identifier = $this->processIdentifier($identifier); |
|
291 | + if ($this->reservoir->add($closure, $identifier)) { |
|
292 | + return $closure; |
|
293 | + } |
|
294 | + return null; |
|
295 | + } |
|
296 | + |
|
297 | + |
|
298 | + /** |
|
299 | + * @param string $identifier |
|
300 | + * @return boolean |
|
301 | + * @throws InvalidIdentifierException |
|
302 | + */ |
|
303 | + public function removeClosure($identifier) |
|
304 | + { |
|
305 | + $identifier = $this->processIdentifier($identifier); |
|
306 | + if ($this->reservoir->has($identifier)) { |
|
307 | + return $this->reservoir->remove($this->reservoir->get($identifier)); |
|
308 | + } |
|
309 | + return false; |
|
310 | + } |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * @param string $identifier Identifier for the entity class that the service applies to |
|
315 | + * Typically a Fully Qualified Class Name |
|
316 | + * @param mixed $service |
|
317 | + * @return bool |
|
318 | + * @throws \EventEspresso\core\services\container\exceptions\InvalidServiceException |
|
319 | + * @throws InvalidIdentifierException |
|
320 | + */ |
|
321 | + public function addService($identifier, $service) |
|
322 | + { |
|
323 | + $identifier = $this->processIdentifier($identifier); |
|
324 | + $service = $this->validateService($identifier, $service); |
|
325 | + return $this->carafe->add($service, $identifier); |
|
326 | + } |
|
327 | + |
|
328 | + |
|
329 | + /** |
|
330 | + * @param string $identifier |
|
331 | + * @return boolean |
|
332 | + * @throws InvalidIdentifierException |
|
333 | + */ |
|
334 | + public function removeService($identifier) |
|
335 | + { |
|
336 | + $identifier = $this->processIdentifier($identifier); |
|
337 | + if ($this->carafe->has($identifier)) { |
|
338 | + return $this->carafe->remove($this->carafe->get($identifier)); |
|
339 | + } |
|
340 | + return false; |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * Adds instructions on how to brew objects |
|
346 | + * |
|
347 | + * @param RecipeInterface $recipe |
|
348 | + * @return mixed |
|
349 | + * @throws InvalidIdentifierException |
|
350 | + */ |
|
351 | + public function addRecipe(RecipeInterface $recipe) |
|
352 | + { |
|
353 | + $this->addAliases($recipe->identifier(), $recipe->filters()); |
|
354 | + $identifier = $this->processIdentifier($recipe->identifier()); |
|
355 | + return $this->recipes->add($recipe, $identifier); |
|
356 | + } |
|
357 | + |
|
358 | + |
|
359 | + /** |
|
360 | + * @param string $identifier The Recipe's identifier |
|
361 | + * @return boolean |
|
362 | + * @throws InvalidIdentifierException |
|
363 | + */ |
|
364 | + public function removeRecipe($identifier) |
|
365 | + { |
|
366 | + $identifier = $this->processIdentifier($identifier); |
|
367 | + if ($this->recipes->has($identifier)) { |
|
368 | + return $this->recipes->remove($this->recipes->get($identifier)); |
|
369 | + } |
|
370 | + return false; |
|
371 | + } |
|
372 | + |
|
373 | + |
|
374 | + /** |
|
375 | + * Get instructions on how to brew objects |
|
376 | + * |
|
377 | + * @param string $identifier Identifier for the entity class that the recipe applies to |
|
378 | + * Typically a Fully Qualified Class Name |
|
379 | + * @param string $type |
|
380 | + * @return RecipeInterface |
|
381 | + * @throws OutOfBoundsException |
|
382 | + * @throws InvalidIdentifierException |
|
383 | + */ |
|
384 | + public function getRecipe($identifier, $type = '') |
|
385 | + { |
|
386 | + $identifier = $this->processIdentifier($identifier); |
|
387 | + if ($this->recipes->has($identifier)) { |
|
388 | + return $this->recipes->get($identifier); |
|
389 | + } |
|
390 | + $default_recipes = $this->getDefaultRecipes(); |
|
391 | + $matches = array(); |
|
392 | + foreach ($default_recipes as $wildcard => $default_recipe) { |
|
393 | + // is the wildcard recipe prefix in the identifier ? |
|
394 | + if (strpos($identifier, $wildcard) !== false) { |
|
395 | + // track matches and use the number of wildcard characters matched for the key |
|
396 | + $matches[ strlen($wildcard) ] = $default_recipe; |
|
397 | + } |
|
398 | + } |
|
399 | + if (count($matches) > 0) { |
|
400 | + // sort our recipes by the number of wildcard characters matched |
|
401 | + ksort($matches); |
|
402 | + // then grab the last recipe form the list, since it had the most matching characters |
|
403 | + $match = array_pop($matches); |
|
404 | + // since we are using a default recipe, we need to set it's identifier and fqcn |
|
405 | + return $this->copyDefaultRecipe($match, $identifier, $type); |
|
406 | + } |
|
407 | + if ($this->recipes->has(Recipe::DEFAULT_ID)) { |
|
408 | + // since we are using a default recipe, we need to set it's identifier and fqcn |
|
409 | + return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type); |
|
410 | + } |
|
411 | + throw new OutOfBoundsException( |
|
412 | + sprintf( |
|
413 | + __('Could not brew coffee because no recipes were found for class "%1$s".', 'event_espresso'), |
|
414 | + $identifier |
|
415 | + ) |
|
416 | + ); |
|
417 | + } |
|
418 | + |
|
419 | + |
|
420 | + /** |
|
421 | + * adds class name aliases to list of filters |
|
422 | + * |
|
423 | + * @param string $identifier Identifier for the entity class that the alias applies to |
|
424 | + * Typically a Fully Qualified Class Name |
|
425 | + * @param array|string $aliases |
|
426 | + * @return void |
|
427 | + * @throws InvalidIdentifierException |
|
428 | + */ |
|
429 | + public function addAliases($identifier, $aliases) |
|
430 | + { |
|
431 | + if (empty($aliases)) { |
|
432 | + return; |
|
433 | + } |
|
434 | + $identifier = $this->processIdentifier($identifier); |
|
435 | + foreach ((array) $aliases as $alias) { |
|
436 | + $this->filters[ $this->processIdentifier($alias) ] = $identifier; |
|
437 | + } |
|
438 | + } |
|
439 | + |
|
440 | + |
|
441 | + /** |
|
442 | + * Adds a service to one of the internal collections |
|
443 | + * |
|
444 | + * @param $identifier |
|
445 | + * @param array $arguments |
|
446 | + * @param string $type |
|
447 | + * @return mixed |
|
448 | + * @throws InvalidDataTypeException |
|
449 | + * @throws InvalidClassException |
|
450 | + * @throws OutOfBoundsException |
|
451 | + * @throws InvalidIdentifierException |
|
452 | + * @throws ServiceExistsException |
|
453 | + */ |
|
454 | + private function makeCoffee($identifier, $arguments = array(), $type = '') |
|
455 | + { |
|
456 | + if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) { |
|
457 | + throw new ServiceExistsException($identifier); |
|
458 | + } |
|
459 | + $identifier = $this->filterIdentifier($identifier); |
|
460 | + $recipe = $this->getRecipe($identifier, $type); |
|
461 | + $type = ! empty($type) ? $type : $recipe->type(); |
|
462 | + $coffee_maker = $this->getCoffeeMaker($type); |
|
463 | + return $coffee_maker->brew($recipe, $arguments); |
|
464 | + } |
|
465 | + |
|
466 | + |
|
467 | + /** |
|
468 | + * filters alias identifiers to find the real class name |
|
469 | + * |
|
470 | + * @param string $identifier Identifier for the entity class that the filter applies to |
|
471 | + * Typically a Fully Qualified Class Name |
|
472 | + * @return string |
|
473 | + * @throws InvalidIdentifierException |
|
474 | + */ |
|
475 | + private function filterIdentifier($identifier) |
|
476 | + { |
|
477 | + $identifier = $this->processIdentifier($identifier); |
|
478 | + return isset($this->filters[ $identifier ]) && ! empty($this->filters[ $identifier ]) |
|
479 | + ? $this->filters[ $identifier ] |
|
480 | + : $identifier; |
|
481 | + } |
|
482 | + |
|
483 | + |
|
484 | + /** |
|
485 | + * verifies and standardizes identifiers |
|
486 | + * |
|
487 | + * @param string $identifier Identifier for the entity class |
|
488 | + * Typically a Fully Qualified Class Name |
|
489 | + * @return string |
|
490 | + * @throws InvalidIdentifierException |
|
491 | + */ |
|
492 | + private function processIdentifier($identifier) |
|
493 | + { |
|
494 | + if (! is_string($identifier)) { |
|
495 | + throw new InvalidIdentifierException( |
|
496 | + is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
497 | + '\Fully\Qualified\ClassName' |
|
498 | + ); |
|
499 | + } |
|
500 | + return ltrim($identifier, '\\'); |
|
501 | + } |
|
502 | + |
|
503 | + |
|
504 | + /** |
|
505 | + * @param string $type |
|
506 | + * @return CoffeeMakerInterface |
|
507 | + * @throws OutOfBoundsException |
|
508 | + * @throws InvalidDataTypeException |
|
509 | + * @throws InvalidClassException |
|
510 | + */ |
|
511 | + private function getCoffeeMaker($type) |
|
512 | + { |
|
513 | + if (! $this->coffee_makers->has($type)) { |
|
514 | + throw new OutOfBoundsException( |
|
515 | + __('The requested coffee maker is either missing or invalid.', 'event_espresso') |
|
516 | + ); |
|
517 | + } |
|
518 | + return $this->coffee_makers->get($type); |
|
519 | + } |
|
520 | + |
|
521 | + |
|
522 | + /** |
|
523 | + * Retrieves all recipes that use a wildcard "*" in their identifier |
|
524 | + * This allows recipes to be set up for handling |
|
525 | + * legacy classes that do not support PSR-4 autoloading. |
|
526 | + * for example: |
|
527 | + * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
|
528 | + * |
|
529 | + * @return array |
|
530 | + */ |
|
531 | + private function getDefaultRecipes() |
|
532 | + { |
|
533 | + $default_recipes = array(); |
|
534 | + $this->recipes->rewind(); |
|
535 | + while ($this->recipes->valid()) { |
|
536 | + $identifier = $this->recipes->getInfo(); |
|
537 | + // does this recipe use a wildcard ? (but is NOT the global default) |
|
538 | + if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
|
539 | + // strip the wildcard and use identifier as key |
|
540 | + $default_recipes[ str_replace('*', '', $identifier) ] = $this->recipes->current(); |
|
541 | + } |
|
542 | + $this->recipes->next(); |
|
543 | + } |
|
544 | + return $default_recipes; |
|
545 | + } |
|
546 | + |
|
547 | + |
|
548 | + /** |
|
549 | + * clones a default recipe and then copies details |
|
550 | + * from the incoming request to it so that it can be used |
|
551 | + * |
|
552 | + * @param RecipeInterface $default_recipe |
|
553 | + * @param string $identifier |
|
554 | + * @param string $type |
|
555 | + * @return RecipeInterface |
|
556 | + */ |
|
557 | + private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
|
558 | + { |
|
559 | + $recipe = clone $default_recipe; |
|
560 | + if (! empty($type)) { |
|
561 | + $recipe->setType($type); |
|
562 | + } |
|
563 | + // is this the base default recipe ? |
|
564 | + if ($default_recipe->identifier() === Recipe::DEFAULT_ID) { |
|
565 | + $recipe->setIdentifier($identifier); |
|
566 | + $recipe->setFqcn($identifier); |
|
567 | + return $recipe; |
|
568 | + } |
|
569 | + $recipe->setIdentifier($identifier); |
|
570 | + foreach ($default_recipe->paths() as $path) { |
|
571 | + $path = str_replace('*', $identifier, $path); |
|
572 | + if (is_readable($path)) { |
|
573 | + $recipe->setPaths($path); |
|
574 | + } |
|
575 | + } |
|
576 | + $recipe->setFqcn($identifier); |
|
577 | + return $recipe; |
|
578 | + } |
|
579 | + |
|
580 | + |
|
581 | + /** |
|
582 | + * @param string $identifier Identifier for the entity class that the service applies to |
|
583 | + * Typically a Fully Qualified Class Name |
|
584 | + * @param mixed $service |
|
585 | + * @return mixed |
|
586 | + * @throws InvalidServiceException |
|
587 | + */ |
|
588 | + private function validateService($identifier, $service) |
|
589 | + { |
|
590 | + if (! is_object($service)) { |
|
591 | + throw new InvalidServiceException( |
|
592 | + $identifier, |
|
593 | + $service |
|
594 | + ); |
|
595 | + } |
|
596 | + return $service; |
|
597 | + } |
|
598 | 598 | } |
@@ -159,7 +159,7 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 | __('The requested coffee maker is either missing or invalid.', 'event_espresso') |
516 | 516 | ); |
@@ -537,7 +537,7 @@ discard block |
||
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 |
||
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 |
||
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 |
@@ -18,156 +18,156 @@ |
||
18 | 18 | abstract class CoffeeMaker implements CoffeeMakerInterface |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given) |
|
23 | - */ |
|
24 | - const BREW_NEW = 'new'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Indicates that CoffeeMaker should always return a SHARED instance |
|
28 | - */ |
|
29 | - const BREW_SHARED = 'shared'; |
|
30 | - |
|
31 | - /** |
|
32 | - * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate |
|
33 | - */ |
|
34 | - const BREW_LOAD_ONLY = 'load_only'; |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * @var CoffeePotInterface $coffee_pot |
|
39 | - */ |
|
40 | - private $coffee_pot; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var DependencyInjector $injector |
|
44 | - */ |
|
45 | - private $injector; |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * @return array |
|
50 | - */ |
|
51 | - public static function getTypes() |
|
52 | - { |
|
53 | - return (array) apply_filters( |
|
54 | - 'FHEE__EventEspresso\core\services\container\CoffeeMaker__getTypes', |
|
55 | - array( |
|
56 | - CoffeeMaker::BREW_NEW, |
|
57 | - CoffeeMaker::BREW_SHARED, |
|
58 | - CoffeeMaker::BREW_LOAD_ONLY, |
|
59 | - ) |
|
60 | - ); |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * @param $type |
|
66 | - * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
67 | - */ |
|
68 | - public static function validateType($type) |
|
69 | - { |
|
70 | - $types = CoffeeMaker::getTypes(); |
|
71 | - if (! in_array($type, $types, true)) { |
|
72 | - throw new InvalidIdentifierException( |
|
73 | - is_object($type) ? get_class($type) : gettype($type), |
|
74 | - __( |
|
75 | - 'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)', |
|
76 | - 'event_espresso' |
|
77 | - ) |
|
78 | - ); |
|
79 | - } |
|
80 | - return $type; |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * CoffeeMaker constructor. |
|
86 | - * |
|
87 | - * @param CoffeePotInterface $coffee_pot |
|
88 | - * @param InjectorInterface $injector |
|
89 | - */ |
|
90 | - public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector) |
|
91 | - { |
|
92 | - $this->coffee_pot = $coffee_pot; |
|
93 | - $this->injector = $injector; |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - /** |
|
98 | - * @return \EventEspresso\core\services\container\CoffeePotInterface |
|
99 | - */ |
|
100 | - protected function coffeePot() |
|
101 | - { |
|
102 | - return $this->coffee_pot; |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * @return \EventEspresso\core\services\container\DependencyInjector |
|
108 | - */ |
|
109 | - protected function injector() |
|
110 | - { |
|
111 | - return $this->injector; |
|
112 | - } |
|
113 | - |
|
114 | - |
|
115 | - /** |
|
116 | - * Examines the constructor to determine which method should be used for instantiation |
|
117 | - * |
|
118 | - * @param \ReflectionClass $reflector |
|
119 | - * @return mixed |
|
120 | - * @throws InstantiationException |
|
121 | - */ |
|
122 | - protected function resolveInstantiationMethod(\ReflectionClass $reflector) |
|
123 | - { |
|
124 | - if ($reflector->getConstructor() === null) { |
|
125 | - return 'NewInstance'; |
|
126 | - } |
|
127 | - if ($reflector->isInstantiable()) { |
|
128 | - return 'NewInstanceArgs'; |
|
129 | - } |
|
130 | - if (method_exists($reflector->getName(), 'instance')) { |
|
131 | - return 'instance'; |
|
132 | - } |
|
133 | - if (method_exists($reflector->getName(), 'new_instance')) { |
|
134 | - return 'new_instance'; |
|
135 | - } |
|
136 | - if (method_exists($reflector->getName(), 'new_instance_from_db')) { |
|
137 | - return 'new_instance_from_db'; |
|
138 | - } |
|
139 | - throw new InstantiationException($reflector->getName()); |
|
140 | - } |
|
141 | - |
|
142 | - |
|
143 | - /** |
|
144 | - * Ensures files for classes that are not PSR-4 compatible are loaded |
|
145 | - * and then verifies that classes exist where applicable |
|
146 | - * |
|
147 | - * @param RecipeInterface $recipe |
|
148 | - * @return bool |
|
149 | - * @throws InvalidClassException |
|
150 | - */ |
|
151 | - protected function resolveClassAndFilepath(RecipeInterface $recipe) |
|
152 | - { |
|
153 | - $paths = $recipe->paths(); |
|
154 | - if (! empty($paths)) { |
|
155 | - foreach ($paths as $path) { |
|
156 | - if (strpos($path, '*') === false && is_readable($path)) { |
|
157 | - require_once($path); |
|
158 | - } |
|
159 | - } |
|
160 | - } |
|
161 | - // re: using "false" for class_exists() second param: |
|
162 | - // if a class name is not already known to PHP, then class_exists() will run through |
|
163 | - // all of the registered spl_autoload functions until it either finds the class, |
|
164 | - // or gets to the end of the registered spl_autoload functions. |
|
165 | - // When the second parameter is true, it will also attempt to load the class file, |
|
166 | - // but it will also trigger an error if the class can not be loaded. |
|
167 | - // We don't want that extra error in the mix, so we have set the second param to "false" |
|
168 | - if ($recipe->type() !== CoffeeMaker::BREW_LOAD_ONLY && ! class_exists($recipe->fqcn(), false)) { |
|
169 | - throw new InvalidClassException($recipe->identifier()); |
|
170 | - } |
|
171 | - return true; |
|
172 | - } |
|
21 | + /** |
|
22 | + * Indicates that CoffeeMaker should construct a NEW entity instance from the provided arguments (if given) |
|
23 | + */ |
|
24 | + const BREW_NEW = 'new'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Indicates that CoffeeMaker should always return a SHARED instance |
|
28 | + */ |
|
29 | + const BREW_SHARED = 'shared'; |
|
30 | + |
|
31 | + /** |
|
32 | + * Indicates that CoffeeMaker should only load the file/class/interface but NOT instantiate |
|
33 | + */ |
|
34 | + const BREW_LOAD_ONLY = 'load_only'; |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * @var CoffeePotInterface $coffee_pot |
|
39 | + */ |
|
40 | + private $coffee_pot; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var DependencyInjector $injector |
|
44 | + */ |
|
45 | + private $injector; |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * @return array |
|
50 | + */ |
|
51 | + public static function getTypes() |
|
52 | + { |
|
53 | + return (array) apply_filters( |
|
54 | + 'FHEE__EventEspresso\core\services\container\CoffeeMaker__getTypes', |
|
55 | + array( |
|
56 | + CoffeeMaker::BREW_NEW, |
|
57 | + CoffeeMaker::BREW_SHARED, |
|
58 | + CoffeeMaker::BREW_LOAD_ONLY, |
|
59 | + ) |
|
60 | + ); |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * @param $type |
|
66 | + * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
|
67 | + */ |
|
68 | + public static function validateType($type) |
|
69 | + { |
|
70 | + $types = CoffeeMaker::getTypes(); |
|
71 | + if (! in_array($type, $types, true)) { |
|
72 | + throw new InvalidIdentifierException( |
|
73 | + is_object($type) ? get_class($type) : gettype($type), |
|
74 | + __( |
|
75 | + 'recipe type (one of the class constants on \EventEspresso\core\services\container\CoffeeMaker)', |
|
76 | + 'event_espresso' |
|
77 | + ) |
|
78 | + ); |
|
79 | + } |
|
80 | + return $type; |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * CoffeeMaker constructor. |
|
86 | + * |
|
87 | + * @param CoffeePotInterface $coffee_pot |
|
88 | + * @param InjectorInterface $injector |
|
89 | + */ |
|
90 | + public function __construct(CoffeePotInterface $coffee_pot, InjectorInterface $injector) |
|
91 | + { |
|
92 | + $this->coffee_pot = $coffee_pot; |
|
93 | + $this->injector = $injector; |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + /** |
|
98 | + * @return \EventEspresso\core\services\container\CoffeePotInterface |
|
99 | + */ |
|
100 | + protected function coffeePot() |
|
101 | + { |
|
102 | + return $this->coffee_pot; |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * @return \EventEspresso\core\services\container\DependencyInjector |
|
108 | + */ |
|
109 | + protected function injector() |
|
110 | + { |
|
111 | + return $this->injector; |
|
112 | + } |
|
113 | + |
|
114 | + |
|
115 | + /** |
|
116 | + * Examines the constructor to determine which method should be used for instantiation |
|
117 | + * |
|
118 | + * @param \ReflectionClass $reflector |
|
119 | + * @return mixed |
|
120 | + * @throws InstantiationException |
|
121 | + */ |
|
122 | + protected function resolveInstantiationMethod(\ReflectionClass $reflector) |
|
123 | + { |
|
124 | + if ($reflector->getConstructor() === null) { |
|
125 | + return 'NewInstance'; |
|
126 | + } |
|
127 | + if ($reflector->isInstantiable()) { |
|
128 | + return 'NewInstanceArgs'; |
|
129 | + } |
|
130 | + if (method_exists($reflector->getName(), 'instance')) { |
|
131 | + return 'instance'; |
|
132 | + } |
|
133 | + if (method_exists($reflector->getName(), 'new_instance')) { |
|
134 | + return 'new_instance'; |
|
135 | + } |
|
136 | + if (method_exists($reflector->getName(), 'new_instance_from_db')) { |
|
137 | + return 'new_instance_from_db'; |
|
138 | + } |
|
139 | + throw new InstantiationException($reflector->getName()); |
|
140 | + } |
|
141 | + |
|
142 | + |
|
143 | + /** |
|
144 | + * Ensures files for classes that are not PSR-4 compatible are loaded |
|
145 | + * and then verifies that classes exist where applicable |
|
146 | + * |
|
147 | + * @param RecipeInterface $recipe |
|
148 | + * @return bool |
|
149 | + * @throws InvalidClassException |
|
150 | + */ |
|
151 | + protected function resolveClassAndFilepath(RecipeInterface $recipe) |
|
152 | + { |
|
153 | + $paths = $recipe->paths(); |
|
154 | + if (! empty($paths)) { |
|
155 | + foreach ($paths as $path) { |
|
156 | + if (strpos($path, '*') === false && is_readable($path)) { |
|
157 | + require_once($path); |
|
158 | + } |
|
159 | + } |
|
160 | + } |
|
161 | + // re: using "false" for class_exists() second param: |
|
162 | + // if a class name is not already known to PHP, then class_exists() will run through |
|
163 | + // all of the registered spl_autoload functions until it either finds the class, |
|
164 | + // or gets to the end of the registered spl_autoload functions. |
|
165 | + // When the second parameter is true, it will also attempt to load the class file, |
|
166 | + // but it will also trigger an error if the class can not be loaded. |
|
167 | + // We don't want that extra error in the mix, so we have set the second param to "false" |
|
168 | + if ($recipe->type() !== CoffeeMaker::BREW_LOAD_ONLY && ! class_exists($recipe->fqcn(), false)) { |
|
169 | + throw new InvalidClassException($recipe->identifier()); |
|
170 | + } |
|
171 | + return true; |
|
172 | + } |
|
173 | 173 | } |
@@ -68,7 +68,7 @@ discard block |
||
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 | __( |
@@ -151,7 +151,7 @@ discard block |
||
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); |
@@ -21,209 +21,209 @@ |
||
21 | 21 | class DependencyInjector implements InjectorInterface |
22 | 22 | { |
23 | 23 | |
24 | - /** |
|
25 | - * @var CoffeePotInterface $coffee_pot |
|
26 | - */ |
|
27 | - private $coffee_pot; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var EEH_Array $array_helper |
|
31 | - */ |
|
32 | - private $array_helper; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var ReflectionClass[] $reflectors |
|
36 | - */ |
|
37 | - private $reflectors; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var ReflectionMethod[] $constructors |
|
41 | - */ |
|
42 | - private $constructors; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var ReflectionParameter[] $parameters |
|
46 | - */ |
|
47 | - private $parameters; |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * DependencyInjector constructor |
|
52 | - * |
|
53 | - * @param CoffeePotInterface $coffee_pot |
|
54 | - * @param EEH_Array $array_helper |
|
55 | - */ |
|
56 | - public function __construct(CoffeePotInterface $coffee_pot, EEH_Array $array_helper) |
|
57 | - { |
|
58 | - $this->coffee_pot = $coffee_pot; |
|
59 | - $this->array_helper = $array_helper; |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * getReflectionClass |
|
65 | - * checks if a ReflectionClass object has already been generated for a class |
|
66 | - * and returns that instead of creating a new one |
|
67 | - * |
|
68 | - * @param string $class_name |
|
69 | - * @return ReflectionClass |
|
70 | - */ |
|
71 | - public function getReflectionClass($class_name) |
|
72 | - { |
|
73 | - if (! isset($this->reflectors[ $class_name ]) |
|
74 | - || ! $this->reflectors[ $class_name ] instanceof ReflectionClass |
|
75 | - ) { |
|
76 | - $this->reflectors[ $class_name ] = new ReflectionClass($class_name); |
|
77 | - } |
|
78 | - return $this->reflectors[ $class_name ]; |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * getConstructor |
|
84 | - * checks if a ReflectionMethod object has already been generated for the class constructor |
|
85 | - * and returns that instead of creating a new one |
|
86 | - * |
|
87 | - * @param ReflectionClass $reflector |
|
88 | - * @return ReflectionMethod |
|
89 | - */ |
|
90 | - protected function getConstructor(ReflectionClass $reflector) |
|
91 | - { |
|
92 | - if (! isset($this->constructors[ $reflector->getName() ]) |
|
93 | - || ! $this->constructors[ $reflector->getName() ] instanceof ReflectionMethod |
|
94 | - ) { |
|
95 | - $this->constructors[ $reflector->getName() ] = $reflector->getConstructor(); |
|
96 | - } |
|
97 | - return $this->constructors[ $reflector->getName() ]; |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * getParameters |
|
103 | - * checks if an array of ReflectionParameter objects has already been generated for the class constructor |
|
104 | - * and returns that instead of creating a new one |
|
105 | - * |
|
106 | - * @param ReflectionMethod $constructor |
|
107 | - * @return ReflectionParameter[] |
|
108 | - */ |
|
109 | - protected function getParameters(ReflectionMethod $constructor) |
|
110 | - { |
|
111 | - if (! isset($this->parameters[ $constructor->class ])) { |
|
112 | - $this->parameters[ $constructor->class ] = $constructor->getParameters(); |
|
113 | - } |
|
114 | - return $this->parameters[ $constructor->class ]; |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * resolveDependencies |
|
120 | - * examines the constructor for the requested class to determine |
|
121 | - * if any dependencies exist, and if they can be injected. |
|
122 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
123 | - * PLZ NOTE: this is achieved by type hinting the constructor params |
|
124 | - * For example: |
|
125 | - * if attempting to load a class "Foo" with the following constructor: |
|
126 | - * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
127 | - * then $bar_class and $grohl_class will be added to the $arguments array, |
|
128 | - * but only IF they are NOT already present in the incoming arguments array, |
|
129 | - * and the correct classes can be loaded |
|
130 | - * |
|
131 | - * @param RecipeInterface $recipe |
|
132 | - * @param ReflectionClass $reflector |
|
133 | - * @param array $arguments |
|
134 | - * @return array |
|
135 | - * @throws UnexpectedValueException |
|
136 | - */ |
|
137 | - public function resolveDependencies(RecipeInterface $recipe, ReflectionClass $reflector, $arguments = array()) |
|
138 | - { |
|
139 | - // if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
140 | - // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
141 | - $arguments = $this->array_helper->is_array_numerically_and_sequentially_indexed($arguments) |
|
142 | - ? $arguments |
|
143 | - : array($arguments); |
|
144 | - $resolved_parameters = array(); |
|
145 | - // let's examine the constructor |
|
146 | - // let's examine the constructor |
|
147 | - $constructor = $this->getConstructor($reflector); |
|
148 | - // whu? huh? nothing? |
|
149 | - if (! $constructor) { |
|
150 | - return $arguments; |
|
151 | - } |
|
152 | - // get constructor parameters |
|
153 | - $params = $this->getParameters($constructor); |
|
154 | - if (empty($params)) { |
|
155 | - return $resolved_parameters; |
|
156 | - } |
|
157 | - $ingredients = $recipe->ingredients(); |
|
158 | - // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
159 | - $argument_keys = array_keys($arguments); |
|
160 | - // now loop thru all of the constructors expected parameters |
|
161 | - foreach ($params as $index => $param) { |
|
162 | - if (! $param instanceof ReflectionParameter) { |
|
163 | - continue; |
|
164 | - } |
|
165 | - // is this a dependency for a specific class ? |
|
166 | - $param_class = $param->getClass() ? $param->getClass()->name : ''; |
|
167 | - $param_name = $param->getName() ? $param->getName() : ''; |
|
168 | - if (// param is not a class but is specified in the list of ingredients for this Recipe |
|
169 | - is_string($param_name) && isset($ingredients[ $param_name ]) |
|
170 | - ) { |
|
171 | - // attempt to inject the dependency |
|
172 | - $resolved_parameters[ $index ] = $ingredients[ $param_name ]; |
|
173 | - } elseif (// param is specified in the list of ingredients for this Recipe |
|
174 | - isset($ingredients[ $param_class ]) |
|
175 | - ) { // attempt to inject the dependency |
|
176 | - $resolved_parameters[ $index ] = $this->injectDependency($reflector, $ingredients[ $param_class ]); |
|
177 | - } elseif (// param is not even a class |
|
178 | - empty($param_class) |
|
179 | - // and something already exists in the incoming arguments for this param |
|
180 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
181 | - ) { |
|
182 | - // add parameter from incoming arguments |
|
183 | - $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
184 | - } elseif (// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
185 | - ! empty($param_class) |
|
186 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
187 | - && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
188 | - ) { |
|
189 | - // add parameter from incoming arguments |
|
190 | - $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
191 | - } elseif (// parameter is type hinted as a class, and should be injected |
|
192 | - ! empty($param_class) |
|
193 | - ) { |
|
194 | - // attempt to inject the dependency |
|
195 | - $resolved_parameters[ $index ] = $this->injectDependency($reflector, $param_class); |
|
196 | - } elseif ($param->isOptional()) { |
|
197 | - $resolved_parameters[ $index ] = $param->getDefaultValue(); |
|
198 | - } else { |
|
199 | - $resolved_parameters[ $index ] = null; |
|
200 | - } |
|
201 | - } |
|
202 | - return $resolved_parameters; |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * @param ReflectionClass $reflector |
|
208 | - * @param string $param_class |
|
209 | - * @return mixed |
|
210 | - * @throws UnexpectedValueException |
|
211 | - */ |
|
212 | - private function injectDependency(ReflectionClass $reflector, $param_class) |
|
213 | - { |
|
214 | - $dependency = $this->coffee_pot->brew($param_class); |
|
215 | - if (! $dependency instanceof $param_class) { |
|
216 | - throw new UnexpectedValueException( |
|
217 | - sprintf( |
|
218 | - esc_html__( |
|
219 | - 'Could not resolve dependency for "%1$s" for the "%2$s" class constructor.', |
|
220 | - 'event_espresso' |
|
221 | - ), |
|
222 | - $param_class, |
|
223 | - $reflector->getName() |
|
224 | - ) |
|
225 | - ); |
|
226 | - } |
|
227 | - return $dependency; |
|
228 | - } |
|
24 | + /** |
|
25 | + * @var CoffeePotInterface $coffee_pot |
|
26 | + */ |
|
27 | + private $coffee_pot; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var EEH_Array $array_helper |
|
31 | + */ |
|
32 | + private $array_helper; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var ReflectionClass[] $reflectors |
|
36 | + */ |
|
37 | + private $reflectors; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var ReflectionMethod[] $constructors |
|
41 | + */ |
|
42 | + private $constructors; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var ReflectionParameter[] $parameters |
|
46 | + */ |
|
47 | + private $parameters; |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * DependencyInjector constructor |
|
52 | + * |
|
53 | + * @param CoffeePotInterface $coffee_pot |
|
54 | + * @param EEH_Array $array_helper |
|
55 | + */ |
|
56 | + public function __construct(CoffeePotInterface $coffee_pot, EEH_Array $array_helper) |
|
57 | + { |
|
58 | + $this->coffee_pot = $coffee_pot; |
|
59 | + $this->array_helper = $array_helper; |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * getReflectionClass |
|
65 | + * checks if a ReflectionClass object has already been generated for a class |
|
66 | + * and returns that instead of creating a new one |
|
67 | + * |
|
68 | + * @param string $class_name |
|
69 | + * @return ReflectionClass |
|
70 | + */ |
|
71 | + public function getReflectionClass($class_name) |
|
72 | + { |
|
73 | + if (! isset($this->reflectors[ $class_name ]) |
|
74 | + || ! $this->reflectors[ $class_name ] instanceof ReflectionClass |
|
75 | + ) { |
|
76 | + $this->reflectors[ $class_name ] = new ReflectionClass($class_name); |
|
77 | + } |
|
78 | + return $this->reflectors[ $class_name ]; |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * getConstructor |
|
84 | + * checks if a ReflectionMethod object has already been generated for the class constructor |
|
85 | + * and returns that instead of creating a new one |
|
86 | + * |
|
87 | + * @param ReflectionClass $reflector |
|
88 | + * @return ReflectionMethod |
|
89 | + */ |
|
90 | + protected function getConstructor(ReflectionClass $reflector) |
|
91 | + { |
|
92 | + if (! isset($this->constructors[ $reflector->getName() ]) |
|
93 | + || ! $this->constructors[ $reflector->getName() ] instanceof ReflectionMethod |
|
94 | + ) { |
|
95 | + $this->constructors[ $reflector->getName() ] = $reflector->getConstructor(); |
|
96 | + } |
|
97 | + return $this->constructors[ $reflector->getName() ]; |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * getParameters |
|
103 | + * checks if an array of ReflectionParameter objects has already been generated for the class constructor |
|
104 | + * and returns that instead of creating a new one |
|
105 | + * |
|
106 | + * @param ReflectionMethod $constructor |
|
107 | + * @return ReflectionParameter[] |
|
108 | + */ |
|
109 | + protected function getParameters(ReflectionMethod $constructor) |
|
110 | + { |
|
111 | + if (! isset($this->parameters[ $constructor->class ])) { |
|
112 | + $this->parameters[ $constructor->class ] = $constructor->getParameters(); |
|
113 | + } |
|
114 | + return $this->parameters[ $constructor->class ]; |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * resolveDependencies |
|
120 | + * examines the constructor for the requested class to determine |
|
121 | + * if any dependencies exist, and if they can be injected. |
|
122 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
123 | + * PLZ NOTE: this is achieved by type hinting the constructor params |
|
124 | + * For example: |
|
125 | + * if attempting to load a class "Foo" with the following constructor: |
|
126 | + * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
127 | + * then $bar_class and $grohl_class will be added to the $arguments array, |
|
128 | + * but only IF they are NOT already present in the incoming arguments array, |
|
129 | + * and the correct classes can be loaded |
|
130 | + * |
|
131 | + * @param RecipeInterface $recipe |
|
132 | + * @param ReflectionClass $reflector |
|
133 | + * @param array $arguments |
|
134 | + * @return array |
|
135 | + * @throws UnexpectedValueException |
|
136 | + */ |
|
137 | + public function resolveDependencies(RecipeInterface $recipe, ReflectionClass $reflector, $arguments = array()) |
|
138 | + { |
|
139 | + // if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
140 | + // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
141 | + $arguments = $this->array_helper->is_array_numerically_and_sequentially_indexed($arguments) |
|
142 | + ? $arguments |
|
143 | + : array($arguments); |
|
144 | + $resolved_parameters = array(); |
|
145 | + // let's examine the constructor |
|
146 | + // let's examine the constructor |
|
147 | + $constructor = $this->getConstructor($reflector); |
|
148 | + // whu? huh? nothing? |
|
149 | + if (! $constructor) { |
|
150 | + return $arguments; |
|
151 | + } |
|
152 | + // get constructor parameters |
|
153 | + $params = $this->getParameters($constructor); |
|
154 | + if (empty($params)) { |
|
155 | + return $resolved_parameters; |
|
156 | + } |
|
157 | + $ingredients = $recipe->ingredients(); |
|
158 | + // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
159 | + $argument_keys = array_keys($arguments); |
|
160 | + // now loop thru all of the constructors expected parameters |
|
161 | + foreach ($params as $index => $param) { |
|
162 | + if (! $param instanceof ReflectionParameter) { |
|
163 | + continue; |
|
164 | + } |
|
165 | + // is this a dependency for a specific class ? |
|
166 | + $param_class = $param->getClass() ? $param->getClass()->name : ''; |
|
167 | + $param_name = $param->getName() ? $param->getName() : ''; |
|
168 | + if (// param is not a class but is specified in the list of ingredients for this Recipe |
|
169 | + is_string($param_name) && isset($ingredients[ $param_name ]) |
|
170 | + ) { |
|
171 | + // attempt to inject the dependency |
|
172 | + $resolved_parameters[ $index ] = $ingredients[ $param_name ]; |
|
173 | + } elseif (// param is specified in the list of ingredients for this Recipe |
|
174 | + isset($ingredients[ $param_class ]) |
|
175 | + ) { // attempt to inject the dependency |
|
176 | + $resolved_parameters[ $index ] = $this->injectDependency($reflector, $ingredients[ $param_class ]); |
|
177 | + } elseif (// param is not even a class |
|
178 | + empty($param_class) |
|
179 | + // and something already exists in the incoming arguments for this param |
|
180 | + && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
181 | + ) { |
|
182 | + // add parameter from incoming arguments |
|
183 | + $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
184 | + } elseif (// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
185 | + ! empty($param_class) |
|
186 | + && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
187 | + && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
188 | + ) { |
|
189 | + // add parameter from incoming arguments |
|
190 | + $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
191 | + } elseif (// parameter is type hinted as a class, and should be injected |
|
192 | + ! empty($param_class) |
|
193 | + ) { |
|
194 | + // attempt to inject the dependency |
|
195 | + $resolved_parameters[ $index ] = $this->injectDependency($reflector, $param_class); |
|
196 | + } elseif ($param->isOptional()) { |
|
197 | + $resolved_parameters[ $index ] = $param->getDefaultValue(); |
|
198 | + } else { |
|
199 | + $resolved_parameters[ $index ] = null; |
|
200 | + } |
|
201 | + } |
|
202 | + return $resolved_parameters; |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * @param ReflectionClass $reflector |
|
208 | + * @param string $param_class |
|
209 | + * @return mixed |
|
210 | + * @throws UnexpectedValueException |
|
211 | + */ |
|
212 | + private function injectDependency(ReflectionClass $reflector, $param_class) |
|
213 | + { |
|
214 | + $dependency = $this->coffee_pot->brew($param_class); |
|
215 | + if (! $dependency instanceof $param_class) { |
|
216 | + throw new UnexpectedValueException( |
|
217 | + sprintf( |
|
218 | + esc_html__( |
|
219 | + 'Could not resolve dependency for "%1$s" for the "%2$s" class constructor.', |
|
220 | + 'event_espresso' |
|
221 | + ), |
|
222 | + $param_class, |
|
223 | + $reflector->getName() |
|
224 | + ) |
|
225 | + ); |
|
226 | + } |
|
227 | + return $dependency; |
|
228 | + } |
|
229 | 229 | } |
@@ -70,12 +70,12 @@ discard block |
||
70 | 70 | */ |
71 | 71 | public function getReflectionClass($class_name) |
72 | 72 | { |
73 | - if (! isset($this->reflectors[ $class_name ]) |
|
74 | - || ! $this->reflectors[ $class_name ] instanceof ReflectionClass |
|
73 | + if ( ! isset($this->reflectors[$class_name]) |
|
74 | + || ! $this->reflectors[$class_name] instanceof ReflectionClass |
|
75 | 75 | ) { |
76 | - $this->reflectors[ $class_name ] = new ReflectionClass($class_name); |
|
76 | + $this->reflectors[$class_name] = new ReflectionClass($class_name); |
|
77 | 77 | } |
78 | - return $this->reflectors[ $class_name ]; |
|
78 | + return $this->reflectors[$class_name]; |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | |
@@ -89,12 +89,12 @@ discard block |
||
89 | 89 | */ |
90 | 90 | protected function getConstructor(ReflectionClass $reflector) |
91 | 91 | { |
92 | - if (! isset($this->constructors[ $reflector->getName() ]) |
|
93 | - || ! $this->constructors[ $reflector->getName() ] instanceof ReflectionMethod |
|
92 | + if ( ! isset($this->constructors[$reflector->getName()]) |
|
93 | + || ! $this->constructors[$reflector->getName()] instanceof ReflectionMethod |
|
94 | 94 | ) { |
95 | - $this->constructors[ $reflector->getName() ] = $reflector->getConstructor(); |
|
95 | + $this->constructors[$reflector->getName()] = $reflector->getConstructor(); |
|
96 | 96 | } |
97 | - return $this->constructors[ $reflector->getName() ]; |
|
97 | + return $this->constructors[$reflector->getName()]; |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | |
@@ -108,10 +108,10 @@ discard block |
||
108 | 108 | */ |
109 | 109 | protected function getParameters(ReflectionMethod $constructor) |
110 | 110 | { |
111 | - if (! isset($this->parameters[ $constructor->class ])) { |
|
112 | - $this->parameters[ $constructor->class ] = $constructor->getParameters(); |
|
111 | + if ( ! isset($this->parameters[$constructor->class])) { |
|
112 | + $this->parameters[$constructor->class] = $constructor->getParameters(); |
|
113 | 113 | } |
114 | - return $this->parameters[ $constructor->class ]; |
|
114 | + return $this->parameters[$constructor->class]; |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | // let's examine the constructor |
147 | 147 | $constructor = $this->getConstructor($reflector); |
148 | 148 | // whu? huh? nothing? |
149 | - if (! $constructor) { |
|
149 | + if ( ! $constructor) { |
|
150 | 150 | return $arguments; |
151 | 151 | } |
152 | 152 | // get constructor parameters |
@@ -159,44 +159,44 @@ discard block |
||
159 | 159 | $argument_keys = array_keys($arguments); |
160 | 160 | // now loop thru all of the constructors expected parameters |
161 | 161 | foreach ($params as $index => $param) { |
162 | - if (! $param instanceof ReflectionParameter) { |
|
162 | + if ( ! $param instanceof ReflectionParameter) { |
|
163 | 163 | continue; |
164 | 164 | } |
165 | 165 | // is this a dependency for a specific class ? |
166 | 166 | $param_class = $param->getClass() ? $param->getClass()->name : ''; |
167 | 167 | $param_name = $param->getName() ? $param->getName() : ''; |
168 | 168 | if (// param is not a class but is specified in the list of ingredients for this Recipe |
169 | - is_string($param_name) && isset($ingredients[ $param_name ]) |
|
169 | + is_string($param_name) && isset($ingredients[$param_name]) |
|
170 | 170 | ) { |
171 | 171 | // attempt to inject the dependency |
172 | - $resolved_parameters[ $index ] = $ingredients[ $param_name ]; |
|
172 | + $resolved_parameters[$index] = $ingredients[$param_name]; |
|
173 | 173 | } elseif (// param is specified in the list of ingredients for this Recipe |
174 | - isset($ingredients[ $param_class ]) |
|
174 | + isset($ingredients[$param_class]) |
|
175 | 175 | ) { // attempt to inject the dependency |
176 | - $resolved_parameters[ $index ] = $this->injectDependency($reflector, $ingredients[ $param_class ]); |
|
176 | + $resolved_parameters[$index] = $this->injectDependency($reflector, $ingredients[$param_class]); |
|
177 | 177 | } elseif (// param is not even a class |
178 | 178 | empty($param_class) |
179 | 179 | // and something already exists in the incoming arguments for this param |
180 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
180 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
181 | 181 | ) { |
182 | 182 | // add parameter from incoming arguments |
183 | - $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
183 | + $resolved_parameters[$index] = $arguments[$argument_keys[$index]]; |
|
184 | 184 | } elseif (// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
185 | 185 | ! empty($param_class) |
186 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
187 | - && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
186 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
187 | + && $arguments[$argument_keys[$index]] instanceof $param_class |
|
188 | 188 | ) { |
189 | 189 | // add parameter from incoming arguments |
190 | - $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
190 | + $resolved_parameters[$index] = $arguments[$argument_keys[$index]]; |
|
191 | 191 | } elseif (// parameter is type hinted as a class, and should be injected |
192 | 192 | ! empty($param_class) |
193 | 193 | ) { |
194 | 194 | // attempt to inject the dependency |
195 | - $resolved_parameters[ $index ] = $this->injectDependency($reflector, $param_class); |
|
195 | + $resolved_parameters[$index] = $this->injectDependency($reflector, $param_class); |
|
196 | 196 | } elseif ($param->isOptional()) { |
197 | - $resolved_parameters[ $index ] = $param->getDefaultValue(); |
|
197 | + $resolved_parameters[$index] = $param->getDefaultValue(); |
|
198 | 198 | } else { |
199 | - $resolved_parameters[ $index ] = null; |
|
199 | + $resolved_parameters[$index] = null; |
|
200 | 200 | } |
201 | 201 | } |
202 | 202 | return $resolved_parameters; |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | private function injectDependency(ReflectionClass $reflector, $param_class) |
213 | 213 | { |
214 | 214 | $dependency = $this->coffee_pot->brew($param_class); |
215 | - if (! $dependency instanceof $param_class) { |
|
215 | + if ( ! $dependency instanceof $param_class) { |
|
216 | 216 | throw new UnexpectedValueException( |
217 | 217 | sprintf( |
218 | 218 | esc_html__( |
@@ -17,130 +17,130 @@ |
||
17 | 17 | class RegistryContainer implements ArrayAccess, CountableTraversableAggregate |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var array $container |
|
22 | - */ |
|
23 | - private $container; |
|
24 | - |
|
25 | - /** |
|
26 | - * RegistryContainer constructor. |
|
27 | - * Container data can be seeded by passing parameters to constructor. |
|
28 | - * Each parameter will become its own element in the container |
|
29 | - */ |
|
30 | - public function __construct() |
|
31 | - { |
|
32 | - $this->container = func_get_args(); |
|
33 | - if (func_num_args() === 0) { |
|
34 | - $this->container = array(); |
|
35 | - } |
|
36 | - } |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * @param mixed $offset |
|
41 | - * @param mixed $value |
|
42 | - */ |
|
43 | - public function offsetSet($offset, $value) |
|
44 | - { |
|
45 | - $this->container[ $offset ] = $value; |
|
46 | - } |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * @param mixed $offset |
|
51 | - * @return bool |
|
52 | - */ |
|
53 | - public function offsetExists($offset) |
|
54 | - { |
|
55 | - return isset($this->container[ $offset ]); |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * @param mixed $offset |
|
61 | - */ |
|
62 | - public function offsetUnset($offset) |
|
63 | - { |
|
64 | - unset($this->container[ $offset ]); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * @param mixed $offset |
|
70 | - * @return mixed|null |
|
71 | - */ |
|
72 | - public function offsetGet($offset) |
|
73 | - { |
|
74 | - return isset($this->container[ $offset ]) ? $this->container[ $offset ] : null; |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * @return int |
|
80 | - */ |
|
81 | - public function count() |
|
82 | - { |
|
83 | - return count($this->container); |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * @return ArrayIterator |
|
89 | - */ |
|
90 | - public function getIterator() |
|
91 | - { |
|
92 | - return new ArrayIterator($this->container); |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * @param $offset |
|
98 | - * @param $value |
|
99 | - */ |
|
100 | - public function __set($offset, $value) |
|
101 | - { |
|
102 | - $this->container[ $offset ] = $value; |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * @param $offset |
|
108 | - * @return mixed |
|
109 | - * @throws OutOfBoundsException |
|
110 | - */ |
|
111 | - public function __get($offset) |
|
112 | - { |
|
113 | - if (array_key_exists($offset, $this->container)) { |
|
114 | - return $this->container[ $offset ]; |
|
115 | - } |
|
116 | - $trace = debug_backtrace(); |
|
117 | - throw new OutOfBoundsException( |
|
118 | - sprintf( |
|
119 | - esc_html__('Invalid offset: %1$s %2$sCalled from %3$s on line %4$d', 'event_espresso'), |
|
120 | - $offset, |
|
121 | - '<br />', |
|
122 | - $trace[0]['file'], |
|
123 | - $trace[0]['line'] |
|
124 | - ) |
|
125 | - ); |
|
126 | - } |
|
127 | - |
|
128 | - |
|
129 | - /** |
|
130 | - * @param $offset |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - public function __isset($offset) |
|
134 | - { |
|
135 | - return isset($this->container[ $offset ]); |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * @param $offset |
|
141 | - */ |
|
142 | - public function __unset($offset) |
|
143 | - { |
|
144 | - unset($this->container[ $offset ]); |
|
145 | - } |
|
20 | + /** |
|
21 | + * @var array $container |
|
22 | + */ |
|
23 | + private $container; |
|
24 | + |
|
25 | + /** |
|
26 | + * RegistryContainer constructor. |
|
27 | + * Container data can be seeded by passing parameters to constructor. |
|
28 | + * Each parameter will become its own element in the container |
|
29 | + */ |
|
30 | + public function __construct() |
|
31 | + { |
|
32 | + $this->container = func_get_args(); |
|
33 | + if (func_num_args() === 0) { |
|
34 | + $this->container = array(); |
|
35 | + } |
|
36 | + } |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * @param mixed $offset |
|
41 | + * @param mixed $value |
|
42 | + */ |
|
43 | + public function offsetSet($offset, $value) |
|
44 | + { |
|
45 | + $this->container[ $offset ] = $value; |
|
46 | + } |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * @param mixed $offset |
|
51 | + * @return bool |
|
52 | + */ |
|
53 | + public function offsetExists($offset) |
|
54 | + { |
|
55 | + return isset($this->container[ $offset ]); |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * @param mixed $offset |
|
61 | + */ |
|
62 | + public function offsetUnset($offset) |
|
63 | + { |
|
64 | + unset($this->container[ $offset ]); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * @param mixed $offset |
|
70 | + * @return mixed|null |
|
71 | + */ |
|
72 | + public function offsetGet($offset) |
|
73 | + { |
|
74 | + return isset($this->container[ $offset ]) ? $this->container[ $offset ] : null; |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * @return int |
|
80 | + */ |
|
81 | + public function count() |
|
82 | + { |
|
83 | + return count($this->container); |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * @return ArrayIterator |
|
89 | + */ |
|
90 | + public function getIterator() |
|
91 | + { |
|
92 | + return new ArrayIterator($this->container); |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * @param $offset |
|
98 | + * @param $value |
|
99 | + */ |
|
100 | + public function __set($offset, $value) |
|
101 | + { |
|
102 | + $this->container[ $offset ] = $value; |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * @param $offset |
|
108 | + * @return mixed |
|
109 | + * @throws OutOfBoundsException |
|
110 | + */ |
|
111 | + public function __get($offset) |
|
112 | + { |
|
113 | + if (array_key_exists($offset, $this->container)) { |
|
114 | + return $this->container[ $offset ]; |
|
115 | + } |
|
116 | + $trace = debug_backtrace(); |
|
117 | + throw new OutOfBoundsException( |
|
118 | + sprintf( |
|
119 | + esc_html__('Invalid offset: %1$s %2$sCalled from %3$s on line %4$d', 'event_espresso'), |
|
120 | + $offset, |
|
121 | + '<br />', |
|
122 | + $trace[0]['file'], |
|
123 | + $trace[0]['line'] |
|
124 | + ) |
|
125 | + ); |
|
126 | + } |
|
127 | + |
|
128 | + |
|
129 | + /** |
|
130 | + * @param $offset |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + public function __isset($offset) |
|
134 | + { |
|
135 | + return isset($this->container[ $offset ]); |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * @param $offset |
|
141 | + */ |
|
142 | + public function __unset($offset) |
|
143 | + { |
|
144 | + unset($this->container[ $offset ]); |
|
145 | + } |
|
146 | 146 | } |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public function offsetSet($offset, $value) |
44 | 44 | { |
45 | - $this->container[ $offset ] = $value; |
|
45 | + $this->container[$offset] = $value; |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | */ |
53 | 53 | public function offsetExists($offset) |
54 | 54 | { |
55 | - return isset($this->container[ $offset ]); |
|
55 | + return isset($this->container[$offset]); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | */ |
62 | 62 | public function offsetUnset($offset) |
63 | 63 | { |
64 | - unset($this->container[ $offset ]); |
|
64 | + unset($this->container[$offset]); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | */ |
72 | 72 | public function offsetGet($offset) |
73 | 73 | { |
74 | - return isset($this->container[ $offset ]) ? $this->container[ $offset ] : null; |
|
74 | + return isset($this->container[$offset]) ? $this->container[$offset] : null; |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public function __set($offset, $value) |
101 | 101 | { |
102 | - $this->container[ $offset ] = $value; |
|
102 | + $this->container[$offset] = $value; |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | public function __get($offset) |
112 | 112 | { |
113 | 113 | if (array_key_exists($offset, $this->container)) { |
114 | - return $this->container[ $offset ]; |
|
114 | + return $this->container[$offset]; |
|
115 | 115 | } |
116 | 116 | $trace = debug_backtrace(); |
117 | 117 | throw new OutOfBoundsException( |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | */ |
133 | 133 | public function __isset($offset) |
134 | 134 | { |
135 | - return isset($this->container[ $offset ]); |
|
135 | + return isset($this->container[$offset]); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | |
@@ -141,6 +141,6 @@ discard block |
||
141 | 141 | */ |
142 | 142 | public function __unset($offset) |
143 | 143 | { |
144 | - unset($this->container[ $offset ]); |
|
144 | + unset($this->container[$offset]); |
|
145 | 145 | } |
146 | 146 | } |
@@ -19,310 +19,310 @@ |
||
19 | 19 | class Recipe implements RecipeInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * A default Recipe to use if none is specified for a class |
|
24 | - */ |
|
25 | - const DEFAULT_ID = '*'; |
|
26 | - |
|
27 | - /** |
|
28 | - * Identifier for the entity class to be constructed. |
|
29 | - * Typically a Fully Qualified Class Name |
|
30 | - * |
|
31 | - * @var string $identifier |
|
32 | - */ |
|
33 | - private $identifier; |
|
34 | - |
|
35 | - /** |
|
36 | - * Fully Qualified Class Name |
|
37 | - * |
|
38 | - * @var string $fqcn |
|
39 | - */ |
|
40 | - private $fqcn; |
|
41 | - |
|
42 | - /** |
|
43 | - * a dependency class map array |
|
44 | - * If a Recipe is for a single class (or group of classes that shares the EXACT SAME constructor arguments), |
|
45 | - * and that class type hints for an interface, then this property allows you to configure what dependencies |
|
46 | - * get used when instantiating the class. |
|
47 | - * For example: |
|
48 | - * There's a class called Coffee, and one of its constructor arguments is BeanInterface |
|
49 | - * There are two implementations of BeanInterface: HonduranBean, and KenyanBean |
|
50 | - * We want one Coffee object to use HonduranBean for its BeanInterface, |
|
51 | - * and the 2nd Coffee object to use KenyanBean for its BeanInterface. |
|
52 | - * To do this, we need to create two Recipes: |
|
53 | - * one with an identifier of 'HonduranCoffee' using the following ingredients : |
|
54 | - * array('BeanInterface' => 'HonduranBean') |
|
55 | - * and the other with an identifier of 'KenyanCoffee' using the following ingredients : |
|
56 | - * array('BeanInterface' => 'KenyanBean') |
|
57 | - * Then, whenever the CoffeeShop brews an instance of HonduranCoffee, |
|
58 | - * an instance of HonduranBean will get injected for the BeanInterface dependency, |
|
59 | - * and whenever the CoffeeShop brews an instance of KenyanCoffee, |
|
60 | - * an instance of KenyanBean will get injected for the BeanInterface dependency |
|
61 | - * |
|
62 | - * @var array $ingredients |
|
63 | - */ |
|
64 | - private $ingredients = array(); |
|
65 | - |
|
66 | - /** |
|
67 | - * one of the class constants from CoffeeShop: |
|
68 | - * CoffeeMaker::BREW_NEW - creates a new instance |
|
69 | - * CoffeeMaker::BREW_SHARED - creates a shared instance |
|
70 | - * CoffeeMaker::BREW_LOAD_ONLY - loads but does not instantiate |
|
71 | - * |
|
72 | - * @var string $type |
|
73 | - */ |
|
74 | - private $type; |
|
75 | - |
|
76 | - /** |
|
77 | - * class name aliases - typically a Fully Qualified Interface that the class implements |
|
78 | - * identifiers passed to the CoffeeShop will be run through the filters to find the correct class name |
|
79 | - * |
|
80 | - * @var array $filters |
|
81 | - */ |
|
82 | - private $filters = array(); |
|
83 | - |
|
84 | - /** |
|
85 | - * array of full server filepaths to files that may contain the class |
|
86 | - * |
|
87 | - * @var array $paths |
|
88 | - */ |
|
89 | - private $paths = array(); |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * Recipe constructor. |
|
94 | - * |
|
95 | - * @param string $identifier class identifier, can be an alias, or FQCN, or whatever |
|
96 | - * @param string $fqcn \Fully\Qualified\ClassName, optional if $identifier is FQCN |
|
97 | - * @param array $ingredients array of dependencies that can not be resolved automatically, |
|
98 | - * used for resolving concrete classes for type hinted interfaces |
|
99 | - * for the dependencies of THIS class |
|
100 | - * @param string $type recipe type: one of the class constants on |
|
101 | - * \EventEspresso\core\services\container\CoffeeMaker |
|
102 | - * @param array $filters array of class aliases, or class interfaces |
|
103 | - * this works somewhat opposite to the $ingredients array above, |
|
104 | - * in that this array specifies interfaces or aliases |
|
105 | - * that this Recipe can be used for when resolving OTHER class's dependencies |
|
106 | - * @param array $paths if class can not be loaded via PSR-4 autoloading, |
|
107 | - * then supply a filepath, or array of filepaths, so that it can be included |
|
108 | - * @throws InvalidIdentifierException |
|
109 | - * @throws RuntimeException |
|
110 | - * @throws InvalidInterfaceException |
|
111 | - * @throws InvalidClassException |
|
112 | - * @throws InvalidDataTypeException |
|
113 | - */ |
|
114 | - public function __construct( |
|
115 | - $identifier, |
|
116 | - $fqcn = '', |
|
117 | - array $filters = array(), |
|
118 | - array $ingredients = array(), |
|
119 | - $type = CoffeeMaker::BREW_NEW, |
|
120 | - array $paths = array() |
|
121 | - ) { |
|
122 | - $this->setIdentifier($identifier); |
|
123 | - $this->setFilters($filters); |
|
124 | - $this->setIngredients($ingredients); |
|
125 | - $this->setType($type); |
|
126 | - $this->setPaths($paths); |
|
127 | - $this->setFqcn($fqcn); |
|
128 | - } |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - public function identifier() |
|
135 | - { |
|
136 | - return $this->identifier; |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - /** |
|
141 | - * @return string |
|
142 | - */ |
|
143 | - public function fqcn() |
|
144 | - { |
|
145 | - return $this->fqcn; |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * @return array |
|
151 | - */ |
|
152 | - public function filters() |
|
153 | - { |
|
154 | - return $this->filters; |
|
155 | - } |
|
156 | - |
|
157 | - |
|
158 | - /** |
|
159 | - * @return array |
|
160 | - */ |
|
161 | - public function ingredients() |
|
162 | - { |
|
163 | - return $this->ingredients; |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * @return string |
|
169 | - */ |
|
170 | - public function type() |
|
171 | - { |
|
172 | - return $this->type; |
|
173 | - } |
|
174 | - |
|
175 | - |
|
176 | - /** |
|
177 | - * @return array |
|
178 | - */ |
|
179 | - public function paths() |
|
180 | - { |
|
181 | - return $this->paths; |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * @param string $identifier Identifier for the entity class that the Recipe applies to |
|
187 | - * Typically a Fully Qualified Class Name |
|
188 | - * @throws InvalidIdentifierException |
|
189 | - */ |
|
190 | - public function setIdentifier($identifier) |
|
191 | - { |
|
192 | - if (! is_string($identifier) || empty($identifier)) { |
|
193 | - throw new InvalidIdentifierException( |
|
194 | - is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
195 | - __('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso') |
|
196 | - ); |
|
197 | - } |
|
198 | - $this->identifier = $identifier; |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - /** |
|
203 | - * Ensures incoming string is a valid Fully Qualified Class Name, |
|
204 | - * except if this is the default wildcard Recipe ( * ), |
|
205 | - * or it's NOT an actual FQCN because the Recipe is using filepaths |
|
206 | - * for classes that are not PSR-4 compatible |
|
207 | - * PLZ NOTE: |
|
208 | - * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
209 | - * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
210 | - * |
|
211 | - * @param string $fqcn |
|
212 | - * @throws InvalidDataTypeException |
|
213 | - * @throws InvalidClassException |
|
214 | - * @throws InvalidInterfaceException |
|
215 | - */ |
|
216 | - public function setFqcn($fqcn) |
|
217 | - { |
|
218 | - $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier; |
|
219 | - if (! is_string($fqcn)) { |
|
220 | - throw new InvalidDataTypeException( |
|
221 | - '$fqcn', |
|
222 | - is_object($fqcn) ? get_class($fqcn) : gettype($fqcn), |
|
223 | - __('string (Fully\Qualified\ClassName)', 'event_espresso') |
|
224 | - ); |
|
225 | - } |
|
226 | - $fqcn = ltrim($fqcn, '\\'); |
|
227 | - if ($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 | - __('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 | - __('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 | - __('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 | - __('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 | - } |
|
22 | + /** |
|
23 | + * A default Recipe to use if none is specified for a class |
|
24 | + */ |
|
25 | + const DEFAULT_ID = '*'; |
|
26 | + |
|
27 | + /** |
|
28 | + * Identifier for the entity class to be constructed. |
|
29 | + * Typically a Fully Qualified Class Name |
|
30 | + * |
|
31 | + * @var string $identifier |
|
32 | + */ |
|
33 | + private $identifier; |
|
34 | + |
|
35 | + /** |
|
36 | + * Fully Qualified Class Name |
|
37 | + * |
|
38 | + * @var string $fqcn |
|
39 | + */ |
|
40 | + private $fqcn; |
|
41 | + |
|
42 | + /** |
|
43 | + * a dependency class map array |
|
44 | + * If a Recipe is for a single class (or group of classes that shares the EXACT SAME constructor arguments), |
|
45 | + * and that class type hints for an interface, then this property allows you to configure what dependencies |
|
46 | + * get used when instantiating the class. |
|
47 | + * For example: |
|
48 | + * There's a class called Coffee, and one of its constructor arguments is BeanInterface |
|
49 | + * There are two implementations of BeanInterface: HonduranBean, and KenyanBean |
|
50 | + * We want one Coffee object to use HonduranBean for its BeanInterface, |
|
51 | + * and the 2nd Coffee object to use KenyanBean for its BeanInterface. |
|
52 | + * To do this, we need to create two Recipes: |
|
53 | + * one with an identifier of 'HonduranCoffee' using the following ingredients : |
|
54 | + * array('BeanInterface' => 'HonduranBean') |
|
55 | + * and the other with an identifier of 'KenyanCoffee' using the following ingredients : |
|
56 | + * array('BeanInterface' => 'KenyanBean') |
|
57 | + * Then, whenever the CoffeeShop brews an instance of HonduranCoffee, |
|
58 | + * an instance of HonduranBean will get injected for the BeanInterface dependency, |
|
59 | + * and whenever the CoffeeShop brews an instance of KenyanCoffee, |
|
60 | + * an instance of KenyanBean will get injected for the BeanInterface dependency |
|
61 | + * |
|
62 | + * @var array $ingredients |
|
63 | + */ |
|
64 | + private $ingredients = array(); |
|
65 | + |
|
66 | + /** |
|
67 | + * one of the class constants from CoffeeShop: |
|
68 | + * CoffeeMaker::BREW_NEW - creates a new instance |
|
69 | + * CoffeeMaker::BREW_SHARED - creates a shared instance |
|
70 | + * CoffeeMaker::BREW_LOAD_ONLY - loads but does not instantiate |
|
71 | + * |
|
72 | + * @var string $type |
|
73 | + */ |
|
74 | + private $type; |
|
75 | + |
|
76 | + /** |
|
77 | + * class name aliases - typically a Fully Qualified Interface that the class implements |
|
78 | + * identifiers passed to the CoffeeShop will be run through the filters to find the correct class name |
|
79 | + * |
|
80 | + * @var array $filters |
|
81 | + */ |
|
82 | + private $filters = array(); |
|
83 | + |
|
84 | + /** |
|
85 | + * array of full server filepaths to files that may contain the class |
|
86 | + * |
|
87 | + * @var array $paths |
|
88 | + */ |
|
89 | + private $paths = array(); |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * Recipe constructor. |
|
94 | + * |
|
95 | + * @param string $identifier class identifier, can be an alias, or FQCN, or whatever |
|
96 | + * @param string $fqcn \Fully\Qualified\ClassName, optional if $identifier is FQCN |
|
97 | + * @param array $ingredients array of dependencies that can not be resolved automatically, |
|
98 | + * used for resolving concrete classes for type hinted interfaces |
|
99 | + * for the dependencies of THIS class |
|
100 | + * @param string $type recipe type: one of the class constants on |
|
101 | + * \EventEspresso\core\services\container\CoffeeMaker |
|
102 | + * @param array $filters array of class aliases, or class interfaces |
|
103 | + * this works somewhat opposite to the $ingredients array above, |
|
104 | + * in that this array specifies interfaces or aliases |
|
105 | + * that this Recipe can be used for when resolving OTHER class's dependencies |
|
106 | + * @param array $paths if class can not be loaded via PSR-4 autoloading, |
|
107 | + * then supply a filepath, or array of filepaths, so that it can be included |
|
108 | + * @throws InvalidIdentifierException |
|
109 | + * @throws RuntimeException |
|
110 | + * @throws InvalidInterfaceException |
|
111 | + * @throws InvalidClassException |
|
112 | + * @throws InvalidDataTypeException |
|
113 | + */ |
|
114 | + public function __construct( |
|
115 | + $identifier, |
|
116 | + $fqcn = '', |
|
117 | + array $filters = array(), |
|
118 | + array $ingredients = array(), |
|
119 | + $type = CoffeeMaker::BREW_NEW, |
|
120 | + array $paths = array() |
|
121 | + ) { |
|
122 | + $this->setIdentifier($identifier); |
|
123 | + $this->setFilters($filters); |
|
124 | + $this->setIngredients($ingredients); |
|
125 | + $this->setType($type); |
|
126 | + $this->setPaths($paths); |
|
127 | + $this->setFqcn($fqcn); |
|
128 | + } |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + public function identifier() |
|
135 | + { |
|
136 | + return $this->identifier; |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + /** |
|
141 | + * @return string |
|
142 | + */ |
|
143 | + public function fqcn() |
|
144 | + { |
|
145 | + return $this->fqcn; |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * @return array |
|
151 | + */ |
|
152 | + public function filters() |
|
153 | + { |
|
154 | + return $this->filters; |
|
155 | + } |
|
156 | + |
|
157 | + |
|
158 | + /** |
|
159 | + * @return array |
|
160 | + */ |
|
161 | + public function ingredients() |
|
162 | + { |
|
163 | + return $this->ingredients; |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * @return string |
|
169 | + */ |
|
170 | + public function type() |
|
171 | + { |
|
172 | + return $this->type; |
|
173 | + } |
|
174 | + |
|
175 | + |
|
176 | + /** |
|
177 | + * @return array |
|
178 | + */ |
|
179 | + public function paths() |
|
180 | + { |
|
181 | + return $this->paths; |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * @param string $identifier Identifier for the entity class that the Recipe applies to |
|
187 | + * Typically a Fully Qualified Class Name |
|
188 | + * @throws InvalidIdentifierException |
|
189 | + */ |
|
190 | + public function setIdentifier($identifier) |
|
191 | + { |
|
192 | + if (! is_string($identifier) || empty($identifier)) { |
|
193 | + throw new InvalidIdentifierException( |
|
194 | + is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
195 | + __('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso') |
|
196 | + ); |
|
197 | + } |
|
198 | + $this->identifier = $identifier; |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + /** |
|
203 | + * Ensures incoming string is a valid Fully Qualified Class Name, |
|
204 | + * except if this is the default wildcard Recipe ( * ), |
|
205 | + * or it's NOT an actual FQCN because the Recipe is using filepaths |
|
206 | + * for classes that are not PSR-4 compatible |
|
207 | + * PLZ NOTE: |
|
208 | + * Recipe::setFqcn() has a check to see if Recipe::$paths is empty or not, |
|
209 | + * therefore you should always call Recipe::setPaths() before Recipe::setFqcn() |
|
210 | + * |
|
211 | + * @param string $fqcn |
|
212 | + * @throws InvalidDataTypeException |
|
213 | + * @throws InvalidClassException |
|
214 | + * @throws InvalidInterfaceException |
|
215 | + */ |
|
216 | + public function setFqcn($fqcn) |
|
217 | + { |
|
218 | + $fqcn = ! empty($fqcn) ? $fqcn : $this->identifier; |
|
219 | + if (! is_string($fqcn)) { |
|
220 | + throw new InvalidDataTypeException( |
|
221 | + '$fqcn', |
|
222 | + is_object($fqcn) ? get_class($fqcn) : gettype($fqcn), |
|
223 | + __('string (Fully\Qualified\ClassName)', 'event_espresso') |
|
224 | + ); |
|
225 | + } |
|
226 | + $fqcn = ltrim($fqcn, '\\'); |
|
227 | + if ($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 | + __('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 | + __('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 | + __('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 | + __('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 | } |
@@ -189,7 +189,7 @@ discard block |
||
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 | __('class identifier (typically a \Fully\Qualified\ClassName)', 'event_espresso') |
@@ -216,7 +216,7 @@ discard block |
||
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), |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | if (empty($ingredients)) { |
247 | 247 | return; |
248 | 248 | } |
249 | - if (! is_array($ingredients)) { |
|
249 | + if ( ! is_array($ingredients)) { |
|
250 | 250 | throw new InvalidDataTypeException( |
251 | 251 | '$ingredients', |
252 | 252 | is_object($ingredients) ? get_class($ingredients) : gettype($ingredients), |
@@ -278,7 +278,7 @@ discard block |
||
278 | 278 | if (empty($filters)) { |
279 | 279 | return; |
280 | 280 | } |
281 | - if (! is_array($filters)) { |
|
281 | + if ( ! is_array($filters)) { |
|
282 | 282 | throw new InvalidDataTypeException( |
283 | 283 | '$filters', |
284 | 284 | is_object($filters) ? get_class($filters) : gettype($filters), |
@@ -305,7 +305,7 @@ discard block |
||
305 | 305 | if (empty($paths)) { |
306 | 306 | return; |
307 | 307 | } |
308 | - if (! (is_string($paths) || is_array($paths))) { |
|
308 | + if ( ! (is_string($paths) || is_array($paths))) { |
|
309 | 309 | throw new InvalidDataTypeException( |
310 | 310 | '$path', |
311 | 311 | is_object($paths) ? get_class($paths) : gettype($paths), |