@@ -531,16 +531,16 @@ |
||
531 | 531 | |
532 | 532 | |
533 | 533 | public static function get_file_size($size, $retstring = null) { |
534 | - // adapted from code at http://aidanlister.com/repos/v/function.size_readable.php |
|
535 | - $sizes = array('bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); |
|
534 | + // adapted from code at http://aidanlister.com/repos/v/function.size_readable.php |
|
535 | + $sizes = array('bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); |
|
536 | 536 | |
537 | - if ($retstring === null) { $retstring = '%01.2f %s'; } |
|
537 | + if ($retstring === null) { $retstring = '%01.2f %s'; } |
|
538 | 538 | |
539 | 539 | $lastsizestring = end($sizes); |
540 | 540 | |
541 | 541 | foreach ($sizes as $sizestring) { |
542 | - if ($size < 1024) { break; } |
|
543 | - if ($sizestring != $lastsizestring) { $size /= 1024; } |
|
542 | + if ($size < 1024) { break; } |
|
543 | + if ($sizestring != $lastsizestring) { $size /= 1024; } |
|
544 | 544 | } |
545 | 545 | |
546 | 546 | if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional |
@@ -1,34 +1,34 @@ discard block |
||
1 | 1 | <?php namespace Myth\Forge; |
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\Controllers\CLIController; |
34 | 34 | use Myth\CLI; |
@@ -43,13 +43,13 @@ discard block |
||
43 | 43 | */ |
44 | 44 | abstract class BaseGenerator extends CLIController { |
45 | 45 | |
46 | - /** |
|
47 | - * Instance of the active themer. |
|
48 | - * @var null |
|
49 | - */ |
|
50 | - protected $themer = null; |
|
46 | + /** |
|
47 | + * Instance of the active themer. |
|
48 | + * @var null |
|
49 | + */ |
|
50 | + protected $themer = null; |
|
51 | 51 | |
52 | - protected $gen_path = null; |
|
52 | + protected $gen_path = null; |
|
53 | 53 | |
54 | 54 | /** |
55 | 55 | * The name of the module being used, if any. |
@@ -62,254 +62,254 @@ discard block |
||
62 | 62 | |
63 | 63 | protected $overwrite = false; |
64 | 64 | |
65 | - //-------------------------------------------------------------------- |
|
65 | + //-------------------------------------------------------------------- |
|
66 | 66 | |
67 | - public function __construct() |
|
68 | - { |
|
69 | - parent::__construct(); |
|
67 | + public function __construct() |
|
68 | + { |
|
69 | + parent::__construct(); |
|
70 | 70 | |
71 | - $this->load->config('forge'); |
|
71 | + $this->load->config('forge'); |
|
72 | 72 | |
73 | - // Detect if we're genning in a module |
|
74 | - if ($module = CLI::option('module')) |
|
75 | - { |
|
76 | - $this->module = $module; |
|
73 | + // Detect if we're genning in a module |
|
74 | + if ($module = CLI::option('module')) |
|
75 | + { |
|
76 | + $this->module = $module; |
|
77 | 77 | |
78 | - $folders = config_item('modules_locations'); |
|
78 | + $folders = config_item('modules_locations'); |
|
79 | 79 | |
80 | - if (is_array($folders)) |
|
81 | - { |
|
82 | - $this->module_path = $folders[0] . strtolower($module) .'/'; |
|
83 | - } |
|
80 | + if (is_array($folders)) |
|
81 | + { |
|
82 | + $this->module_path = $folders[0] . strtolower($module) .'/'; |
|
83 | + } |
|
84 | 84 | } |
85 | 85 | |
86 | - // Should we overwrite files? |
|
87 | - if (CLI::option('overwrite')) |
|
88 | - { |
|
89 | - $this->overwrite = true; |
|
90 | - } |
|
91 | - } |
|
86 | + // Should we overwrite files? |
|
87 | + if (CLI::option('overwrite')) |
|
88 | + { |
|
89 | + $this->overwrite = true; |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - //-------------------------------------------------------------------- |
|
93 | + //-------------------------------------------------------------------- |
|
94 | 94 | |
95 | 95 | |
96 | - /** |
|
97 | - * The method called by the main generator script. This must be |
|
98 | - * overridden by child classes to implement the actual logic used. |
|
99 | - * |
|
100 | - * @param array $segments |
|
101 | - * @param bool $quiet If true, models should accept default values. |
|
102 | - * @return mixed |
|
103 | - */ |
|
104 | - abstract function run($segments=[], $quiet=false); |
|
96 | + /** |
|
97 | + * The method called by the main generator script. This must be |
|
98 | + * overridden by child classes to implement the actual logic used. |
|
99 | + * |
|
100 | + * @param array $segments |
|
101 | + * @param bool $quiet If true, models should accept default values. |
|
102 | + * @return mixed |
|
103 | + */ |
|
104 | + abstract function run($segments=[], $quiet=false); |
|
105 | 105 | |
106 | - //-------------------------------------------------------------------- |
|
106 | + //-------------------------------------------------------------------- |
|
107 | 107 | |
108 | 108 | |
109 | - /** |
|
110 | - * Creates a file at the specified path with the given contents. |
|
111 | - * |
|
112 | - * @param $path |
|
113 | - * @param null $contents |
|
114 | - * |
|
115 | - * @return bool |
|
116 | - */ |
|
117 | - public function createFile($path, $contents=null, $overwrite=false, $perms=0644) |
|
118 | - { |
|
119 | - $path = $this->sandbox($path); |
|
109 | + /** |
|
110 | + * Creates a file at the specified path with the given contents. |
|
111 | + * |
|
112 | + * @param $path |
|
113 | + * @param null $contents |
|
114 | + * |
|
115 | + * @return bool |
|
116 | + */ |
|
117 | + public function createFile($path, $contents=null, $overwrite=false, $perms=0644) |
|
118 | + { |
|
119 | + $path = $this->sandbox($path); |
|
120 | 120 | |
121 | - $file_exists = is_file($path); |
|
121 | + $file_exists = is_file($path); |
|
122 | 122 | |
123 | - // Does file already exist? |
|
124 | - if ($file_exists) |
|
125 | - { |
|
126 | - if (! $overwrite) { |
|
127 | - CLI::write( CLI::color("\t". strtolower(lang('exists')) .": ", 'blue') . str_replace(APPPATH, '', $path ) ); |
|
128 | - return true; |
|
129 | - } |
|
123 | + // Does file already exist? |
|
124 | + if ($file_exists) |
|
125 | + { |
|
126 | + if (! $overwrite) { |
|
127 | + CLI::write( CLI::color("\t". strtolower(lang('exists')) .": ", 'blue') . str_replace(APPPATH, '', $path ) ); |
|
128 | + return true; |
|
129 | + } |
|
130 | 130 | |
131 | - unlink($path); |
|
132 | - } |
|
131 | + unlink($path); |
|
132 | + } |
|
133 | 133 | |
134 | - // Do we need to create the directory? |
|
135 | - $segments = explode('/', $path); |
|
134 | + // Do we need to create the directory? |
|
135 | + $segments = explode('/', $path); |
|
136 | 136 | array_pop($segments); |
137 | 137 | $folder = implode('/', $segments); |
138 | 138 | |
139 | - if (! is_dir($folder)) |
|
140 | - { |
|
141 | - $this->createDirectory($folder); |
|
142 | - } |
|
139 | + if (! is_dir($folder)) |
|
140 | + { |
|
141 | + $this->createDirectory($folder); |
|
142 | + } |
|
143 | 143 | |
144 | - get_instance()->load->helper('file'); |
|
144 | + get_instance()->load->helper('file'); |
|
145 | 145 | |
146 | - if (! write_file($path, $contents)) |
|
147 | - { |
|
148 | - throw new \RuntimeException( sprintf( lang('errors.writing_file'), $path) ); |
|
149 | - } |
|
146 | + if (! write_file($path, $contents)) |
|
147 | + { |
|
148 | + throw new \RuntimeException( sprintf( lang('errors.writing_file'), $path) ); |
|
149 | + } |
|
150 | 150 | |
151 | - chmod($path, $perms); |
|
151 | + chmod($path, $perms); |
|
152 | 152 | |
153 | - if ($overwrite && $file_exists) |
|
154 | - { |
|
155 | - CLI::write( CLI::color("\t". strtolower( lang('overwrote') ) ." ", 'light_red') . str_replace(APPPATH, '', $path ) ); |
|
156 | - } |
|
157 | - else |
|
158 | - { |
|
159 | - CLI::write( CLI::color("\t". strtolower( lang('created') ) ." ", 'yellow') . str_replace(APPPATH, '', $path ) ); |
|
160 | - } |
|
153 | + if ($overwrite && $file_exists) |
|
154 | + { |
|
155 | + CLI::write( CLI::color("\t". strtolower( lang('overwrote') ) ." ", 'light_red') . str_replace(APPPATH, '', $path ) ); |
|
156 | + } |
|
157 | + else |
|
158 | + { |
|
159 | + CLI::write( CLI::color("\t". strtolower( lang('created') ) ." ", 'yellow') . str_replace(APPPATH, '', $path ) ); |
|
160 | + } |
|
161 | 161 | |
162 | - return $this; |
|
163 | - } |
|
162 | + return $this; |
|
163 | + } |
|
164 | 164 | |
165 | - //-------------------------------------------------------------------- |
|
166 | - |
|
167 | - /** |
|
168 | - * Creates a new directory at the specified path. |
|
169 | - * |
|
170 | - * @param $path |
|
171 | - * @param int|string $perms |
|
172 | - * |
|
173 | - * @return bool |
|
174 | - */ |
|
175 | - public function createDirectory($path, $perms=0755) |
|
176 | - { |
|
177 | - $path = $this->sandbox($path); |
|
178 | - |
|
179 | - if (is_dir($path)) |
|
180 | - { |
|
181 | - return $this; |
|
182 | - } |
|
183 | - |
|
184 | - if (! mkdir($path, $perms, true) ) |
|
185 | - { |
|
186 | - throw new \RuntimeException( sprintf( lang('errors.creating_dir'), $path) ); |
|
187 | - } |
|
188 | - |
|
189 | - return $this; |
|
190 | - } |
|
191 | - |
|
192 | - //-------------------------------------------------------------------- |
|
193 | - |
|
194 | - /** |
|
195 | - * Copies a file from the current template group to the destination. |
|
196 | - * |
|
197 | - * @param $source |
|
198 | - * @param $destination |
|
199 | - * @param bool $overwrite |
|
200 | - * |
|
201 | - * @return bool |
|
202 | - */ |
|
203 | - public function copyFile($source, $destination, $overwrite=false) |
|
204 | - { |
|
205 | - $source = $this->sandbox($source); |
|
206 | - |
|
207 | - if (! file_exists($source)) |
|
208 | - { |
|
209 | - return null; |
|
210 | - } |
|
211 | - |
|
212 | - $content = file_get_contents($source); |
|
213 | - |
|
214 | - return $this->createFile($destination, $content, $overwrite); |
|
215 | - } |
|
216 | - |
|
217 | - //-------------------------------------------------------------------- |
|
218 | - |
|
219 | - /** |
|
220 | - * Attempts to locate a template within the current template group, |
|
221 | - * parses it with the passed in data, and writes to the new location. |
|
222 | - * |
|
223 | - * @param $template |
|
224 | - * @param $destination |
|
225 | - * @param array $data |
|
226 | - * @param bool $overwrite |
|
227 | - * |
|
228 | - * @return $this |
|
229 | - */ |
|
230 | - public function copyTemplate($template, $destination, $data=[], $overwrite=false) |
|
231 | - { |
|
232 | - if (! is_array($data)) |
|
233 | - { |
|
234 | - $data = array($data); |
|
235 | - } |
|
236 | - |
|
237 | - $content = $this->render($template, $data); |
|
238 | - |
|
239 | - return $this->createFile($destination, $content, $overwrite); |
|
240 | - } |
|
241 | - |
|
242 | - //-------------------------------------------------------------------- |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * Injects a block of code into an existing file. Using options |
|
247 | - * you can specify where the code should be inserted. Available options |
|
248 | - * are: |
|
249 | - * prepend - Place at beginning of file |
|
250 | - * append - Place at end of file |
|
251 | - * before => '' - Insert just prior to this line of text (don't forget the line ending "\n") |
|
252 | - * after => '' - Insert just after this line of text (don't forget the line ending "\n") |
|
253 | - * replace => '' - a simple string to be replaced. All locations will be replaced. |
|
254 | - * regex => '' - a pregex pattern to use to replace all locations. |
|
255 | - * |
|
256 | - * @param $path |
|
257 | - * @param $content |
|
258 | - * @param array $options |
|
259 | - * |
|
260 | - * @return $this |
|
261 | - */ |
|
262 | - public function injectIntoFile($path, $content, $options='append') |
|
263 | - { |
|
264 | - $kit = new FileKit(); |
|
265 | - |
|
266 | - if (is_string($options)) |
|
267 | - { |
|
268 | - $action = $options; |
|
269 | - } |
|
270 | - else if (is_array($options) && count($options)) |
|
271 | - { |
|
272 | - $keys = array_keys($options); |
|
273 | - $action = array_shift( $keys ); |
|
274 | - $param = $options[$action]; |
|
275 | - } |
|
276 | - |
|
277 | - switch ( strtolower($action) ) |
|
278 | - { |
|
279 | - case 'prepend': |
|
280 | - $success = $kit->prepend($path, $content); |
|
281 | - break; |
|
282 | - case 'before': |
|
283 | - $success = $kit->before($path, $param, $content); |
|
284 | - break; |
|
285 | - case 'after': |
|
286 | - $success = $kit->after($path, $param, $content); |
|
287 | - break; |
|
288 | - case 'replace': |
|
289 | - $success = $kit->replaceIn($path, $param, $content); |
|
290 | - break; |
|
291 | - case 'regex': |
|
292 | - $success = $kit->replaceWithRegex($path, $param, $content); |
|
293 | - break; |
|
294 | - case 'append': |
|
295 | - default: |
|
296 | - $success = $kit->append($path, $content); |
|
297 | - break; |
|
298 | - } |
|
299 | - |
|
300 | - if ($success) |
|
301 | - { |
|
302 | - CLI::write( CLI::color("\t". strtolower( lang('modified') ) ." ", 'cyan') . str_replace(APPPATH, '', $path ) ); |
|
303 | - } |
|
304 | - else |
|
305 | - { |
|
306 | - CLI::write( CLI::color("\t". strtolower( lang('error') ) ." ", 'light_red') . str_replace(APPPATH, '', $path ) ); |
|
307 | - } |
|
308 | - |
|
309 | - return $this; |
|
310 | - } |
|
165 | + //-------------------------------------------------------------------- |
|
166 | + |
|
167 | + /** |
|
168 | + * Creates a new directory at the specified path. |
|
169 | + * |
|
170 | + * @param $path |
|
171 | + * @param int|string $perms |
|
172 | + * |
|
173 | + * @return bool |
|
174 | + */ |
|
175 | + public function createDirectory($path, $perms=0755) |
|
176 | + { |
|
177 | + $path = $this->sandbox($path); |
|
178 | + |
|
179 | + if (is_dir($path)) |
|
180 | + { |
|
181 | + return $this; |
|
182 | + } |
|
183 | + |
|
184 | + if (! mkdir($path, $perms, true) ) |
|
185 | + { |
|
186 | + throw new \RuntimeException( sprintf( lang('errors.creating_dir'), $path) ); |
|
187 | + } |
|
188 | + |
|
189 | + return $this; |
|
190 | + } |
|
191 | + |
|
192 | + //-------------------------------------------------------------------- |
|
193 | + |
|
194 | + /** |
|
195 | + * Copies a file from the current template group to the destination. |
|
196 | + * |
|
197 | + * @param $source |
|
198 | + * @param $destination |
|
199 | + * @param bool $overwrite |
|
200 | + * |
|
201 | + * @return bool |
|
202 | + */ |
|
203 | + public function copyFile($source, $destination, $overwrite=false) |
|
204 | + { |
|
205 | + $source = $this->sandbox($source); |
|
206 | + |
|
207 | + if (! file_exists($source)) |
|
208 | + { |
|
209 | + return null; |
|
210 | + } |
|
211 | + |
|
212 | + $content = file_get_contents($source); |
|
213 | + |
|
214 | + return $this->createFile($destination, $content, $overwrite); |
|
215 | + } |
|
216 | + |
|
217 | + //-------------------------------------------------------------------- |
|
218 | + |
|
219 | + /** |
|
220 | + * Attempts to locate a template within the current template group, |
|
221 | + * parses it with the passed in data, and writes to the new location. |
|
222 | + * |
|
223 | + * @param $template |
|
224 | + * @param $destination |
|
225 | + * @param array $data |
|
226 | + * @param bool $overwrite |
|
227 | + * |
|
228 | + * @return $this |
|
229 | + */ |
|
230 | + public function copyTemplate($template, $destination, $data=[], $overwrite=false) |
|
231 | + { |
|
232 | + if (! is_array($data)) |
|
233 | + { |
|
234 | + $data = array($data); |
|
235 | + } |
|
236 | + |
|
237 | + $content = $this->render($template, $data); |
|
238 | + |
|
239 | + return $this->createFile($destination, $content, $overwrite); |
|
240 | + } |
|
241 | + |
|
242 | + //-------------------------------------------------------------------- |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * Injects a block of code into an existing file. Using options |
|
247 | + * you can specify where the code should be inserted. Available options |
|
248 | + * are: |
|
249 | + * prepend - Place at beginning of file |
|
250 | + * append - Place at end of file |
|
251 | + * before => '' - Insert just prior to this line of text (don't forget the line ending "\n") |
|
252 | + * after => '' - Insert just after this line of text (don't forget the line ending "\n") |
|
253 | + * replace => '' - a simple string to be replaced. All locations will be replaced. |
|
254 | + * regex => '' - a pregex pattern to use to replace all locations. |
|
255 | + * |
|
256 | + * @param $path |
|
257 | + * @param $content |
|
258 | + * @param array $options |
|
259 | + * |
|
260 | + * @return $this |
|
261 | + */ |
|
262 | + public function injectIntoFile($path, $content, $options='append') |
|
263 | + { |
|
264 | + $kit = new FileKit(); |
|
265 | + |
|
266 | + if (is_string($options)) |
|
267 | + { |
|
268 | + $action = $options; |
|
269 | + } |
|
270 | + else if (is_array($options) && count($options)) |
|
271 | + { |
|
272 | + $keys = array_keys($options); |
|
273 | + $action = array_shift( $keys ); |
|
274 | + $param = $options[$action]; |
|
275 | + } |
|
276 | + |
|
277 | + switch ( strtolower($action) ) |
|
278 | + { |
|
279 | + case 'prepend': |
|
280 | + $success = $kit->prepend($path, $content); |
|
281 | + break; |
|
282 | + case 'before': |
|
283 | + $success = $kit->before($path, $param, $content); |
|
284 | + break; |
|
285 | + case 'after': |
|
286 | + $success = $kit->after($path, $param, $content); |
|
287 | + break; |
|
288 | + case 'replace': |
|
289 | + $success = $kit->replaceIn($path, $param, $content); |
|
290 | + break; |
|
291 | + case 'regex': |
|
292 | + $success = $kit->replaceWithRegex($path, $param, $content); |
|
293 | + break; |
|
294 | + case 'append': |
|
295 | + default: |
|
296 | + $success = $kit->append($path, $content); |
|
297 | + break; |
|
298 | + } |
|
299 | + |
|
300 | + if ($success) |
|
301 | + { |
|
302 | + CLI::write( CLI::color("\t". strtolower( lang('modified') ) ." ", 'cyan') . str_replace(APPPATH, '', $path ) ); |
|
303 | + } |
|
304 | + else |
|
305 | + { |
|
306 | + CLI::write( CLI::color("\t". strtolower( lang('error') ) ." ", 'light_red') . str_replace(APPPATH, '', $path ) ); |
|
307 | + } |
|
308 | + |
|
309 | + return $this; |
|
310 | + } |
|
311 | 311 | |
312 | - //-------------------------------------------------------------------- |
|
312 | + //-------------------------------------------------------------------- |
|
313 | 313 | |
314 | 314 | /** |
315 | 315 | * Runs another generator. The first parameter is the name of the |
@@ -320,130 +320,130 @@ discard block |
||
320 | 320 | * @param $command |
321 | 321 | * @param null $options |
322 | 322 | */ |
323 | - public function generate($command, $options = '', $quiet=false) |
|
324 | - { |
|
323 | + public function generate($command, $options = '', $quiet=false) |
|
324 | + { |
|
325 | 325 | $orig_options = CLI::optionString(); |
326 | - $options = $orig_options .' '. $options; |
|
327 | - |
|
328 | - if ($quiet === true) |
|
329 | - { |
|
330 | - $options .= ' -quiet'; |
|
331 | - } |
|
332 | - |
|
333 | - if ($this->overwrite === true) |
|
334 | - { |
|
335 | - $options .= ' -overwrite'; |
|
336 | - } |
|
337 | - |
|
338 | - passthru( "php sprint forge {$command} {$options}" ); |
|
339 | - } |
|
340 | - |
|
341 | - //-------------------------------------------------------------------- |
|
342 | - |
|
343 | - /** |
|
344 | - * Adds a new route to the application's route file. |
|
345 | - * |
|
346 | - * @param $left |
|
347 | - * @param $right |
|
348 | - * |
|
349 | - * @return \Myth\Forge\BaseGenerator |
|
350 | - */ |
|
351 | - public function route($left, $right, $options=[], $method='any') |
|
352 | - { |
|
353 | - $option_str = '['; |
|
354 | - |
|
355 | - foreach ($options as $key => $value) |
|
356 | - { |
|
357 | - $option_str .= ""; |
|
358 | - } |
|
359 | - |
|
360 | - $option_str .= ']'; |
|
361 | - |
|
362 | - $content = "\$routes->{$method}('{$left}', '{$right}', {$option_str});\n"; |
|
363 | - |
|
364 | - return $this->injectIntoFile(APPPATH .'config/routes.php', $content, ['after' => "// Auto-generated routes go here\n"]); |
|
365 | - } |
|
326 | + $options = $orig_options .' '. $options; |
|
327 | + |
|
328 | + if ($quiet === true) |
|
329 | + { |
|
330 | + $options .= ' -quiet'; |
|
331 | + } |
|
332 | + |
|
333 | + if ($this->overwrite === true) |
|
334 | + { |
|
335 | + $options .= ' -overwrite'; |
|
336 | + } |
|
337 | + |
|
338 | + passthru( "php sprint forge {$command} {$options}" ); |
|
339 | + } |
|
340 | + |
|
341 | + //-------------------------------------------------------------------- |
|
342 | + |
|
343 | + /** |
|
344 | + * Adds a new route to the application's route file. |
|
345 | + * |
|
346 | + * @param $left |
|
347 | + * @param $right |
|
348 | + * |
|
349 | + * @return \Myth\Forge\BaseGenerator |
|
350 | + */ |
|
351 | + public function route($left, $right, $options=[], $method='any') |
|
352 | + { |
|
353 | + $option_str = '['; |
|
354 | + |
|
355 | + foreach ($options as $key => $value) |
|
356 | + { |
|
357 | + $option_str .= ""; |
|
358 | + } |
|
359 | + |
|
360 | + $option_str .= ']'; |
|
361 | + |
|
362 | + $content = "\$routes->{$method}('{$left}', '{$right}', {$option_str});\n"; |
|
363 | + |
|
364 | + return $this->injectIntoFile(APPPATH .'config/routes.php', $content, ['after' => "// Auto-generated routes go here\n"]); |
|
365 | + } |
|
366 | 366 | |
367 | - //-------------------------------------------------------------------- |
|
367 | + //-------------------------------------------------------------------- |
|
368 | 368 | |
369 | - /** |
|
370 | - * Outputs the contents of the file in the template's source path. |
|
371 | - */ |
|
372 | - public function readme($file='readme.txt') |
|
373 | - { |
|
374 | - $name = str_replace('Generator', '', get_class($this)); |
|
369 | + /** |
|
370 | + * Outputs the contents of the file in the template's source path. |
|
371 | + */ |
|
372 | + public function readme($file='readme.txt') |
|
373 | + { |
|
374 | + $name = str_replace('Generator', '', get_class($this)); |
|
375 | 375 | |
376 | - $path = $this->locateGenerator($name); |
|
376 | + $path = $this->locateGenerator($name); |
|
377 | 377 | |
378 | - if (! file_exists($path . $file)) |
|
379 | - { |
|
380 | - CLI::error(sprintf( lang('forge.cant_find_readme'), $file) ); |
|
381 | - } |
|
378 | + if (! file_exists($path . $file)) |
|
379 | + { |
|
380 | + CLI::error(sprintf( lang('forge.cant_find_readme'), $file) ); |
|
381 | + } |
|
382 | 382 | |
383 | - $contents = file_get_contents($path . $file); |
|
383 | + $contents = file_get_contents($path . $file); |
|
384 | 384 | |
385 | - CLI::new_line(2); |
|
386 | - CLI::write( CLI::wrap($contents), 'green' ); |
|
387 | - CLI::new_line(); |
|
388 | - } |
|
385 | + CLI::new_line(2); |
|
386 | + CLI::write( CLI::wrap($contents), 'green' ); |
|
387 | + CLI::new_line(); |
|
388 | + } |
|
389 | 389 | |
390 | - //-------------------------------------------------------------------- |
|
390 | + //-------------------------------------------------------------------- |
|
391 | 391 | |
392 | - /** |
|
393 | - * Renders a single generator template. The file must be in a folder |
|
394 | - * under the template group named the same as $this->generator_name. |
|
395 | - * The file must have a '.tpl.php' file extension. |
|
396 | - * |
|
397 | - * @param $template_name |
|
398 | - * @param array $data |
|
399 | - * |
|
400 | - * @return string The rendered template |
|
401 | - */ |
|
402 | - public function render($template_name, $data=[], $folder=null) |
|
403 | - { |
|
404 | - if (empty($this->themer)) |
|
405 | - { |
|
406 | - $this->setupThemer(); |
|
407 | - } |
|
392 | + /** |
|
393 | + * Renders a single generator template. The file must be in a folder |
|
394 | + * under the template group named the same as $this->generator_name. |
|
395 | + * The file must have a '.tpl.php' file extension. |
|
396 | + * |
|
397 | + * @param $template_name |
|
398 | + * @param array $data |
|
399 | + * |
|
400 | + * @return string The rendered template |
|
401 | + */ |
|
402 | + public function render($template_name, $data=[], $folder=null) |
|
403 | + { |
|
404 | + if (empty($this->themer)) |
|
405 | + { |
|
406 | + $this->setupThemer(); |
|
407 | + } |
|
408 | 408 | |
409 | - $data['uikit'] = $this->loadUIKit(); |
|
409 | + $data['uikit'] = $this->loadUIKit(); |
|
410 | 410 | |
411 | - $output = null; |
|
411 | + $output = null; |
|
412 | 412 | |
413 | - $view = $template_name .'.tpl'; |
|
413 | + $view = $template_name .'.tpl'; |
|
414 | 414 | |
415 | - $groups = config_item('forge.collections'); |
|
415 | + $groups = config_item('forge.collections'); |
|
416 | 416 | |
417 | - $name = str_replace('Generator', '', get_class($this) ); |
|
417 | + $name = str_replace('Generator', '', get_class($this) ); |
|
418 | 418 | |
419 | - foreach ($groups as $group => $path) |
|
420 | - { |
|
421 | - $path = rtrim($path, '/ ') .'/'; |
|
422 | - $folders = scandir($path); |
|
419 | + foreach ($groups as $group => $path) |
|
420 | + { |
|
421 | + $path = rtrim($path, '/ ') .'/'; |
|
422 | + $folders = scandir($path); |
|
423 | 423 | |
424 | - if (! $i = array_search(ucfirst($name), $folders)) |
|
425 | - { |
|
426 | - continue; |
|
427 | - } |
|
424 | + if (! $i = array_search(ucfirst($name), $folders)) |
|
425 | + { |
|
426 | + continue; |
|
427 | + } |
|
428 | 428 | |
429 | - $view = $folders[$i] . '/'. $view; |
|
429 | + $view = $folders[$i] . '/'. $view; |
|
430 | 430 | |
431 | - if (realpath($path . $view .'.php')) |
|
432 | - { |
|
433 | - $output = $this->themer->display($group .':'. $view, $data); |
|
434 | - break; |
|
435 | - } |
|
436 | - } |
|
431 | + if (realpath($path . $view .'.php')) |
|
432 | + { |
|
433 | + $output = $this->themer->display($group .':'. $view, $data); |
|
434 | + break; |
|
435 | + } |
|
436 | + } |
|
437 | 437 | |
438 | - // To allow for including any PHP code in the templates, |
|
439 | - // replace any '@php' and '@=' tags with their correct PHP syntax. |
|
440 | - $output = str_replace('@php', '<?php', $output); |
|
441 | - $output = str_replace('@=', '<?=', $output); |
|
438 | + // To allow for including any PHP code in the templates, |
|
439 | + // replace any '@php' and '@=' tags with their correct PHP syntax. |
|
440 | + $output = str_replace('@php', '<?php', $output); |
|
441 | + $output = str_replace('@=', '<?=', $output); |
|
442 | 442 | |
443 | - return $output; |
|
444 | - } |
|
443 | + return $output; |
|
444 | + } |
|
445 | 445 | |
446 | - //-------------------------------------------------------------------- |
|
446 | + //-------------------------------------------------------------------- |
|
447 | 447 | |
448 | 448 | /** |
449 | 449 | * Forces a path to exist within the current application's folder. |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | */ |
459 | 459 | public function sandbox($path) |
460 | 460 | { |
461 | - $path = $this->normalizePath($path); |
|
461 | + $path = $this->normalizePath($path); |
|
462 | 462 | |
463 | 463 | // If it's writing to BASEPATH - FIX IT |
464 | 464 | if (strpos($path, $this->normalizePath(BASEPATH) ) === 0) |
@@ -478,37 +478,37 @@ discard block |
||
478 | 478 | return $path; |
479 | 479 | } |
480 | 480 | |
481 | - return APPPATH . $path; |
|
481 | + return APPPATH . $path; |
|
482 | 482 | } |
483 | 483 | |
484 | 484 | //-------------------------------------------------------------------- |
485 | 485 | |
486 | 486 | |
487 | 487 | |
488 | - //-------------------------------------------------------------------- |
|
489 | - // Private Methods |
|
490 | - //-------------------------------------------------------------------- |
|
488 | + //-------------------------------------------------------------------- |
|
489 | + // Private Methods |
|
490 | + //-------------------------------------------------------------------- |
|
491 | 491 | |
492 | - protected function setupThemer() |
|
493 | - { |
|
494 | - $themer_name = config_item('forge.themer'); |
|
492 | + protected function setupThemer() |
|
493 | + { |
|
494 | + $themer_name = config_item('forge.themer'); |
|
495 | 495 | |
496 | - if (! $themer_name) |
|
497 | - { |
|
498 | - throw new \RuntimeException( lang('forge.no_themer') ); |
|
499 | - } |
|
496 | + if (! $themer_name) |
|
497 | + { |
|
498 | + throw new \RuntimeException( lang('forge.no_themer') ); |
|
499 | + } |
|
500 | 500 | |
501 | - $this->themer = new $themer_name( get_instance() ); |
|
501 | + $this->themer = new $themer_name( get_instance() ); |
|
502 | 502 | |
503 | - // Register our paths with the themer |
|
504 | - $paths = config_item('forge.collections'); |
|
503 | + // Register our paths with the themer |
|
504 | + $paths = config_item('forge.collections'); |
|
505 | 505 | |
506 | - foreach ($paths as $key => $path) { |
|
507 | - $this->themer->addThemePath($key, $path); |
|
508 | - } |
|
509 | - } |
|
506 | + foreach ($paths as $key => $path) { |
|
507 | + $this->themer->addThemePath($key, $path); |
|
508 | + } |
|
509 | + } |
|
510 | 510 | |
511 | - //-------------------------------------------------------------------- |
|
511 | + //-------------------------------------------------------------------- |
|
512 | 512 | |
513 | 513 | public function loadUIKit() |
514 | 514 | { |
@@ -521,7 +521,7 @@ discard block |
||
521 | 521 | |
522 | 522 | $uikit = new $kit_name(); |
523 | 523 | |
524 | - return $uikit; |
|
524 | + return $uikit; |
|
525 | 525 | } |
526 | 526 | |
527 | 527 | //-------------------------------------------------------------------- |
@@ -639,66 +639,66 @@ discard block |
||
639 | 639 | |
640 | 640 | //-------------------------------------------------------------------- |
641 | 641 | |
642 | - /** |
|
643 | - * Normalizes a path and cleans it up for healthy use within |
|
644 | - * realpath() and helps to mitigate changes between Windows and *nix |
|
645 | - * operating systems. |
|
646 | - * |
|
647 | - * Found at http://php.net/manual/en/function.realpath.php#112367 |
|
648 | - * |
|
649 | - * @param $path |
|
650 | - * |
|
651 | - * @return string |
|
652 | - */ |
|
653 | - protected function normalizePath($path) |
|
654 | - { |
|
655 | - // Array to build a new path from the good parts |
|
656 | - $parts = array(); |
|
657 | - |
|
658 | - // Replace backslashes with forward slashes |
|
659 | - $path = str_replace('\\', '/', $path); |
|
660 | - |
|
661 | - // Combine multiple slashes into a single slash |
|
662 | - $path = preg_replace('/\/+/', '/', $path); |
|
663 | - |
|
664 | - // Collect path segments |
|
665 | - $segments = explode('/', $path); |
|
666 | - |
|
667 | - // Initialize testing variable |
|
668 | - $test = ''; |
|
669 | - |
|
670 | - foreach($segments as $segment) |
|
671 | - { |
|
672 | - if($segment != '.') |
|
673 | - { |
|
674 | - $test = array_pop($parts); |
|
675 | - |
|
676 | - if(is_null($test)) |
|
677 | - { |
|
678 | - $parts[] = $segment; |
|
679 | - } |
|
680 | - else if ($segment == '..') |
|
681 | - { |
|
682 | - if ($test == '..') |
|
683 | - { |
|
684 | - $parts[] = $test; |
|
685 | - } |
|
686 | - |
|
687 | - if ($test == '..' || $test == '') |
|
688 | - { |
|
689 | - $parts[] = $segment; |
|
690 | - } |
|
691 | - } |
|
692 | - else |
|
693 | - { |
|
694 | - $parts[] = $test; |
|
695 | - $parts[] = $segment; |
|
696 | - } |
|
697 | - } |
|
698 | - } |
|
699 | - return implode('/', $parts); |
|
700 | - } |
|
701 | - |
|
702 | - //-------------------------------------------------------------------- |
|
642 | + /** |
|
643 | + * Normalizes a path and cleans it up for healthy use within |
|
644 | + * realpath() and helps to mitigate changes between Windows and *nix |
|
645 | + * operating systems. |
|
646 | + * |
|
647 | + * Found at http://php.net/manual/en/function.realpath.php#112367 |
|
648 | + * |
|
649 | + * @param $path |
|
650 | + * |
|
651 | + * @return string |
|
652 | + */ |
|
653 | + protected function normalizePath($path) |
|
654 | + { |
|
655 | + // Array to build a new path from the good parts |
|
656 | + $parts = array(); |
|
657 | + |
|
658 | + // Replace backslashes with forward slashes |
|
659 | + $path = str_replace('\\', '/', $path); |
|
660 | + |
|
661 | + // Combine multiple slashes into a single slash |
|
662 | + $path = preg_replace('/\/+/', '/', $path); |
|
663 | + |
|
664 | + // Collect path segments |
|
665 | + $segments = explode('/', $path); |
|
666 | + |
|
667 | + // Initialize testing variable |
|
668 | + $test = ''; |
|
669 | + |
|
670 | + foreach($segments as $segment) |
|
671 | + { |
|
672 | + if($segment != '.') |
|
673 | + { |
|
674 | + $test = array_pop($parts); |
|
675 | + |
|
676 | + if(is_null($test)) |
|
677 | + { |
|
678 | + $parts[] = $segment; |
|
679 | + } |
|
680 | + else if ($segment == '..') |
|
681 | + { |
|
682 | + if ($test == '..') |
|
683 | + { |
|
684 | + $parts[] = $test; |
|
685 | + } |
|
686 | + |
|
687 | + if ($test == '..' || $test == '') |
|
688 | + { |
|
689 | + $parts[] = $segment; |
|
690 | + } |
|
691 | + } |
|
692 | + else |
|
693 | + { |
|
694 | + $parts[] = $test; |
|
695 | + $parts[] = $segment; |
|
696 | + } |
|
697 | + } |
|
698 | + } |
|
699 | + return implode('/', $parts); |
|
700 | + } |
|
701 | + |
|
702 | + //-------------------------------------------------------------------- |
|
703 | 703 | |
704 | 704 | } |
@@ -32,206 +32,206 @@ |
||
32 | 32 | |
33 | 33 | class FileKit { |
34 | 34 | |
35 | - /** |
|
36 | - * Appends data to the end of a file. |
|
37 | - * |
|
38 | - * @param $file |
|
39 | - * @param $content |
|
40 | - * @return bool|int |
|
41 | - */ |
|
42 | - public function append($file, $content) |
|
43 | - { |
|
44 | - if (empty($content)) |
|
45 | - { |
|
46 | - return true; |
|
47 | - } |
|
48 | - |
|
49 | - // Ensure that $content has a newline at the end |
|
50 | - $content = rtrim($content) ."\n"; |
|
51 | - |
|
52 | - $fh = fopen($file, 'a'); |
|
53 | - $result = fwrite($fh, $content); |
|
54 | - fclose($fh); |
|
55 | - |
|
56 | - return $result; |
|
57 | - } |
|
58 | - |
|
59 | - //-------------------------------------------------------------------- |
|
60 | - |
|
61 | - /** |
|
62 | - * Prepends string content to a file. For very large files |
|
63 | - * this method could have memory issues, but the primary usage |
|
64 | - * of source files shouldn't ever get large enough to cause issues. |
|
65 | - * |
|
66 | - * @param $file |
|
67 | - * @param $content |
|
68 | - * @return bool|int |
|
69 | - */ |
|
70 | - public function prepend($file, $content) |
|
71 | - { |
|
72 | - if (empty($content)) |
|
73 | - { |
|
74 | - return true; |
|
75 | - } |
|
76 | - |
|
77 | - // Ensure that $content has a newline at the end |
|
78 | - $content = rtrim($content) ."\n"; |
|
79 | - |
|
80 | - $file_contents = file_get_contents($file); |
|
81 | - |
|
82 | - if ($file_contents === false) |
|
83 | - { |
|
84 | - throw new \RuntimeException( sprintf(lang('errors.reading_file'), $file)); |
|
85 | - } |
|
86 | - |
|
87 | - $result = file_put_contents($file, $content . $file_contents); |
|
88 | - |
|
89 | - return (bool)$result; |
|
90 | - } |
|
91 | - |
|
92 | - //-------------------------------------------------------------------- |
|
93 | - |
|
94 | - /** |
|
95 | - * Inserts $content before the line that matches $before. NOT case- |
|
96 | - * sensitive. |
|
97 | - * |
|
98 | - * @param $file |
|
99 | - * @param $before |
|
100 | - * @param $content |
|
101 | - * @return int |
|
102 | - */ |
|
103 | - public function before($file, $before, $content) |
|
104 | - { |
|
105 | - if (empty($content)) |
|
106 | - { |
|
107 | - return true; |
|
108 | - } |
|
109 | - |
|
110 | - // Ensure that $content has a newline at the end |
|
111 | - $content = rtrim($content) ."\n"; |
|
112 | - |
|
113 | - $lines = file($file); |
|
114 | - |
|
115 | - if ($lines === false) |
|
116 | - { |
|
117 | - throw new \RuntimeException( sprintf( lang('errors.file_not_found'), $file )); |
|
118 | - } |
|
119 | - |
|
120 | - // Where to insert the row. |
|
121 | - $location = null; |
|
122 | - |
|
123 | - foreach ($lines as $index => $line) |
|
124 | - { |
|
125 | - if (strtolower($line) == strtolower($before) ) |
|
126 | - { |
|
127 | - $location = $index; |
|
128 | - break; |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - array_splice($lines, $location, 0, $content); |
|
133 | - |
|
134 | - $result = file_put_contents($file, $lines); |
|
135 | - |
|
136 | - return (bool)$result; |
|
137 | - } |
|
138 | - |
|
139 | - //-------------------------------------------------------------------- |
|
140 | - |
|
141 | - public function after($file, $after, $content) |
|
142 | - { |
|
143 | - if (empty($content)) |
|
144 | - { |
|
145 | - return true; |
|
146 | - } |
|
147 | - |
|
148 | - // Ensure that $content has a newline at the end |
|
149 | - $content = rtrim($content) ."\n"; |
|
150 | - |
|
151 | - $lines = file($file); |
|
152 | - |
|
153 | - if ($lines === false) |
|
154 | - { |
|
155 | - throw new \RuntimeException( sprintf( lang('errors.file_not_found'), $file ) ); |
|
156 | - } |
|
157 | - |
|
158 | - // Where to insert the row. |
|
159 | - $location = null; |
|
160 | - |
|
161 | - foreach ($lines as $index => $line) |
|
162 | - { |
|
163 | - if (strtolower($line) == strtolower($after) ) |
|
164 | - { |
|
165 | - $location = $index; |
|
166 | - break; |
|
167 | - } |
|
168 | - } |
|
169 | - |
|
170 | - array_splice($lines, $location +1, 0, $content); |
|
171 | - |
|
172 | - $result = file_put_contents($file, $lines); |
|
173 | - |
|
174 | - return (bool)$result; |
|
175 | - } |
|
176 | - |
|
177 | - //-------------------------------------------------------------------- |
|
178 | - |
|
179 | - /** |
|
180 | - * Replaces all instances of $search in the file with $replace. |
|
181 | - * |
|
182 | - * @param $file |
|
183 | - * @param $search |
|
184 | - * @param $replace |
|
185 | - * @return int |
|
186 | - */ |
|
187 | - public function replaceIn($file, $search, $replace) |
|
188 | - { |
|
189 | - $file_contents = file_get_contents($file); |
|
190 | - |
|
191 | - if ($file_contents === false) |
|
192 | - { |
|
193 | - throw new \RuntimeException( sprintf( lang('errors.reading_file'), $file ) ); |
|
194 | - } |
|
195 | - |
|
196 | - $file_contents = str_replace($search, $replace, $file_contents); |
|
197 | - |
|
198 | - $result = file_put_contents($file, $file_contents); |
|
199 | - |
|
200 | - return (bool)$result; |
|
201 | - } |
|
202 | - |
|
203 | - //-------------------------------------------------------------------- |
|
204 | - |
|
205 | - /** |
|
206 | - * Uses preg_replace to replace content within the file. |
|
207 | - * |
|
208 | - * @param $file |
|
209 | - * @param $pattern |
|
210 | - * @param $replace |
|
211 | - * @return int |
|
212 | - */ |
|
213 | - public function replaceWithRegex($file, $pattern, $replace) |
|
214 | - { |
|
215 | - $file_contents = file_get_contents($file); |
|
216 | - |
|
217 | - if ($file_contents === false) |
|
218 | - { |
|
219 | - throw new \RuntimeException( sprintf( lang('errors.reading_file'), $file ) ); |
|
220 | - } |
|
35 | + /** |
|
36 | + * Appends data to the end of a file. |
|
37 | + * |
|
38 | + * @param $file |
|
39 | + * @param $content |
|
40 | + * @return bool|int |
|
41 | + */ |
|
42 | + public function append($file, $content) |
|
43 | + { |
|
44 | + if (empty($content)) |
|
45 | + { |
|
46 | + return true; |
|
47 | + } |
|
48 | + |
|
49 | + // Ensure that $content has a newline at the end |
|
50 | + $content = rtrim($content) ."\n"; |
|
51 | + |
|
52 | + $fh = fopen($file, 'a'); |
|
53 | + $result = fwrite($fh, $content); |
|
54 | + fclose($fh); |
|
55 | + |
|
56 | + return $result; |
|
57 | + } |
|
58 | + |
|
59 | + //-------------------------------------------------------------------- |
|
60 | + |
|
61 | + /** |
|
62 | + * Prepends string content to a file. For very large files |
|
63 | + * this method could have memory issues, but the primary usage |
|
64 | + * of source files shouldn't ever get large enough to cause issues. |
|
65 | + * |
|
66 | + * @param $file |
|
67 | + * @param $content |
|
68 | + * @return bool|int |
|
69 | + */ |
|
70 | + public function prepend($file, $content) |
|
71 | + { |
|
72 | + if (empty($content)) |
|
73 | + { |
|
74 | + return true; |
|
75 | + } |
|
76 | + |
|
77 | + // Ensure that $content has a newline at the end |
|
78 | + $content = rtrim($content) ."\n"; |
|
79 | + |
|
80 | + $file_contents = file_get_contents($file); |
|
81 | + |
|
82 | + if ($file_contents === false) |
|
83 | + { |
|
84 | + throw new \RuntimeException( sprintf(lang('errors.reading_file'), $file)); |
|
85 | + } |
|
86 | + |
|
87 | + $result = file_put_contents($file, $content . $file_contents); |
|
88 | + |
|
89 | + return (bool)$result; |
|
90 | + } |
|
91 | + |
|
92 | + //-------------------------------------------------------------------- |
|
93 | + |
|
94 | + /** |
|
95 | + * Inserts $content before the line that matches $before. NOT case- |
|
96 | + * sensitive. |
|
97 | + * |
|
98 | + * @param $file |
|
99 | + * @param $before |
|
100 | + * @param $content |
|
101 | + * @return int |
|
102 | + */ |
|
103 | + public function before($file, $before, $content) |
|
104 | + { |
|
105 | + if (empty($content)) |
|
106 | + { |
|
107 | + return true; |
|
108 | + } |
|
109 | + |
|
110 | + // Ensure that $content has a newline at the end |
|
111 | + $content = rtrim($content) ."\n"; |
|
112 | + |
|
113 | + $lines = file($file); |
|
114 | + |
|
115 | + if ($lines === false) |
|
116 | + { |
|
117 | + throw new \RuntimeException( sprintf( lang('errors.file_not_found'), $file )); |
|
118 | + } |
|
119 | + |
|
120 | + // Where to insert the row. |
|
121 | + $location = null; |
|
122 | + |
|
123 | + foreach ($lines as $index => $line) |
|
124 | + { |
|
125 | + if (strtolower($line) == strtolower($before) ) |
|
126 | + { |
|
127 | + $location = $index; |
|
128 | + break; |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + array_splice($lines, $location, 0, $content); |
|
133 | + |
|
134 | + $result = file_put_contents($file, $lines); |
|
135 | + |
|
136 | + return (bool)$result; |
|
137 | + } |
|
138 | + |
|
139 | + //-------------------------------------------------------------------- |
|
140 | + |
|
141 | + public function after($file, $after, $content) |
|
142 | + { |
|
143 | + if (empty($content)) |
|
144 | + { |
|
145 | + return true; |
|
146 | + } |
|
147 | + |
|
148 | + // Ensure that $content has a newline at the end |
|
149 | + $content = rtrim($content) ."\n"; |
|
150 | + |
|
151 | + $lines = file($file); |
|
152 | + |
|
153 | + if ($lines === false) |
|
154 | + { |
|
155 | + throw new \RuntimeException( sprintf( lang('errors.file_not_found'), $file ) ); |
|
156 | + } |
|
157 | + |
|
158 | + // Where to insert the row. |
|
159 | + $location = null; |
|
160 | + |
|
161 | + foreach ($lines as $index => $line) |
|
162 | + { |
|
163 | + if (strtolower($line) == strtolower($after) ) |
|
164 | + { |
|
165 | + $location = $index; |
|
166 | + break; |
|
167 | + } |
|
168 | + } |
|
169 | + |
|
170 | + array_splice($lines, $location +1, 0, $content); |
|
171 | + |
|
172 | + $result = file_put_contents($file, $lines); |
|
173 | + |
|
174 | + return (bool)$result; |
|
175 | + } |
|
176 | + |
|
177 | + //-------------------------------------------------------------------- |
|
178 | + |
|
179 | + /** |
|
180 | + * Replaces all instances of $search in the file with $replace. |
|
181 | + * |
|
182 | + * @param $file |
|
183 | + * @param $search |
|
184 | + * @param $replace |
|
185 | + * @return int |
|
186 | + */ |
|
187 | + public function replaceIn($file, $search, $replace) |
|
188 | + { |
|
189 | + $file_contents = file_get_contents($file); |
|
190 | + |
|
191 | + if ($file_contents === false) |
|
192 | + { |
|
193 | + throw new \RuntimeException( sprintf( lang('errors.reading_file'), $file ) ); |
|
194 | + } |
|
195 | + |
|
196 | + $file_contents = str_replace($search, $replace, $file_contents); |
|
197 | + |
|
198 | + $result = file_put_contents($file, $file_contents); |
|
199 | + |
|
200 | + return (bool)$result; |
|
201 | + } |
|
202 | + |
|
203 | + //-------------------------------------------------------------------- |
|
204 | + |
|
205 | + /** |
|
206 | + * Uses preg_replace to replace content within the file. |
|
207 | + * |
|
208 | + * @param $file |
|
209 | + * @param $pattern |
|
210 | + * @param $replace |
|
211 | + * @return int |
|
212 | + */ |
|
213 | + public function replaceWithRegex($file, $pattern, $replace) |
|
214 | + { |
|
215 | + $file_contents = file_get_contents($file); |
|
216 | + |
|
217 | + if ($file_contents === false) |
|
218 | + { |
|
219 | + throw new \RuntimeException( sprintf( lang('errors.reading_file'), $file ) ); |
|
220 | + } |
|
221 | 221 | |
222 | - $file_contents = preg_replace($pattern, $replace, $file_contents); |
|
223 | - |
|
224 | - $result = false; |
|
222 | + $file_contents = preg_replace($pattern, $replace, $file_contents); |
|
223 | + |
|
224 | + $result = false; |
|
225 | 225 | |
226 | - // Don't let us erase a file! |
|
227 | - if (! empty($file_contents)) |
|
228 | - { |
|
229 | - $result = file_put_contents( $file, $file_contents ); |
|
230 | - } |
|
226 | + // Don't let us erase a file! |
|
227 | + if (! empty($file_contents)) |
|
228 | + { |
|
229 | + $result = file_put_contents( $file, $file_contents ); |
|
230 | + } |
|
231 | 231 | |
232 | - return (bool)$result; |
|
233 | - } |
|
232 | + return (bool)$result; |
|
233 | + } |
|
234 | 234 | |
235 | - //-------------------------------------------------------------------- |
|
235 | + //-------------------------------------------------------------------- |
|
236 | 236 | |
237 | 237 | } |
@@ -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 | } |
@@ -42,145 +42,145 @@ |
||
42 | 42 | */ |
43 | 43 | interface MailServiceInterface { |
44 | 44 | |
45 | - /** |
|
46 | - * Does the actual delivery of a message. |
|
47 | - * |
|
48 | - * @param bool $clear_after If TRUE, will reset the class after sending. |
|
49 | - * |
|
50 | - * @return mixed |
|
51 | - */ |
|
52 | - public function send($clear_after=true); |
|
53 | - |
|
54 | - //-------------------------------------------------------------------- |
|
55 | - |
|
56 | - /** |
|
57 | - * Adds an attachment to the current email that is being built. |
|
58 | - * |
|
59 | - * @param string $filename |
|
60 | - * @param string $disposition like 'inline'. Default is 'attachment' |
|
61 | - * @param string $newname If you'd like to rename the file for delivery |
|
62 | - * @param string $mime Custom defined mime type. |
|
63 | - */ |
|
64 | - public function attach($filename, $disposition=null, $newname=null, $mime=null); |
|
65 | - |
|
66 | - //-------------------------------------------------------------------- |
|
67 | - |
|
68 | - /** |
|
69 | - * Sets a header value for the email. Not every service will provide this. |
|
70 | - * |
|
71 | - * @param $field |
|
72 | - * @param $value |
|
73 | - * @return mixed |
|
74 | - */ |
|
75 | - public function setHeader($field, $value); |
|
76 | - |
|
77 | - //-------------------------------------------------------------------- |
|
78 | - |
|
79 | - //-------------------------------------------------------------------- |
|
80 | - // Options |
|
81 | - //-------------------------------------------------------------------- |
|
82 | - |
|
83 | - /** |
|
84 | - * Sets the email address to send the email to. |
|
85 | - * |
|
86 | - * @param $email |
|
87 | - * @return mixed |
|
88 | - */ |
|
89 | - public function to($email); |
|
45 | + /** |
|
46 | + * Does the actual delivery of a message. |
|
47 | + * |
|
48 | + * @param bool $clear_after If TRUE, will reset the class after sending. |
|
49 | + * |
|
50 | + * @return mixed |
|
51 | + */ |
|
52 | + public function send($clear_after=true); |
|
53 | + |
|
54 | + //-------------------------------------------------------------------- |
|
55 | + |
|
56 | + /** |
|
57 | + * Adds an attachment to the current email that is being built. |
|
58 | + * |
|
59 | + * @param string $filename |
|
60 | + * @param string $disposition like 'inline'. Default is 'attachment' |
|
61 | + * @param string $newname If you'd like to rename the file for delivery |
|
62 | + * @param string $mime Custom defined mime type. |
|
63 | + */ |
|
64 | + public function attach($filename, $disposition=null, $newname=null, $mime=null); |
|
65 | + |
|
66 | + //-------------------------------------------------------------------- |
|
67 | + |
|
68 | + /** |
|
69 | + * Sets a header value for the email. Not every service will provide this. |
|
70 | + * |
|
71 | + * @param $field |
|
72 | + * @param $value |
|
73 | + * @return mixed |
|
74 | + */ |
|
75 | + public function setHeader($field, $value); |
|
76 | + |
|
77 | + //-------------------------------------------------------------------- |
|
78 | + |
|
79 | + //-------------------------------------------------------------------- |
|
80 | + // Options |
|
81 | + //-------------------------------------------------------------------- |
|
82 | + |
|
83 | + /** |
|
84 | + * Sets the email address to send the email to. |
|
85 | + * |
|
86 | + * @param $email |
|
87 | + * @return mixed |
|
88 | + */ |
|
89 | + public function to($email); |
|
90 | 90 | |
91 | - //-------------------------------------------------------------------- |
|
92 | - |
|
93 | - /** |
|
94 | - * Sets who the email is coming from. |
|
95 | - * |
|
96 | - * @param $email |
|
97 | - * @param null $name |
|
98 | - * @return mixed |
|
99 | - */ |
|
100 | - public function from($email, $name=null); |
|
101 | - |
|
102 | - //-------------------------------------------------------------------- |
|
103 | - |
|
104 | - /** |
|
105 | - * Sets a single additional email address to 'cc'. |
|
106 | - * |
|
107 | - * @param $email |
|
108 | - * @return mixed |
|
109 | - */ |
|
110 | - public function cc($email); |
|
111 | - |
|
112 | - //-------------------------------------------------------------------- |
|
113 | - |
|
114 | - /** |
|
115 | - * Sets a single email address to 'bcc' to. |
|
116 | - * |
|
117 | - * @param $email |
|
118 | - * @return mixed |
|
119 | - */ |
|
120 | - public function bcc($email); |
|
121 | - |
|
122 | - //-------------------------------------------------------------------- |
|
123 | - |
|
124 | - /** |
|
125 | - * Sets the reply to address. |
|
126 | - * |
|
127 | - * @param $email |
|
128 | - * @param $name |
|
129 | - * @return mixed |
|
130 | - */ |
|
131 | - public function reply_to($email, $name=null); |
|
132 | - |
|
133 | - //-------------------------------------------------------------------- |
|
134 | - |
|
135 | - /** |
|
136 | - * Sets the subject line of the email. |
|
137 | - * |
|
138 | - * @param $subject |
|
139 | - * @return mixed |
|
140 | - */ |
|
141 | - public function subject($subject); |
|
142 | - |
|
143 | - //-------------------------------------------------------------------- |
|
144 | - |
|
145 | - /** |
|
146 | - * Sets the HTML portion of the email address. Optional. |
|
147 | - * |
|
148 | - * @param $message |
|
149 | - * @return mixed |
|
150 | - */ |
|
151 | - public function html_message($message); |
|
152 | - |
|
153 | - //-------------------------------------------------------------------- |
|
154 | - |
|
155 | - /** |
|
156 | - * Sets the text portion of the email address. Optional. |
|
157 | - * |
|
158 | - * @param $message |
|
159 | - * @return mixed |
|
160 | - */ |
|
161 | - public function text_message($message); |
|
162 | - |
|
163 | - //-------------------------------------------------------------------- |
|
164 | - |
|
165 | - /** |
|
166 | - * Sets the format to send the email in. Either 'html' or 'text'. |
|
167 | - * |
|
168 | - * @param $format |
|
169 | - * @return mixed |
|
170 | - */ |
|
171 | - public function format($format); |
|
172 | - |
|
173 | - //-------------------------------------------------------------------- |
|
174 | - |
|
175 | - /** |
|
176 | - * Resets the state to blank, ready for a new email. Useful when |
|
177 | - * sending emails in a loop and you need to make sure that the |
|
178 | - * email is reset. |
|
179 | - * |
|
180 | - * @param bool $clear_attachments |
|
181 | - * @return mixed |
|
182 | - */ |
|
183 | - public function reset($clear_attachments=true); |
|
184 | - |
|
185 | - //-------------------------------------------------------------------- |
|
91 | + //-------------------------------------------------------------------- |
|
92 | + |
|
93 | + /** |
|
94 | + * Sets who the email is coming from. |
|
95 | + * |
|
96 | + * @param $email |
|
97 | + * @param null $name |
|
98 | + * @return mixed |
|
99 | + */ |
|
100 | + public function from($email, $name=null); |
|
101 | + |
|
102 | + //-------------------------------------------------------------------- |
|
103 | + |
|
104 | + /** |
|
105 | + * Sets a single additional email address to 'cc'. |
|
106 | + * |
|
107 | + * @param $email |
|
108 | + * @return mixed |
|
109 | + */ |
|
110 | + public function cc($email); |
|
111 | + |
|
112 | + //-------------------------------------------------------------------- |
|
113 | + |
|
114 | + /** |
|
115 | + * Sets a single email address to 'bcc' to. |
|
116 | + * |
|
117 | + * @param $email |
|
118 | + * @return mixed |
|
119 | + */ |
|
120 | + public function bcc($email); |
|
121 | + |
|
122 | + //-------------------------------------------------------------------- |
|
123 | + |
|
124 | + /** |
|
125 | + * Sets the reply to address. |
|
126 | + * |
|
127 | + * @param $email |
|
128 | + * @param $name |
|
129 | + * @return mixed |
|
130 | + */ |
|
131 | + public function reply_to($email, $name=null); |
|
132 | + |
|
133 | + //-------------------------------------------------------------------- |
|
134 | + |
|
135 | + /** |
|
136 | + * Sets the subject line of the email. |
|
137 | + * |
|
138 | + * @param $subject |
|
139 | + * @return mixed |
|
140 | + */ |
|
141 | + public function subject($subject); |
|
142 | + |
|
143 | + //-------------------------------------------------------------------- |
|
144 | + |
|
145 | + /** |
|
146 | + * Sets the HTML portion of the email address. Optional. |
|
147 | + * |
|
148 | + * @param $message |
|
149 | + * @return mixed |
|
150 | + */ |
|
151 | + public function html_message($message); |
|
152 | + |
|
153 | + //-------------------------------------------------------------------- |
|
154 | + |
|
155 | + /** |
|
156 | + * Sets the text portion of the email address. Optional. |
|
157 | + * |
|
158 | + * @param $message |
|
159 | + * @return mixed |
|
160 | + */ |
|
161 | + public function text_message($message); |
|
162 | + |
|
163 | + //-------------------------------------------------------------------- |
|
164 | + |
|
165 | + /** |
|
166 | + * Sets the format to send the email in. Either 'html' or 'text'. |
|
167 | + * |
|
168 | + * @param $format |
|
169 | + * @return mixed |
|
170 | + */ |
|
171 | + public function format($format); |
|
172 | + |
|
173 | + //-------------------------------------------------------------------- |
|
174 | + |
|
175 | + /** |
|
176 | + * Resets the state to blank, ready for a new email. Useful when |
|
177 | + * sending emails in a loop and you need to make sure that the |
|
178 | + * email is reset. |
|
179 | + * |
|
180 | + * @param bool $clear_attachments |
|
181 | + * @return mixed |
|
182 | + */ |
|
183 | + public function reset($clear_attachments=true); |
|
184 | + |
|
185 | + //-------------------------------------------------------------------- |
|
186 | 186 | } |
@@ -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 | } |