@@ -40,290 +40,290 @@ |
||
40 | 40 | */ |
41 | 41 | class BaseMailer { |
42 | 42 | |
43 | - /** |
|
44 | - * How the email is delivered. |
|
45 | - * Either 'send' or 'queue'. |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $action = 'send'; |
|
49 | - |
|
50 | - protected $from = null; |
|
51 | - protected $to = null; |
|
52 | - protected $reply_to = null; |
|
53 | - protected $cc = null; |
|
54 | - protected $bcc = null; |
|
55 | - |
|
56 | - protected $message = null; |
|
57 | - |
|
58 | - protected $theme = 'email'; |
|
59 | - protected $layout = 'index'; |
|
60 | - protected $view = null; |
|
61 | - |
|
62 | - /** |
|
63 | - * The MailService to use. If NULL |
|
64 | - * will use the system default. |
|
65 | - * @var null |
|
66 | - */ |
|
67 | - protected $service_name = null; |
|
68 | - |
|
69 | - protected $service = null; |
|
70 | - |
|
71 | - /** |
|
72 | - * Used for theming the email messages. |
|
73 | - * @var null |
|
74 | - */ |
|
75 | - protected $themer = null; |
|
76 | - |
|
77 | - //-------------------------------------------------------------------- |
|
78 | - |
|
79 | - /** |
|
80 | - * Constructor |
|
81 | - * |
|
82 | - * Simply allows us to override the default settings for this mailer. |
|
83 | - * |
|
84 | - * @param null $options |
|
85 | - */ |
|
86 | - public function __construct($options=null) |
|
87 | - { |
|
88 | - if (! empty($options)) |
|
89 | - { |
|
90 | - $this->setOptions($options); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - //-------------------------------------------------------------------- |
|
95 | - |
|
96 | - /** |
|
97 | - * Sets the basic options available to the mailer, like 'from', 'to', |
|
98 | - * 'cc', 'bcc', etc. |
|
99 | - * |
|
100 | - * @param $options |
|
101 | - */ |
|
102 | - public function setOptions($options) |
|
103 | - { |
|
104 | - if (is_array($options)) |
|
105 | - { |
|
106 | - foreach ($options as $key => $value) |
|
107 | - { |
|
108 | - if ($key == 'service') |
|
109 | - { |
|
110 | - $this->service =& $value; |
|
111 | - continue; |
|
112 | - } |
|
113 | - |
|
114 | - if (property_exists($this, $key)) |
|
115 | - { |
|
116 | - $this->$key = $value; |
|
117 | - } |
|
118 | - } |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - //-------------------------------------------------------------------- |
|
123 | - |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * Sends an email immediately using the system-defined MailService. |
|
128 | - * |
|
129 | - * @param string $to // Who the email is being sent to. |
|
130 | - * @param string $subject // The subject line for the email |
|
131 | - * @param strign $data // the key/value pairs to send to the views. |
|
132 | - * @param string $view // You can override the view used for the email here. |
|
133 | - * // You can change themes by prepending theme name |
|
134 | - * // like: 'newtheme:newview' |
|
135 | - * |
|
136 | - * @return bool |
|
137 | - */ |
|
138 | - public function send($to, $subject, $data=[], $view=null) |
|
139 | - { |
|
140 | - // Are we pretending to send? |
|
141 | - if (config_item('mail.pretend') === true) |
|
142 | - { |
|
143 | - return true; |
|
144 | - } |
|
145 | - |
|
146 | - $this->startMailService(); |
|
147 | - |
|
148 | - $this->service->to($to); |
|
149 | - $this->service->subject($subject); |
|
150 | - |
|
151 | - if (is_array($this->from)) { |
|
152 | - $this->service->from($this->from[0], $this->from[1]); |
|
153 | - } |
|
154 | - else |
|
155 | - { |
|
156 | - $this->service->from($this->from); |
|
157 | - } |
|
158 | - |
|
159 | - if (! empty($this->cc)) $this->service->cc($this->cc); |
|
160 | - if (! empty($this->bcc)) $this->service->bcc($this->bcc); |
|
161 | - |
|
162 | - if (is_array($this->reply_to)) { |
|
163 | - $this->service->reply_to($this->reply_to[0], $this->reply_to[1]); |
|
164 | - } |
|
165 | - else |
|
166 | - { |
|
167 | - $this->service->reply_to($this->reply_to); |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - // Determine the view to use. We have to hack this a bit with |
|
172 | - // the debug_backtrace, though, to make it all function in the background. |
|
173 | - list(, $method) = debug_backtrace(false); |
|
174 | - |
|
175 | - $view = 'emails/'. strtolower( (new \ReflectionClass($this))->getShortName() ) .'/'. $method['function']; |
|
176 | - |
|
177 | - // Get our message's text and html versions based on which files exist... |
|
178 | - $basepath = APPPATH .'views/'. $view; |
|
179 | - |
|
180 | - // Is a text version available? |
|
181 | - if (file_exists($basepath .'.text.php')) |
|
182 | - { |
|
183 | - $text = $this->load->view($view .'.text.php', $data, true); |
|
184 | - $this->service->text_message($text); |
|
185 | - } |
|
186 | - |
|
187 | - // If an html version is around, we need to theme it out |
|
188 | - if (file_exists($basepath .'.html.php')) |
|
189 | - { |
|
190 | - $this->startThemer(); |
|
191 | - |
|
192 | - $this->themer->setTheme($this->theme); |
|
193 | - |
|
194 | - // Determine the correct layout to use |
|
195 | - $layout = ! empty($this->layout) ? $this->layout : NULL; |
|
196 | - $this->themer->setLayout($layout); |
|
197 | - |
|
198 | - $this->themer->set($data); |
|
199 | - |
|
200 | - // Render the view into a var we can pass to the layout. |
|
201 | - $content = $this->themer->display($view .'.html.php'); |
|
202 | - |
|
203 | - $this->themer->set('content', $content); |
|
204 | - |
|
205 | - $this->service->html_message( $this->themer->display($this->theme .':'. $layout) ); |
|
206 | - } |
|
207 | - |
|
208 | - if (! $this->service->send() ) |
|
209 | - { |
|
210 | - // todo do something here |
|
211 | - return false; |
|
212 | - } |
|
213 | - |
|
214 | - return true; |
|
215 | - } |
|
216 | - |
|
217 | - //-------------------------------------------------------------------- |
|
218 | - |
|
219 | - /** |
|
220 | - * Allows you to customize the headers sent with the email. You can |
|
221 | - * do them one at a time by passing $field and $value, or pass an array |
|
222 | - * of $field => $value pairs as the first parameter. |
|
223 | - * |
|
224 | - * @param string|array $field |
|
225 | - * @param string $value |
|
226 | - */ |
|
227 | - public function header($field, $value=null) |
|
228 | - { |
|
229 | - $this->startMailService(); |
|
230 | - |
|
231 | - $this->service->setHeader($field, $value); |
|
232 | - } |
|
233 | - |
|
234 | - //-------------------------------------------------------------------- |
|
235 | - |
|
236 | - /** |
|
237 | - * Adds an attachment to the current email that is being built. |
|
238 | - * |
|
239 | - * @param string $filename |
|
240 | - * @param string $disposition like 'inline'. Default is 'attachment' |
|
241 | - * @param string $newname If you'd like to rename the file for delivery |
|
242 | - * @param string $mime Custom defined mime type. |
|
243 | - */ |
|
244 | - public function attach($filename, $disposition=null, $newname=null, $mime=null) |
|
245 | - { |
|
246 | - $this->startMailService(); |
|
247 | - |
|
248 | - $this->service->attach($filename, $disposition, $newname, $mime); |
|
249 | - } |
|
250 | - |
|
251 | - //-------------------------------------------------------------------- |
|
252 | - |
|
253 | - //-------------------------------------------------------------------- |
|
254 | - // Private Methods |
|
255 | - //-------------------------------------------------------------------- |
|
256 | - |
|
257 | - /** |
|
258 | - * Starts up the service name specified in $service_name. |
|
259 | - * |
|
260 | - * @param $service_name |
|
261 | - */ |
|
262 | - protected function startMailService() |
|
263 | - { |
|
264 | - // Only once! |
|
265 | - if (! empty($this->service) && is_object($this->service)) |
|
266 | - { |
|
267 | - return; |
|
268 | - } |
|
269 | - |
|
270 | - $service_name = ! empty($this->service_name) ? $this->service_name : config_item('mail.default_service'); |
|
271 | - |
|
272 | - if (! class_exists($service_name)) |
|
273 | - { |
|
274 | - throw new \RuntimeException( sprintf( lang('mail.invalid_service'), $service_name) ); |
|
275 | - } |
|
276 | - |
|
277 | - $this->service = new $service_name(); |
|
278 | - } |
|
279 | - |
|
280 | - //-------------------------------------------------------------------- |
|
281 | - |
|
282 | - /** |
|
283 | - * Fires up the default themer so we can use it to theme our HTML messages. |
|
284 | - */ |
|
285 | - protected function startThemer() |
|
286 | - { |
|
287 | - /* |
|
43 | + /** |
|
44 | + * How the email is delivered. |
|
45 | + * Either 'send' or 'queue'. |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $action = 'send'; |
|
49 | + |
|
50 | + protected $from = null; |
|
51 | + protected $to = null; |
|
52 | + protected $reply_to = null; |
|
53 | + protected $cc = null; |
|
54 | + protected $bcc = null; |
|
55 | + |
|
56 | + protected $message = null; |
|
57 | + |
|
58 | + protected $theme = 'email'; |
|
59 | + protected $layout = 'index'; |
|
60 | + protected $view = null; |
|
61 | + |
|
62 | + /** |
|
63 | + * The MailService to use. If NULL |
|
64 | + * will use the system default. |
|
65 | + * @var null |
|
66 | + */ |
|
67 | + protected $service_name = null; |
|
68 | + |
|
69 | + protected $service = null; |
|
70 | + |
|
71 | + /** |
|
72 | + * Used for theming the email messages. |
|
73 | + * @var null |
|
74 | + */ |
|
75 | + protected $themer = null; |
|
76 | + |
|
77 | + //-------------------------------------------------------------------- |
|
78 | + |
|
79 | + /** |
|
80 | + * Constructor |
|
81 | + * |
|
82 | + * Simply allows us to override the default settings for this mailer. |
|
83 | + * |
|
84 | + * @param null $options |
|
85 | + */ |
|
86 | + public function __construct($options=null) |
|
87 | + { |
|
88 | + if (! empty($options)) |
|
89 | + { |
|
90 | + $this->setOptions($options); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + //-------------------------------------------------------------------- |
|
95 | + |
|
96 | + /** |
|
97 | + * Sets the basic options available to the mailer, like 'from', 'to', |
|
98 | + * 'cc', 'bcc', etc. |
|
99 | + * |
|
100 | + * @param $options |
|
101 | + */ |
|
102 | + public function setOptions($options) |
|
103 | + { |
|
104 | + if (is_array($options)) |
|
105 | + { |
|
106 | + foreach ($options as $key => $value) |
|
107 | + { |
|
108 | + if ($key == 'service') |
|
109 | + { |
|
110 | + $this->service =& $value; |
|
111 | + continue; |
|
112 | + } |
|
113 | + |
|
114 | + if (property_exists($this, $key)) |
|
115 | + { |
|
116 | + $this->$key = $value; |
|
117 | + } |
|
118 | + } |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + //-------------------------------------------------------------------- |
|
123 | + |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * Sends an email immediately using the system-defined MailService. |
|
128 | + * |
|
129 | + * @param string $to // Who the email is being sent to. |
|
130 | + * @param string $subject // The subject line for the email |
|
131 | + * @param strign $data // the key/value pairs to send to the views. |
|
132 | + * @param string $view // You can override the view used for the email here. |
|
133 | + * // You can change themes by prepending theme name |
|
134 | + * // like: 'newtheme:newview' |
|
135 | + * |
|
136 | + * @return bool |
|
137 | + */ |
|
138 | + public function send($to, $subject, $data=[], $view=null) |
|
139 | + { |
|
140 | + // Are we pretending to send? |
|
141 | + if (config_item('mail.pretend') === true) |
|
142 | + { |
|
143 | + return true; |
|
144 | + } |
|
145 | + |
|
146 | + $this->startMailService(); |
|
147 | + |
|
148 | + $this->service->to($to); |
|
149 | + $this->service->subject($subject); |
|
150 | + |
|
151 | + if (is_array($this->from)) { |
|
152 | + $this->service->from($this->from[0], $this->from[1]); |
|
153 | + } |
|
154 | + else |
|
155 | + { |
|
156 | + $this->service->from($this->from); |
|
157 | + } |
|
158 | + |
|
159 | + if (! empty($this->cc)) $this->service->cc($this->cc); |
|
160 | + if (! empty($this->bcc)) $this->service->bcc($this->bcc); |
|
161 | + |
|
162 | + if (is_array($this->reply_to)) { |
|
163 | + $this->service->reply_to($this->reply_to[0], $this->reply_to[1]); |
|
164 | + } |
|
165 | + else |
|
166 | + { |
|
167 | + $this->service->reply_to($this->reply_to); |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + // Determine the view to use. We have to hack this a bit with |
|
172 | + // the debug_backtrace, though, to make it all function in the background. |
|
173 | + list(, $method) = debug_backtrace(false); |
|
174 | + |
|
175 | + $view = 'emails/'. strtolower( (new \ReflectionClass($this))->getShortName() ) .'/'. $method['function']; |
|
176 | + |
|
177 | + // Get our message's text and html versions based on which files exist... |
|
178 | + $basepath = APPPATH .'views/'. $view; |
|
179 | + |
|
180 | + // Is a text version available? |
|
181 | + if (file_exists($basepath .'.text.php')) |
|
182 | + { |
|
183 | + $text = $this->load->view($view .'.text.php', $data, true); |
|
184 | + $this->service->text_message($text); |
|
185 | + } |
|
186 | + |
|
187 | + // If an html version is around, we need to theme it out |
|
188 | + if (file_exists($basepath .'.html.php')) |
|
189 | + { |
|
190 | + $this->startThemer(); |
|
191 | + |
|
192 | + $this->themer->setTheme($this->theme); |
|
193 | + |
|
194 | + // Determine the correct layout to use |
|
195 | + $layout = ! empty($this->layout) ? $this->layout : NULL; |
|
196 | + $this->themer->setLayout($layout); |
|
197 | + |
|
198 | + $this->themer->set($data); |
|
199 | + |
|
200 | + // Render the view into a var we can pass to the layout. |
|
201 | + $content = $this->themer->display($view .'.html.php'); |
|
202 | + |
|
203 | + $this->themer->set('content', $content); |
|
204 | + |
|
205 | + $this->service->html_message( $this->themer->display($this->theme .':'. $layout) ); |
|
206 | + } |
|
207 | + |
|
208 | + if (! $this->service->send() ) |
|
209 | + { |
|
210 | + // todo do something here |
|
211 | + return false; |
|
212 | + } |
|
213 | + |
|
214 | + return true; |
|
215 | + } |
|
216 | + |
|
217 | + //-------------------------------------------------------------------- |
|
218 | + |
|
219 | + /** |
|
220 | + * Allows you to customize the headers sent with the email. You can |
|
221 | + * do them one at a time by passing $field and $value, or pass an array |
|
222 | + * of $field => $value pairs as the first parameter. |
|
223 | + * |
|
224 | + * @param string|array $field |
|
225 | + * @param string $value |
|
226 | + */ |
|
227 | + public function header($field, $value=null) |
|
228 | + { |
|
229 | + $this->startMailService(); |
|
230 | + |
|
231 | + $this->service->setHeader($field, $value); |
|
232 | + } |
|
233 | + |
|
234 | + //-------------------------------------------------------------------- |
|
235 | + |
|
236 | + /** |
|
237 | + * Adds an attachment to the current email that is being built. |
|
238 | + * |
|
239 | + * @param string $filename |
|
240 | + * @param string $disposition like 'inline'. Default is 'attachment' |
|
241 | + * @param string $newname If you'd like to rename the file for delivery |
|
242 | + * @param string $mime Custom defined mime type. |
|
243 | + */ |
|
244 | + public function attach($filename, $disposition=null, $newname=null, $mime=null) |
|
245 | + { |
|
246 | + $this->startMailService(); |
|
247 | + |
|
248 | + $this->service->attach($filename, $disposition, $newname, $mime); |
|
249 | + } |
|
250 | + |
|
251 | + //-------------------------------------------------------------------- |
|
252 | + |
|
253 | + //-------------------------------------------------------------------- |
|
254 | + // Private Methods |
|
255 | + //-------------------------------------------------------------------- |
|
256 | + |
|
257 | + /** |
|
258 | + * Starts up the service name specified in $service_name. |
|
259 | + * |
|
260 | + * @param $service_name |
|
261 | + */ |
|
262 | + protected function startMailService() |
|
263 | + { |
|
264 | + // Only once! |
|
265 | + if (! empty($this->service) && is_object($this->service)) |
|
266 | + { |
|
267 | + return; |
|
268 | + } |
|
269 | + |
|
270 | + $service_name = ! empty($this->service_name) ? $this->service_name : config_item('mail.default_service'); |
|
271 | + |
|
272 | + if (! class_exists($service_name)) |
|
273 | + { |
|
274 | + throw new \RuntimeException( sprintf( lang('mail.invalid_service'), $service_name) ); |
|
275 | + } |
|
276 | + |
|
277 | + $this->service = new $service_name(); |
|
278 | + } |
|
279 | + |
|
280 | + //-------------------------------------------------------------------- |
|
281 | + |
|
282 | + /** |
|
283 | + * Fires up the default themer so we can use it to theme our HTML messages. |
|
284 | + */ |
|
285 | + protected function startThemer() |
|
286 | + { |
|
287 | + /* |
|
288 | 288 | * Setup our Template Engine |
289 | 289 | */ |
290 | - $themer = config_item('active_themer'); |
|
291 | - |
|
292 | - if (empty($themer)) { |
|
293 | - throw new \RuntimeException( lang('no_themer') ); |
|
294 | - } |
|
295 | - |
|
296 | - if (empty($this->themer)) |
|
297 | - { |
|
298 | - $this->themer = new $themer( get_instance() ); |
|
299 | - } |
|
300 | - |
|
301 | - // Register our paths with the themer |
|
302 | - $paths = config_item('theme.paths'); |
|
303 | - |
|
304 | - foreach ($paths as $key => $path) { |
|
305 | - $this->themer->addThemePath($key, $path); |
|
306 | - } |
|
307 | - |
|
308 | - // Set our default theme. |
|
309 | - $this->themer->setDefaultTheme( 'email' ); |
|
310 | - } |
|
311 | - |
|
312 | - //-------------------------------------------------------------------- |
|
313 | - |
|
314 | - /** |
|
315 | - * __get magic |
|
316 | - * |
|
317 | - * Allows models to access CI's loaded classes using the same |
|
318 | - * syntax as controllers. |
|
319 | - * |
|
320 | - * @param string $key |
|
321 | - */ |
|
322 | - public function __get($key) |
|
323 | - { |
|
324 | - return get_instance()->$key; |
|
325 | - } |
|
326 | - |
|
327 | - //-------------------------------------------------------------------- |
|
290 | + $themer = config_item('active_themer'); |
|
291 | + |
|
292 | + if (empty($themer)) { |
|
293 | + throw new \RuntimeException( lang('no_themer') ); |
|
294 | + } |
|
295 | + |
|
296 | + if (empty($this->themer)) |
|
297 | + { |
|
298 | + $this->themer = new $themer( get_instance() ); |
|
299 | + } |
|
300 | + |
|
301 | + // Register our paths with the themer |
|
302 | + $paths = config_item('theme.paths'); |
|
303 | + |
|
304 | + foreach ($paths as $key => $path) { |
|
305 | + $this->themer->addThemePath($key, $path); |
|
306 | + } |
|
307 | + |
|
308 | + // Set our default theme. |
|
309 | + $this->themer->setDefaultTheme( 'email' ); |
|
310 | + } |
|
311 | + |
|
312 | + //-------------------------------------------------------------------- |
|
313 | + |
|
314 | + /** |
|
315 | + * __get magic |
|
316 | + * |
|
317 | + * Allows models to access CI's loaded classes using the same |
|
318 | + * syntax as controllers. |
|
319 | + * |
|
320 | + * @param string $key |
|
321 | + */ |
|
322 | + public function __get($key) |
|
323 | + { |
|
324 | + return get_instance()->$key; |
|
325 | + } |
|
326 | + |
|
327 | + //-------------------------------------------------------------------- |
|
328 | 328 | |
329 | 329 | } |
@@ -40,226 +40,226 @@ |
||
40 | 40 | */ |
41 | 41 | class CIMailService implements MailServiceInterface { |
42 | 42 | |
43 | - protected $ci = null; |
|
44 | - |
|
45 | - protected $format = 'html'; |
|
46 | - |
|
47 | - //-------------------------------------------------------------------- |
|
48 | - |
|
49 | - public function __construct() |
|
50 | - { |
|
51 | - $this->ci =& get_instance(); |
|
52 | - |
|
53 | - $this->ci->load->library('email'); |
|
54 | - } |
|
55 | - |
|
56 | - //-------------------------------------------------------------------- |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * Does the actual delivery of a message. |
|
61 | - * |
|
62 | - * @param bool $clear_after If TRUE, will reset the class after sending. |
|
63 | - * |
|
64 | - * @return mixed |
|
65 | - */ |
|
66 | - public function send($clear_after=true) |
|
67 | - { |
|
68 | - $this->ci->email->mailtype = $this->format; |
|
69 | - |
|
70 | - $result = $this->ci->email->send(false); |
|
71 | - |
|
72 | - return $result; |
|
73 | - } |
|
74 | - |
|
75 | - //-------------------------------------------------------------------- |
|
76 | - |
|
77 | - /** |
|
78 | - * Adds an attachment to the current email that is being built. |
|
79 | - * |
|
80 | - * @param string $filename |
|
81 | - * @param string $disposition like 'inline'. Default is 'attachment' |
|
82 | - * @param string $newname If you'd like to rename the file for delivery |
|
83 | - * @param string $mime Custom defined mime type. |
|
84 | - */ |
|
85 | - public function attach($filename, $disposition=null, $newname=null, $mime=null) |
|
86 | - { |
|
87 | - $this->ci->email->attach($filename, $disposition, $newname, $mime); |
|
88 | - } |
|
89 | - |
|
90 | - //-------------------------------------------------------------------- |
|
91 | - |
|
92 | - /** |
|
93 | - * Sets a header value for the email. Not every service will provide this. |
|
94 | - * |
|
95 | - * @param $field |
|
96 | - * @param $value |
|
97 | - * @return mixed |
|
98 | - */ |
|
99 | - public function setHeader($field, $value) |
|
100 | - { |
|
101 | - $this->ci->email->set_header($field, $value); |
|
102 | - |
|
103 | - return $this; |
|
104 | - } |
|
105 | - |
|
106 | - //-------------------------------------------------------------------- |
|
107 | - |
|
108 | - //-------------------------------------------------------------------- |
|
109 | - // Options |
|
110 | - //-------------------------------------------------------------------- |
|
111 | - |
|
112 | - /** |
|
113 | - * Sets the email address to send the email to. |
|
114 | - * |
|
115 | - * @param $email |
|
116 | - * @return mixed |
|
117 | - */ |
|
118 | - public function to($email) |
|
119 | - { |
|
120 | - $this->ci->email->to($email); |
|
121 | - |
|
122 | - return $this; |
|
123 | - } |
|
124 | - |
|
125 | - //-------------------------------------------------------------------- |
|
126 | - |
|
127 | - /** |
|
128 | - * Sets who the email is coming from. |
|
129 | - * |
|
130 | - * @param $email |
|
131 | - * @param null $name |
|
132 | - * @return mixed |
|
133 | - */ |
|
134 | - public function from($email, $name=null) |
|
135 | - { |
|
136 | - $this->ci->email->from($email, $name); |
|
137 | - |
|
138 | - return $this; |
|
139 | - } |
|
140 | - |
|
141 | - //-------------------------------------------------------------------- |
|
142 | - |
|
143 | - /** |
|
144 | - * Sets a single additional email address to 'cc'. |
|
145 | - * |
|
146 | - * @param $email |
|
147 | - * @return mixed |
|
148 | - */ |
|
149 | - public function cc($email) |
|
150 | - { |
|
151 | - $this->ci->email->cc($email); |
|
152 | - |
|
153 | - return $this; |
|
154 | - } |
|
155 | - |
|
156 | - //-------------------------------------------------------------------- |
|
157 | - |
|
158 | - /** |
|
159 | - * Sets a single email address to 'bcc' to. |
|
160 | - * |
|
161 | - * @param $email |
|
162 | - * @return mixed |
|
163 | - */ |
|
164 | - public function bcc($email) |
|
165 | - { |
|
166 | - $this->ci->email->bcc($email); |
|
167 | - |
|
168 | - return $this; |
|
169 | - } |
|
170 | - |
|
171 | - //-------------------------------------------------------------------- |
|
172 | - |
|
173 | - /** |
|
174 | - * Sets the reply to address. |
|
175 | - * |
|
176 | - * @param $email |
|
177 | - * @return mixed |
|
178 | - */ |
|
179 | - public function reply_to($email, $name=null) |
|
180 | - { |
|
181 | - $this->ci->email->reply_to($email, $name); |
|
182 | - } |
|
183 | - |
|
184 | - //-------------------------------------------------------------------- |
|
185 | - |
|
186 | - /** |
|
187 | - * Sets the subject line of the email. |
|
188 | - * |
|
189 | - * @param $subject |
|
190 | - * @return mixed |
|
191 | - */ |
|
192 | - public function subject($subject) |
|
193 | - { |
|
194 | - $this->ci->email->subject($subject); |
|
195 | - |
|
196 | - return $this; |
|
197 | - } |
|
198 | - |
|
199 | - //-------------------------------------------------------------------- |
|
200 | - |
|
201 | - /** |
|
202 | - * Sets the HTML portion of the email address. Optional. |
|
203 | - * |
|
204 | - * @param $message |
|
205 | - * @return mixed |
|
206 | - */ |
|
207 | - public function html_message($message) |
|
208 | - { |
|
209 | - $this->ci->email->message($message); |
|
210 | - |
|
211 | - $this->format = 'html'; |
|
212 | - |
|
213 | - return $this; |
|
214 | - } |
|
215 | - |
|
216 | - //-------------------------------------------------------------------- |
|
217 | - |
|
218 | - /** |
|
219 | - * Sets the text portion of the email address. Optional. |
|
220 | - * |
|
221 | - * @param $message |
|
222 | - * @return mixed |
|
223 | - */ |
|
224 | - public function text_message($message) |
|
225 | - { |
|
226 | - $this->ci->email->set_alt_message($message); |
|
227 | - |
|
228 | - $this->format = 'text'; |
|
229 | - |
|
230 | - return $this; |
|
231 | - } |
|
232 | - |
|
233 | - //-------------------------------------------------------------------- |
|
234 | - |
|
235 | - /** |
|
236 | - * Sets the format to send the email in. Either 'html' or 'text'. |
|
237 | - * |
|
238 | - * @param $format |
|
239 | - * @return mixed |
|
240 | - */ |
|
241 | - public function format($format) |
|
242 | - { |
|
243 | - $this->format = $format; |
|
244 | - |
|
245 | - return $this; |
|
246 | - } |
|
247 | - |
|
248 | - //-------------------------------------------------------------------- |
|
249 | - /** |
|
250 | - * Resets the state to blank, ready for a new email. Useful when |
|
251 | - * sending emails in a loop and you need to make sure that the |
|
252 | - * email is reset. |
|
253 | - * |
|
254 | - * @param bool $clear_attachments |
|
255 | - * @return mixed |
|
256 | - */ |
|
257 | - public function reset($clear_attachments=true) |
|
258 | - { |
|
259 | - $this->ci->email->clear($clear_attachments); |
|
260 | - |
|
261 | - return $this; |
|
262 | - } |
|
263 | - |
|
264 | - //-------------------------------------------------------------------- |
|
43 | + protected $ci = null; |
|
44 | + |
|
45 | + protected $format = 'html'; |
|
46 | + |
|
47 | + //-------------------------------------------------------------------- |
|
48 | + |
|
49 | + public function __construct() |
|
50 | + { |
|
51 | + $this->ci =& get_instance(); |
|
52 | + |
|
53 | + $this->ci->load->library('email'); |
|
54 | + } |
|
55 | + |
|
56 | + //-------------------------------------------------------------------- |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * Does the actual delivery of a message. |
|
61 | + * |
|
62 | + * @param bool $clear_after If TRUE, will reset the class after sending. |
|
63 | + * |
|
64 | + * @return mixed |
|
65 | + */ |
|
66 | + public function send($clear_after=true) |
|
67 | + { |
|
68 | + $this->ci->email->mailtype = $this->format; |
|
69 | + |
|
70 | + $result = $this->ci->email->send(false); |
|
71 | + |
|
72 | + return $result; |
|
73 | + } |
|
74 | + |
|
75 | + //-------------------------------------------------------------------- |
|
76 | + |
|
77 | + /** |
|
78 | + * Adds an attachment to the current email that is being built. |
|
79 | + * |
|
80 | + * @param string $filename |
|
81 | + * @param string $disposition like 'inline'. Default is 'attachment' |
|
82 | + * @param string $newname If you'd like to rename the file for delivery |
|
83 | + * @param string $mime Custom defined mime type. |
|
84 | + */ |
|
85 | + public function attach($filename, $disposition=null, $newname=null, $mime=null) |
|
86 | + { |
|
87 | + $this->ci->email->attach($filename, $disposition, $newname, $mime); |
|
88 | + } |
|
89 | + |
|
90 | + //-------------------------------------------------------------------- |
|
91 | + |
|
92 | + /** |
|
93 | + * Sets a header value for the email. Not every service will provide this. |
|
94 | + * |
|
95 | + * @param $field |
|
96 | + * @param $value |
|
97 | + * @return mixed |
|
98 | + */ |
|
99 | + public function setHeader($field, $value) |
|
100 | + { |
|
101 | + $this->ci->email->set_header($field, $value); |
|
102 | + |
|
103 | + return $this; |
|
104 | + } |
|
105 | + |
|
106 | + //-------------------------------------------------------------------- |
|
107 | + |
|
108 | + //-------------------------------------------------------------------- |
|
109 | + // Options |
|
110 | + //-------------------------------------------------------------------- |
|
111 | + |
|
112 | + /** |
|
113 | + * Sets the email address to send the email to. |
|
114 | + * |
|
115 | + * @param $email |
|
116 | + * @return mixed |
|
117 | + */ |
|
118 | + public function to($email) |
|
119 | + { |
|
120 | + $this->ci->email->to($email); |
|
121 | + |
|
122 | + return $this; |
|
123 | + } |
|
124 | + |
|
125 | + //-------------------------------------------------------------------- |
|
126 | + |
|
127 | + /** |
|
128 | + * Sets who the email is coming from. |
|
129 | + * |
|
130 | + * @param $email |
|
131 | + * @param null $name |
|
132 | + * @return mixed |
|
133 | + */ |
|
134 | + public function from($email, $name=null) |
|
135 | + { |
|
136 | + $this->ci->email->from($email, $name); |
|
137 | + |
|
138 | + return $this; |
|
139 | + } |
|
140 | + |
|
141 | + //-------------------------------------------------------------------- |
|
142 | + |
|
143 | + /** |
|
144 | + * Sets a single additional email address to 'cc'. |
|
145 | + * |
|
146 | + * @param $email |
|
147 | + * @return mixed |
|
148 | + */ |
|
149 | + public function cc($email) |
|
150 | + { |
|
151 | + $this->ci->email->cc($email); |
|
152 | + |
|
153 | + return $this; |
|
154 | + } |
|
155 | + |
|
156 | + //-------------------------------------------------------------------- |
|
157 | + |
|
158 | + /** |
|
159 | + * Sets a single email address to 'bcc' to. |
|
160 | + * |
|
161 | + * @param $email |
|
162 | + * @return mixed |
|
163 | + */ |
|
164 | + public function bcc($email) |
|
165 | + { |
|
166 | + $this->ci->email->bcc($email); |
|
167 | + |
|
168 | + return $this; |
|
169 | + } |
|
170 | + |
|
171 | + //-------------------------------------------------------------------- |
|
172 | + |
|
173 | + /** |
|
174 | + * Sets the reply to address. |
|
175 | + * |
|
176 | + * @param $email |
|
177 | + * @return mixed |
|
178 | + */ |
|
179 | + public function reply_to($email, $name=null) |
|
180 | + { |
|
181 | + $this->ci->email->reply_to($email, $name); |
|
182 | + } |
|
183 | + |
|
184 | + //-------------------------------------------------------------------- |
|
185 | + |
|
186 | + /** |
|
187 | + * Sets the subject line of the email. |
|
188 | + * |
|
189 | + * @param $subject |
|
190 | + * @return mixed |
|
191 | + */ |
|
192 | + public function subject($subject) |
|
193 | + { |
|
194 | + $this->ci->email->subject($subject); |
|
195 | + |
|
196 | + return $this; |
|
197 | + } |
|
198 | + |
|
199 | + //-------------------------------------------------------------------- |
|
200 | + |
|
201 | + /** |
|
202 | + * Sets the HTML portion of the email address. Optional. |
|
203 | + * |
|
204 | + * @param $message |
|
205 | + * @return mixed |
|
206 | + */ |
|
207 | + public function html_message($message) |
|
208 | + { |
|
209 | + $this->ci->email->message($message); |
|
210 | + |
|
211 | + $this->format = 'html'; |
|
212 | + |
|
213 | + return $this; |
|
214 | + } |
|
215 | + |
|
216 | + //-------------------------------------------------------------------- |
|
217 | + |
|
218 | + /** |
|
219 | + * Sets the text portion of the email address. Optional. |
|
220 | + * |
|
221 | + * @param $message |
|
222 | + * @return mixed |
|
223 | + */ |
|
224 | + public function text_message($message) |
|
225 | + { |
|
226 | + $this->ci->email->set_alt_message($message); |
|
227 | + |
|
228 | + $this->format = 'text'; |
|
229 | + |
|
230 | + return $this; |
|
231 | + } |
|
232 | + |
|
233 | + //-------------------------------------------------------------------- |
|
234 | + |
|
235 | + /** |
|
236 | + * Sets the format to send the email in. Either 'html' or 'text'. |
|
237 | + * |
|
238 | + * @param $format |
|
239 | + * @return mixed |
|
240 | + */ |
|
241 | + public function format($format) |
|
242 | + { |
|
243 | + $this->format = $format; |
|
244 | + |
|
245 | + return $this; |
|
246 | + } |
|
247 | + |
|
248 | + //-------------------------------------------------------------------- |
|
249 | + /** |
|
250 | + * Resets the state to blank, ready for a new email. Useful when |
|
251 | + * sending emails in a loop and you need to make sure that the |
|
252 | + * email is reset. |
|
253 | + * |
|
254 | + * @param bool $clear_attachments |
|
255 | + * @return mixed |
|
256 | + */ |
|
257 | + public function reset($clear_attachments=true) |
|
258 | + { |
|
259 | + $this->ci->email->clear($clear_attachments); |
|
260 | + |
|
261 | + return $this; |
|
262 | + } |
|
263 | + |
|
264 | + //-------------------------------------------------------------------- |
|
265 | 265 | } |
@@ -1,34 +1,34 @@ discard block |
||
1 | 1 | <?php namespace Myth\Mail; |
2 | 2 | /** |
3 | - * Sprint |
|
4 | - * |
|
5 | - * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | - * |
|
7 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | - * of this software and associated documentation files (the "Software"), to deal |
|
9 | - * in the Software without restriction, including without limitation the rights |
|
10 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | - * copies of the Software, and to permit persons to whom the Software is |
|
12 | - * furnished to do so, subject to the following conditions: |
|
13 | - * |
|
14 | - * The above copyright notice and this permission notice shall be included in |
|
15 | - * all copies or substantial portions of the Software. |
|
16 | - * |
|
17 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | - * THE SOFTWARE. |
|
24 | - * |
|
25 | - * @package Sprint |
|
26 | - * @author Lonnie Ezell |
|
27 | - * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | - * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | - * @link http://sprintphp.com |
|
30 | - * @since Version 1.0 |
|
31 | - */ |
|
3 | + * Sprint |
|
4 | + * |
|
5 | + * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | + * |
|
7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | + * of this software and associated documentation files (the "Software"), to deal |
|
9 | + * in the Software without restriction, including without limitation the rights |
|
10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | + * copies of the Software, and to permit persons to whom the Software is |
|
12 | + * furnished to do so, subject to the following conditions: |
|
13 | + * |
|
14 | + * The above copyright notice and this permission notice shall be included in |
|
15 | + * all copies or substantial portions of the Software. |
|
16 | + * |
|
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | + * THE SOFTWARE. |
|
24 | + * |
|
25 | + * @package Sprint |
|
26 | + * @author Lonnie Ezell |
|
27 | + * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | + * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | + * @link http://sprintphp.com |
|
30 | + * @since Version 1.0 |
|
31 | + */ |
|
32 | 32 | |
33 | 33 | /** |
34 | 34 | * Class LogMailService |
@@ -40,289 +40,289 @@ discard block |
||
40 | 40 | */ |
41 | 41 | class LogMailService implements MailServiceInterface { |
42 | 42 | |
43 | - protected $ci = null; |
|
44 | - |
|
45 | - protected $format = 'html'; |
|
46 | - protected $headers = []; |
|
47 | - protected $subject = null; |
|
48 | - |
|
49 | - protected $html_message = null; |
|
50 | - protected $text_message = null; |
|
51 | - |
|
52 | - //-------------------------------------------------------------------- |
|
53 | - |
|
54 | - public function __construct() |
|
55 | - { |
|
56 | - $this->ci =& get_instance(); |
|
57 | - |
|
58 | - $this->ci->load->library('email'); |
|
59 | - } |
|
60 | - |
|
61 | - //-------------------------------------------------------------------- |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Does the actual delivery of a message. In this case, though, we simply |
|
66 | - * write the html and text files out to the log folder/emails. |
|
67 | - * |
|
68 | - * The filename format is: yyyymmddhhiiss_email.{format} |
|
69 | - * |
|
70 | - * @param bool $clear_after If TRUE, will reset the class after sending. |
|
71 | - * |
|
72 | - * @return mixed |
|
73 | - */ |
|
74 | - public function send($clear_after=true) |
|
75 | - { |
|
76 | - |
|
77 | - // Ensure we have enough data |
|
78 | - if (empty($this->to) || empty($this->subject) || |
|
79 | - (empty($this->html_message) && empty($this->text_message)) |
|
80 | - ) |
|
81 | - { |
|
82 | - throw new \RuntimeException( lang('mail.invalid_log_data') ); |
|
83 | - } |
|
84 | - |
|
85 | - $symbols = ['#', '%', '&', '{', '}', '\\', '/', '<', '>', '*', '?', ' ', '$', '!', '\'', '"', ':', '@', '+', '`', '=']; |
|
86 | - |
|
87 | - $email = str_replace($symbols, '.', strtolower($this->to) ); |
|
88 | - |
|
89 | - $filename = date('YmdHis_'). $email; |
|
90 | - |
|
91 | - // Ensure the emails folder exists in the log folder. |
|
92 | - $path = config_item('log_path'); |
|
93 | - $path = ! empty( $path ) ? $path : APPPATH .'logs/'; |
|
94 | - $path = rtrim($path, '/ ') .'/email/'; |
|
95 | - |
|
96 | - if (! is_dir($path)) |
|
97 | - { |
|
98 | - mkdir($path, 0777, true); |
|
99 | - } |
|
100 | - |
|
101 | - get_instance()->load->helper('file'); |
|
102 | - |
|
103 | - // Write our HTML file out |
|
104 | - if (! empty($this->html_message) && ! write_file( $path . $filename . '.html', $this->html_message ) ) |
|
105 | - { |
|
106 | - throw new \RuntimeException( sprintf( lang('mail.error_html_log'), $path, $filename) ); |
|
107 | - } |
|
108 | - |
|
109 | - // Write our TEXT file out |
|
110 | - if (! empty($this->text_message) && ! write_file( $path . $filename . '.txt', $this->text_message ) ) |
|
111 | - { |
|
112 | - throw new \RuntimeException( sprintf( lang('mail.error_text_log'), $path, $filename) ); |
|
113 | - } |
|
114 | - |
|
115 | - return true; |
|
116 | - } |
|
117 | - |
|
118 | - //-------------------------------------------------------------------- |
|
119 | - |
|
120 | - /** |
|
121 | - * Adds an attachment to the current email that is being built. |
|
122 | - * |
|
123 | - * @param string $filename |
|
124 | - * @param string $disposition like 'inline'. Default is 'attachment' |
|
125 | - * @param string $newname If you'd like to rename the file for delivery |
|
126 | - * @param string $mime Custom defined mime type. |
|
127 | - */ |
|
128 | - public function attach($filename, $disposition=null, $newname=null, $mime=null) |
|
129 | - { |
|
130 | - return; |
|
131 | - } |
|
132 | - |
|
133 | - //-------------------------------------------------------------------- |
|
134 | - |
|
135 | - /** |
|
136 | - * Sets a header value for the email. Not every service will provide this. |
|
137 | - * |
|
138 | - * @param $field |
|
139 | - * @param $value |
|
140 | - * @return mixed |
|
141 | - */ |
|
142 | - public function setHeader($field, $value) |
|
143 | - { |
|
144 | - $this->headers[$field] = $value; |
|
145 | - |
|
146 | - return $this; |
|
147 | - } |
|
148 | - |
|
149 | - //-------------------------------------------------------------------- |
|
150 | - |
|
151 | - //-------------------------------------------------------------------- |
|
152 | - // Options |
|
153 | - //-------------------------------------------------------------------- |
|
154 | - |
|
155 | - /** |
|
156 | - * Sets the email address to send the email to. |
|
157 | - * |
|
158 | - * @param $email |
|
159 | - * @return mixed |
|
160 | - */ |
|
161 | - public function to($email) |
|
162 | - { |
|
163 | - $this->to = $email; |
|
164 | - |
|
165 | - return $this; |
|
166 | - } |
|
167 | - |
|
168 | - //-------------------------------------------------------------------- |
|
169 | - |
|
170 | - /** |
|
171 | - * Sets who the email is coming from. |
|
172 | - * |
|
173 | - * @param $email |
|
174 | - * @param null $name |
|
175 | - * @return mixed |
|
176 | - */ |
|
177 | - public function from($email, $name=null) |
|
178 | - { |
|
179 | - if (! empty($name)) |
|
180 | - { |
|
181 | - $this->from = [$email, $name]; |
|
182 | - } |
|
183 | - else |
|
184 | - { |
|
185 | - $this->from = $email; |
|
186 | - } |
|
187 | - |
|
188 | - return $this; |
|
189 | - } |
|
190 | - |
|
191 | - //-------------------------------------------------------------------- |
|
192 | - |
|
193 | - /** |
|
194 | - * Sets a single additional email address to 'cc'. |
|
195 | - * |
|
196 | - * @param $email |
|
197 | - * @return mixed |
|
198 | - */ |
|
199 | - public function cc($email) |
|
200 | - { |
|
201 | - $this->cc = $email; |
|
202 | - |
|
203 | - return $this; |
|
204 | - } |
|
205 | - |
|
206 | - //-------------------------------------------------------------------- |
|
207 | - |
|
208 | - /** |
|
209 | - * Sets a single email address to 'bcc' to. |
|
210 | - * |
|
211 | - * @param $email |
|
212 | - * @return mixed |
|
213 | - */ |
|
214 | - public function bcc($email) |
|
215 | - { |
|
216 | - $this->bcc = $email; |
|
217 | - |
|
218 | - return $this; |
|
219 | - } |
|
220 | - |
|
221 | - //-------------------------------------------------------------------- |
|
222 | - |
|
223 | - /** |
|
224 | - * Sets the reply to address. |
|
225 | - * |
|
226 | - * @param $email |
|
227 | - * @return mixed |
|
228 | - */ |
|
229 | - public function reply_to($email, $name=null) |
|
230 | - { |
|
231 | - if (! empty($name)) |
|
232 | - { |
|
233 | - $this->reply_to = [$email, $name]; |
|
234 | - } |
|
235 | - else |
|
236 | - { |
|
237 | - $this->reply_to = $email; |
|
238 | - } |
|
239 | - |
|
240 | - return $this; |
|
241 | - } |
|
242 | - |
|
243 | - //-------------------------------------------------------------------- |
|
244 | - |
|
245 | - /** |
|
246 | - * Sets the subject line of the email. |
|
247 | - * |
|
248 | - * @param $subject |
|
249 | - * @return mixed |
|
250 | - */ |
|
251 | - public function subject($subject) |
|
252 | - { |
|
253 | - $this->subject = $subject; |
|
254 | - |
|
255 | - return $this; |
|
256 | - } |
|
257 | - |
|
258 | - //-------------------------------------------------------------------- |
|
259 | - |
|
260 | - /** |
|
261 | - * Sets the HTML portion of the email address. Optional. |
|
262 | - * |
|
263 | - * @param $message |
|
264 | - * @return mixed |
|
265 | - */ |
|
266 | - public function html_message($message) |
|
267 | - { |
|
268 | - $this->html_message = $message; |
|
269 | - |
|
270 | - return $this; |
|
271 | - } |
|
272 | - |
|
273 | - //-------------------------------------------------------------------- |
|
274 | - |
|
275 | - /** |
|
276 | - * Sets the text portion of the email address. Optional. |
|
277 | - * |
|
278 | - * @param $message |
|
279 | - * @return mixed |
|
280 | - */ |
|
281 | - public function text_message($message) |
|
282 | - { |
|
283 | - $this->text_message = $message; |
|
284 | - |
|
285 | - return $this; |
|
286 | - } |
|
287 | - |
|
288 | - //-------------------------------------------------------------------- |
|
289 | - |
|
290 | - /** |
|
291 | - * Sets the format to send the email in. Either 'html' or 'text'. |
|
292 | - * |
|
293 | - * @param $format |
|
294 | - * @return mixed |
|
295 | - */ |
|
296 | - public function format($format) |
|
297 | - { |
|
298 | - $this->format = $format; |
|
299 | - |
|
300 | - return $this; |
|
301 | - } |
|
302 | - |
|
303 | - //-------------------------------------------------------------------- |
|
304 | - /** |
|
305 | - * Resets the state to blank, ready for a new email. Useful when |
|
306 | - * sending emails in a loop and you need to make sure that the |
|
307 | - * email is reset. |
|
308 | - * |
|
309 | - * @param bool $clear_attachments |
|
310 | - * @return mixed |
|
311 | - */ |
|
312 | - public function reset($clear_attachments=true) |
|
313 | - { |
|
314 | - $this->to = null; |
|
315 | - $this->from = null; |
|
316 | - $this->reply_to = null; |
|
317 | - $this->cc = null; |
|
318 | - $this->bcc = null; |
|
319 | - $this->subject = null; |
|
320 | - $this->html_message = null; |
|
321 | - $this->text_message = null; |
|
322 | - $this->headers = []; |
|
323 | - |
|
324 | - return $this; |
|
325 | - } |
|
326 | - |
|
327 | - //-------------------------------------------------------------------- |
|
43 | + protected $ci = null; |
|
44 | + |
|
45 | + protected $format = 'html'; |
|
46 | + protected $headers = []; |
|
47 | + protected $subject = null; |
|
48 | + |
|
49 | + protected $html_message = null; |
|
50 | + protected $text_message = null; |
|
51 | + |
|
52 | + //-------------------------------------------------------------------- |
|
53 | + |
|
54 | + public function __construct() |
|
55 | + { |
|
56 | + $this->ci =& get_instance(); |
|
57 | + |
|
58 | + $this->ci->load->library('email'); |
|
59 | + } |
|
60 | + |
|
61 | + //-------------------------------------------------------------------- |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Does the actual delivery of a message. In this case, though, we simply |
|
66 | + * write the html and text files out to the log folder/emails. |
|
67 | + * |
|
68 | + * The filename format is: yyyymmddhhiiss_email.{format} |
|
69 | + * |
|
70 | + * @param bool $clear_after If TRUE, will reset the class after sending. |
|
71 | + * |
|
72 | + * @return mixed |
|
73 | + */ |
|
74 | + public function send($clear_after=true) |
|
75 | + { |
|
76 | + |
|
77 | + // Ensure we have enough data |
|
78 | + if (empty($this->to) || empty($this->subject) || |
|
79 | + (empty($this->html_message) && empty($this->text_message)) |
|
80 | + ) |
|
81 | + { |
|
82 | + throw new \RuntimeException( lang('mail.invalid_log_data') ); |
|
83 | + } |
|
84 | + |
|
85 | + $symbols = ['#', '%', '&', '{', '}', '\\', '/', '<', '>', '*', '?', ' ', '$', '!', '\'', '"', ':', '@', '+', '`', '=']; |
|
86 | + |
|
87 | + $email = str_replace($symbols, '.', strtolower($this->to) ); |
|
88 | + |
|
89 | + $filename = date('YmdHis_'). $email; |
|
90 | + |
|
91 | + // Ensure the emails folder exists in the log folder. |
|
92 | + $path = config_item('log_path'); |
|
93 | + $path = ! empty( $path ) ? $path : APPPATH .'logs/'; |
|
94 | + $path = rtrim($path, '/ ') .'/email/'; |
|
95 | + |
|
96 | + if (! is_dir($path)) |
|
97 | + { |
|
98 | + mkdir($path, 0777, true); |
|
99 | + } |
|
100 | + |
|
101 | + get_instance()->load->helper('file'); |
|
102 | + |
|
103 | + // Write our HTML file out |
|
104 | + if (! empty($this->html_message) && ! write_file( $path . $filename . '.html', $this->html_message ) ) |
|
105 | + { |
|
106 | + throw new \RuntimeException( sprintf( lang('mail.error_html_log'), $path, $filename) ); |
|
107 | + } |
|
108 | + |
|
109 | + // Write our TEXT file out |
|
110 | + if (! empty($this->text_message) && ! write_file( $path . $filename . '.txt', $this->text_message ) ) |
|
111 | + { |
|
112 | + throw new \RuntimeException( sprintf( lang('mail.error_text_log'), $path, $filename) ); |
|
113 | + } |
|
114 | + |
|
115 | + return true; |
|
116 | + } |
|
117 | + |
|
118 | + //-------------------------------------------------------------------- |
|
119 | + |
|
120 | + /** |
|
121 | + * Adds an attachment to the current email that is being built. |
|
122 | + * |
|
123 | + * @param string $filename |
|
124 | + * @param string $disposition like 'inline'. Default is 'attachment' |
|
125 | + * @param string $newname If you'd like to rename the file for delivery |
|
126 | + * @param string $mime Custom defined mime type. |
|
127 | + */ |
|
128 | + public function attach($filename, $disposition=null, $newname=null, $mime=null) |
|
129 | + { |
|
130 | + return; |
|
131 | + } |
|
132 | + |
|
133 | + //-------------------------------------------------------------------- |
|
134 | + |
|
135 | + /** |
|
136 | + * Sets a header value for the email. Not every service will provide this. |
|
137 | + * |
|
138 | + * @param $field |
|
139 | + * @param $value |
|
140 | + * @return mixed |
|
141 | + */ |
|
142 | + public function setHeader($field, $value) |
|
143 | + { |
|
144 | + $this->headers[$field] = $value; |
|
145 | + |
|
146 | + return $this; |
|
147 | + } |
|
148 | + |
|
149 | + //-------------------------------------------------------------------- |
|
150 | + |
|
151 | + //-------------------------------------------------------------------- |
|
152 | + // Options |
|
153 | + //-------------------------------------------------------------------- |
|
154 | + |
|
155 | + /** |
|
156 | + * Sets the email address to send the email to. |
|
157 | + * |
|
158 | + * @param $email |
|
159 | + * @return mixed |
|
160 | + */ |
|
161 | + public function to($email) |
|
162 | + { |
|
163 | + $this->to = $email; |
|
164 | + |
|
165 | + return $this; |
|
166 | + } |
|
167 | + |
|
168 | + //-------------------------------------------------------------------- |
|
169 | + |
|
170 | + /** |
|
171 | + * Sets who the email is coming from. |
|
172 | + * |
|
173 | + * @param $email |
|
174 | + * @param null $name |
|
175 | + * @return mixed |
|
176 | + */ |
|
177 | + public function from($email, $name=null) |
|
178 | + { |
|
179 | + if (! empty($name)) |
|
180 | + { |
|
181 | + $this->from = [$email, $name]; |
|
182 | + } |
|
183 | + else |
|
184 | + { |
|
185 | + $this->from = $email; |
|
186 | + } |
|
187 | + |
|
188 | + return $this; |
|
189 | + } |
|
190 | + |
|
191 | + //-------------------------------------------------------------------- |
|
192 | + |
|
193 | + /** |
|
194 | + * Sets a single additional email address to 'cc'. |
|
195 | + * |
|
196 | + * @param $email |
|
197 | + * @return mixed |
|
198 | + */ |
|
199 | + public function cc($email) |
|
200 | + { |
|
201 | + $this->cc = $email; |
|
202 | + |
|
203 | + return $this; |
|
204 | + } |
|
205 | + |
|
206 | + //-------------------------------------------------------------------- |
|
207 | + |
|
208 | + /** |
|
209 | + * Sets a single email address to 'bcc' to. |
|
210 | + * |
|
211 | + * @param $email |
|
212 | + * @return mixed |
|
213 | + */ |
|
214 | + public function bcc($email) |
|
215 | + { |
|
216 | + $this->bcc = $email; |
|
217 | + |
|
218 | + return $this; |
|
219 | + } |
|
220 | + |
|
221 | + //-------------------------------------------------------------------- |
|
222 | + |
|
223 | + /** |
|
224 | + * Sets the reply to address. |
|
225 | + * |
|
226 | + * @param $email |
|
227 | + * @return mixed |
|
228 | + */ |
|
229 | + public function reply_to($email, $name=null) |
|
230 | + { |
|
231 | + if (! empty($name)) |
|
232 | + { |
|
233 | + $this->reply_to = [$email, $name]; |
|
234 | + } |
|
235 | + else |
|
236 | + { |
|
237 | + $this->reply_to = $email; |
|
238 | + } |
|
239 | + |
|
240 | + return $this; |
|
241 | + } |
|
242 | + |
|
243 | + //-------------------------------------------------------------------- |
|
244 | + |
|
245 | + /** |
|
246 | + * Sets the subject line of the email. |
|
247 | + * |
|
248 | + * @param $subject |
|
249 | + * @return mixed |
|
250 | + */ |
|
251 | + public function subject($subject) |
|
252 | + { |
|
253 | + $this->subject = $subject; |
|
254 | + |
|
255 | + return $this; |
|
256 | + } |
|
257 | + |
|
258 | + //-------------------------------------------------------------------- |
|
259 | + |
|
260 | + /** |
|
261 | + * Sets the HTML portion of the email address. Optional. |
|
262 | + * |
|
263 | + * @param $message |
|
264 | + * @return mixed |
|
265 | + */ |
|
266 | + public function html_message($message) |
|
267 | + { |
|
268 | + $this->html_message = $message; |
|
269 | + |
|
270 | + return $this; |
|
271 | + } |
|
272 | + |
|
273 | + //-------------------------------------------------------------------- |
|
274 | + |
|
275 | + /** |
|
276 | + * Sets the text portion of the email address. Optional. |
|
277 | + * |
|
278 | + * @param $message |
|
279 | + * @return mixed |
|
280 | + */ |
|
281 | + public function text_message($message) |
|
282 | + { |
|
283 | + $this->text_message = $message; |
|
284 | + |
|
285 | + return $this; |
|
286 | + } |
|
287 | + |
|
288 | + //-------------------------------------------------------------------- |
|
289 | + |
|
290 | + /** |
|
291 | + * Sets the format to send the email in. Either 'html' or 'text'. |
|
292 | + * |
|
293 | + * @param $format |
|
294 | + * @return mixed |
|
295 | + */ |
|
296 | + public function format($format) |
|
297 | + { |
|
298 | + $this->format = $format; |
|
299 | + |
|
300 | + return $this; |
|
301 | + } |
|
302 | + |
|
303 | + //-------------------------------------------------------------------- |
|
304 | + /** |
|
305 | + * Resets the state to blank, ready for a new email. Useful when |
|
306 | + * sending emails in a loop and you need to make sure that the |
|
307 | + * email is reset. |
|
308 | + * |
|
309 | + * @param bool $clear_attachments |
|
310 | + * @return mixed |
|
311 | + */ |
|
312 | + public function reset($clear_attachments=true) |
|
313 | + { |
|
314 | + $this->to = null; |
|
315 | + $this->from = null; |
|
316 | + $this->reply_to = null; |
|
317 | + $this->cc = null; |
|
318 | + $this->bcc = null; |
|
319 | + $this->subject = null; |
|
320 | + $this->html_message = null; |
|
321 | + $this->text_message = null; |
|
322 | + $this->headers = []; |
|
323 | + |
|
324 | + return $this; |
|
325 | + } |
|
326 | + |
|
327 | + //-------------------------------------------------------------------- |
|
328 | 328 | } |
@@ -1,172 +1,172 @@ |
||
1 | 1 | <?php namespace Myth\Mail; |
2 | 2 | /** |
3 | - * Sprint |
|
4 | - * |
|
5 | - * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | - * |
|
7 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | - * of this software and associated documentation files (the "Software"), to deal |
|
9 | - * in the Software without restriction, including without limitation the rights |
|
10 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | - * copies of the Software, and to permit persons to whom the Software is |
|
12 | - * furnished to do so, subject to the following conditions: |
|
13 | - * |
|
14 | - * The above copyright notice and this permission notice shall be included in |
|
15 | - * all copies or substantial portions of the Software. |
|
16 | - * |
|
17 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | - * THE SOFTWARE. |
|
24 | - * |
|
25 | - * @package Sprint |
|
26 | - * @author Lonnie Ezell |
|
27 | - * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | - * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | - * @link http://sprintphp.com |
|
30 | - * @since Version 1.0 |
|
31 | - */ |
|
3 | + * Sprint |
|
4 | + * |
|
5 | + * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | + * |
|
7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | + * of this software and associated documentation files (the "Software"), to deal |
|
9 | + * in the Software without restriction, including without limitation the rights |
|
10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | + * copies of the Software, and to permit persons to whom the Software is |
|
12 | + * furnished to do so, subject to the following conditions: |
|
13 | + * |
|
14 | + * The above copyright notice and this permission notice shall be included in |
|
15 | + * all copies or substantial portions of the Software. |
|
16 | + * |
|
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | + * THE SOFTWARE. |
|
24 | + * |
|
25 | + * @package Sprint |
|
26 | + * @author Lonnie Ezell |
|
27 | + * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | + * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | + * @link http://sprintphp.com |
|
30 | + * @since Version 1.0 |
|
31 | + */ |
|
32 | 32 | |
33 | 33 | class Mail { |
34 | 34 | |
35 | - /** |
|
36 | - * Sends an email, using an existing Mailer, which can |
|
37 | - * be found in application/mailers/. The mailer is the one responsible |
|
38 | - * for determining whether the email will be sent immediately or queued |
|
39 | - * to be sent later. |
|
40 | - * |
|
41 | - * The $mailer_name must include both the mailer name as well as the |
|
42 | - * task, separated by a single colon: |
|
43 | - * 'UserMailer:newUser' |
|
44 | - * |
|
45 | - * @param $mailer_name |
|
46 | - * @param array $params |
|
47 | - * @param array $options |
|
48 | - * @return mixed |
|
49 | - */ |
|
50 | - public static function deliver($mailer_name, $params=[], $options=[]) |
|
51 | - { |
|
52 | - // Protect users from themselves here. |
|
53 | - str_replace('::', ':', $mailer_name); |
|
54 | - |
|
55 | - // Try to load our mailer class. |
|
56 | - list($class, $method) = explode(':', $mailer_name); |
|
57 | - |
|
58 | - if (! is_file(APPPATH .'mailers/'. $class .'.php')) |
|
59 | - { |
|
60 | - throw new \RuntimeException( sprintf( lang('mail.cant_find_mailer'), $class) ); |
|
61 | - } |
|
62 | - |
|
63 | - require_once APPPATH .'mailers/'. $class .'.php'; |
|
64 | - |
|
65 | - if (! class_exists($class, false)) |
|
66 | - { |
|
67 | - throw new \RuntimeException( sprintf( lang('errors.cant_instantiate'), $class) ); |
|
68 | - } |
|
69 | - |
|
70 | - $mailer = new $class( $options ); |
|
71 | - |
|
72 | - if (! method_exists($mailer, $method)) |
|
73 | - { |
|
74 | - throw new \BadMethodCallException( sprintf( lang('mail.invalid_mailer_method'), $class, $method) ); |
|
75 | - } |
|
76 | - |
|
77 | - // try to deliver the mail, but don't send back the contents |
|
78 | - // since we don't want to force the mailers to return anything. |
|
79 | - if (call_user_func_array([$mailer, $method], $params) ) |
|
80 | - { |
|
81 | - return true; |
|
82 | - } |
|
83 | - |
|
84 | - return false; |
|
85 | - } |
|
86 | - |
|
87 | - //-------------------------------------------------------------------- |
|
88 | - |
|
89 | - /** |
|
90 | - * Adds an item to the email queue to be sent out next time. |
|
91 | - * |
|
92 | - * @param string $mailer_name |
|
93 | - * @param array $params |
|
94 | - * @param array $options |
|
95 | - * @param \Myth\Mail\Queue $queue |
|
96 | - * |
|
97 | - * @return mixed |
|
98 | - */ |
|
99 | - public static function queue($mailer_name, $params=[], $options=[], &$queue=null) |
|
100 | - { |
|
101 | - $data = [ |
|
102 | - 'mailer' => $mailer_name, |
|
103 | - 'params' => serialize($params), |
|
104 | - 'options' => serialize($options) |
|
105 | - ]; |
|
106 | - |
|
107 | - if (empty($queue)) |
|
108 | - { |
|
109 | - $queue = new \Myth\Mail\Queue(); |
|
110 | - } |
|
111 | - |
|
112 | - return $queue->insert($data); |
|
113 | - } |
|
114 | - |
|
115 | - //-------------------------------------------------------------------- |
|
116 | - |
|
117 | - /** |
|
118 | - * Processes the Email queue sending out emails in chunks. |
|
119 | - * Typically used in a cronjob to send out all queued emails. |
|
120 | - * |
|
121 | - * @param int $chunk_size // How many emails to send per batch. |
|
122 | - * @return string // The output of the cronjob... |
|
123 | - */ |
|
124 | - public static function process($chunk_size=50, &$db=null) |
|
125 | - { |
|
126 | - if (empty($db)) |
|
127 | - { |
|
128 | - $db = new \Myth\Mail\Queue(); |
|
129 | - } |
|
130 | - |
|
131 | - // Grab our batch of emails to process |
|
132 | - $queue = $db->find_many_by('sent', 0); |
|
133 | - |
|
134 | - if (! $queue) |
|
135 | - { |
|
136 | - // We didn't have an error, we simply |
|
137 | - // didn't have anything to do. |
|
138 | - return true; |
|
139 | - } |
|
140 | - |
|
141 | - $output = 'Started processing email Queue at '. date('Y-m-d H:i:s') .".\n\n"; |
|
142 | - |
|
143 | - foreach ($queue as $item) |
|
144 | - { |
|
145 | - try { |
|
146 | - if (! Mail::deliver($item->mailer, unserialize($item->params), unserialize($item->options))) { |
|
147 | - $output .= '[FAILED] '; |
|
148 | - } else { |
|
149 | - $data = [ |
|
150 | - 'sent' => 1, |
|
151 | - 'sent_on' => date('Y-m-d H:i:s') |
|
152 | - ]; |
|
153 | - |
|
154 | - $db->update($item->id, $data); |
|
155 | - } |
|
156 | - |
|
157 | - $output .= "ID: {$item->id}, Mailer: {$item->mailer}. \n"; |
|
158 | - } |
|
159 | - catch (\Exception $e) |
|
160 | - { |
|
161 | - $output .= "[EXCEPTION] ". $e->getMessage() ."\n"; |
|
162 | - } |
|
163 | - } |
|
164 | - |
|
165 | - $output .= "Done processing email Queue at ". date('H:i:s') .".\n"; |
|
166 | - |
|
167 | - return $output; |
|
168 | - } |
|
169 | - |
|
170 | - //-------------------------------------------------------------------- |
|
35 | + /** |
|
36 | + * Sends an email, using an existing Mailer, which can |
|
37 | + * be found in application/mailers/. The mailer is the one responsible |
|
38 | + * for determining whether the email will be sent immediately or queued |
|
39 | + * to be sent later. |
|
40 | + * |
|
41 | + * The $mailer_name must include both the mailer name as well as the |
|
42 | + * task, separated by a single colon: |
|
43 | + * 'UserMailer:newUser' |
|
44 | + * |
|
45 | + * @param $mailer_name |
|
46 | + * @param array $params |
|
47 | + * @param array $options |
|
48 | + * @return mixed |
|
49 | + */ |
|
50 | + public static function deliver($mailer_name, $params=[], $options=[]) |
|
51 | + { |
|
52 | + // Protect users from themselves here. |
|
53 | + str_replace('::', ':', $mailer_name); |
|
54 | + |
|
55 | + // Try to load our mailer class. |
|
56 | + list($class, $method) = explode(':', $mailer_name); |
|
57 | + |
|
58 | + if (! is_file(APPPATH .'mailers/'. $class .'.php')) |
|
59 | + { |
|
60 | + throw new \RuntimeException( sprintf( lang('mail.cant_find_mailer'), $class) ); |
|
61 | + } |
|
62 | + |
|
63 | + require_once APPPATH .'mailers/'. $class .'.php'; |
|
64 | + |
|
65 | + if (! class_exists($class, false)) |
|
66 | + { |
|
67 | + throw new \RuntimeException( sprintf( lang('errors.cant_instantiate'), $class) ); |
|
68 | + } |
|
69 | + |
|
70 | + $mailer = new $class( $options ); |
|
71 | + |
|
72 | + if (! method_exists($mailer, $method)) |
|
73 | + { |
|
74 | + throw new \BadMethodCallException( sprintf( lang('mail.invalid_mailer_method'), $class, $method) ); |
|
75 | + } |
|
76 | + |
|
77 | + // try to deliver the mail, but don't send back the contents |
|
78 | + // since we don't want to force the mailers to return anything. |
|
79 | + if (call_user_func_array([$mailer, $method], $params) ) |
|
80 | + { |
|
81 | + return true; |
|
82 | + } |
|
83 | + |
|
84 | + return false; |
|
85 | + } |
|
86 | + |
|
87 | + //-------------------------------------------------------------------- |
|
88 | + |
|
89 | + /** |
|
90 | + * Adds an item to the email queue to be sent out next time. |
|
91 | + * |
|
92 | + * @param string $mailer_name |
|
93 | + * @param array $params |
|
94 | + * @param array $options |
|
95 | + * @param \Myth\Mail\Queue $queue |
|
96 | + * |
|
97 | + * @return mixed |
|
98 | + */ |
|
99 | + public static function queue($mailer_name, $params=[], $options=[], &$queue=null) |
|
100 | + { |
|
101 | + $data = [ |
|
102 | + 'mailer' => $mailer_name, |
|
103 | + 'params' => serialize($params), |
|
104 | + 'options' => serialize($options) |
|
105 | + ]; |
|
106 | + |
|
107 | + if (empty($queue)) |
|
108 | + { |
|
109 | + $queue = new \Myth\Mail\Queue(); |
|
110 | + } |
|
111 | + |
|
112 | + return $queue->insert($data); |
|
113 | + } |
|
114 | + |
|
115 | + //-------------------------------------------------------------------- |
|
116 | + |
|
117 | + /** |
|
118 | + * Processes the Email queue sending out emails in chunks. |
|
119 | + * Typically used in a cronjob to send out all queued emails. |
|
120 | + * |
|
121 | + * @param int $chunk_size // How many emails to send per batch. |
|
122 | + * @return string // The output of the cronjob... |
|
123 | + */ |
|
124 | + public static function process($chunk_size=50, &$db=null) |
|
125 | + { |
|
126 | + if (empty($db)) |
|
127 | + { |
|
128 | + $db = new \Myth\Mail\Queue(); |
|
129 | + } |
|
130 | + |
|
131 | + // Grab our batch of emails to process |
|
132 | + $queue = $db->find_many_by('sent', 0); |
|
133 | + |
|
134 | + if (! $queue) |
|
135 | + { |
|
136 | + // We didn't have an error, we simply |
|
137 | + // didn't have anything to do. |
|
138 | + return true; |
|
139 | + } |
|
140 | + |
|
141 | + $output = 'Started processing email Queue at '. date('Y-m-d H:i:s') .".\n\n"; |
|
142 | + |
|
143 | + foreach ($queue as $item) |
|
144 | + { |
|
145 | + try { |
|
146 | + if (! Mail::deliver($item->mailer, unserialize($item->params), unserialize($item->options))) { |
|
147 | + $output .= '[FAILED] '; |
|
148 | + } else { |
|
149 | + $data = [ |
|
150 | + 'sent' => 1, |
|
151 | + 'sent_on' => date('Y-m-d H:i:s') |
|
152 | + ]; |
|
153 | + |
|
154 | + $db->update($item->id, $data); |
|
155 | + } |
|
156 | + |
|
157 | + $output .= "ID: {$item->id}, Mailer: {$item->mailer}. \n"; |
|
158 | + } |
|
159 | + catch (\Exception $e) |
|
160 | + { |
|
161 | + $output .= "[EXCEPTION] ". $e->getMessage() ."\n"; |
|
162 | + } |
|
163 | + } |
|
164 | + |
|
165 | + $output .= "Done processing email Queue at ". date('H:i:s') .".\n"; |
|
166 | + |
|
167 | + return $output; |
|
168 | + } |
|
169 | + |
|
170 | + //-------------------------------------------------------------------- |
|
171 | 171 | |
172 | 172 | } |
@@ -1,41 +1,41 @@ |
||
1 | 1 | <?php namespace Myth\Mail; |
2 | 2 | /** |
3 | - * Sprint |
|
4 | - * |
|
5 | - * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | - * |
|
7 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | - * of this software and associated documentation files (the "Software"), to deal |
|
9 | - * in the Software without restriction, including without limitation the rights |
|
10 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | - * copies of the Software, and to permit persons to whom the Software is |
|
12 | - * furnished to do so, subject to the following conditions: |
|
13 | - * |
|
14 | - * The above copyright notice and this permission notice shall be included in |
|
15 | - * all copies or substantial portions of the Software. |
|
16 | - * |
|
17 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | - * THE SOFTWARE. |
|
24 | - * |
|
25 | - * @package Sprint |
|
26 | - * @author Lonnie Ezell |
|
27 | - * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | - * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | - * @link http://sprintphp.com |
|
30 | - * @since Version 1.0 |
|
31 | - */ |
|
3 | + * Sprint |
|
4 | + * |
|
5 | + * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | + * |
|
7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | + * of this software and associated documentation files (the "Software"), to deal |
|
9 | + * in the Software without restriction, including without limitation the rights |
|
10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | + * copies of the Software, and to permit persons to whom the Software is |
|
12 | + * furnished to do so, subject to the following conditions: |
|
13 | + * |
|
14 | + * The above copyright notice and this permission notice shall be included in |
|
15 | + * all copies or substantial portions of the Software. |
|
16 | + * |
|
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | + * THE SOFTWARE. |
|
24 | + * |
|
25 | + * @package Sprint |
|
26 | + * @author Lonnie Ezell |
|
27 | + * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | + * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | + * @link http://sprintphp.com |
|
30 | + * @since Version 1.0 |
|
31 | + */ |
|
32 | 32 | |
33 | 33 | use Myth\Models\CIDbModel; |
34 | 34 | |
35 | 35 | class Queue extends CIDbModel { |
36 | 36 | |
37 | - protected $table_name = 'mail_queue'; |
|
37 | + protected $table_name = 'mail_queue'; |
|
38 | 38 | |
39 | - protected $set_created = false; |
|
40 | - protected $set_modified = false; |
|
39 | + protected $set_created = false; |
|
40 | + protected $set_modified = false; |
|
41 | 41 | } |
@@ -43,644 +43,644 @@ |
||
43 | 43 | class Route |
44 | 44 | { |
45 | 45 | |
46 | - // Our routes, ripe for the picking. |
|
47 | - public $routes = array(); |
|
48 | - |
|
49 | - // Holds key/value pairs of named routes |
|
50 | - public static $names = array(); |
|
51 | - |
|
52 | - // Used for grouping routes together. |
|
53 | - public $group = null; |
|
54 | - |
|
55 | - // Holds the 'areas' of the site. |
|
56 | - public static $areas = array(); |
|
57 | - |
|
58 | - // The default controller to use in case |
|
59 | - // 'default_controller' is not in the routes file. |
|
60 | - protected $default_home = 'home'; |
|
61 | - |
|
62 | - // The default constraint to use in route building |
|
63 | - protected $default_constraint = 'any'; |
|
64 | - |
|
65 | - protected $constraints = [ |
|
66 | - 'any' => '(:any)', |
|
67 | - 'num' => '(:num)', |
|
68 | - 'id' => '(:num)', |
|
69 | - 'name' => "([a-zA-Z']+)" |
|
70 | - ]; |
|
71 | - |
|
72 | - protected $current_subdomain = null; |
|
73 | - |
|
74 | - //-------------------------------------------------------------------- |
|
75 | - |
|
76 | - /** |
|
77 | - * Combines the routes that we've defined with the Route class with the |
|
78 | - * routes passed in. This is intended to be used after all routes have been |
|
79 | - * defined to merge CI's default $route array with our routes. |
|
80 | - * |
|
81 | - * Example: |
|
82 | - * $route['default_controller'] = 'home'; |
|
83 | - * Route::resource('posts'); |
|
84 | - * $route = Route::map($route); |
|
85 | - * |
|
86 | - * @param array $routes |
|
87 | - * @internal param array $route The array to merge |
|
88 | - * @return array The merge route array. |
|
89 | - */ |
|
90 | - public function map($routes = array()) |
|
91 | - { |
|
92 | - $controller = isset($routes['default_controller']) ? $routes['default_controller'] : $this->default_home; |
|
93 | - |
|
94 | - $routes = array_merge($routes, $this->routes); |
|
95 | - |
|
96 | - foreach ($routes as $from => $to) { |
|
97 | - $routes[$from] = str_ireplace('{default_controller}', $controller, $to); |
|
98 | - } |
|
99 | - |
|
100 | - return $routes; |
|
101 | - } |
|
102 | - |
|
103 | - //-------------------------------------------------------------------- |
|
104 | - |
|
105 | - /** |
|
106 | - * A single point to the basic routing. Can be used in place of CI's $route |
|
107 | - * array if desired. Used internally by many of the methods. |
|
108 | - * |
|
109 | - * Available options are currently: |
|
110 | - * 'as' - remembers the route via a name that can be called outside of it. |
|
111 | - * 'offset' - Offsets and parameters ($1, $2, etc) in routes by the specified amount. |
|
112 | - * Useful while doing versioning of API's, etc. |
|
113 | - * |
|
114 | - * Example: |
|
115 | - * $route->any('news', 'posts/index'); |
|
116 | - * |
|
117 | - * @param string $from |
|
118 | - * @param string $to |
|
119 | - * @param array $options |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public function any($from, $to, $options = array()) |
|
123 | - { |
|
124 | - $this->create($from, $to, $options); |
|
125 | - } |
|
126 | - |
|
127 | - //-------------------------------------------------------------------- |
|
128 | - |
|
129 | - /** |
|
130 | - * Sets the default constraint to be used in the system. Typically |
|
131 | - * for use with the 'resources' method. |
|
132 | - * |
|
133 | - * @param $constraint |
|
134 | - */ |
|
135 | - public function setDefaultConstraint($constraint) |
|
136 | - { |
|
137 | - if (array_key_exists($constraint, $this->constraints)) { |
|
138 | - $this->default_constraint = $constraint; |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - //-------------------------------------------------------------------- |
|
143 | - |
|
144 | - /** |
|
145 | - * Registers a new constraint to be used internally. Useful for creating |
|
146 | - * very specific regex patterns, or simply to allow your routes to be |
|
147 | - * a tad more readable. |
|
148 | - * |
|
149 | - * Example: |
|
150 | - * $route->registerConstraint('hero', '(^.*)'); |
|
151 | - * |
|
152 | - * $route->any('home/{hero}', 'heroes/journey'); |
|
153 | - * |
|
154 | - * // Route then looks like: |
|
155 | - * $route['home/(^.*)'] = 'heroes/journey'; |
|
156 | - * |
|
157 | - * @param $name |
|
158 | - * @param $pattern |
|
159 | - * @param bool $overwrite |
|
160 | - */ |
|
161 | - public function registerConstraint($name, $pattern, $overwrite = false) |
|
162 | - { |
|
163 | - // Ensure consistency |
|
164 | - $name = trim($name, '{} '); |
|
165 | - $pattern = '(' . trim($pattern, '() ') . ')'; |
|
166 | - |
|
167 | - // Not here? Add it and leave... |
|
168 | - if (! array_key_exists($name, $this->constraints)) { |
|
169 | - $this->constraints[$name] = $pattern; |
|
170 | - |
|
171 | - return; |
|
172 | - } |
|
173 | - |
|
174 | - // Here? Then it exists. Should we overwrite it? |
|
175 | - if ($overwrite) { |
|
176 | - $this->constraints[$name] = $pattern; |
|
177 | - } |
|
178 | - } |
|
179 | - |
|
180 | - //-------------------------------------------------------------------- |
|
181 | - |
|
182 | - //-------------------------------------------------------------------- |
|
183 | - // Named Routes |
|
184 | - //-------------------------------------------------------------------- |
|
185 | - |
|
186 | - /** |
|
187 | - * Returns the value of a named route. Useful for getting named |
|
188 | - * routes for use while building with site_url() or in templates |
|
189 | - * where you don't need to instantiate the route class. |
|
190 | - * |
|
191 | - * Example: |
|
192 | - * $route->any('news', 'posts/index', ['as' => 'blog']); |
|
193 | - * |
|
194 | - * // Returns http://mysite.com/news |
|
195 | - * site_url( Route::named('blog') ); |
|
196 | - * |
|
197 | - * @param [type] $name [description] |
|
198 | - * @return [type] [description] |
|
199 | - */ |
|
200 | - public static function named($name) |
|
201 | - { |
|
202 | - if (isset(self::$names[$name])) { |
|
203 | - return self::$names[$name]; |
|
204 | - } |
|
205 | - |
|
206 | - return null; |
|
207 | - } |
|
208 | - |
|
209 | - //-------------------------------------------------------------------- |
|
210 | - |
|
211 | - //-------------------------------------------------------------------- |
|
212 | - // Grouping Routes |
|
213 | - //-------------------------------------------------------------------- |
|
214 | - |
|
215 | - /** |
|
216 | - * Group a series of routes under a single URL segment. This is handy |
|
217 | - * for grouping items into an admin area, like: |
|
218 | - * |
|
219 | - * Example: |
|
220 | - * $route->group('admin', function() { |
|
221 | - * $route->resources('users'); |
|
222 | - * }); |
|
223 | - * |
|
224 | - * @param string $name The name to group/prefix the routes with. |
|
225 | - * @param \Closure $callback An anonymous function that allows you route inside of this group. |
|
226 | - * @return void |
|
227 | - */ |
|
228 | - public function group($name, \Closure $callback) |
|
229 | - { |
|
230 | - $old_group = $this->group; |
|
231 | - |
|
232 | - // To register a route, we'll set a flag so that our router |
|
233 | - // so it will see the groupname. |
|
234 | - $this->group = ltrim($old_group . '/' . $name, '/'); |
|
235 | - |
|
236 | - call_user_func($callback); |
|
237 | - |
|
238 | - // Make sure to clear the group name so we don't accidentally |
|
239 | - // group any ones we didn't want to. |
|
240 | - $this->group = $old_group; |
|
241 | - } |
|
242 | - |
|
243 | - //-------------------------------------------------------------------- |
|
244 | - |
|
245 | - //-------------------------------------------------------------------- |
|
246 | - // HTTP Verb-based routing |
|
247 | - //-------------------------------------------------------------------- |
|
248 | - // Routing works here because, as the routes config file is read in, |
|
249 | - // the various HTTP verb-based routes will only be added to the in-memory |
|
250 | - // routes if it is a call that should respond to that verb. |
|
251 | - // |
|
252 | - // The options array is typically used to pass in an 'as' or var, but may |
|
253 | - // be expanded in the future. See the docblock for 'any' method above for |
|
254 | - // current list of globally available options. |
|
255 | - // |
|
256 | - |
|
257 | - /** |
|
258 | - * Specifies a single route to match for multiple HTTP Verbs. |
|
259 | - * |
|
260 | - * Example: |
|
261 | - * $route->match( ['get', 'post'], 'users/(:num)', 'users/$1); |
|
262 | - * |
|
263 | - * @param array $verbs |
|
264 | - * @param $from |
|
265 | - * @param $to |
|
266 | - * @param array $options |
|
267 | - */ |
|
268 | - public function match($verbs = [], $from, $to, $options = []) |
|
269 | - { |
|
270 | - foreach ($verbs as $verb) { |
|
271 | - $verb = strtolower($verb); |
|
272 | - |
|
273 | - $this->{$verb}($from, $to, $options); |
|
274 | - } |
|
275 | - } |
|
276 | - |
|
277 | - //-------------------------------------------------------------------- |
|
278 | - |
|
279 | - /** |
|
280 | - * Specifies a route that is only available to GET requests. |
|
281 | - * |
|
282 | - * @param $from |
|
283 | - * @param $to |
|
284 | - * @param array $options |
|
285 | - */ |
|
286 | - public function get($from, $to, $options = []) |
|
287 | - { |
|
288 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'GET') { |
|
289 | - $this->create($from, $to, $options); |
|
290 | - } |
|
291 | - } |
|
292 | - |
|
293 | - //-------------------------------------------------------------------- |
|
294 | - |
|
295 | - /** |
|
296 | - * Specifies a route that is only available to POST requests. |
|
297 | - * |
|
298 | - * @param $from |
|
299 | - * @param $to |
|
300 | - * @param array $options |
|
301 | - */ |
|
302 | - public function post($from, $to, $options = []) |
|
303 | - { |
|
304 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') { |
|
305 | - $this->create($from, $to, $options); |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - //-------------------------------------------------------------------- |
|
310 | - |
|
311 | - /** |
|
312 | - * Specifies a route that is only available to PUT requests. |
|
313 | - * |
|
314 | - * @param $from |
|
315 | - * @param $to |
|
316 | - * @param array $options |
|
317 | - */ |
|
318 | - public function put($from, $to, $options = []) |
|
319 | - { |
|
320 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'PUT') { |
|
321 | - $this->create($from, $to, $options); |
|
322 | - } |
|
323 | - } |
|
324 | - |
|
325 | - //-------------------------------------------------------------------- |
|
326 | - |
|
327 | - /** |
|
328 | - * Specifies a route that is only available to DELETE requests. |
|
329 | - * |
|
330 | - * @param $from |
|
331 | - * @param $to |
|
332 | - * @param array $options |
|
333 | - */ |
|
334 | - public function delete($from, $to, $options = []) |
|
335 | - { |
|
336 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'DELETE') { |
|
337 | - $this->create($from, $to, $options); |
|
338 | - } |
|
339 | - } |
|
340 | - |
|
341 | - //-------------------------------------------------------------------- |
|
342 | - |
|
343 | - /** |
|
344 | - * Specifies a route that is only available to HEAD requests. |
|
345 | - * |
|
346 | - * @param $from |
|
347 | - * @param $to |
|
348 | - * @param array $options |
|
349 | - */ |
|
350 | - public function head($from, $to, $options = []) |
|
351 | - { |
|
352 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'HEAD') { |
|
353 | - $this->create($from, $to, $options); |
|
354 | - } |
|
355 | - } |
|
356 | - |
|
357 | - //-------------------------------------------------------------------- |
|
358 | - |
|
359 | - /** |
|
360 | - * Specifies a route that is only available to PATCH requests. |
|
361 | - * |
|
362 | - * @param $from |
|
363 | - * @param $to |
|
364 | - * @param array $options |
|
365 | - */ |
|
366 | - public function patch($from, $to, $options = []) |
|
367 | - { |
|
368 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'PATCH') { |
|
369 | - $this->create($from, $to, $options); |
|
370 | - } |
|
371 | - } |
|
372 | - |
|
373 | - //-------------------------------------------------------------------- |
|
374 | - |
|
375 | - /** |
|
376 | - * Specifies a route that is only available to OPTIONS requests. |
|
377 | - * |
|
378 | - * @param $from |
|
379 | - * @param $to |
|
380 | - * @param array $options |
|
381 | - */ |
|
382 | - public function options($from, $to, $options = []) |
|
383 | - { |
|
384 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') { |
|
385 | - $this->create($from, $to, $options); |
|
386 | - } |
|
387 | - } |
|
388 | - |
|
389 | - //-------------------------------------------------------------------- |
|
390 | - |
|
391 | - /** |
|
392 | - * Creates a collections of HTTP-verb based routes for a controller. |
|
393 | - * |
|
394 | - * Possible Options: |
|
395 | - * 'controller' - Customize the name of the controller used in the 'to' route |
|
396 | - * 'module' - Prepend a module name to the generate 'to' routes |
|
397 | - * 'constraint' - The regex used by the Router. Defaults to '(:any)' |
|
398 | - * |
|
399 | - * Example: |
|
400 | - * $route->resources('photos'); |
|
401 | - * |
|
402 | - * // Generates the following routes: |
|
403 | - * HTTP Verb | Path | Action | Used for... |
|
404 | - * ----------+-------------+---------------+----------------- |
|
405 | - * GET /photos index display a list of photos |
|
406 | - * GET /photos/new creation_form return an HTML form for creating a new photo |
|
407 | - * GET /photos/{id} show display a specific photo |
|
408 | - * GET /photos/{id}/edit editing_form return an HTML form for editing the photo |
|
409 | - * POST /photos create create a new photo |
|
410 | - * PUT /photos/{id} update update an existing photo |
|
411 | - * DELETE /photos/{id}/delete delete delete an existing photo |
|
412 | - * |
|
413 | - * @param string $name The name of the controller to route to. |
|
414 | - * @param array $options An list of possible ways to customize the routing. |
|
415 | - */ |
|
416 | - public function resources($name, $options = []) |
|
417 | - { |
|
418 | - // In order to allow customization of the route the |
|
419 | - // resources are sent to, we need to have a new name |
|
420 | - // to store the values in. |
|
421 | - $new_name = $name; |
|
422 | - |
|
423 | - // If a new controller is specified, then we replace the |
|
424 | - // $name value with the name of the new controller. |
|
425 | - if (isset($options['controller'])) { |
|
426 | - $new_name = $options['controller']; |
|
427 | - } |
|
428 | - |
|
429 | - // If a new module was specified, simply put that path |
|
430 | - // in front of the controller. |
|
431 | - if (isset($options['module'])) { |
|
432 | - $new_name = $options['module'] . '/' . $new_name; |
|
433 | - } |
|
434 | - |
|
435 | - // In order to allow customization of allowed id values |
|
436 | - // we need someplace to store them. |
|
437 | - $id = isset($this->constraints[$this->default_constraint]) ? $this->constraints[$this->default_constraint] : |
|
438 | - '(:any)'; |
|
439 | - |
|
440 | - if (isset($options['constraint'])) { |
|
441 | - $id = $options['constraint']; |
|
442 | - } |
|
443 | - |
|
444 | - $this->get($name, $new_name . '/list_all', $options); |
|
445 | - $this->get($name . '/' . $id, $new_name . '/show/$1', $options); |
|
446 | - $this->post($name, $new_name . '/create', $options); |
|
447 | - $this->put($name . '/' . $id, $new_name . '/update/$1', $options); |
|
448 | - $this->delete($name . '/' . $id, $new_name . '/delete/$1', $options); |
|
449 | - $this->options($name, $new_name . '/index', $options); |
|
450 | - } |
|
451 | - |
|
452 | - //-------------------------------------------------------------------- |
|
453 | - |
|
454 | - /** |
|
455 | - * Lets the system know about different 'areas' within the site, like |
|
456 | - * the admin area, that maps to certain controllers. |
|
457 | - * |
|
458 | - * @param string $area The name of the area. |
|
459 | - * @param string $controller The controller name to look for. |
|
460 | - * @param $options |
|
461 | - */ |
|
462 | - public function area($area, $controller = null, $options = []) |
|
463 | - { |
|
464 | - // No controller? Match the area name. |
|
465 | - $controller = is_null($controller) ? $area : $controller; |
|
466 | - |
|
467 | - // Save the area so we can recognize it later. |
|
468 | - self::$areas[$area] = $controller; |
|
469 | - |
|
470 | - // Create routes for this area. |
|
471 | - $this->create($area . '/(:any)/(:any)/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3/$4/$5', $options); |
|
472 | - $this->create($area . '/(:any)/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3/$4', $options); |
|
473 | - $this->create($area . '/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3', $options); |
|
474 | - $this->create($area . '/(:any)/(:any)', '$1/' . $controller . '/$2', $options); |
|
475 | - $this->create($area . '/(:any)', '$1/' . $controller, $options); |
|
476 | - } |
|
477 | - |
|
478 | - //-------------------------------------------------------------------- |
|
479 | - |
|
480 | - /** |
|
481 | - * Returns the name of the area based on the controller name. |
|
482 | - * |
|
483 | - * @param string $controller The name of the controller |
|
484 | - * @return string The name of the corresponding area |
|
485 | - */ |
|
486 | - public static function getAreaName($controller) |
|
487 | - { |
|
488 | - foreach (self::$areas as $area => $cont) { |
|
489 | - if ($controller == $cont) { |
|
490 | - return $area; |
|
491 | - } |
|
492 | - } |
|
493 | - |
|
494 | - return null; |
|
495 | - } |
|
496 | - |
|
497 | - //-------------------------------------------------------------------- |
|
498 | - |
|
499 | - /** |
|
500 | - * Limits the routes to a specified ENVIRONMENT or they won't run. |
|
501 | - * |
|
502 | - * @param $env |
|
503 | - * @param callable $callback |
|
504 | - * |
|
505 | - * @return bool|null |
|
506 | - */ |
|
507 | - public function environment($env, \Closure $callback) |
|
508 | - { |
|
509 | - if (ENVIRONMENT == $env) |
|
510 | - { |
|
511 | - call_user_func($callback); |
|
512 | - return true; |
|
513 | - } |
|
514 | - |
|
515 | - return null; |
|
516 | - } |
|
517 | - |
|
518 | - //-------------------------------------------------------------------- |
|
519 | - |
|
520 | - |
|
521 | - |
|
522 | - /** |
|
523 | - * Allows you to easily block access to any number of routes by setting |
|
524 | - * that route to an empty path (''). |
|
525 | - * |
|
526 | - * Example: |
|
527 | - * Route::block('posts', 'photos/(:num)'); |
|
528 | - * |
|
529 | - * // Same as... |
|
530 | - * $route['posts'] = ''; |
|
531 | - * $route['photos/(:num)'] = ''; |
|
532 | - */ |
|
533 | - public function block() |
|
534 | - { |
|
535 | - $paths = func_get_args(); |
|
536 | - |
|
537 | - if (! is_array($paths) || ! count($paths)) { |
|
538 | - return; |
|
539 | - } |
|
540 | - |
|
541 | - foreach ($paths as $path) { |
|
542 | - $this->create($path, ''); |
|
543 | - } |
|
544 | - } |
|
545 | - |
|
546 | - //-------------------------------------------------------------------- |
|
547 | - |
|
548 | - /** |
|
549 | - * Empties all named and un-named routes from the system. |
|
550 | - * |
|
551 | - * @return void |
|
552 | - */ |
|
553 | - public function reset() |
|
554 | - { |
|
555 | - $this->routes = array(); |
|
556 | - $this->names = array(); |
|
557 | - $this->group = null; |
|
558 | - self::$areas = array(); |
|
559 | - } |
|
560 | - |
|
561 | - //-------------------------------------------------------------------- |
|
562 | - |
|
563 | - //-------------------------------------------------------------------- |
|
564 | - // Private Methods |
|
565 | - //-------------------------------------------------------------------- |
|
566 | - |
|
567 | - /** |
|
568 | - * Does the heavy lifting of creating an actual route. You must specify |
|
569 | - * the request method(s) that this route will work for. They can be separated |
|
570 | - * by a pipe character "|" if there is more than one. |
|
571 | - * |
|
572 | - * @param string $from |
|
573 | - * @param array $to |
|
574 | - * @param array $options |
|
575 | - * |
|
576 | - * @return array The built route. |
|
577 | - */ |
|
578 | - private function create($from, $to, $options = array()) |
|
579 | - { |
|
580 | - $prefix = is_null($this->group) ? '' : $this->group . '/'; |
|
581 | - |
|
582 | - $from = $prefix . $from; |
|
583 | - |
|
584 | - // Are we saving the name for this one? |
|
585 | - if (isset($options['as']) && !empty($options['as'])) { |
|
586 | - self::$names[$options['as']] = $from; |
|
587 | - } |
|
588 | - |
|
589 | - // Limiting to subdomains? |
|
590 | - if (isset($options['subdomain']) && !empty($options['subdomain'])) { |
|
591 | - // If we don't match the current subdomain, then |
|
592 | - // we don't need to add the route. |
|
593 | - if (!$this->checkSubdomains($options['subdomain'])) { |
|
594 | - return; |
|
595 | - } |
|
596 | - } |
|
597 | - |
|
598 | - // Are we offsetting the parameters? |
|
599 | - // If so, take care of them here in one |
|
600 | - // fell swoop. |
|
601 | - if (isset($options['offset'])) { |
|
602 | - // Get a constant string to work with. |
|
603 | - $to = preg_replace('/(\$\d+)/', '$X', $to); |
|
604 | - |
|
605 | - for ($i = (int)$options['offset'] + 1; $i < (int)$options['offset'] + 7; $i ++) { |
|
606 | - $to = preg_replace_callback( |
|
607 | - '/\$X/', |
|
608 | - function ($m) use ($i) { |
|
609 | - return '$' . $i; |
|
610 | - }, |
|
611 | - $to, |
|
612 | - 1 |
|
613 | - ); |
|
614 | - } |
|
615 | - } |
|
616 | - |
|
617 | - // Convert any custom constraints to the CI/pattern equivalent |
|
618 | - foreach ($this->constraints as $name => $pattern) { |
|
619 | - $from = str_replace('{' . $name . '}', $pattern, $from); |
|
620 | - } |
|
621 | - |
|
622 | - $this->routes[$from] = $to; |
|
623 | - } |
|
624 | - |
|
625 | - //-------------------------------------------------------------------- |
|
626 | - |
|
627 | - /** |
|
628 | - * Compares the subdomain(s) passed in against the current subdomain |
|
629 | - * on this page request. |
|
630 | - * |
|
631 | - * @param $subdomains |
|
632 | - * @return bool |
|
633 | - */ |
|
634 | - private function checkSubdomains($subdomains) |
|
635 | - { |
|
636 | - if (is_null($this->current_subdomain)) { |
|
637 | - $this->determineCurrentSubdomain(); |
|
638 | - } |
|
639 | - |
|
640 | - if (!is_array($subdomains)) { |
|
641 | - $subdomains = array($subdomains); |
|
642 | - } |
|
643 | - |
|
644 | - $matched = false; |
|
645 | - |
|
646 | - array_walk( |
|
647 | - $subdomains, |
|
648 | - function ($subdomain) use (&$matched) { |
|
649 | - if ($subdomain == $this->current_subdomain || $subdomain == '*') { |
|
650 | - $matched = true; |
|
651 | - } |
|
652 | - } |
|
653 | - ); |
|
654 | - |
|
655 | - return $matched; |
|
656 | - } |
|
657 | - |
|
658 | - //-------------------------------------------------------------------- |
|
659 | - |
|
660 | - /** |
|
661 | - * Examines the HTTP_HOST to get a best match for the subdomain. It |
|
662 | - * won't be perfect, but should work for our needs. |
|
663 | - */ |
|
664 | - private function determineCurrentSubdomain() |
|
665 | - { |
|
666 | - $parsedUrl = parse_url($_SERVER['HTTP_HOST']); |
|
667 | - |
|
668 | - $host = explode('.', $parsedUrl['host']); |
|
669 | - |
|
670 | - // If we only have 2 parts, then we don't have a subdomain. |
|
671 | - // This won't be totally accurate, since URL's like example.co.uk |
|
672 | - // would still pass, but it helps to separate the chaff... |
|
673 | - if (!is_array($host) || count($host) == 2) { |
|
674 | - // Set it to false so we don't make it back here again. |
|
675 | - $this->current_subdomain = false; |
|
676 | - return; |
|
677 | - } |
|
678 | - |
|
679 | - // Now, we'll simply take the first element of the array. This should |
|
680 | - // be fine even in cases like example.co.uk, since they won't be looking |
|
681 | - // for 'example' when they try to match the subdomain, in most all cases. |
|
682 | - $this->current_subdomain = array_shift($host); |
|
683 | - } |
|
684 | - //-------------------------------------------------------------------- |
|
46 | + // Our routes, ripe for the picking. |
|
47 | + public $routes = array(); |
|
48 | + |
|
49 | + // Holds key/value pairs of named routes |
|
50 | + public static $names = array(); |
|
51 | + |
|
52 | + // Used for grouping routes together. |
|
53 | + public $group = null; |
|
54 | + |
|
55 | + // Holds the 'areas' of the site. |
|
56 | + public static $areas = array(); |
|
57 | + |
|
58 | + // The default controller to use in case |
|
59 | + // 'default_controller' is not in the routes file. |
|
60 | + protected $default_home = 'home'; |
|
61 | + |
|
62 | + // The default constraint to use in route building |
|
63 | + protected $default_constraint = 'any'; |
|
64 | + |
|
65 | + protected $constraints = [ |
|
66 | + 'any' => '(:any)', |
|
67 | + 'num' => '(:num)', |
|
68 | + 'id' => '(:num)', |
|
69 | + 'name' => "([a-zA-Z']+)" |
|
70 | + ]; |
|
71 | + |
|
72 | + protected $current_subdomain = null; |
|
73 | + |
|
74 | + //-------------------------------------------------------------------- |
|
75 | + |
|
76 | + /** |
|
77 | + * Combines the routes that we've defined with the Route class with the |
|
78 | + * routes passed in. This is intended to be used after all routes have been |
|
79 | + * defined to merge CI's default $route array with our routes. |
|
80 | + * |
|
81 | + * Example: |
|
82 | + * $route['default_controller'] = 'home'; |
|
83 | + * Route::resource('posts'); |
|
84 | + * $route = Route::map($route); |
|
85 | + * |
|
86 | + * @param array $routes |
|
87 | + * @internal param array $route The array to merge |
|
88 | + * @return array The merge route array. |
|
89 | + */ |
|
90 | + public function map($routes = array()) |
|
91 | + { |
|
92 | + $controller = isset($routes['default_controller']) ? $routes['default_controller'] : $this->default_home; |
|
93 | + |
|
94 | + $routes = array_merge($routes, $this->routes); |
|
95 | + |
|
96 | + foreach ($routes as $from => $to) { |
|
97 | + $routes[$from] = str_ireplace('{default_controller}', $controller, $to); |
|
98 | + } |
|
99 | + |
|
100 | + return $routes; |
|
101 | + } |
|
102 | + |
|
103 | + //-------------------------------------------------------------------- |
|
104 | + |
|
105 | + /** |
|
106 | + * A single point to the basic routing. Can be used in place of CI's $route |
|
107 | + * array if desired. Used internally by many of the methods. |
|
108 | + * |
|
109 | + * Available options are currently: |
|
110 | + * 'as' - remembers the route via a name that can be called outside of it. |
|
111 | + * 'offset' - Offsets and parameters ($1, $2, etc) in routes by the specified amount. |
|
112 | + * Useful while doing versioning of API's, etc. |
|
113 | + * |
|
114 | + * Example: |
|
115 | + * $route->any('news', 'posts/index'); |
|
116 | + * |
|
117 | + * @param string $from |
|
118 | + * @param string $to |
|
119 | + * @param array $options |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public function any($from, $to, $options = array()) |
|
123 | + { |
|
124 | + $this->create($from, $to, $options); |
|
125 | + } |
|
126 | + |
|
127 | + //-------------------------------------------------------------------- |
|
128 | + |
|
129 | + /** |
|
130 | + * Sets the default constraint to be used in the system. Typically |
|
131 | + * for use with the 'resources' method. |
|
132 | + * |
|
133 | + * @param $constraint |
|
134 | + */ |
|
135 | + public function setDefaultConstraint($constraint) |
|
136 | + { |
|
137 | + if (array_key_exists($constraint, $this->constraints)) { |
|
138 | + $this->default_constraint = $constraint; |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + //-------------------------------------------------------------------- |
|
143 | + |
|
144 | + /** |
|
145 | + * Registers a new constraint to be used internally. Useful for creating |
|
146 | + * very specific regex patterns, or simply to allow your routes to be |
|
147 | + * a tad more readable. |
|
148 | + * |
|
149 | + * Example: |
|
150 | + * $route->registerConstraint('hero', '(^.*)'); |
|
151 | + * |
|
152 | + * $route->any('home/{hero}', 'heroes/journey'); |
|
153 | + * |
|
154 | + * // Route then looks like: |
|
155 | + * $route['home/(^.*)'] = 'heroes/journey'; |
|
156 | + * |
|
157 | + * @param $name |
|
158 | + * @param $pattern |
|
159 | + * @param bool $overwrite |
|
160 | + */ |
|
161 | + public function registerConstraint($name, $pattern, $overwrite = false) |
|
162 | + { |
|
163 | + // Ensure consistency |
|
164 | + $name = trim($name, '{} '); |
|
165 | + $pattern = '(' . trim($pattern, '() ') . ')'; |
|
166 | + |
|
167 | + // Not here? Add it and leave... |
|
168 | + if (! array_key_exists($name, $this->constraints)) { |
|
169 | + $this->constraints[$name] = $pattern; |
|
170 | + |
|
171 | + return; |
|
172 | + } |
|
173 | + |
|
174 | + // Here? Then it exists. Should we overwrite it? |
|
175 | + if ($overwrite) { |
|
176 | + $this->constraints[$name] = $pattern; |
|
177 | + } |
|
178 | + } |
|
179 | + |
|
180 | + //-------------------------------------------------------------------- |
|
181 | + |
|
182 | + //-------------------------------------------------------------------- |
|
183 | + // Named Routes |
|
184 | + //-------------------------------------------------------------------- |
|
185 | + |
|
186 | + /** |
|
187 | + * Returns the value of a named route. Useful for getting named |
|
188 | + * routes for use while building with site_url() or in templates |
|
189 | + * where you don't need to instantiate the route class. |
|
190 | + * |
|
191 | + * Example: |
|
192 | + * $route->any('news', 'posts/index', ['as' => 'blog']); |
|
193 | + * |
|
194 | + * // Returns http://mysite.com/news |
|
195 | + * site_url( Route::named('blog') ); |
|
196 | + * |
|
197 | + * @param [type] $name [description] |
|
198 | + * @return [type] [description] |
|
199 | + */ |
|
200 | + public static function named($name) |
|
201 | + { |
|
202 | + if (isset(self::$names[$name])) { |
|
203 | + return self::$names[$name]; |
|
204 | + } |
|
205 | + |
|
206 | + return null; |
|
207 | + } |
|
208 | + |
|
209 | + //-------------------------------------------------------------------- |
|
210 | + |
|
211 | + //-------------------------------------------------------------------- |
|
212 | + // Grouping Routes |
|
213 | + //-------------------------------------------------------------------- |
|
214 | + |
|
215 | + /** |
|
216 | + * Group a series of routes under a single URL segment. This is handy |
|
217 | + * for grouping items into an admin area, like: |
|
218 | + * |
|
219 | + * Example: |
|
220 | + * $route->group('admin', function() { |
|
221 | + * $route->resources('users'); |
|
222 | + * }); |
|
223 | + * |
|
224 | + * @param string $name The name to group/prefix the routes with. |
|
225 | + * @param \Closure $callback An anonymous function that allows you route inside of this group. |
|
226 | + * @return void |
|
227 | + */ |
|
228 | + public function group($name, \Closure $callback) |
|
229 | + { |
|
230 | + $old_group = $this->group; |
|
231 | + |
|
232 | + // To register a route, we'll set a flag so that our router |
|
233 | + // so it will see the groupname. |
|
234 | + $this->group = ltrim($old_group . '/' . $name, '/'); |
|
235 | + |
|
236 | + call_user_func($callback); |
|
237 | + |
|
238 | + // Make sure to clear the group name so we don't accidentally |
|
239 | + // group any ones we didn't want to. |
|
240 | + $this->group = $old_group; |
|
241 | + } |
|
242 | + |
|
243 | + //-------------------------------------------------------------------- |
|
244 | + |
|
245 | + //-------------------------------------------------------------------- |
|
246 | + // HTTP Verb-based routing |
|
247 | + //-------------------------------------------------------------------- |
|
248 | + // Routing works here because, as the routes config file is read in, |
|
249 | + // the various HTTP verb-based routes will only be added to the in-memory |
|
250 | + // routes if it is a call that should respond to that verb. |
|
251 | + // |
|
252 | + // The options array is typically used to pass in an 'as' or var, but may |
|
253 | + // be expanded in the future. See the docblock for 'any' method above for |
|
254 | + // current list of globally available options. |
|
255 | + // |
|
256 | + |
|
257 | + /** |
|
258 | + * Specifies a single route to match for multiple HTTP Verbs. |
|
259 | + * |
|
260 | + * Example: |
|
261 | + * $route->match( ['get', 'post'], 'users/(:num)', 'users/$1); |
|
262 | + * |
|
263 | + * @param array $verbs |
|
264 | + * @param $from |
|
265 | + * @param $to |
|
266 | + * @param array $options |
|
267 | + */ |
|
268 | + public function match($verbs = [], $from, $to, $options = []) |
|
269 | + { |
|
270 | + foreach ($verbs as $verb) { |
|
271 | + $verb = strtolower($verb); |
|
272 | + |
|
273 | + $this->{$verb}($from, $to, $options); |
|
274 | + } |
|
275 | + } |
|
276 | + |
|
277 | + //-------------------------------------------------------------------- |
|
278 | + |
|
279 | + /** |
|
280 | + * Specifies a route that is only available to GET requests. |
|
281 | + * |
|
282 | + * @param $from |
|
283 | + * @param $to |
|
284 | + * @param array $options |
|
285 | + */ |
|
286 | + public function get($from, $to, $options = []) |
|
287 | + { |
|
288 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'GET') { |
|
289 | + $this->create($from, $to, $options); |
|
290 | + } |
|
291 | + } |
|
292 | + |
|
293 | + //-------------------------------------------------------------------- |
|
294 | + |
|
295 | + /** |
|
296 | + * Specifies a route that is only available to POST requests. |
|
297 | + * |
|
298 | + * @param $from |
|
299 | + * @param $to |
|
300 | + * @param array $options |
|
301 | + */ |
|
302 | + public function post($from, $to, $options = []) |
|
303 | + { |
|
304 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') { |
|
305 | + $this->create($from, $to, $options); |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + //-------------------------------------------------------------------- |
|
310 | + |
|
311 | + /** |
|
312 | + * Specifies a route that is only available to PUT requests. |
|
313 | + * |
|
314 | + * @param $from |
|
315 | + * @param $to |
|
316 | + * @param array $options |
|
317 | + */ |
|
318 | + public function put($from, $to, $options = []) |
|
319 | + { |
|
320 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'PUT') { |
|
321 | + $this->create($from, $to, $options); |
|
322 | + } |
|
323 | + } |
|
324 | + |
|
325 | + //-------------------------------------------------------------------- |
|
326 | + |
|
327 | + /** |
|
328 | + * Specifies a route that is only available to DELETE requests. |
|
329 | + * |
|
330 | + * @param $from |
|
331 | + * @param $to |
|
332 | + * @param array $options |
|
333 | + */ |
|
334 | + public function delete($from, $to, $options = []) |
|
335 | + { |
|
336 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'DELETE') { |
|
337 | + $this->create($from, $to, $options); |
|
338 | + } |
|
339 | + } |
|
340 | + |
|
341 | + //-------------------------------------------------------------------- |
|
342 | + |
|
343 | + /** |
|
344 | + * Specifies a route that is only available to HEAD requests. |
|
345 | + * |
|
346 | + * @param $from |
|
347 | + * @param $to |
|
348 | + * @param array $options |
|
349 | + */ |
|
350 | + public function head($from, $to, $options = []) |
|
351 | + { |
|
352 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'HEAD') { |
|
353 | + $this->create($from, $to, $options); |
|
354 | + } |
|
355 | + } |
|
356 | + |
|
357 | + //-------------------------------------------------------------------- |
|
358 | + |
|
359 | + /** |
|
360 | + * Specifies a route that is only available to PATCH requests. |
|
361 | + * |
|
362 | + * @param $from |
|
363 | + * @param $to |
|
364 | + * @param array $options |
|
365 | + */ |
|
366 | + public function patch($from, $to, $options = []) |
|
367 | + { |
|
368 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'PATCH') { |
|
369 | + $this->create($from, $to, $options); |
|
370 | + } |
|
371 | + } |
|
372 | + |
|
373 | + //-------------------------------------------------------------------- |
|
374 | + |
|
375 | + /** |
|
376 | + * Specifies a route that is only available to OPTIONS requests. |
|
377 | + * |
|
378 | + * @param $from |
|
379 | + * @param $to |
|
380 | + * @param array $options |
|
381 | + */ |
|
382 | + public function options($from, $to, $options = []) |
|
383 | + { |
|
384 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') { |
|
385 | + $this->create($from, $to, $options); |
|
386 | + } |
|
387 | + } |
|
388 | + |
|
389 | + //-------------------------------------------------------------------- |
|
390 | + |
|
391 | + /** |
|
392 | + * Creates a collections of HTTP-verb based routes for a controller. |
|
393 | + * |
|
394 | + * Possible Options: |
|
395 | + * 'controller' - Customize the name of the controller used in the 'to' route |
|
396 | + * 'module' - Prepend a module name to the generate 'to' routes |
|
397 | + * 'constraint' - The regex used by the Router. Defaults to '(:any)' |
|
398 | + * |
|
399 | + * Example: |
|
400 | + * $route->resources('photos'); |
|
401 | + * |
|
402 | + * // Generates the following routes: |
|
403 | + * HTTP Verb | Path | Action | Used for... |
|
404 | + * ----------+-------------+---------------+----------------- |
|
405 | + * GET /photos index display a list of photos |
|
406 | + * GET /photos/new creation_form return an HTML form for creating a new photo |
|
407 | + * GET /photos/{id} show display a specific photo |
|
408 | + * GET /photos/{id}/edit editing_form return an HTML form for editing the photo |
|
409 | + * POST /photos create create a new photo |
|
410 | + * PUT /photos/{id} update update an existing photo |
|
411 | + * DELETE /photos/{id}/delete delete delete an existing photo |
|
412 | + * |
|
413 | + * @param string $name The name of the controller to route to. |
|
414 | + * @param array $options An list of possible ways to customize the routing. |
|
415 | + */ |
|
416 | + public function resources($name, $options = []) |
|
417 | + { |
|
418 | + // In order to allow customization of the route the |
|
419 | + // resources are sent to, we need to have a new name |
|
420 | + // to store the values in. |
|
421 | + $new_name = $name; |
|
422 | + |
|
423 | + // If a new controller is specified, then we replace the |
|
424 | + // $name value with the name of the new controller. |
|
425 | + if (isset($options['controller'])) { |
|
426 | + $new_name = $options['controller']; |
|
427 | + } |
|
428 | + |
|
429 | + // If a new module was specified, simply put that path |
|
430 | + // in front of the controller. |
|
431 | + if (isset($options['module'])) { |
|
432 | + $new_name = $options['module'] . '/' . $new_name; |
|
433 | + } |
|
434 | + |
|
435 | + // In order to allow customization of allowed id values |
|
436 | + // we need someplace to store them. |
|
437 | + $id = isset($this->constraints[$this->default_constraint]) ? $this->constraints[$this->default_constraint] : |
|
438 | + '(:any)'; |
|
439 | + |
|
440 | + if (isset($options['constraint'])) { |
|
441 | + $id = $options['constraint']; |
|
442 | + } |
|
443 | + |
|
444 | + $this->get($name, $new_name . '/list_all', $options); |
|
445 | + $this->get($name . '/' . $id, $new_name . '/show/$1', $options); |
|
446 | + $this->post($name, $new_name . '/create', $options); |
|
447 | + $this->put($name . '/' . $id, $new_name . '/update/$1', $options); |
|
448 | + $this->delete($name . '/' . $id, $new_name . '/delete/$1', $options); |
|
449 | + $this->options($name, $new_name . '/index', $options); |
|
450 | + } |
|
451 | + |
|
452 | + //-------------------------------------------------------------------- |
|
453 | + |
|
454 | + /** |
|
455 | + * Lets the system know about different 'areas' within the site, like |
|
456 | + * the admin area, that maps to certain controllers. |
|
457 | + * |
|
458 | + * @param string $area The name of the area. |
|
459 | + * @param string $controller The controller name to look for. |
|
460 | + * @param $options |
|
461 | + */ |
|
462 | + public function area($area, $controller = null, $options = []) |
|
463 | + { |
|
464 | + // No controller? Match the area name. |
|
465 | + $controller = is_null($controller) ? $area : $controller; |
|
466 | + |
|
467 | + // Save the area so we can recognize it later. |
|
468 | + self::$areas[$area] = $controller; |
|
469 | + |
|
470 | + // Create routes for this area. |
|
471 | + $this->create($area . '/(:any)/(:any)/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3/$4/$5', $options); |
|
472 | + $this->create($area . '/(:any)/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3/$4', $options); |
|
473 | + $this->create($area . '/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3', $options); |
|
474 | + $this->create($area . '/(:any)/(:any)', '$1/' . $controller . '/$2', $options); |
|
475 | + $this->create($area . '/(:any)', '$1/' . $controller, $options); |
|
476 | + } |
|
477 | + |
|
478 | + //-------------------------------------------------------------------- |
|
479 | + |
|
480 | + /** |
|
481 | + * Returns the name of the area based on the controller name. |
|
482 | + * |
|
483 | + * @param string $controller The name of the controller |
|
484 | + * @return string The name of the corresponding area |
|
485 | + */ |
|
486 | + public static function getAreaName($controller) |
|
487 | + { |
|
488 | + foreach (self::$areas as $area => $cont) { |
|
489 | + if ($controller == $cont) { |
|
490 | + return $area; |
|
491 | + } |
|
492 | + } |
|
493 | + |
|
494 | + return null; |
|
495 | + } |
|
496 | + |
|
497 | + //-------------------------------------------------------------------- |
|
498 | + |
|
499 | + /** |
|
500 | + * Limits the routes to a specified ENVIRONMENT or they won't run. |
|
501 | + * |
|
502 | + * @param $env |
|
503 | + * @param callable $callback |
|
504 | + * |
|
505 | + * @return bool|null |
|
506 | + */ |
|
507 | + public function environment($env, \Closure $callback) |
|
508 | + { |
|
509 | + if (ENVIRONMENT == $env) |
|
510 | + { |
|
511 | + call_user_func($callback); |
|
512 | + return true; |
|
513 | + } |
|
514 | + |
|
515 | + return null; |
|
516 | + } |
|
517 | + |
|
518 | + //-------------------------------------------------------------------- |
|
519 | + |
|
520 | + |
|
521 | + |
|
522 | + /** |
|
523 | + * Allows you to easily block access to any number of routes by setting |
|
524 | + * that route to an empty path (''). |
|
525 | + * |
|
526 | + * Example: |
|
527 | + * Route::block('posts', 'photos/(:num)'); |
|
528 | + * |
|
529 | + * // Same as... |
|
530 | + * $route['posts'] = ''; |
|
531 | + * $route['photos/(:num)'] = ''; |
|
532 | + */ |
|
533 | + public function block() |
|
534 | + { |
|
535 | + $paths = func_get_args(); |
|
536 | + |
|
537 | + if (! is_array($paths) || ! count($paths)) { |
|
538 | + return; |
|
539 | + } |
|
540 | + |
|
541 | + foreach ($paths as $path) { |
|
542 | + $this->create($path, ''); |
|
543 | + } |
|
544 | + } |
|
545 | + |
|
546 | + //-------------------------------------------------------------------- |
|
547 | + |
|
548 | + /** |
|
549 | + * Empties all named and un-named routes from the system. |
|
550 | + * |
|
551 | + * @return void |
|
552 | + */ |
|
553 | + public function reset() |
|
554 | + { |
|
555 | + $this->routes = array(); |
|
556 | + $this->names = array(); |
|
557 | + $this->group = null; |
|
558 | + self::$areas = array(); |
|
559 | + } |
|
560 | + |
|
561 | + //-------------------------------------------------------------------- |
|
562 | + |
|
563 | + //-------------------------------------------------------------------- |
|
564 | + // Private Methods |
|
565 | + //-------------------------------------------------------------------- |
|
566 | + |
|
567 | + /** |
|
568 | + * Does the heavy lifting of creating an actual route. You must specify |
|
569 | + * the request method(s) that this route will work for. They can be separated |
|
570 | + * by a pipe character "|" if there is more than one. |
|
571 | + * |
|
572 | + * @param string $from |
|
573 | + * @param array $to |
|
574 | + * @param array $options |
|
575 | + * |
|
576 | + * @return array The built route. |
|
577 | + */ |
|
578 | + private function create($from, $to, $options = array()) |
|
579 | + { |
|
580 | + $prefix = is_null($this->group) ? '' : $this->group . '/'; |
|
581 | + |
|
582 | + $from = $prefix . $from; |
|
583 | + |
|
584 | + // Are we saving the name for this one? |
|
585 | + if (isset($options['as']) && !empty($options['as'])) { |
|
586 | + self::$names[$options['as']] = $from; |
|
587 | + } |
|
588 | + |
|
589 | + // Limiting to subdomains? |
|
590 | + if (isset($options['subdomain']) && !empty($options['subdomain'])) { |
|
591 | + // If we don't match the current subdomain, then |
|
592 | + // we don't need to add the route. |
|
593 | + if (!$this->checkSubdomains($options['subdomain'])) { |
|
594 | + return; |
|
595 | + } |
|
596 | + } |
|
597 | + |
|
598 | + // Are we offsetting the parameters? |
|
599 | + // If so, take care of them here in one |
|
600 | + // fell swoop. |
|
601 | + if (isset($options['offset'])) { |
|
602 | + // Get a constant string to work with. |
|
603 | + $to = preg_replace('/(\$\d+)/', '$X', $to); |
|
604 | + |
|
605 | + for ($i = (int)$options['offset'] + 1; $i < (int)$options['offset'] + 7; $i ++) { |
|
606 | + $to = preg_replace_callback( |
|
607 | + '/\$X/', |
|
608 | + function ($m) use ($i) { |
|
609 | + return '$' . $i; |
|
610 | + }, |
|
611 | + $to, |
|
612 | + 1 |
|
613 | + ); |
|
614 | + } |
|
615 | + } |
|
616 | + |
|
617 | + // Convert any custom constraints to the CI/pattern equivalent |
|
618 | + foreach ($this->constraints as $name => $pattern) { |
|
619 | + $from = str_replace('{' . $name . '}', $pattern, $from); |
|
620 | + } |
|
621 | + |
|
622 | + $this->routes[$from] = $to; |
|
623 | + } |
|
624 | + |
|
625 | + //-------------------------------------------------------------------- |
|
626 | + |
|
627 | + /** |
|
628 | + * Compares the subdomain(s) passed in against the current subdomain |
|
629 | + * on this page request. |
|
630 | + * |
|
631 | + * @param $subdomains |
|
632 | + * @return bool |
|
633 | + */ |
|
634 | + private function checkSubdomains($subdomains) |
|
635 | + { |
|
636 | + if (is_null($this->current_subdomain)) { |
|
637 | + $this->determineCurrentSubdomain(); |
|
638 | + } |
|
639 | + |
|
640 | + if (!is_array($subdomains)) { |
|
641 | + $subdomains = array($subdomains); |
|
642 | + } |
|
643 | + |
|
644 | + $matched = false; |
|
645 | + |
|
646 | + array_walk( |
|
647 | + $subdomains, |
|
648 | + function ($subdomain) use (&$matched) { |
|
649 | + if ($subdomain == $this->current_subdomain || $subdomain == '*') { |
|
650 | + $matched = true; |
|
651 | + } |
|
652 | + } |
|
653 | + ); |
|
654 | + |
|
655 | + return $matched; |
|
656 | + } |
|
657 | + |
|
658 | + //-------------------------------------------------------------------- |
|
659 | + |
|
660 | + /** |
|
661 | + * Examines the HTTP_HOST to get a best match for the subdomain. It |
|
662 | + * won't be perfect, but should work for our needs. |
|
663 | + */ |
|
664 | + private function determineCurrentSubdomain() |
|
665 | + { |
|
666 | + $parsedUrl = parse_url($_SERVER['HTTP_HOST']); |
|
667 | + |
|
668 | + $host = explode('.', $parsedUrl['host']); |
|
669 | + |
|
670 | + // If we only have 2 parts, then we don't have a subdomain. |
|
671 | + // This won't be totally accurate, since URL's like example.co.uk |
|
672 | + // would still pass, but it helps to separate the chaff... |
|
673 | + if (!is_array($host) || count($host) == 2) { |
|
674 | + // Set it to false so we don't make it back here again. |
|
675 | + $this->current_subdomain = false; |
|
676 | + return; |
|
677 | + } |
|
678 | + |
|
679 | + // Now, we'll simply take the first element of the array. This should |
|
680 | + // be fine even in cases like example.co.uk, since they won't be looking |
|
681 | + // for 'example' when they try to match the subdomain, in most all cases. |
|
682 | + $this->current_subdomain = array_shift($host); |
|
683 | + } |
|
684 | + //-------------------------------------------------------------------- |
|
685 | 685 | |
686 | 686 | } |
@@ -49,143 +49,143 @@ |
||
49 | 49 | */ |
50 | 50 | class ConfigStore implements SettingsStoreInterface { |
51 | 51 | |
52 | - protected $ci; |
|
53 | - |
|
54 | - //-------------------------------------------------------------------- |
|
55 | - |
|
56 | - public function __construct( $ci=null ) |
|
57 | - { |
|
58 | - if (is_object($ci)) |
|
59 | - { |
|
60 | - $this->ci =& $ci; |
|
61 | - } |
|
62 | - else { |
|
63 | - $this->ci =& get_instance(); |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - //-------------------------------------------------------------------- |
|
68 | - |
|
69 | - /** |
|
70 | - * Inserts/Replaces a single setting item. |
|
71 | - * |
|
72 | - * @param $key |
|
73 | - * @param null $value |
|
74 | - * @param string $group |
|
75 | - */ |
|
76 | - public function save($key, $value=null, $group='app') |
|
77 | - { |
|
78 | - // Treat the 'app' group as our primary, un-sectioned |
|
79 | - // config files. |
|
80 | - if ($group != 'app') |
|
81 | - { |
|
82 | - return $this->ci->config->set_item($key, $value); |
|
83 | - } |
|
84 | - |
|
85 | - // Otherwise, we'll need to ensure a section exists |
|
86 | - if (! isset($this->ci->config->config[$group])) |
|
87 | - { |
|
88 | - $this->ci->config->config[$group] = []; |
|
89 | - } |
|
90 | - |
|
91 | - $this->ci->config->config[$group][$key] = $value; |
|
92 | - } |
|
93 | - |
|
94 | - //-------------------------------------------------------------------- |
|
95 | - |
|
96 | - /** |
|
97 | - * Retrieves a single item. If the config system doesn't have the |
|
98 | - * value loaded, yet, it will attempt to load a config file matching |
|
99 | - * the 'group' name and grab the value from that. |
|
100 | - * |
|
101 | - * @param $key |
|
102 | - * @param string $group |
|
103 | - * @return mixed |
|
104 | - */ |
|
105 | - public function get($key, $group='application') |
|
106 | - { |
|
107 | - // First, see if CI already has this key loaded. |
|
108 | - $result = $this->ci->config->item($key); |
|
109 | - |
|
110 | - // This bit will fail when the actual value is NULL |
|
111 | - // but should result in false hits infrequently. |
|
112 | - if ($result !== null) |
|
113 | - { |
|
114 | - return $result; |
|
115 | - } |
|
116 | - |
|
117 | - // Try to load the 'group' file, then try to load a |
|
118 | - // config file that matches the group name |
|
119 | - $this->ci->load->config($group, false, true); |
|
52 | + protected $ci; |
|
53 | + |
|
54 | + //-------------------------------------------------------------------- |
|
55 | + |
|
56 | + public function __construct( $ci=null ) |
|
57 | + { |
|
58 | + if (is_object($ci)) |
|
59 | + { |
|
60 | + $this->ci =& $ci; |
|
61 | + } |
|
62 | + else { |
|
63 | + $this->ci =& get_instance(); |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + //-------------------------------------------------------------------- |
|
68 | + |
|
69 | + /** |
|
70 | + * Inserts/Replaces a single setting item. |
|
71 | + * |
|
72 | + * @param $key |
|
73 | + * @param null $value |
|
74 | + * @param string $group |
|
75 | + */ |
|
76 | + public function save($key, $value=null, $group='app') |
|
77 | + { |
|
78 | + // Treat the 'app' group as our primary, un-sectioned |
|
79 | + // config files. |
|
80 | + if ($group != 'app') |
|
81 | + { |
|
82 | + return $this->ci->config->set_item($key, $value); |
|
83 | + } |
|
84 | + |
|
85 | + // Otherwise, we'll need to ensure a section exists |
|
86 | + if (! isset($this->ci->config->config[$group])) |
|
87 | + { |
|
88 | + $this->ci->config->config[$group] = []; |
|
89 | + } |
|
90 | + |
|
91 | + $this->ci->config->config[$group][$key] = $value; |
|
92 | + } |
|
93 | + |
|
94 | + //-------------------------------------------------------------------- |
|
95 | + |
|
96 | + /** |
|
97 | + * Retrieves a single item. If the config system doesn't have the |
|
98 | + * value loaded, yet, it will attempt to load a config file matching |
|
99 | + * the 'group' name and grab the value from that. |
|
100 | + * |
|
101 | + * @param $key |
|
102 | + * @param string $group |
|
103 | + * @return mixed |
|
104 | + */ |
|
105 | + public function get($key, $group='application') |
|
106 | + { |
|
107 | + // First, see if CI already has this key loaded. |
|
108 | + $result = $this->ci->config->item($key); |
|
109 | + |
|
110 | + // This bit will fail when the actual value is NULL |
|
111 | + // but should result in false hits infrequently. |
|
112 | + if ($result !== null) |
|
113 | + { |
|
114 | + return $result; |
|
115 | + } |
|
116 | + |
|
117 | + // Try to load the 'group' file, then try to load a |
|
118 | + // config file that matches the group name |
|
119 | + $this->ci->load->config($group, false, true); |
|
120 | 120 | |
121 | - $result = $this->ci->config->item($key); |
|
122 | - |
|
123 | - return $result; |
|
124 | - } |
|
125 | - |
|
126 | - //-------------------------------------------------------------------- |
|
127 | - |
|
128 | - /** |
|
129 | - * Deletes a single item. |
|
130 | - * |
|
131 | - * NOT supported by this store. |
|
132 | - * |
|
133 | - * @param $key |
|
134 | - * @param $group |
|
135 | - * @return mixed |
|
136 | - */ |
|
137 | - public function delete($key, $group='app') |
|
138 | - { |
|
139 | - return false; |
|
140 | - } |
|
141 | - |
|
142 | - //-------------------------------------------------------------------- |
|
143 | - |
|
144 | - /** |
|
145 | - * Searches the store for any items with $field = $value. |
|
146 | - * |
|
147 | - * Does not work. |
|
148 | - * |
|
149 | - * @param $field |
|
150 | - * @param $value |
|
151 | - * @return mixed |
|
152 | - */ |
|
153 | - public function findBy($field, $value) |
|
154 | - { |
|
155 | - return false; |
|
156 | - } |
|
157 | - |
|
158 | - //-------------------------------------------------------------------- |
|
159 | - |
|
160 | - /** |
|
161 | - * Retrieves all items in the store either globally or for a single group. |
|
162 | - * |
|
163 | - * @param string $group |
|
164 | - * @return mixed |
|
165 | - */ |
|
166 | - public function all($group=null) |
|
167 | - { |
|
168 | - if (empty($group)) |
|
169 | - { |
|
170 | - return $this->ci->config->config; |
|
171 | - } |
|
172 | - |
|
173 | - // If we're a group, does the group already exists |
|
174 | - // as a 'section' in the config array? |
|
175 | - if (isset($this->ci->config->config[$group])) |
|
176 | - { |
|
177 | - return $this->ci->config->config[$group]; |
|
178 | - } |
|
179 | - |
|
180 | - // Still here? Attempt to load the file into a section |
|
181 | - // and try one last time. |
|
182 | - if ($this->ci->load->config($group)) |
|
183 | - { |
|
184 | - return $this->ci->config->config($group); |
|
185 | - } |
|
186 | - |
|
187 | - return null; |
|
188 | - } |
|
189 | - |
|
190 | - //-------------------------------------------------------------------- |
|
121 | + $result = $this->ci->config->item($key); |
|
122 | + |
|
123 | + return $result; |
|
124 | + } |
|
125 | + |
|
126 | + //-------------------------------------------------------------------- |
|
127 | + |
|
128 | + /** |
|
129 | + * Deletes a single item. |
|
130 | + * |
|
131 | + * NOT supported by this store. |
|
132 | + * |
|
133 | + * @param $key |
|
134 | + * @param $group |
|
135 | + * @return mixed |
|
136 | + */ |
|
137 | + public function delete($key, $group='app') |
|
138 | + { |
|
139 | + return false; |
|
140 | + } |
|
141 | + |
|
142 | + //-------------------------------------------------------------------- |
|
143 | + |
|
144 | + /** |
|
145 | + * Searches the store for any items with $field = $value. |
|
146 | + * |
|
147 | + * Does not work. |
|
148 | + * |
|
149 | + * @param $field |
|
150 | + * @param $value |
|
151 | + * @return mixed |
|
152 | + */ |
|
153 | + public function findBy($field, $value) |
|
154 | + { |
|
155 | + return false; |
|
156 | + } |
|
157 | + |
|
158 | + //-------------------------------------------------------------------- |
|
159 | + |
|
160 | + /** |
|
161 | + * Retrieves all items in the store either globally or for a single group. |
|
162 | + * |
|
163 | + * @param string $group |
|
164 | + * @return mixed |
|
165 | + */ |
|
166 | + public function all($group=null) |
|
167 | + { |
|
168 | + if (empty($group)) |
|
169 | + { |
|
170 | + return $this->ci->config->config; |
|
171 | + } |
|
172 | + |
|
173 | + // If we're a group, does the group already exists |
|
174 | + // as a 'section' in the config array? |
|
175 | + if (isset($this->ci->config->config[$group])) |
|
176 | + { |
|
177 | + return $this->ci->config->config[$group]; |
|
178 | + } |
|
179 | + |
|
180 | + // Still here? Attempt to load the file into a section |
|
181 | + // and try one last time. |
|
182 | + if ($this->ci->load->config($group)) |
|
183 | + { |
|
184 | + return $this->ci->config->config($group); |
|
185 | + } |
|
186 | + |
|
187 | + return null; |
|
188 | + } |
|
189 | + |
|
190 | + //-------------------------------------------------------------------- |
|
191 | 191 | } |
@@ -1,34 +1,34 @@ discard block |
||
1 | 1 | <?php namespace Myth\Settings; |
2 | 2 | /** |
3 | - * Sprint |
|
4 | - * |
|
5 | - * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | - * |
|
7 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | - * of this software and associated documentation files (the "Software"), to deal |
|
9 | - * in the Software without restriction, including without limitation the rights |
|
10 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | - * copies of the Software, and to permit persons to whom the Software is |
|
12 | - * furnished to do so, subject to the following conditions: |
|
13 | - * |
|
14 | - * The above copyright notice and this permission notice shall be included in |
|
15 | - * all copies or substantial portions of the Software. |
|
16 | - * |
|
17 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | - * THE SOFTWARE. |
|
24 | - * |
|
25 | - * @package Sprint |
|
26 | - * @author Lonnie Ezell |
|
27 | - * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | - * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | - * @link http://sprintphp.com |
|
30 | - * @since Version 1.0 |
|
31 | - */ |
|
3 | + * Sprint |
|
4 | + * |
|
5 | + * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | + * |
|
7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | + * of this software and associated documentation files (the "Software"), to deal |
|
9 | + * in the Software without restriction, including without limitation the rights |
|
10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | + * copies of the Software, and to permit persons to whom the Software is |
|
12 | + * furnished to do so, subject to the following conditions: |
|
13 | + * |
|
14 | + * The above copyright notice and this permission notice shall be included in |
|
15 | + * all copies or substantial portions of the Software. |
|
16 | + * |
|
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | + * THE SOFTWARE. |
|
24 | + * |
|
25 | + * @package Sprint |
|
26 | + * @author Lonnie Ezell |
|
27 | + * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | + * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | + * @link http://sprintphp.com |
|
30 | + * @since Version 1.0 |
|
31 | + */ |
|
32 | 32 | |
33 | 33 | /** |
34 | 34 | * Class Settings |
@@ -40,253 +40,253 @@ discard block |
||
40 | 40 | */ |
41 | 41 | class Settings { |
42 | 42 | |
43 | - /** |
|
44 | - * Holds instantiated data stores. |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - protected static $stores = []; |
|
49 | - |
|
50 | - protected static $default_store = ''; |
|
51 | - |
|
52 | - protected static $initialized = false; |
|
53 | - |
|
54 | - //-------------------------------------------------------------------- |
|
55 | - |
|
56 | - /** |
|
57 | - * Fires up instances of the datastores and gets us ready to roll. |
|
58 | - */ |
|
59 | - public static function init() |
|
60 | - { |
|
61 | - if (self::$initialized === true) |
|
62 | - { |
|
63 | - return; |
|
64 | - } |
|
65 | - |
|
66 | - // Load up and instantiate our setting stores. |
|
67 | - $user_stores = config_item('settings.stores'); |
|
68 | - |
|
69 | - if (is_array($user_stores)) |
|
70 | - { |
|
71 | - foreach ($user_stores as $alias => $class) |
|
72 | - { |
|
73 | - self::$stores[ strtolower($alias) ] = new $class(); |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - self::$default_store = config_item('settings.default_store'); |
|
78 | - |
|
79 | - if (empty(self::$stores[ self::$default_store ])) |
|
80 | - { |
|
81 | - show_error( lang('settings.bad_default') ); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - //-------------------------------------------------------------------- |
|
86 | - |
|
87 | - /** |
|
88 | - * Inserts/Replaces a single setting item. |
|
89 | - * |
|
90 | - * @param $key |
|
91 | - * @param null $value |
|
92 | - * @param string $group |
|
93 | - * @param string $store |
|
94 | - */ |
|
95 | - public static function save($key, $value=null, $group='app', $store=null) |
|
96 | - { |
|
97 | - self::init(); |
|
98 | - |
|
99 | - if (empty($store) || empty(self::$stores[$store])) |
|
100 | - { |
|
101 | - // we've already determined that the default store exists |
|
102 | - // in the init() method. |
|
103 | - $store = self::$default_store; |
|
104 | - } |
|
105 | - |
|
106 | - return self::$stores[ $store ]->save($key, $value, $group); |
|
107 | - } |
|
108 | - |
|
109 | - //-------------------------------------------------------------------- |
|
110 | - |
|
111 | - /** |
|
112 | - * Retrieves a single item. If not $store is specified, will loop |
|
113 | - * through the stores in order until it finds it. If $store is |
|
114 | - * specified, will only look within that store for it. |
|
115 | - * |
|
116 | - * @param $key |
|
117 | - * @param string $group |
|
118 | - * @param string $store |
|
119 | - * @return mixed |
|
120 | - */ |
|
121 | - public static function get($key, $group='app', $store=null) |
|
122 | - { |
|
123 | - self::init(); |
|
124 | - |
|
125 | - $group = strtolower($group); |
|
126 | - |
|
127 | - // If the store is specified but doesn't exist, crap out |
|
128 | - // so that they developer has a chance to fix it. |
|
129 | - if (! is_null($store) && empty(self::$stores[$store])) |
|
130 | - { |
|
131 | - show_error( sprintf( lang('settings.cant_retrieve'), $store ) ); |
|
132 | - } |
|
133 | - |
|
134 | - // If $store is set, get the value from that store only. |
|
135 | - if (! empty($store)) |
|
136 | - { |
|
137 | - return self::$stores[$store]->get($key, $group); |
|
138 | - } |
|
139 | - |
|
140 | - // Otherwise loop through each store until we find it |
|
141 | - foreach (self::$stores as $s) |
|
142 | - { |
|
143 | - if ($found = $s->get($key, $group)) |
|
144 | - { |
|
145 | - return $found; |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - // Still here... guess we didn't find anything, then... |
|
150 | - return false; |
|
151 | - } |
|
152 | - |
|
153 | - //-------------------------------------------------------------------- |
|
154 | - |
|
155 | - /** |
|
156 | - * Deletes a single item. |
|
157 | - * |
|
158 | - * @param $key |
|
159 | - * @param $group |
|
160 | - * @param $store |
|
161 | - * @return mixed |
|
162 | - */ |
|
163 | - public static function delete($key, $group='app', $store=null) |
|
164 | - { |
|
165 | - self::init(); |
|
166 | - |
|
167 | - $group = strtolower($group); |
|
168 | - |
|
169 | - // If the store is specified but doesn't exist, crap out |
|
170 | - // so that they developer has a chance to fix it. |
|
171 | - if (! is_null($store) && empty(self::$stores[$store])) |
|
172 | - { |
|
173 | - show_error( sprintf( lang('settings.cant_retrieve'), $store ) ); |
|
174 | - } |
|
175 | - |
|
176 | - // If $store is set, get the value from that store only. |
|
177 | - if (! empty($store)) |
|
178 | - { |
|
179 | - return self::$stores[$store]->delete($key, $group); |
|
180 | - } |
|
181 | - |
|
182 | - // Otherwise delete from the default store |
|
183 | - return self::$stores[ self::$default_store ]->delete($key, $group); |
|
184 | - } |
|
185 | - |
|
186 | - //-------------------------------------------------------------------- |
|
187 | - |
|
188 | - /** |
|
189 | - * Searches the store for any items with $field = $value. |
|
190 | - * |
|
191 | - * @param $field |
|
192 | - * @param $value |
|
193 | - * @return mixed |
|
194 | - */ |
|
195 | - public static function findBy($field, $value) |
|
196 | - { |
|
197 | - self::init(); |
|
198 | - |
|
199 | - foreach (self::$stores as $s) |
|
200 | - { |
|
201 | - if ($found = $s->findBy($field, $value)) |
|
202 | - { |
|
203 | - return $found; |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - return false; |
|
208 | - } |
|
209 | - |
|
210 | - //-------------------------------------------------------------------- |
|
211 | - |
|
212 | - /** |
|
213 | - * Retrieves all items in the store either globally or for a single group. |
|
214 | - * |
|
215 | - * @param string $group |
|
216 | - * @return mixed |
|
217 | - */ |
|
218 | - public static function all($group=null, $store=null) |
|
219 | - { |
|
220 | - self::init(); |
|
221 | - |
|
222 | - $group = strtolower($group); |
|
223 | - |
|
224 | - if (! empty($store)) |
|
225 | - { |
|
226 | - return self::$stores[ $store ]->all($group); |
|
227 | - } |
|
228 | - |
|
229 | - // Otherwise combine the results from all stores |
|
230 | - $results = []; |
|
231 | - |
|
232 | - foreach (self::$stores as $s) |
|
233 | - { |
|
234 | - $found = $s->all($group); |
|
235 | - if ($found) |
|
236 | - { |
|
237 | - $results = array_merge($results, (array)$found); |
|
238 | - } |
|
239 | - } |
|
240 | - |
|
241 | - return $results; |
|
242 | - } |
|
243 | - |
|
244 | - //-------------------------------------------------------------------- |
|
245 | - |
|
246 | - /** |
|
247 | - * Appends a new datastore the end of the curent stores. |
|
248 | - * |
|
249 | - * @param $alias |
|
250 | - * @param $class |
|
251 | - * @return bool |
|
252 | - */ |
|
253 | - public static function addStore($alias, $class) |
|
254 | - { |
|
255 | - self::init(); |
|
256 | - |
|
257 | - if (class_exists($class)) |
|
258 | - { |
|
259 | - self::$stores[ strtolower($alias) ] = new $class(); |
|
260 | - return true; |
|
261 | - } |
|
262 | - |
|
263 | - return false; |
|
264 | - } |
|
265 | - |
|
266 | - //-------------------------------------------------------------------- |
|
267 | - |
|
268 | - /** |
|
269 | - * Removes a datastore from the list of stores currently available. |
|
270 | - * |
|
271 | - * @param $alias |
|
272 | - * @return bool |
|
273 | - */ |
|
274 | - public static function removeStore($alias) |
|
275 | - { |
|
276 | - self::init(); |
|
277 | - |
|
278 | - $alias = strtolower($alias); |
|
279 | - |
|
280 | - if (empty(self::$stores[$alias])) |
|
281 | - { |
|
282 | - return false; |
|
283 | - } |
|
284 | - |
|
285 | - unset(self::$stores[$alias]); |
|
286 | - |
|
287 | - return true; |
|
288 | - } |
|
289 | - |
|
290 | - //-------------------------------------------------------------------- |
|
43 | + /** |
|
44 | + * Holds instantiated data stores. |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + protected static $stores = []; |
|
49 | + |
|
50 | + protected static $default_store = ''; |
|
51 | + |
|
52 | + protected static $initialized = false; |
|
53 | + |
|
54 | + //-------------------------------------------------------------------- |
|
55 | + |
|
56 | + /** |
|
57 | + * Fires up instances of the datastores and gets us ready to roll. |
|
58 | + */ |
|
59 | + public static function init() |
|
60 | + { |
|
61 | + if (self::$initialized === true) |
|
62 | + { |
|
63 | + return; |
|
64 | + } |
|
65 | + |
|
66 | + // Load up and instantiate our setting stores. |
|
67 | + $user_stores = config_item('settings.stores'); |
|
68 | + |
|
69 | + if (is_array($user_stores)) |
|
70 | + { |
|
71 | + foreach ($user_stores as $alias => $class) |
|
72 | + { |
|
73 | + self::$stores[ strtolower($alias) ] = new $class(); |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + self::$default_store = config_item('settings.default_store'); |
|
78 | + |
|
79 | + if (empty(self::$stores[ self::$default_store ])) |
|
80 | + { |
|
81 | + show_error( lang('settings.bad_default') ); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + //-------------------------------------------------------------------- |
|
86 | + |
|
87 | + /** |
|
88 | + * Inserts/Replaces a single setting item. |
|
89 | + * |
|
90 | + * @param $key |
|
91 | + * @param null $value |
|
92 | + * @param string $group |
|
93 | + * @param string $store |
|
94 | + */ |
|
95 | + public static function save($key, $value=null, $group='app', $store=null) |
|
96 | + { |
|
97 | + self::init(); |
|
98 | + |
|
99 | + if (empty($store) || empty(self::$stores[$store])) |
|
100 | + { |
|
101 | + // we've already determined that the default store exists |
|
102 | + // in the init() method. |
|
103 | + $store = self::$default_store; |
|
104 | + } |
|
105 | + |
|
106 | + return self::$stores[ $store ]->save($key, $value, $group); |
|
107 | + } |
|
108 | + |
|
109 | + //-------------------------------------------------------------------- |
|
110 | + |
|
111 | + /** |
|
112 | + * Retrieves a single item. If not $store is specified, will loop |
|
113 | + * through the stores in order until it finds it. If $store is |
|
114 | + * specified, will only look within that store for it. |
|
115 | + * |
|
116 | + * @param $key |
|
117 | + * @param string $group |
|
118 | + * @param string $store |
|
119 | + * @return mixed |
|
120 | + */ |
|
121 | + public static function get($key, $group='app', $store=null) |
|
122 | + { |
|
123 | + self::init(); |
|
124 | + |
|
125 | + $group = strtolower($group); |
|
126 | + |
|
127 | + // If the store is specified but doesn't exist, crap out |
|
128 | + // so that they developer has a chance to fix it. |
|
129 | + if (! is_null($store) && empty(self::$stores[$store])) |
|
130 | + { |
|
131 | + show_error( sprintf( lang('settings.cant_retrieve'), $store ) ); |
|
132 | + } |
|
133 | + |
|
134 | + // If $store is set, get the value from that store only. |
|
135 | + if (! empty($store)) |
|
136 | + { |
|
137 | + return self::$stores[$store]->get($key, $group); |
|
138 | + } |
|
139 | + |
|
140 | + // Otherwise loop through each store until we find it |
|
141 | + foreach (self::$stores as $s) |
|
142 | + { |
|
143 | + if ($found = $s->get($key, $group)) |
|
144 | + { |
|
145 | + return $found; |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + // Still here... guess we didn't find anything, then... |
|
150 | + return false; |
|
151 | + } |
|
152 | + |
|
153 | + //-------------------------------------------------------------------- |
|
154 | + |
|
155 | + /** |
|
156 | + * Deletes a single item. |
|
157 | + * |
|
158 | + * @param $key |
|
159 | + * @param $group |
|
160 | + * @param $store |
|
161 | + * @return mixed |
|
162 | + */ |
|
163 | + public static function delete($key, $group='app', $store=null) |
|
164 | + { |
|
165 | + self::init(); |
|
166 | + |
|
167 | + $group = strtolower($group); |
|
168 | + |
|
169 | + // If the store is specified but doesn't exist, crap out |
|
170 | + // so that they developer has a chance to fix it. |
|
171 | + if (! is_null($store) && empty(self::$stores[$store])) |
|
172 | + { |
|
173 | + show_error( sprintf( lang('settings.cant_retrieve'), $store ) ); |
|
174 | + } |
|
175 | + |
|
176 | + // If $store is set, get the value from that store only. |
|
177 | + if (! empty($store)) |
|
178 | + { |
|
179 | + return self::$stores[$store]->delete($key, $group); |
|
180 | + } |
|
181 | + |
|
182 | + // Otherwise delete from the default store |
|
183 | + return self::$stores[ self::$default_store ]->delete($key, $group); |
|
184 | + } |
|
185 | + |
|
186 | + //-------------------------------------------------------------------- |
|
187 | + |
|
188 | + /** |
|
189 | + * Searches the store for any items with $field = $value. |
|
190 | + * |
|
191 | + * @param $field |
|
192 | + * @param $value |
|
193 | + * @return mixed |
|
194 | + */ |
|
195 | + public static function findBy($field, $value) |
|
196 | + { |
|
197 | + self::init(); |
|
198 | + |
|
199 | + foreach (self::$stores as $s) |
|
200 | + { |
|
201 | + if ($found = $s->findBy($field, $value)) |
|
202 | + { |
|
203 | + return $found; |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + return false; |
|
208 | + } |
|
209 | + |
|
210 | + //-------------------------------------------------------------------- |
|
211 | + |
|
212 | + /** |
|
213 | + * Retrieves all items in the store either globally or for a single group. |
|
214 | + * |
|
215 | + * @param string $group |
|
216 | + * @return mixed |
|
217 | + */ |
|
218 | + public static function all($group=null, $store=null) |
|
219 | + { |
|
220 | + self::init(); |
|
221 | + |
|
222 | + $group = strtolower($group); |
|
223 | + |
|
224 | + if (! empty($store)) |
|
225 | + { |
|
226 | + return self::$stores[ $store ]->all($group); |
|
227 | + } |
|
228 | + |
|
229 | + // Otherwise combine the results from all stores |
|
230 | + $results = []; |
|
231 | + |
|
232 | + foreach (self::$stores as $s) |
|
233 | + { |
|
234 | + $found = $s->all($group); |
|
235 | + if ($found) |
|
236 | + { |
|
237 | + $results = array_merge($results, (array)$found); |
|
238 | + } |
|
239 | + } |
|
240 | + |
|
241 | + return $results; |
|
242 | + } |
|
243 | + |
|
244 | + //-------------------------------------------------------------------- |
|
245 | + |
|
246 | + /** |
|
247 | + * Appends a new datastore the end of the curent stores. |
|
248 | + * |
|
249 | + * @param $alias |
|
250 | + * @param $class |
|
251 | + * @return bool |
|
252 | + */ |
|
253 | + public static function addStore($alias, $class) |
|
254 | + { |
|
255 | + self::init(); |
|
256 | + |
|
257 | + if (class_exists($class)) |
|
258 | + { |
|
259 | + self::$stores[ strtolower($alias) ] = new $class(); |
|
260 | + return true; |
|
261 | + } |
|
262 | + |
|
263 | + return false; |
|
264 | + } |
|
265 | + |
|
266 | + //-------------------------------------------------------------------- |
|
267 | + |
|
268 | + /** |
|
269 | + * Removes a datastore from the list of stores currently available. |
|
270 | + * |
|
271 | + * @param $alias |
|
272 | + * @return bool |
|
273 | + */ |
|
274 | + public static function removeStore($alias) |
|
275 | + { |
|
276 | + self::init(); |
|
277 | + |
|
278 | + $alias = strtolower($alias); |
|
279 | + |
|
280 | + if (empty(self::$stores[$alias])) |
|
281 | + { |
|
282 | + return false; |
|
283 | + } |
|
284 | + |
|
285 | + unset(self::$stores[$alias]); |
|
286 | + |
|
287 | + return true; |
|
288 | + } |
|
289 | + |
|
290 | + //-------------------------------------------------------------------- |
|
291 | 291 | |
292 | 292 | } |
@@ -40,58 +40,58 @@ |
||
40 | 40 | */ |
41 | 41 | interface SettingsStoreInterface { |
42 | 42 | |
43 | - /** |
|
44 | - * Inserts/Replaces a single setting item. |
|
45 | - * |
|
46 | - * @param $key |
|
47 | - * @param null $value |
|
48 | - * @param string $group |
|
49 | - */ |
|
50 | - public function save($key, $value=null, $group='app'); |
|
43 | + /** |
|
44 | + * Inserts/Replaces a single setting item. |
|
45 | + * |
|
46 | + * @param $key |
|
47 | + * @param null $value |
|
48 | + * @param string $group |
|
49 | + */ |
|
50 | + public function save($key, $value=null, $group='app'); |
|
51 | 51 | |
52 | - //-------------------------------------------------------------------- |
|
52 | + //-------------------------------------------------------------------- |
|
53 | 53 | |
54 | - /** |
|
55 | - * Retrieves a single item. |
|
56 | - * |
|
57 | - * @param $key |
|
58 | - * @param string $group |
|
59 | - * @return mixed |
|
60 | - */ |
|
61 | - public function get($key, $group='app'); |
|
54 | + /** |
|
55 | + * Retrieves a single item. |
|
56 | + * |
|
57 | + * @param $key |
|
58 | + * @param string $group |
|
59 | + * @return mixed |
|
60 | + */ |
|
61 | + public function get($key, $group='app'); |
|
62 | 62 | |
63 | - //-------------------------------------------------------------------- |
|
63 | + //-------------------------------------------------------------------- |
|
64 | 64 | |
65 | - /** |
|
66 | - * Deletes a single item. |
|
67 | - * |
|
68 | - * @param $key |
|
69 | - * @param $group |
|
70 | - * @return mixed |
|
71 | - */ |
|
72 | - public function delete($key, $group='app'); |
|
65 | + /** |
|
66 | + * Deletes a single item. |
|
67 | + * |
|
68 | + * @param $key |
|
69 | + * @param $group |
|
70 | + * @return mixed |
|
71 | + */ |
|
72 | + public function delete($key, $group='app'); |
|
73 | 73 | |
74 | - //-------------------------------------------------------------------- |
|
74 | + //-------------------------------------------------------------------- |
|
75 | 75 | |
76 | - /** |
|
77 | - * Searches the store for any items with $field = $value. |
|
78 | - * |
|
79 | - * @param $field |
|
80 | - * @param $value |
|
81 | - * @return mixed |
|
82 | - */ |
|
83 | - public function findBy($field, $value); |
|
76 | + /** |
|
77 | + * Searches the store for any items with $field = $value. |
|
78 | + * |
|
79 | + * @param $field |
|
80 | + * @param $value |
|
81 | + * @return mixed |
|
82 | + */ |
|
83 | + public function findBy($field, $value); |
|
84 | 84 | |
85 | - //-------------------------------------------------------------------- |
|
85 | + //-------------------------------------------------------------------- |
|
86 | 86 | |
87 | - /** |
|
88 | - * Retrieves all items in the store either globally or for a single group. |
|
89 | - * |
|
90 | - * @param string $group |
|
91 | - * @return mixed |
|
92 | - */ |
|
93 | - public function all($group=null); |
|
87 | + /** |
|
88 | + * Retrieves all items in the store either globally or for a single group. |
|
89 | + * |
|
90 | + * @param string $group |
|
91 | + * @return mixed |
|
92 | + */ |
|
93 | + public function all($group=null); |
|
94 | 94 | |
95 | - //-------------------------------------------------------------------- |
|
95 | + //-------------------------------------------------------------------- |
|
96 | 96 | |
97 | 97 | } |