@@ -41,144 +41,144 @@ |
||
41 | 41 | */ |
42 | 42 | class CLIController extends \CI_Controller { |
43 | 43 | |
44 | - /** |
|
45 | - * Holds short descriptions for the public functions in this class. |
|
46 | - * Each 'key' in the array should match the function name. |
|
47 | - * |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - protected $descriptions = []; |
|
51 | - |
|
52 | - /** |
|
53 | - * Holds long descriptions for the public functions in this class. |
|
54 | - * Each 'key' in the array should match the function name. |
|
55 | - * @var array |
|
56 | - */ |
|
57 | - protected $long_descriptions = []; |
|
58 | - |
|
59 | - //-------------------------------------------------------------------- |
|
60 | - |
|
61 | - /** |
|
62 | - * Ensures that we are running on the CLI, and collects basic settings |
|
63 | - * like collecting our command line arguments into a pretty array. |
|
64 | - */ |
|
65 | - public function __construct() |
|
66 | - { |
|
67 | - parent::__construct(); |
|
68 | - |
|
69 | - // Restrict usage to the command line. |
|
70 | - if (! is_cli() ) |
|
71 | - { |
|
72 | - show_error( lang('cli_required') ); |
|
73 | - } |
|
74 | - |
|
75 | - // Make sure the CLI library is loaded and ready. |
|
44 | + /** |
|
45 | + * Holds short descriptions for the public functions in this class. |
|
46 | + * Each 'key' in the array should match the function name. |
|
47 | + * |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + protected $descriptions = []; |
|
51 | + |
|
52 | + /** |
|
53 | + * Holds long descriptions for the public functions in this class. |
|
54 | + * Each 'key' in the array should match the function name. |
|
55 | + * @var array |
|
56 | + */ |
|
57 | + protected $long_descriptions = []; |
|
58 | + |
|
59 | + //-------------------------------------------------------------------- |
|
60 | + |
|
61 | + /** |
|
62 | + * Ensures that we are running on the CLI, and collects basic settings |
|
63 | + * like collecting our command line arguments into a pretty array. |
|
64 | + */ |
|
65 | + public function __construct() |
|
66 | + { |
|
67 | + parent::__construct(); |
|
68 | + |
|
69 | + // Restrict usage to the command line. |
|
70 | + if (! is_cli() ) |
|
71 | + { |
|
72 | + show_error( lang('cli_required') ); |
|
73 | + } |
|
74 | + |
|
75 | + // Make sure the CLI library is loaded and ready. |
|
76 | 76 | // CLI::_init(); |
77 | - } |
|
78 | - |
|
79 | - //-------------------------------------------------------------------- |
|
80 | - |
|
81 | - /** |
|
82 | - * A default index method that all CLI Controllers can share. Will |
|
83 | - * list all of the methods and their short descriptions, if available. |
|
84 | - */ |
|
85 | - public function index() |
|
86 | - { |
|
87 | - CLI::new_line(); |
|
88 | - CLI::write( lang('cli.available_commands') ); |
|
89 | - |
|
90 | - $this->sayDescriptions($this->descriptions); |
|
91 | - |
|
92 | - CLI::new_line(); |
|
93 | - } |
|
94 | - |
|
95 | - //-------------------------------------------------------------------- |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * Grabs the short description of a command, if it exists. |
|
100 | - * |
|
101 | - * @param null $method |
|
102 | - */ |
|
103 | - public function describeMethod($method=null) |
|
104 | - { |
|
105 | - if (empty($this->descriptions[$method])) |
|
106 | - { |
|
107 | - return CLI::error( lang('cli.bad_description') ); |
|
108 | - } |
|
109 | - |
|
110 | - CLI::write("\t{$this->descriptions[$method]}", 'yellow'); |
|
111 | - } |
|
112 | - |
|
113 | - //-------------------------------------------------------------------- |
|
114 | - |
|
115 | - public function longDescribeMethod($method=null) |
|
116 | - { |
|
117 | - if (empty($this->long_descriptions[$method])) |
|
118 | - { |
|
119 | - return CLI::error( lang('cli.no_help') ); |
|
120 | - } |
|
121 | - |
|
122 | - CLI::write("\t{$this->long_descriptions[$method]}", 'yellow'); |
|
123 | - } |
|
124 | - |
|
125 | - //-------------------------------------------------------------------- |
|
126 | - |
|
127 | - //-------------------------------------------------------------------- |
|
128 | - // Private Methods |
|
129 | - //-------------------------------------------------------------------- |
|
130 | - |
|
131 | - protected function sayDescriptions($descriptions) |
|
132 | - { |
|
133 | - $names = array_keys($descriptions); |
|
134 | - $syntaxes = array_column($descriptions, 0); |
|
135 | - $descs = array_column($descriptions, 1); |
|
136 | - |
|
137 | - // Pad each item to the same length |
|
138 | - $names = $this->padArray($names); |
|
139 | - $syntaxes = $this->padArray($syntaxes); |
|
140 | - |
|
141 | - for ($i=0; $i < count($names); $i++) |
|
142 | - { |
|
143 | - $out = CLI::color($names[$i], 'yellow'); |
|
144 | - |
|
145 | - // The rest of the items stay default color. |
|
146 | - if (isset($syntaxes[$i])) |
|
147 | - { |
|
148 | - $out .= $syntaxes[$i]; |
|
149 | - } |
|
150 | - |
|
151 | - if (isset($descs[$i])) |
|
152 | - { |
|
153 | - $out .= CLI::wrap($descs[$i], 125, strlen($names[$i]) + strlen($syntaxes[$i])); |
|
154 | - } |
|
155 | - |
|
156 | - CLI::write($out); |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - //-------------------------------------------------------------------- |
|
161 | - |
|
162 | - /** |
|
163 | - * Returns a new array where all of the string elements have |
|
164 | - * been padding with trailling spaces to be the same length. |
|
165 | - * |
|
166 | - * @param array $array |
|
167 | - * @param int $extra // How many extra spaces to add at the end |
|
168 | - * @return array |
|
169 | - */ |
|
170 | - protected function padArray($array, $extra=2) |
|
171 | - { |
|
172 | - $max = max(array_map('strlen', $array)) + $extra; |
|
173 | - |
|
174 | - foreach ($array as &$item) |
|
175 | - { |
|
176 | - $item = str_pad($item, $max); |
|
177 | - } |
|
178 | - |
|
179 | - return $array; |
|
180 | - } |
|
181 | - |
|
182 | - //-------------------------------------------------------------------- |
|
77 | + } |
|
78 | + |
|
79 | + //-------------------------------------------------------------------- |
|
80 | + |
|
81 | + /** |
|
82 | + * A default index method that all CLI Controllers can share. Will |
|
83 | + * list all of the methods and their short descriptions, if available. |
|
84 | + */ |
|
85 | + public function index() |
|
86 | + { |
|
87 | + CLI::new_line(); |
|
88 | + CLI::write( lang('cli.available_commands') ); |
|
89 | + |
|
90 | + $this->sayDescriptions($this->descriptions); |
|
91 | + |
|
92 | + CLI::new_line(); |
|
93 | + } |
|
94 | + |
|
95 | + //-------------------------------------------------------------------- |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * Grabs the short description of a command, if it exists. |
|
100 | + * |
|
101 | + * @param null $method |
|
102 | + */ |
|
103 | + public function describeMethod($method=null) |
|
104 | + { |
|
105 | + if (empty($this->descriptions[$method])) |
|
106 | + { |
|
107 | + return CLI::error( lang('cli.bad_description') ); |
|
108 | + } |
|
109 | + |
|
110 | + CLI::write("\t{$this->descriptions[$method]}", 'yellow'); |
|
111 | + } |
|
112 | + |
|
113 | + //-------------------------------------------------------------------- |
|
114 | + |
|
115 | + public function longDescribeMethod($method=null) |
|
116 | + { |
|
117 | + if (empty($this->long_descriptions[$method])) |
|
118 | + { |
|
119 | + return CLI::error( lang('cli.no_help') ); |
|
120 | + } |
|
121 | + |
|
122 | + CLI::write("\t{$this->long_descriptions[$method]}", 'yellow'); |
|
123 | + } |
|
124 | + |
|
125 | + //-------------------------------------------------------------------- |
|
126 | + |
|
127 | + //-------------------------------------------------------------------- |
|
128 | + // Private Methods |
|
129 | + //-------------------------------------------------------------------- |
|
130 | + |
|
131 | + protected function sayDescriptions($descriptions) |
|
132 | + { |
|
133 | + $names = array_keys($descriptions); |
|
134 | + $syntaxes = array_column($descriptions, 0); |
|
135 | + $descs = array_column($descriptions, 1); |
|
136 | + |
|
137 | + // Pad each item to the same length |
|
138 | + $names = $this->padArray($names); |
|
139 | + $syntaxes = $this->padArray($syntaxes); |
|
140 | + |
|
141 | + for ($i=0; $i < count($names); $i++) |
|
142 | + { |
|
143 | + $out = CLI::color($names[$i], 'yellow'); |
|
144 | + |
|
145 | + // The rest of the items stay default color. |
|
146 | + if (isset($syntaxes[$i])) |
|
147 | + { |
|
148 | + $out .= $syntaxes[$i]; |
|
149 | + } |
|
150 | + |
|
151 | + if (isset($descs[$i])) |
|
152 | + { |
|
153 | + $out .= CLI::wrap($descs[$i], 125, strlen($names[$i]) + strlen($syntaxes[$i])); |
|
154 | + } |
|
155 | + |
|
156 | + CLI::write($out); |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + //-------------------------------------------------------------------- |
|
161 | + |
|
162 | + /** |
|
163 | + * Returns a new array where all of the string elements have |
|
164 | + * been padding with trailling spaces to be the same length. |
|
165 | + * |
|
166 | + * @param array $array |
|
167 | + * @param int $extra // How many extra spaces to add at the end |
|
168 | + * @return array |
|
169 | + */ |
|
170 | + protected function padArray($array, $extra=2) |
|
171 | + { |
|
172 | + $max = max(array_map('strlen', $array)) + $extra; |
|
173 | + |
|
174 | + foreach ($array as &$item) |
|
175 | + { |
|
176 | + $item = str_pad($item, $max); |
|
177 | + } |
|
178 | + |
|
179 | + return $array; |
|
180 | + } |
|
181 | + |
|
182 | + //-------------------------------------------------------------------- |
|
183 | 183 | |
184 | 184 | } |
@@ -67,9 +67,9 @@ discard block |
||
67 | 67 | parent::__construct(); |
68 | 68 | |
69 | 69 | // Restrict usage to the command line. |
70 | - if (! is_cli() ) |
|
70 | + if ( ! is_cli()) |
|
71 | 71 | { |
72 | - show_error( lang('cli_required') ); |
|
72 | + show_error(lang('cli_required')); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | // Make sure the CLI library is loaded and ready. |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | public function index() |
86 | 86 | { |
87 | 87 | CLI::new_line(); |
88 | - CLI::write( lang('cli.available_commands') ); |
|
88 | + CLI::write(lang('cli.available_commands')); |
|
89 | 89 | |
90 | 90 | $this->sayDescriptions($this->descriptions); |
91 | 91 | |
@@ -100,11 +100,11 @@ discard block |
||
100 | 100 | * |
101 | 101 | * @param null $method |
102 | 102 | */ |
103 | - public function describeMethod($method=null) |
|
103 | + public function describeMethod($method = null) |
|
104 | 104 | { |
105 | 105 | if (empty($this->descriptions[$method])) |
106 | 106 | { |
107 | - return CLI::error( lang('cli.bad_description') ); |
|
107 | + return CLI::error(lang('cli.bad_description')); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | CLI::write("\t{$this->descriptions[$method]}", 'yellow'); |
@@ -112,11 +112,11 @@ discard block |
||
112 | 112 | |
113 | 113 | //-------------------------------------------------------------------- |
114 | 114 | |
115 | - public function longDescribeMethod($method=null) |
|
115 | + public function longDescribeMethod($method = null) |
|
116 | 116 | { |
117 | 117 | if (empty($this->long_descriptions[$method])) |
118 | 118 | { |
119 | - return CLI::error( lang('cli.no_help') ); |
|
119 | + return CLI::error(lang('cli.no_help')); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | CLI::write("\t{$this->long_descriptions[$method]}", 'yellow'); |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | $names = $this->padArray($names); |
139 | 139 | $syntaxes = $this->padArray($syntaxes); |
140 | 140 | |
141 | - for ($i=0; $i < count($names); $i++) |
|
141 | + for ($i = 0; $i < count($names); $i++) |
|
142 | 142 | { |
143 | 143 | $out = CLI::color($names[$i], 'yellow'); |
144 | 144 | |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | * @param int $extra // How many extra spaces to add at the end |
168 | 168 | * @return array |
169 | 169 | */ |
170 | - protected function padArray($array, $extra=2) |
|
170 | + protected function padArray($array, $extra = 2) |
|
171 | 171 | { |
172 | 172 | $max = max(array_map('strlen', $array)) + $extra; |
173 | 173 |
@@ -34,257 +34,257 @@ |
||
34 | 34 | |
35 | 35 | class CronManager { |
36 | 36 | |
37 | - protected static $tasks = []; |
|
38 | - |
|
39 | - //-------------------------------------------------------------------- |
|
40 | - |
|
41 | - /** |
|
42 | - * Schedules a new task in the system. |
|
43 | - * |
|
44 | - * @param $alias |
|
45 | - * @param $time_string |
|
46 | - * @param callable|string $task |
|
47 | - */ |
|
48 | - public static function schedule($alias, $time_string, $task) |
|
49 | - { |
|
50 | - // Valid Alias? |
|
51 | - if (! is_string($alias) || empty($alias)) |
|
52 | - { |
|
53 | - throw new \RuntimeException( lang('cron.bad_alias') ); |
|
54 | - } |
|
55 | - |
|
56 | - // Valid TimeString? |
|
57 | - if (! is_string($time_string) || empty($time_string)) |
|
58 | - { |
|
59 | - throw new \RuntimeException( lang('cron.bad_timestring') ); |
|
60 | - } |
|
61 | - |
|
62 | - // Valid Task? |
|
63 | - if (! is_callable($task) && ! is_string($task)) |
|
64 | - { |
|
65 | - throw new \RuntimeException( lang('cron.bad_task') . $alias); |
|
66 | - } |
|
67 | - |
|
68 | - static::$tasks[$alias] = new CronTask($time_string, $task); |
|
69 | - } |
|
70 | - |
|
71 | - //-------------------------------------------------------------------- |
|
72 | - |
|
73 | - /** |
|
74 | - * Removes a single task from the system. |
|
75 | - * |
|
76 | - * @param $alias |
|
77 | - */ |
|
78 | - public static function remove($alias) |
|
79 | - { |
|
80 | - if (empty(static::$tasks[$alias])) |
|
81 | - { |
|
82 | - return null; |
|
83 | - } |
|
84 | - |
|
85 | - unset(static::$tasks[$alias]); |
|
86 | - |
|
87 | - return true; |
|
88 | - } |
|
89 | - |
|
90 | - //-------------------------------------------------------------------- |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Provides an array of all tasks in the system. The format will be |
|
95 | - * like: |
|
96 | - * [ |
|
97 | - * 'alias' => [ |
|
98 | - * 'next_run' => '123456789', |
|
99 | - * 'prev_run' => '123456789', |
|
100 | - * 'task' => mixed |
|
101 | - * ], |
|
102 | - * ... |
|
103 | - * ] |
|
104 | - */ |
|
105 | - public static function listAll($current_time='now') |
|
106 | - { |
|
107 | - if (! count(static::$tasks)) |
|
108 | - { |
|
109 | - return null; |
|
110 | - } |
|
111 | - |
|
112 | - $output = array(); |
|
113 | - |
|
114 | - foreach (static::$tasks as $alias => $task) |
|
115 | - { |
|
116 | - $output[$alias] = [ |
|
117 | - 'next_run' => $task->nextRunDate($current_time), |
|
118 | - 'prev_run' => $task->previousRunDate($current_time), |
|
119 | - 'task' => $task->task() |
|
120 | - ]; |
|
121 | - } |
|
122 | - |
|
123 | - return $output; |
|
124 | - } |
|
125 | - |
|
126 | - //-------------------------------------------------------------------- |
|
127 | - |
|
128 | - /** |
|
129 | - * Gets the task object assigned to an alias. |
|
130 | - * |
|
131 | - * @param $alias |
|
132 | - * @return null|CronTask object |
|
133 | - */ |
|
134 | - public static function task($alias) |
|
135 | - { |
|
136 | - if (empty(static::$tasks[$alias]) ) |
|
137 | - { |
|
138 | - return null; |
|
139 | - } |
|
140 | - |
|
141 | - return static::$tasks[$alias]; |
|
142 | - } |
|
143 | - |
|
144 | - //-------------------------------------------------------------------- |
|
145 | - |
|
146 | - /** |
|
147 | - * Returns all tasks currently in the system. |
|
148 | - * |
|
149 | - * @return array |
|
150 | - */ |
|
151 | - public static function tasks() |
|
152 | - { |
|
153 | - return count(static::$tasks) ? static::$tasks : null; |
|
154 | - } |
|
155 | - |
|
156 | - //-------------------------------------------------------------------- |
|
157 | - |
|
158 | - /** |
|
159 | - * Runs all tasks scheduled to run right now. |
|
160 | - * |
|
161 | - * Can be limited to a single task by passing it's alias in the first param. |
|
162 | - * |
|
163 | - * Returns the output of all task runs as a single string. Perfect for |
|
164 | - * emailing to users on completion |
|
165 | - * |
|
166 | - * @param null $alias |
|
167 | - * @param bool $force_run |
|
168 | - * @param string $current_time |
|
169 | - * @return string |
|
170 | - */ |
|
171 | - public static function run($alias=null, $force_run=false, $current_time='now') |
|
172 | - { |
|
173 | - $tasks = static::$tasks; |
|
174 | - |
|
175 | - if (! empty($alias) && isset($tasks[$alias])) |
|
176 | - { |
|
177 | - $tasks = [$alias => $tasks[$alias] ]; |
|
178 | - } |
|
179 | - |
|
180 | - $output = ''; |
|
181 | - |
|
182 | - $count = 0; // How many tasks have ran? |
|
183 | - |
|
184 | - foreach ($tasks as $alias => $task) |
|
185 | - { |
|
186 | - if ($task->isDue($current_time) || $force_run === true) |
|
187 | - { |
|
188 | - $output .= sprintf( lang('cron.running_task'), $alias); |
|
189 | - |
|
190 | - try { |
|
191 | - $result = self::runTask($alias); |
|
192 | - |
|
193 | - if (is_bool($result)) |
|
194 | - { |
|
195 | - $output .= $result === true ? lang('done') ."\n" : lang('failed') ."\n"; |
|
196 | - } |
|
197 | - else if (is_string($result)) |
|
198 | - { |
|
199 | - $output .= sprintf( lang('cron.done_with_msg'), $result); |
|
200 | - } |
|
201 | - } |
|
202 | - catch (\Exception $e) |
|
203 | - { |
|
204 | - $output .= "[Exception] ". $e->getMessage() ."\n"; |
|
205 | - } |
|
206 | - |
|
207 | - $count++; |
|
208 | - } |
|
209 | - else |
|
210 | - { |
|
211 | - $output .= sprintf( lang('cron.not_scheduled_until'), $alias, $task->nextRunDate() ); |
|
212 | - } |
|
213 | - } |
|
214 | - |
|
215 | - if (! $count) |
|
216 | - { |
|
217 | - $output .= lang('cron.nothing_scheduled'); |
|
218 | - } |
|
219 | - |
|
220 | - return $output; |
|
221 | - } |
|
222 | - |
|
223 | - //-------------------------------------------------------------------- |
|
224 | - |
|
225 | - /** |
|
226 | - * Clears all tasks from the system |
|
227 | - */ |
|
228 | - public static function reset() |
|
229 | - { |
|
230 | - static::$tasks = []; |
|
231 | - } |
|
232 | - |
|
233 | - //-------------------------------------------------------------------- |
|
234 | - |
|
235 | - //-------------------------------------------------------------------- |
|
236 | - // Protected Methods |
|
237 | - //-------------------------------------------------------------------- |
|
238 | - |
|
239 | - /** |
|
240 | - * Runs a single Task. |
|
241 | - * |
|
242 | - * NOTE: Tasks MUST return a true/false value only. |
|
243 | - * |
|
244 | - * @param $alias |
|
245 | - * @return bool |
|
246 | - */ |
|
247 | - protected static function runTask($alias) |
|
248 | - { |
|
249 | - $task = static::$tasks[$alias]->task(); |
|
250 | - |
|
251 | - $success = false; |
|
252 | - |
|
253 | - // If it's a standard callable item, |
|
254 | - // then run it. |
|
255 | - if (is_callable($task)) |
|
256 | - { |
|
257 | - $success = call_user_func($task); |
|
258 | - } |
|
259 | - |
|
260 | - // Otherwise, if it's a string it should be |
|
261 | - // a library:method string so try to run it. |
|
262 | - else if (is_string($task) && ! empty($task) && strpos($task, ':') !== false) |
|
263 | - { |
|
264 | - list($class, $method) = explode(':', $task); |
|
265 | - |
|
266 | - // Let PHP try to autoload it through any available autoloaders |
|
267 | - // (including Composer and user's custom autoloaders). If we |
|
268 | - // don't find it, then assume it's a CI library that we can reach. |
|
269 | - if (class_exists($class)) { |
|
270 | - $class = new $class(); |
|
271 | - } else { |
|
272 | - get_instance()->load->library($class); |
|
273 | - $class =& get_instance()->$class; |
|
274 | - } |
|
275 | - |
|
276 | - if (! method_exists($class, $method)) { |
|
277 | - log_message('error', "[CRON] Method not found: {$class}::{$method}"); |
|
278 | - return $success; |
|
279 | - } |
|
280 | - |
|
281 | - // Call the class with our parameters |
|
282 | - $success = $class->{$method}(); |
|
283 | - } |
|
284 | - |
|
285 | - return $success; |
|
286 | - } |
|
287 | - |
|
288 | - //-------------------------------------------------------------------- |
|
37 | + protected static $tasks = []; |
|
38 | + |
|
39 | + //-------------------------------------------------------------------- |
|
40 | + |
|
41 | + /** |
|
42 | + * Schedules a new task in the system. |
|
43 | + * |
|
44 | + * @param $alias |
|
45 | + * @param $time_string |
|
46 | + * @param callable|string $task |
|
47 | + */ |
|
48 | + public static function schedule($alias, $time_string, $task) |
|
49 | + { |
|
50 | + // Valid Alias? |
|
51 | + if (! is_string($alias) || empty($alias)) |
|
52 | + { |
|
53 | + throw new \RuntimeException( lang('cron.bad_alias') ); |
|
54 | + } |
|
55 | + |
|
56 | + // Valid TimeString? |
|
57 | + if (! is_string($time_string) || empty($time_string)) |
|
58 | + { |
|
59 | + throw new \RuntimeException( lang('cron.bad_timestring') ); |
|
60 | + } |
|
61 | + |
|
62 | + // Valid Task? |
|
63 | + if (! is_callable($task) && ! is_string($task)) |
|
64 | + { |
|
65 | + throw new \RuntimeException( lang('cron.bad_task') . $alias); |
|
66 | + } |
|
67 | + |
|
68 | + static::$tasks[$alias] = new CronTask($time_string, $task); |
|
69 | + } |
|
70 | + |
|
71 | + //-------------------------------------------------------------------- |
|
72 | + |
|
73 | + /** |
|
74 | + * Removes a single task from the system. |
|
75 | + * |
|
76 | + * @param $alias |
|
77 | + */ |
|
78 | + public static function remove($alias) |
|
79 | + { |
|
80 | + if (empty(static::$tasks[$alias])) |
|
81 | + { |
|
82 | + return null; |
|
83 | + } |
|
84 | + |
|
85 | + unset(static::$tasks[$alias]); |
|
86 | + |
|
87 | + return true; |
|
88 | + } |
|
89 | + |
|
90 | + //-------------------------------------------------------------------- |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Provides an array of all tasks in the system. The format will be |
|
95 | + * like: |
|
96 | + * [ |
|
97 | + * 'alias' => [ |
|
98 | + * 'next_run' => '123456789', |
|
99 | + * 'prev_run' => '123456789', |
|
100 | + * 'task' => mixed |
|
101 | + * ], |
|
102 | + * ... |
|
103 | + * ] |
|
104 | + */ |
|
105 | + public static function listAll($current_time='now') |
|
106 | + { |
|
107 | + if (! count(static::$tasks)) |
|
108 | + { |
|
109 | + return null; |
|
110 | + } |
|
111 | + |
|
112 | + $output = array(); |
|
113 | + |
|
114 | + foreach (static::$tasks as $alias => $task) |
|
115 | + { |
|
116 | + $output[$alias] = [ |
|
117 | + 'next_run' => $task->nextRunDate($current_time), |
|
118 | + 'prev_run' => $task->previousRunDate($current_time), |
|
119 | + 'task' => $task->task() |
|
120 | + ]; |
|
121 | + } |
|
122 | + |
|
123 | + return $output; |
|
124 | + } |
|
125 | + |
|
126 | + //-------------------------------------------------------------------- |
|
127 | + |
|
128 | + /** |
|
129 | + * Gets the task object assigned to an alias. |
|
130 | + * |
|
131 | + * @param $alias |
|
132 | + * @return null|CronTask object |
|
133 | + */ |
|
134 | + public static function task($alias) |
|
135 | + { |
|
136 | + if (empty(static::$tasks[$alias]) ) |
|
137 | + { |
|
138 | + return null; |
|
139 | + } |
|
140 | + |
|
141 | + return static::$tasks[$alias]; |
|
142 | + } |
|
143 | + |
|
144 | + //-------------------------------------------------------------------- |
|
145 | + |
|
146 | + /** |
|
147 | + * Returns all tasks currently in the system. |
|
148 | + * |
|
149 | + * @return array |
|
150 | + */ |
|
151 | + public static function tasks() |
|
152 | + { |
|
153 | + return count(static::$tasks) ? static::$tasks : null; |
|
154 | + } |
|
155 | + |
|
156 | + //-------------------------------------------------------------------- |
|
157 | + |
|
158 | + /** |
|
159 | + * Runs all tasks scheduled to run right now. |
|
160 | + * |
|
161 | + * Can be limited to a single task by passing it's alias in the first param. |
|
162 | + * |
|
163 | + * Returns the output of all task runs as a single string. Perfect for |
|
164 | + * emailing to users on completion |
|
165 | + * |
|
166 | + * @param null $alias |
|
167 | + * @param bool $force_run |
|
168 | + * @param string $current_time |
|
169 | + * @return string |
|
170 | + */ |
|
171 | + public static function run($alias=null, $force_run=false, $current_time='now') |
|
172 | + { |
|
173 | + $tasks = static::$tasks; |
|
174 | + |
|
175 | + if (! empty($alias) && isset($tasks[$alias])) |
|
176 | + { |
|
177 | + $tasks = [$alias => $tasks[$alias] ]; |
|
178 | + } |
|
179 | + |
|
180 | + $output = ''; |
|
181 | + |
|
182 | + $count = 0; // How many tasks have ran? |
|
183 | + |
|
184 | + foreach ($tasks as $alias => $task) |
|
185 | + { |
|
186 | + if ($task->isDue($current_time) || $force_run === true) |
|
187 | + { |
|
188 | + $output .= sprintf( lang('cron.running_task'), $alias); |
|
189 | + |
|
190 | + try { |
|
191 | + $result = self::runTask($alias); |
|
192 | + |
|
193 | + if (is_bool($result)) |
|
194 | + { |
|
195 | + $output .= $result === true ? lang('done') ."\n" : lang('failed') ."\n"; |
|
196 | + } |
|
197 | + else if (is_string($result)) |
|
198 | + { |
|
199 | + $output .= sprintf( lang('cron.done_with_msg'), $result); |
|
200 | + } |
|
201 | + } |
|
202 | + catch (\Exception $e) |
|
203 | + { |
|
204 | + $output .= "[Exception] ". $e->getMessage() ."\n"; |
|
205 | + } |
|
206 | + |
|
207 | + $count++; |
|
208 | + } |
|
209 | + else |
|
210 | + { |
|
211 | + $output .= sprintf( lang('cron.not_scheduled_until'), $alias, $task->nextRunDate() ); |
|
212 | + } |
|
213 | + } |
|
214 | + |
|
215 | + if (! $count) |
|
216 | + { |
|
217 | + $output .= lang('cron.nothing_scheduled'); |
|
218 | + } |
|
219 | + |
|
220 | + return $output; |
|
221 | + } |
|
222 | + |
|
223 | + //-------------------------------------------------------------------- |
|
224 | + |
|
225 | + /** |
|
226 | + * Clears all tasks from the system |
|
227 | + */ |
|
228 | + public static function reset() |
|
229 | + { |
|
230 | + static::$tasks = []; |
|
231 | + } |
|
232 | + |
|
233 | + //-------------------------------------------------------------------- |
|
234 | + |
|
235 | + //-------------------------------------------------------------------- |
|
236 | + // Protected Methods |
|
237 | + //-------------------------------------------------------------------- |
|
238 | + |
|
239 | + /** |
|
240 | + * Runs a single Task. |
|
241 | + * |
|
242 | + * NOTE: Tasks MUST return a true/false value only. |
|
243 | + * |
|
244 | + * @param $alias |
|
245 | + * @return bool |
|
246 | + */ |
|
247 | + protected static function runTask($alias) |
|
248 | + { |
|
249 | + $task = static::$tasks[$alias]->task(); |
|
250 | + |
|
251 | + $success = false; |
|
252 | + |
|
253 | + // If it's a standard callable item, |
|
254 | + // then run it. |
|
255 | + if (is_callable($task)) |
|
256 | + { |
|
257 | + $success = call_user_func($task); |
|
258 | + } |
|
259 | + |
|
260 | + // Otherwise, if it's a string it should be |
|
261 | + // a library:method string so try to run it. |
|
262 | + else if (is_string($task) && ! empty($task) && strpos($task, ':') !== false) |
|
263 | + { |
|
264 | + list($class, $method) = explode(':', $task); |
|
265 | + |
|
266 | + // Let PHP try to autoload it through any available autoloaders |
|
267 | + // (including Composer and user's custom autoloaders). If we |
|
268 | + // don't find it, then assume it's a CI library that we can reach. |
|
269 | + if (class_exists($class)) { |
|
270 | + $class = new $class(); |
|
271 | + } else { |
|
272 | + get_instance()->load->library($class); |
|
273 | + $class =& get_instance()->$class; |
|
274 | + } |
|
275 | + |
|
276 | + if (! method_exists($class, $method)) { |
|
277 | + log_message('error', "[CRON] Method not found: {$class}::{$method}"); |
|
278 | + return $success; |
|
279 | + } |
|
280 | + |
|
281 | + // Call the class with our parameters |
|
282 | + $success = $class->{$method}(); |
|
283 | + } |
|
284 | + |
|
285 | + return $success; |
|
286 | + } |
|
287 | + |
|
288 | + //-------------------------------------------------------------------- |
|
289 | 289 | |
290 | 290 | } |
@@ -48,21 +48,21 @@ discard block |
||
48 | 48 | public static function schedule($alias, $time_string, $task) |
49 | 49 | { |
50 | 50 | // Valid Alias? |
51 | - if (! is_string($alias) || empty($alias)) |
|
51 | + if ( ! is_string($alias) || empty($alias)) |
|
52 | 52 | { |
53 | - throw new \RuntimeException( lang('cron.bad_alias') ); |
|
53 | + throw new \RuntimeException(lang('cron.bad_alias')); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | // Valid TimeString? |
57 | - if (! is_string($time_string) || empty($time_string)) |
|
57 | + if ( ! is_string($time_string) || empty($time_string)) |
|
58 | 58 | { |
59 | - throw new \RuntimeException( lang('cron.bad_timestring') ); |
|
59 | + throw new \RuntimeException(lang('cron.bad_timestring')); |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | // Valid Task? |
63 | - if (! is_callable($task) && ! is_string($task)) |
|
63 | + if ( ! is_callable($task) && ! is_string($task)) |
|
64 | 64 | { |
65 | - throw new \RuntimeException( lang('cron.bad_task') . $alias); |
|
65 | + throw new \RuntimeException(lang('cron.bad_task').$alias); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | static::$tasks[$alias] = new CronTask($time_string, $task); |
@@ -102,9 +102,9 @@ discard block |
||
102 | 102 | * ... |
103 | 103 | * ] |
104 | 104 | */ |
105 | - public static function listAll($current_time='now') |
|
105 | + public static function listAll($current_time = 'now') |
|
106 | 106 | { |
107 | - if (! count(static::$tasks)) |
|
107 | + if ( ! count(static::$tasks)) |
|
108 | 108 | { |
109 | 109 | return null; |
110 | 110 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public static function task($alias) |
135 | 135 | { |
136 | - if (empty(static::$tasks[$alias]) ) |
|
136 | + if (empty(static::$tasks[$alias])) |
|
137 | 137 | { |
138 | 138 | return null; |
139 | 139 | } |
@@ -168,13 +168,13 @@ discard block |
||
168 | 168 | * @param string $current_time |
169 | 169 | * @return string |
170 | 170 | */ |
171 | - public static function run($alias=null, $force_run=false, $current_time='now') |
|
171 | + public static function run($alias = null, $force_run = false, $current_time = 'now') |
|
172 | 172 | { |
173 | 173 | $tasks = static::$tasks; |
174 | 174 | |
175 | - if (! empty($alias) && isset($tasks[$alias])) |
|
175 | + if ( ! empty($alias) && isset($tasks[$alias])) |
|
176 | 176 | { |
177 | - $tasks = [$alias => $tasks[$alias] ]; |
|
177 | + $tasks = [$alias => $tasks[$alias]]; |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | $output = ''; |
@@ -185,34 +185,34 @@ discard block |
||
185 | 185 | { |
186 | 186 | if ($task->isDue($current_time) || $force_run === true) |
187 | 187 | { |
188 | - $output .= sprintf( lang('cron.running_task'), $alias); |
|
188 | + $output .= sprintf(lang('cron.running_task'), $alias); |
|
189 | 189 | |
190 | 190 | try { |
191 | 191 | $result = self::runTask($alias); |
192 | 192 | |
193 | 193 | if (is_bool($result)) |
194 | 194 | { |
195 | - $output .= $result === true ? lang('done') ."\n" : lang('failed') ."\n"; |
|
195 | + $output .= $result === true ? lang('done')."\n" : lang('failed')."\n"; |
|
196 | 196 | } |
197 | 197 | else if (is_string($result)) |
198 | 198 | { |
199 | - $output .= sprintf( lang('cron.done_with_msg'), $result); |
|
199 | + $output .= sprintf(lang('cron.done_with_msg'), $result); |
|
200 | 200 | } |
201 | 201 | } |
202 | 202 | catch (\Exception $e) |
203 | 203 | { |
204 | - $output .= "[Exception] ". $e->getMessage() ."\n"; |
|
204 | + $output .= "[Exception] ".$e->getMessage()."\n"; |
|
205 | 205 | } |
206 | 206 | |
207 | 207 | $count++; |
208 | 208 | } |
209 | 209 | else |
210 | 210 | { |
211 | - $output .= sprintf( lang('cron.not_scheduled_until'), $alias, $task->nextRunDate() ); |
|
211 | + $output .= sprintf(lang('cron.not_scheduled_until'), $alias, $task->nextRunDate()); |
|
212 | 212 | } |
213 | 213 | } |
214 | 214 | |
215 | - if (! $count) |
|
215 | + if ( ! $count) |
|
216 | 216 | { |
217 | 217 | $output .= lang('cron.nothing_scheduled'); |
218 | 218 | } |
@@ -270,10 +270,10 @@ discard block |
||
270 | 270 | $class = new $class(); |
271 | 271 | } else { |
272 | 272 | get_instance()->load->library($class); |
273 | - $class =& get_instance()->$class; |
|
273 | + $class = & get_instance()->$class; |
|
274 | 274 | } |
275 | 275 | |
276 | - if (! method_exists($class, $method)) { |
|
276 | + if ( ! method_exists($class, $method)) { |
|
277 | 277 | log_message('error', "[CRON] Method not found: {$class}::{$method}"); |
278 | 278 | return $success; |
279 | 279 | } |
@@ -193,20 +193,17 @@ |
||
193 | 193 | if (is_bool($result)) |
194 | 194 | { |
195 | 195 | $output .= $result === true ? lang('done') ."\n" : lang('failed') ."\n"; |
196 | - } |
|
197 | - else if (is_string($result)) |
|
196 | + } else if (is_string($result)) |
|
198 | 197 | { |
199 | 198 | $output .= sprintf( lang('cron.done_with_msg'), $result); |
200 | 199 | } |
201 | - } |
|
202 | - catch (\Exception $e) |
|
200 | + } catch (\Exception $e) |
|
203 | 201 | { |
204 | 202 | $output .= "[Exception] ". $e->getMessage() ."\n"; |
205 | 203 | } |
206 | 204 | |
207 | 205 | $count++; |
208 | - } |
|
209 | - else |
|
206 | + } else |
|
210 | 207 | { |
211 | 208 | $output .= sprintf( lang('cron.not_scheduled_until'), $alias, $task->nextRunDate() ); |
212 | 209 | } |
@@ -45,301 +45,301 @@ |
||
45 | 45 | */ |
46 | 46 | class CronTask { |
47 | 47 | |
48 | - /** |
|
49 | - * The original scheduled string. |
|
50 | - * Any valid relative time string. |
|
51 | - * http://php.net/manual/en/datetime.formats.relative.php |
|
52 | - * |
|
53 | - * @var |
|
54 | - */ |
|
55 | - protected $schedule; |
|
56 | - |
|
57 | - /** |
|
58 | - * Stores the callable or library name:method to run. |
|
59 | - * |
|
60 | - * @var |
|
61 | - */ |
|
62 | - protected $task; |
|
63 | - |
|
64 | - //-------------------------------------------------------------------- |
|
65 | - |
|
66 | - /** |
|
67 | - * Stores our scheduled string and actual task. |
|
68 | - * |
|
69 | - * @param $schedule |
|
70 | - * @param callable $task |
|
71 | - */ |
|
72 | - public function __construct($schedule, $task) |
|
73 | - { |
|
74 | - $this->schedule = $schedule; |
|
75 | - |
|
76 | - // If $task is not callable, it should be a library:method |
|
77 | - // string that we can parse. But it must have the colon in the string. |
|
78 | - if (! is_callable($task) && strpos($task, ':') === false) |
|
79 | - { |
|
80 | - throw new \RuntimeException( lang('cron.invalid_task') ); |
|
81 | - } |
|
82 | - |
|
83 | - $this->task = $task; |
|
84 | - } |
|
85 | - |
|
86 | - //-------------------------------------------------------------------- |
|
87 | - |
|
88 | - /** |
|
89 | - * Calculates the next date this task is supposed to run. |
|
90 | - * |
|
91 | - * @param int|'now' $current_time |
|
92 | - * |
|
93 | - * @return timestamp|null |
|
94 | - */ |
|
95 | - public function nextRunDate($current_time='now') |
|
96 | - { |
|
97 | - $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
98 | - |
|
99 | - $scheduleType = $this->determineScheduleType($this->schedule); |
|
100 | - |
|
101 | - switch ($scheduleType) |
|
102 | - { |
|
103 | - case 'time': |
|
104 | - return $this->findDateInterval($this->schedule, 'next', $current_time); |
|
105 | - break; |
|
106 | - case 'ordinal': |
|
107 | - return strtotime($this->schedule, $current_time); |
|
108 | - break; |
|
109 | - case 'increment': |
|
110 | - return strtotime($this->schedule, $current_time); |
|
111 | - break; |
|
112 | - } |
|
113 | - |
|
114 | - return null; |
|
115 | - } |
|
48 | + /** |
|
49 | + * The original scheduled string. |
|
50 | + * Any valid relative time string. |
|
51 | + * http://php.net/manual/en/datetime.formats.relative.php |
|
52 | + * |
|
53 | + * @var |
|
54 | + */ |
|
55 | + protected $schedule; |
|
56 | + |
|
57 | + /** |
|
58 | + * Stores the callable or library name:method to run. |
|
59 | + * |
|
60 | + * @var |
|
61 | + */ |
|
62 | + protected $task; |
|
63 | + |
|
64 | + //-------------------------------------------------------------------- |
|
65 | + |
|
66 | + /** |
|
67 | + * Stores our scheduled string and actual task. |
|
68 | + * |
|
69 | + * @param $schedule |
|
70 | + * @param callable $task |
|
71 | + */ |
|
72 | + public function __construct($schedule, $task) |
|
73 | + { |
|
74 | + $this->schedule = $schedule; |
|
75 | + |
|
76 | + // If $task is not callable, it should be a library:method |
|
77 | + // string that we can parse. But it must have the colon in the string. |
|
78 | + if (! is_callable($task) && strpos($task, ':') === false) |
|
79 | + { |
|
80 | + throw new \RuntimeException( lang('cron.invalid_task') ); |
|
81 | + } |
|
82 | + |
|
83 | + $this->task = $task; |
|
84 | + } |
|
85 | + |
|
86 | + //-------------------------------------------------------------------- |
|
87 | + |
|
88 | + /** |
|
89 | + * Calculates the next date this task is supposed to run. |
|
90 | + * |
|
91 | + * @param int|'now' $current_time |
|
92 | + * |
|
93 | + * @return timestamp|null |
|
94 | + */ |
|
95 | + public function nextRunDate($current_time='now') |
|
96 | + { |
|
97 | + $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
98 | + |
|
99 | + $scheduleType = $this->determineScheduleType($this->schedule); |
|
100 | + |
|
101 | + switch ($scheduleType) |
|
102 | + { |
|
103 | + case 'time': |
|
104 | + return $this->findDateInterval($this->schedule, 'next', $current_time); |
|
105 | + break; |
|
106 | + case 'ordinal': |
|
107 | + return strtotime($this->schedule, $current_time); |
|
108 | + break; |
|
109 | + case 'increment': |
|
110 | + return strtotime($this->schedule, $current_time); |
|
111 | + break; |
|
112 | + } |
|
113 | + |
|
114 | + return null; |
|
115 | + } |
|
116 | 116 | |
117 | - //-------------------------------------------------------------------- |
|
118 | - |
|
119 | - /** |
|
120 | - * Calculates the last time the task should have ran. |
|
121 | - * |
|
122 | - * @param int|'now' $current_time |
|
123 | - * |
|
124 | - * @return timestamp|null |
|
125 | - */ |
|
126 | - public function previousRunDate($current_time='now') |
|
127 | - { |
|
128 | - $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
129 | - |
|
130 | - $scheduleType = $this->determineScheduleType($this->schedule); |
|
131 | - |
|
132 | - switch ($scheduleType) |
|
133 | - { |
|
134 | - case 'time': |
|
135 | - return $this->findDateInterval($this->schedule, 'prev', $current_time); |
|
136 | - break; |
|
137 | - case 'ordinal': |
|
138 | - return $this->findPreviousOrdinal($this->schedule, $current_time); |
|
139 | - break; |
|
140 | - case 'increment': |
|
141 | - return strtotime('-1 '. $this->schedule, $current_time); |
|
142 | - break; |
|
143 | - } |
|
144 | - |
|
145 | - return null; |
|
146 | - } |
|
147 | - |
|
148 | - //-------------------------------------------------------------------- |
|
149 | - |
|
150 | - /** |
|
151 | - * Determines if the task is due to be run now. |
|
152 | - * |
|
153 | - * @param string $current_time |
|
154 | - * @internal param $ int|'now' $current_time |
|
155 | - * |
|
156 | - * @return bool |
|
157 | - */ |
|
158 | - public function isDue($current_time='now') |
|
159 | - { |
|
160 | - $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
161 | - |
|
162 | - // For easier matching, and I can't imagine people needing cronjob |
|
163 | - // accuracy to the seconds, we'll just take the current minute. |
|
164 | - return date('Y-m-d H:i', $current_time) == date('Y-m-d H:i', $this->nextRunDate($current_time) ); |
|
165 | - } |
|
117 | + //-------------------------------------------------------------------- |
|
118 | + |
|
119 | + /** |
|
120 | + * Calculates the last time the task should have ran. |
|
121 | + * |
|
122 | + * @param int|'now' $current_time |
|
123 | + * |
|
124 | + * @return timestamp|null |
|
125 | + */ |
|
126 | + public function previousRunDate($current_time='now') |
|
127 | + { |
|
128 | + $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
129 | + |
|
130 | + $scheduleType = $this->determineScheduleType($this->schedule); |
|
131 | + |
|
132 | + switch ($scheduleType) |
|
133 | + { |
|
134 | + case 'time': |
|
135 | + return $this->findDateInterval($this->schedule, 'prev', $current_time); |
|
136 | + break; |
|
137 | + case 'ordinal': |
|
138 | + return $this->findPreviousOrdinal($this->schedule, $current_time); |
|
139 | + break; |
|
140 | + case 'increment': |
|
141 | + return strtotime('-1 '. $this->schedule, $current_time); |
|
142 | + break; |
|
143 | + } |
|
144 | + |
|
145 | + return null; |
|
146 | + } |
|
147 | + |
|
148 | + //-------------------------------------------------------------------- |
|
149 | + |
|
150 | + /** |
|
151 | + * Determines if the task is due to be run now. |
|
152 | + * |
|
153 | + * @param string $current_time |
|
154 | + * @internal param $ int|'now' $current_time |
|
155 | + * |
|
156 | + * @return bool |
|
157 | + */ |
|
158 | + public function isDue($current_time='now') |
|
159 | + { |
|
160 | + $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
161 | + |
|
162 | + // For easier matching, and I can't imagine people needing cronjob |
|
163 | + // accuracy to the seconds, we'll just take the current minute. |
|
164 | + return date('Y-m-d H:i', $current_time) == date('Y-m-d H:i', $this->nextRunDate($current_time) ); |
|
165 | + } |
|
166 | 166 | |
167 | - //-------------------------------------------------------------------- |
|
168 | - |
|
169 | - /** |
|
170 | - * Formats the timestamp produced by nextRunDate and previousRunDate |
|
171 | - * into any format available to date. |
|
172 | - * |
|
173 | - * @param $format_string |
|
174 | - * @return bool|string |
|
175 | - */ |
|
176 | - public function format($format_string) |
|
177 | - { |
|
178 | - return date($format_string, strtotime($this->schedule)); |
|
179 | - } |
|
180 | - |
|
181 | - //-------------------------------------------------------------------- |
|
182 | - |
|
183 | - /** |
|
184 | - * Gets the associated task. |
|
185 | - * |
|
186 | - * return callable|string |
|
187 | - */ |
|
188 | - public function task() |
|
189 | - { |
|
190 | - return $this->task; |
|
191 | - } |
|
192 | - |
|
193 | - //-------------------------------------------------------------------- |
|
194 | - |
|
195 | - /** |
|
196 | - * Gets the original schedule string. |
|
197 | - */ |
|
198 | - public function schedule() |
|
199 | - { |
|
200 | - return $this->schedule; |
|
201 | - } |
|
202 | - |
|
203 | - //-------------------------------------------------------------------- |
|
204 | - |
|
205 | - /** |
|
206 | - * Checks the schedule text and determines how we have to treat |
|
207 | - * the schedule when determining next and previous values. |
|
208 | - * |
|
209 | - * Potential Types are: |
|
210 | - * |
|
211 | - * - increment Can simply add a +x/-x to the front to get the value. |
|
212 | - * - time Something like "every 5 minutes" |
|
213 | - * - ordinal Like "first", "second", etc. |
|
214 | - * |
|
215 | - * @param $schedule |
|
216 | - * @return null|string |
|
217 | - */ |
|
218 | - public function determineScheduleType($schedule) |
|
219 | - { |
|
220 | - $incs = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sun', 'mon', 'tue', |
|
221 | - 'wed', 'thu', 'fri', 'sat', 'weekday', 'weekdays', 'midnight', 'noon']; |
|
222 | - $bigger_incs = [ 'back of', 'front of', 'first day of', 'last day of']; |
|
223 | - $ordinals = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth']; |
|
224 | - $schedule = trim( strtolower($schedule) ); |
|
225 | - |
|
226 | - $multiple_words = strpos($schedule, ' '); |
|
227 | - $first_word = substr($schedule, 0, $multiple_words ? $multiple_words : strlen($schedule)); |
|
228 | - |
|
229 | - // Is the first character a number? Then it's a time |
|
230 | - if ( is_numeric( $first_word ) ) |
|
231 | - { |
|
232 | - return 'time'; |
|
233 | - } |
|
234 | - |
|
235 | - |
|
236 | - // First, try the shorter increments. We do increments in |
|
237 | - // two passes becuase this should be faster than the loop. |
|
238 | - if (in_array($first_word, $incs)) |
|
239 | - { |
|
240 | - return 'increment'; |
|
241 | - } |
|
242 | - |
|
243 | - // But we have to loop before checking ordinals since |
|
244 | - // ordinals may have same first word as these phrases. |
|
245 | - foreach ($bigger_incs as $test) |
|
246 | - { |
|
247 | - if (strpos($schedule, $test) === 0) |
|
248 | - { |
|
249 | - return 'increment'; |
|
250 | - } |
|
251 | - } |
|
252 | - |
|
253 | - if (in_array($first_word, $ordinals)) |
|
254 | - { |
|
255 | - return 'ordinal'; |
|
256 | - } |
|
257 | - |
|
258 | - return null; |
|
259 | - } |
|
260 | - |
|
261 | - //-------------------------------------------------------------------- |
|
262 | - |
|
263 | - /** |
|
264 | - * Determines the correct time for 'time' type intervals where |
|
265 | - * the timestamp is expected to happen every 'x period', like |
|
266 | - * 'every 5 minutes', every 3 days, etc. |
|
267 | - * |
|
268 | - * @param $schedule |
|
269 | - * @param $type |
|
270 | - * @return float|int|null |
|
271 | - */ |
|
272 | - public function findDateInterval($schedule, $type, $current_time='now') |
|
273 | - { |
|
274 | - $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
167 | + //-------------------------------------------------------------------- |
|
168 | + |
|
169 | + /** |
|
170 | + * Formats the timestamp produced by nextRunDate and previousRunDate |
|
171 | + * into any format available to date. |
|
172 | + * |
|
173 | + * @param $format_string |
|
174 | + * @return bool|string |
|
175 | + */ |
|
176 | + public function format($format_string) |
|
177 | + { |
|
178 | + return date($format_string, strtotime($this->schedule)); |
|
179 | + } |
|
180 | + |
|
181 | + //-------------------------------------------------------------------- |
|
182 | + |
|
183 | + /** |
|
184 | + * Gets the associated task. |
|
185 | + * |
|
186 | + * return callable|string |
|
187 | + */ |
|
188 | + public function task() |
|
189 | + { |
|
190 | + return $this->task; |
|
191 | + } |
|
192 | + |
|
193 | + //-------------------------------------------------------------------- |
|
194 | + |
|
195 | + /** |
|
196 | + * Gets the original schedule string. |
|
197 | + */ |
|
198 | + public function schedule() |
|
199 | + { |
|
200 | + return $this->schedule; |
|
201 | + } |
|
202 | + |
|
203 | + //-------------------------------------------------------------------- |
|
204 | + |
|
205 | + /** |
|
206 | + * Checks the schedule text and determines how we have to treat |
|
207 | + * the schedule when determining next and previous values. |
|
208 | + * |
|
209 | + * Potential Types are: |
|
210 | + * |
|
211 | + * - increment Can simply add a +x/-x to the front to get the value. |
|
212 | + * - time Something like "every 5 minutes" |
|
213 | + * - ordinal Like "first", "second", etc. |
|
214 | + * |
|
215 | + * @param $schedule |
|
216 | + * @return null|string |
|
217 | + */ |
|
218 | + public function determineScheduleType($schedule) |
|
219 | + { |
|
220 | + $incs = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sun', 'mon', 'tue', |
|
221 | + 'wed', 'thu', 'fri', 'sat', 'weekday', 'weekdays', 'midnight', 'noon']; |
|
222 | + $bigger_incs = [ 'back of', 'front of', 'first day of', 'last day of']; |
|
223 | + $ordinals = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth']; |
|
224 | + $schedule = trim( strtolower($schedule) ); |
|
225 | + |
|
226 | + $multiple_words = strpos($schedule, ' '); |
|
227 | + $first_word = substr($schedule, 0, $multiple_words ? $multiple_words : strlen($schedule)); |
|
228 | + |
|
229 | + // Is the first character a number? Then it's a time |
|
230 | + if ( is_numeric( $first_word ) ) |
|
231 | + { |
|
232 | + return 'time'; |
|
233 | + } |
|
234 | + |
|
235 | + |
|
236 | + // First, try the shorter increments. We do increments in |
|
237 | + // two passes becuase this should be faster than the loop. |
|
238 | + if (in_array($first_word, $incs)) |
|
239 | + { |
|
240 | + return 'increment'; |
|
241 | + } |
|
242 | + |
|
243 | + // But we have to loop before checking ordinals since |
|
244 | + // ordinals may have same first word as these phrases. |
|
245 | + foreach ($bigger_incs as $test) |
|
246 | + { |
|
247 | + if (strpos($schedule, $test) === 0) |
|
248 | + { |
|
249 | + return 'increment'; |
|
250 | + } |
|
251 | + } |
|
252 | + |
|
253 | + if (in_array($first_word, $ordinals)) |
|
254 | + { |
|
255 | + return 'ordinal'; |
|
256 | + } |
|
257 | + |
|
258 | + return null; |
|
259 | + } |
|
260 | + |
|
261 | + //-------------------------------------------------------------------- |
|
262 | + |
|
263 | + /** |
|
264 | + * Determines the correct time for 'time' type intervals where |
|
265 | + * the timestamp is expected to happen every 'x period', like |
|
266 | + * 'every 5 minutes', every 3 days, etc. |
|
267 | + * |
|
268 | + * @param $schedule |
|
269 | + * @param $type |
|
270 | + * @return float|int|null |
|
271 | + */ |
|
272 | + public function findDateInterval($schedule, $type, $current_time='now') |
|
273 | + { |
|
274 | + $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
275 | 275 | |
276 | 276 | // list($int, $period) = explode(' ', $schedule); |
277 | 277 | |
278 | - $diff = strtotime($schedule, $current_time) - $current_time; |
|
279 | - |
|
280 | - $return = null; |
|
281 | - |
|
282 | - switch ($type) |
|
283 | - { |
|
284 | - case 'next': |
|
285 | - $next = floor($current_time / $diff) * $diff; |
|
286 | - |
|
287 | - // Does next already match the current time? |
|
288 | - if (date('Y-m-d H:i', $next) == date('Y-m-d H:i', $current_time)) |
|
289 | - { |
|
290 | - $return = $next; |
|
291 | - } |
|
292 | - else { |
|
293 | - $return = $next + $diff; |
|
294 | - } |
|
295 | - break; |
|
296 | - case 'prev': |
|
297 | - $next = ceil($current_time / $diff) * $diff; |
|
298 | - $return = $next - $diff; |
|
299 | - break; |
|
300 | - } |
|
301 | - |
|
302 | - if (is_numeric($return)) |
|
303 | - { |
|
304 | - $return = (int)$return; |
|
305 | - } |
|
306 | - |
|
307 | - return $return; |
|
308 | - } |
|
309 | - |
|
310 | - //-------------------------------------------------------------------- |
|
311 | - |
|
312 | - /** |
|
313 | - * Determines the timestamp of the previous ordinal-based time, like |
|
314 | - * 'second Monday'. |
|
315 | - * |
|
316 | - * @param $schedule |
|
317 | - * @param string $current_time |
|
318 | - * @return int|null |
|
319 | - */ |
|
320 | - public function findPreviousOrdinal($schedule, $current_time='now') |
|
321 | - { |
|
322 | - $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
323 | - |
|
324 | - if (empty($schedule)) return null; |
|
325 | - |
|
326 | - // Loop through months in reverse, checking each one to |
|
327 | - // see if the ordinal is in the past. If so - wer'e done. |
|
328 | - foreach ([0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12] as $i) |
|
329 | - { |
|
330 | - $lastmonth = strtotime("last day of {$i} month", $current_time); |
|
331 | - |
|
332 | - $test = strtotime($schedule, $lastmonth); |
|
333 | - |
|
334 | - if ($test <= $current_time) |
|
335 | - { |
|
336 | - return $test; |
|
337 | - } |
|
338 | - } |
|
339 | - |
|
340 | - return null; |
|
341 | - } |
|
342 | - |
|
343 | - //-------------------------------------------------------------------- |
|
278 | + $diff = strtotime($schedule, $current_time) - $current_time; |
|
279 | + |
|
280 | + $return = null; |
|
281 | + |
|
282 | + switch ($type) |
|
283 | + { |
|
284 | + case 'next': |
|
285 | + $next = floor($current_time / $diff) * $diff; |
|
286 | + |
|
287 | + // Does next already match the current time? |
|
288 | + if (date('Y-m-d H:i', $next) == date('Y-m-d H:i', $current_time)) |
|
289 | + { |
|
290 | + $return = $next; |
|
291 | + } |
|
292 | + else { |
|
293 | + $return = $next + $diff; |
|
294 | + } |
|
295 | + break; |
|
296 | + case 'prev': |
|
297 | + $next = ceil($current_time / $diff) * $diff; |
|
298 | + $return = $next - $diff; |
|
299 | + break; |
|
300 | + } |
|
301 | + |
|
302 | + if (is_numeric($return)) |
|
303 | + { |
|
304 | + $return = (int)$return; |
|
305 | + } |
|
306 | + |
|
307 | + return $return; |
|
308 | + } |
|
309 | + |
|
310 | + //-------------------------------------------------------------------- |
|
311 | + |
|
312 | + /** |
|
313 | + * Determines the timestamp of the previous ordinal-based time, like |
|
314 | + * 'second Monday'. |
|
315 | + * |
|
316 | + * @param $schedule |
|
317 | + * @param string $current_time |
|
318 | + * @return int|null |
|
319 | + */ |
|
320 | + public function findPreviousOrdinal($schedule, $current_time='now') |
|
321 | + { |
|
322 | + $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
323 | + |
|
324 | + if (empty($schedule)) return null; |
|
325 | + |
|
326 | + // Loop through months in reverse, checking each one to |
|
327 | + // see if the ordinal is in the past. If so - wer'e done. |
|
328 | + foreach ([0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12] as $i) |
|
329 | + { |
|
330 | + $lastmonth = strtotime("last day of {$i} month", $current_time); |
|
331 | + |
|
332 | + $test = strtotime($schedule, $lastmonth); |
|
333 | + |
|
334 | + if ($test <= $current_time) |
|
335 | + { |
|
336 | + return $test; |
|
337 | + } |
|
338 | + } |
|
339 | + |
|
340 | + return null; |
|
341 | + } |
|
342 | + |
|
343 | + //-------------------------------------------------------------------- |
|
344 | 344 | |
345 | 345 | } |
@@ -75,9 +75,9 @@ discard block |
||
75 | 75 | |
76 | 76 | // If $task is not callable, it should be a library:method |
77 | 77 | // string that we can parse. But it must have the colon in the string. |
78 | - if (! is_callable($task) && strpos($task, ':') === false) |
|
78 | + if ( ! is_callable($task) && strpos($task, ':') === false) |
|
79 | 79 | { |
80 | - throw new \RuntimeException( lang('cron.invalid_task') ); |
|
80 | + throw new \RuntimeException(lang('cron.invalid_task')); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | $this->task = $task; |
@@ -92,9 +92,9 @@ discard block |
||
92 | 92 | * |
93 | 93 | * @return timestamp|null |
94 | 94 | */ |
95 | - public function nextRunDate($current_time='now') |
|
95 | + public function nextRunDate($current_time = 'now') |
|
96 | 96 | { |
97 | - $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
97 | + $current_time = is_numeric($current_time) ? (int) $current_time : strtotime($current_time); |
|
98 | 98 | |
99 | 99 | $scheduleType = $this->determineScheduleType($this->schedule); |
100 | 100 | |
@@ -123,9 +123,9 @@ discard block |
||
123 | 123 | * |
124 | 124 | * @return timestamp|null |
125 | 125 | */ |
126 | - public function previousRunDate($current_time='now') |
|
126 | + public function previousRunDate($current_time = 'now') |
|
127 | 127 | { |
128 | - $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
128 | + $current_time = is_numeric($current_time) ? (int) $current_time : strtotime($current_time); |
|
129 | 129 | |
130 | 130 | $scheduleType = $this->determineScheduleType($this->schedule); |
131 | 131 | |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | return $this->findPreviousOrdinal($this->schedule, $current_time); |
139 | 139 | break; |
140 | 140 | case 'increment': |
141 | - return strtotime('-1 '. $this->schedule, $current_time); |
|
141 | + return strtotime('-1 '.$this->schedule, $current_time); |
|
142 | 142 | break; |
143 | 143 | } |
144 | 144 | |
@@ -155,13 +155,13 @@ discard block |
||
155 | 155 | * |
156 | 156 | * @return bool |
157 | 157 | */ |
158 | - public function isDue($current_time='now') |
|
158 | + public function isDue($current_time = 'now') |
|
159 | 159 | { |
160 | - $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
160 | + $current_time = is_numeric($current_time) ? (int) $current_time : strtotime($current_time); |
|
161 | 161 | |
162 | 162 | // For easier matching, and I can't imagine people needing cronjob |
163 | 163 | // accuracy to the seconds, we'll just take the current minute. |
164 | - return date('Y-m-d H:i', $current_time) == date('Y-m-d H:i', $this->nextRunDate($current_time) ); |
|
164 | + return date('Y-m-d H:i', $current_time) == date('Y-m-d H:i', $this->nextRunDate($current_time)); |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | //-------------------------------------------------------------------- |
@@ -219,15 +219,15 @@ discard block |
||
219 | 219 | { |
220 | 220 | $incs = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sun', 'mon', 'tue', |
221 | 221 | 'wed', 'thu', 'fri', 'sat', 'weekday', 'weekdays', 'midnight', 'noon']; |
222 | - $bigger_incs = [ 'back of', 'front of', 'first day of', 'last day of']; |
|
222 | + $bigger_incs = ['back of', 'front of', 'first day of', 'last day of']; |
|
223 | 223 | $ordinals = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth']; |
224 | - $schedule = trim( strtolower($schedule) ); |
|
224 | + $schedule = trim(strtolower($schedule)); |
|
225 | 225 | |
226 | 226 | $multiple_words = strpos($schedule, ' '); |
227 | 227 | $first_word = substr($schedule, 0, $multiple_words ? $multiple_words : strlen($schedule)); |
228 | 228 | |
229 | 229 | // Is the first character a number? Then it's a time |
230 | - if ( is_numeric( $first_word ) ) |
|
230 | + if (is_numeric($first_word)) |
|
231 | 231 | { |
232 | 232 | return 'time'; |
233 | 233 | } |
@@ -269,9 +269,9 @@ discard block |
||
269 | 269 | * @param $type |
270 | 270 | * @return float|int|null |
271 | 271 | */ |
272 | - public function findDateInterval($schedule, $type, $current_time='now') |
|
272 | + public function findDateInterval($schedule, $type, $current_time = 'now') |
|
273 | 273 | { |
274 | - $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
274 | + $current_time = is_numeric($current_time) ? (int) $current_time : strtotime($current_time); |
|
275 | 275 | |
276 | 276 | // list($int, $period) = explode(' ', $schedule); |
277 | 277 | |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | |
302 | 302 | if (is_numeric($return)) |
303 | 303 | { |
304 | - $return = (int)$return; |
|
304 | + $return = (int) $return; |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | return $return; |
@@ -317,9 +317,9 @@ discard block |
||
317 | 317 | * @param string $current_time |
318 | 318 | * @return int|null |
319 | 319 | */ |
320 | - public function findPreviousOrdinal($schedule, $current_time='now') |
|
320 | + public function findPreviousOrdinal($schedule, $current_time = 'now') |
|
321 | 321 | { |
322 | - $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
|
322 | + $current_time = is_numeric($current_time) ? (int) $current_time : strtotime($current_time); |
|
323 | 323 | |
324 | 324 | if (empty($schedule)) return null; |
325 | 325 |
@@ -288,8 +288,7 @@ discard block |
||
288 | 288 | if (date('Y-m-d H:i', $next) == date('Y-m-d H:i', $current_time)) |
289 | 289 | { |
290 | 290 | $return = $next; |
291 | - } |
|
292 | - else { |
|
291 | + } else { |
|
293 | 292 | $return = $next + $diff; |
294 | 293 | } |
295 | 294 | break; |
@@ -321,7 +320,9 @@ discard block |
||
321 | 320 | { |
322 | 321 | $current_time = is_numeric($current_time) ? (int)$current_time : strtotime($current_time); |
323 | 322 | |
324 | - if (empty($schedule)) return null; |
|
323 | + if (empty($schedule)) { |
|
324 | + return null; |
|
325 | + } |
|
325 | 326 | |
326 | 327 | // Loop through months in reverse, checking each one to |
327 | 328 | // see if the ordinal is in the past. If so - wer'e done. |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | /** |
67 | 67 | * Stores our scheduled string and actual task. |
68 | 68 | * |
69 | - * @param $schedule |
|
69 | + * @param string $schedule |
|
70 | 70 | * @param callable $task |
71 | 71 | */ |
72 | 72 | public function __construct($schedule, $task) |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * |
91 | 91 | * @param int|'now' $current_time |
92 | 92 | * |
93 | - * @return timestamp|null |
|
93 | + * @return integer|null |
|
94 | 94 | */ |
95 | 95 | public function nextRunDate($current_time='now') |
96 | 96 | { |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * |
122 | 122 | * @param int|'now' $current_time |
123 | 123 | * |
124 | - * @return timestamp|null |
|
124 | + * @return integer|null |
|
125 | 125 | */ |
126 | 126 | public function previousRunDate($current_time='now') |
127 | 127 | { |
@@ -266,8 +266,8 @@ discard block |
||
266 | 266 | * 'every 5 minutes', every 3 days, etc. |
267 | 267 | * |
268 | 268 | * @param $schedule |
269 | - * @param $type |
|
270 | - * @return float|int|null |
|
269 | + * @param string $type |
|
270 | + * @return integer|null |
|
271 | 271 | */ |
272 | 272 | public function findDateInterval($schedule, $type, $current_time='now') |
273 | 273 | { |
@@ -40,117 +40,117 @@ |
||
40 | 40 | */ |
41 | 41 | interface DocBuilderInterface |
42 | 42 | { |
43 | - /** |
|
44 | - * Does the actual work of reading in and parsing the help file. |
|
45 | - * If a folder Nickname (see addDocFolder() ) is passed as the second parameter, |
|
46 | - * it will limit it's search to that single folder. If nothing is passed, it will |
|
47 | - * search through all of the folders in the order they were given to the library, |
|
48 | - * until it finds the first one. |
|
49 | - * |
|
50 | - * @param string $path The 'path' of the file (relative to the docs |
|
51 | - * folder. Usually from the URI) |
|
52 | - * @param string $restrictToFolder (Optional) The folder nickname |
|
53 | - * |
|
54 | - * @return string |
|
55 | - */ |
|
56 | - public function readPage($path, $restrictToFolder = null); |
|
43 | + /** |
|
44 | + * Does the actual work of reading in and parsing the help file. |
|
45 | + * If a folder Nickname (see addDocFolder() ) is passed as the second parameter, |
|
46 | + * it will limit it's search to that single folder. If nothing is passed, it will |
|
47 | + * search through all of the folders in the order they were given to the library, |
|
48 | + * until it finds the first one. |
|
49 | + * |
|
50 | + * @param string $path The 'path' of the file (relative to the docs |
|
51 | + * folder. Usually from the URI) |
|
52 | + * @param string $restrictToFolder (Optional) The folder nickname |
|
53 | + * |
|
54 | + * @return string |
|
55 | + */ |
|
56 | + public function readPage($path, $restrictToFolder = null); |
|
57 | 57 | |
58 | - /** |
|
59 | - * Parses the contents. Currently runs through the Markdown Extended |
|
60 | - * parser to convert to HTML. |
|
61 | - * |
|
62 | - * @param $str |
|
63 | - * @return mixed |
|
64 | - */ |
|
65 | - public function parse($str); |
|
58 | + /** |
|
59 | + * Parses the contents. Currently runs through the Markdown Extended |
|
60 | + * parser to convert to HTML. |
|
61 | + * |
|
62 | + * @param $str |
|
63 | + * @return mixed |
|
64 | + */ |
|
65 | + public function parse($str); |
|
66 | 66 | |
67 | - /** |
|
68 | - * Perform a few housekeeping tasks on a page, like rewriting URLs to full |
|
69 | - * URLs, not relative, ensuring they link correctly, etc. |
|
70 | - * |
|
71 | - * @param $content |
|
72 | - * @param null $site_url |
|
73 | - * @param null $current_url |
|
74 | - * @return string The post-processed HTML. |
|
75 | - */ |
|
76 | - public function postProcess($content, $site_url = null, $current_url = null); |
|
67 | + /** |
|
68 | + * Perform a few housekeeping tasks on a page, like rewriting URLs to full |
|
69 | + * URLs, not relative, ensuring they link correctly, etc. |
|
70 | + * |
|
71 | + * @param $content |
|
72 | + * @param null $site_url |
|
73 | + * @param null $current_url |
|
74 | + * @return string The post-processed HTML. |
|
75 | + */ |
|
76 | + public function postProcess($content, $site_url = null, $current_url = null); |
|
77 | 77 | |
78 | - /** |
|
79 | - * Allows users to define the classes that are attached to |
|
80 | - * generated tables. |
|
81 | - * |
|
82 | - * @param null $classes |
|
83 | - * @return $this |
|
84 | - */ |
|
85 | - public function setTableClasses($classes = null); |
|
78 | + /** |
|
79 | + * Allows users to define the classes that are attached to |
|
80 | + * generated tables. |
|
81 | + * |
|
82 | + * @param null $classes |
|
83 | + * @return $this |
|
84 | + */ |
|
85 | + public function setTableClasses($classes = null); |
|
86 | 86 | |
87 | - /** |
|
88 | - * Given the contents to render, will build a list of links for the sidebar |
|
89 | - * out of the headings in the file. |
|
90 | - * |
|
91 | - * Note: Will ONLY use h2 and h3 to build the links from. |
|
92 | - * |
|
93 | - * Note: The $content passed in WILL be modified by adding named anchors |
|
94 | - * that match up with the locations. |
|
95 | - * |
|
96 | - * @param string $content The HTML to analyse for headings. |
|
97 | - * @return string |
|
98 | - */ |
|
99 | - public function buildDocumentMap(&$content); |
|
87 | + /** |
|
88 | + * Given the contents to render, will build a list of links for the sidebar |
|
89 | + * out of the headings in the file. |
|
90 | + * |
|
91 | + * Note: Will ONLY use h2 and h3 to build the links from. |
|
92 | + * |
|
93 | + * Note: The $content passed in WILL be modified by adding named anchors |
|
94 | + * that match up with the locations. |
|
95 | + * |
|
96 | + * @param string $content The HTML to analyse for headings. |
|
97 | + * @return string |
|
98 | + */ |
|
99 | + public function buildDocumentMap(&$content); |
|
100 | 100 | |
101 | - /** |
|
102 | - * Stores the name of the callback method to run to convert the source |
|
103 | - * files to viewable files. By default, this should be used to register |
|
104 | - * a Mardown Extended formatter with the system, but could be used to |
|
105 | - * extend the |
|
106 | - * |
|
107 | - * @param string $callback_name |
|
108 | - * @param bool $cascade // If FALSE the formatting of a component ends here. If TRUE, will be passed to next formatter. |
|
109 | - * @return $this |
|
110 | - */ |
|
111 | - public function registerFormatter($callback_name = '', $cascade = false); |
|
101 | + /** |
|
102 | + * Stores the name of the callback method to run to convert the source |
|
103 | + * files to viewable files. By default, this should be used to register |
|
104 | + * a Mardown Extended formatter with the system, but could be used to |
|
105 | + * extend the |
|
106 | + * |
|
107 | + * @param string $callback_name |
|
108 | + * @param bool $cascade // If FALSE the formatting of a component ends here. If TRUE, will be passed to next formatter. |
|
109 | + * @return $this |
|
110 | + */ |
|
111 | + public function registerFormatter($callback_name = '', $cascade = false); |
|
112 | 112 | |
113 | - /** |
|
114 | - * Runs the text through the registered formatters. |
|
115 | - * |
|
116 | - * @param $str |
|
117 | - * @return mixed |
|
118 | - */ |
|
119 | - public function format($str); |
|
113 | + /** |
|
114 | + * Runs the text through the registered formatters. |
|
115 | + * |
|
116 | + * @param $str |
|
117 | + * @return mixed |
|
118 | + */ |
|
119 | + public function format($str); |
|
120 | 120 | |
121 | - /** |
|
122 | - * Retrieves the list of files in a folder and preps the name and filename |
|
123 | - * so it's ready for creating the HTML. |
|
124 | - * |
|
125 | - * @param String $folder The path to the folder to retrieve. |
|
126 | - * |
|
127 | - * @return Array An associative array @see parse_ini_file for format |
|
128 | - * details. |
|
129 | - */ |
|
130 | - public function buildTOC($folder); |
|
121 | + /** |
|
122 | + * Retrieves the list of files in a folder and preps the name and filename |
|
123 | + * so it's ready for creating the HTML. |
|
124 | + * |
|
125 | + * @param String $folder The path to the folder to retrieve. |
|
126 | + * |
|
127 | + * @return Array An associative array @see parse_ini_file for format |
|
128 | + * details. |
|
129 | + */ |
|
130 | + public function buildTOC($folder); |
|
131 | 131 | |
132 | - /** |
|
133 | - * Returns the current docFolders array. |
|
134 | - * |
|
135 | - * @return array |
|
136 | - */ |
|
137 | - public function docFolders(); |
|
132 | + /** |
|
133 | + * Returns the current docFolders array. |
|
134 | + * |
|
135 | + * @return array |
|
136 | + */ |
|
137 | + public function docFolders(); |
|
138 | 138 | |
139 | - /** |
|
140 | - * Registers a path to be used when searching for documentation files. |
|
141 | - * |
|
142 | - * @param $name A nickname to reference it by later. |
|
143 | - * @param $path The server path to the folder. |
|
144 | - * @return $this |
|
145 | - */ |
|
146 | - public function addDocFolder($name, $path); |
|
139 | + /** |
|
140 | + * Registers a path to be used when searching for documentation files. |
|
141 | + * |
|
142 | + * @param $name A nickname to reference it by later. |
|
143 | + * @param $path The server path to the folder. |
|
144 | + * @return $this |
|
145 | + */ |
|
146 | + public function addDocFolder($name, $path); |
|
147 | 147 | |
148 | - /** |
|
149 | - * Removes a folder from the folders we scan for documentation files |
|
150 | - * within. |
|
151 | - * |
|
152 | - * @param $name |
|
153 | - * @return $this |
|
154 | - */ |
|
155 | - public function removeDocFolder($name); |
|
148 | + /** |
|
149 | + * Removes a folder from the folders we scan for documentation files |
|
150 | + * within. |
|
151 | + * |
|
152 | + * @param $name |
|
153 | + * @return $this |
|
154 | + */ |
|
155 | + public function removeDocFolder($name); |
|
156 | 156 | } |
@@ -54,7 +54,7 @@ |
||
54 | 54 | * @param bool $cascade // If FALSE the formatting of a component ends here. If TRUE, will be passed to next formatter. |
55 | 55 | * @return $this |
56 | 56 | */ |
57 | - public function registerFormatter($callback_name='', $cascade=false); |
|
57 | + public function registerFormatter($callback_name = '', $cascade = false); |
|
58 | 58 | |
59 | 59 | //-------------------------------------------------------------------- |
60 | 60 |
@@ -32,40 +32,40 @@ |
||
32 | 32 | |
33 | 33 | interface DocSearchInterface { |
34 | 34 | |
35 | - /** |
|
36 | - * The entry point for performing a search of the documentation. |
|
37 | - * |
|
38 | - * @param null $terms |
|
39 | - * @param array $folders |
|
40 | - * |
|
41 | - * @return array|null |
|
42 | - */ |
|
43 | - public function search($terms = null, $folders = []); |
|
35 | + /** |
|
36 | + * The entry point for performing a search of the documentation. |
|
37 | + * |
|
38 | + * @param null $terms |
|
39 | + * @param array $folders |
|
40 | + * |
|
41 | + * @return array|null |
|
42 | + */ |
|
43 | + public function search($terms = null, $folders = []); |
|
44 | 44 | |
45 | - //-------------------------------------------------------------------- |
|
45 | + //-------------------------------------------------------------------- |
|
46 | 46 | |
47 | - /** |
|
48 | - * Stores the name of the callback method to run to convert the source |
|
49 | - * files to viewable files. By default, this should be used to register |
|
50 | - * a Mardown Extended formatter with the system, but could be used to |
|
51 | - * extend the |
|
52 | - * |
|
53 | - * @param string $callback_name |
|
54 | - * @param bool $cascade // If FALSE the formatting of a component ends here. If TRUE, will be passed to next formatter. |
|
55 | - * @return $this |
|
56 | - */ |
|
57 | - public function registerFormatter($callback_name='', $cascade=false); |
|
47 | + /** |
|
48 | + * Stores the name of the callback method to run to convert the source |
|
49 | + * files to viewable files. By default, this should be used to register |
|
50 | + * a Mardown Extended formatter with the system, but could be used to |
|
51 | + * extend the |
|
52 | + * |
|
53 | + * @param string $callback_name |
|
54 | + * @param bool $cascade // If FALSE the formatting of a component ends here. If TRUE, will be passed to next formatter. |
|
55 | + * @return $this |
|
56 | + */ |
|
57 | + public function registerFormatter($callback_name='', $cascade=false); |
|
58 | 58 | |
59 | - //-------------------------------------------------------------------- |
|
59 | + //-------------------------------------------------------------------- |
|
60 | 60 | |
61 | - /** |
|
62 | - * Runs the text through the registered formatters. |
|
63 | - * |
|
64 | - * @param $str |
|
65 | - * @return mixed |
|
66 | - */ |
|
67 | - public function format($str); |
|
61 | + /** |
|
62 | + * Runs the text through the registered formatters. |
|
63 | + * |
|
64 | + * @param $str |
|
65 | + * @return mixed |
|
66 | + */ |
|
67 | + public function format($str); |
|
68 | 68 | |
69 | - //-------------------------------------------------------------------- |
|
69 | + //-------------------------------------------------------------------- |
|
70 | 70 | |
71 | 71 | } |
@@ -41,420 +41,420 @@ |
||
41 | 41 | class Search implements DocSearchInterface |
42 | 42 | { |
43 | 43 | |
44 | - /** |
|
45 | - * Minimum characters that can be submitted for a search. |
|
46 | - * |
|
47 | - * @var int |
|
48 | - */ |
|
49 | - protected $min_chars = 3; |
|
50 | - |
|
51 | - /** |
|
52 | - * Maximum characters that can be submitted for a search. |
|
53 | - * |
|
54 | - * @var int |
|
55 | - */ |
|
56 | - protected $max_chars = 30; |
|
57 | - |
|
58 | - /** |
|
59 | - * Valid file extensions we can search in. |
|
60 | - * |
|
61 | - * @var string |
|
62 | - */ |
|
63 | - protected $allowed_file_types = 'html|htm|php|php4|php5|txt|md'; |
|
64 | - |
|
65 | - /** |
|
66 | - * Which files should we skip over during our search? |
|
67 | - * |
|
68 | - * @var array |
|
69 | - */ |
|
70 | - protected $skip_files = ['.', '..', '_404.md', '_toc.ini']; |
|
71 | - |
|
72 | - /** |
|
73 | - * How much of each file should we read. |
|
74 | - * Use lower values for faster searches. |
|
75 | - * |
|
76 | - * @var int |
|
77 | - */ |
|
78 | - protected $byte_size = 51200; |
|
79 | - |
|
80 | - /** |
|
81 | - * Number of words long (approximately) |
|
82 | - * the result excerpt should be. |
|
83 | - * |
|
84 | - * @var int |
|
85 | - */ |
|
86 | - protected $excerpt_length = 60; |
|
87 | - |
|
88 | - /** |
|
89 | - * The maximum number of results allowed from a single file. |
|
90 | - * |
|
91 | - * @var int |
|
92 | - */ |
|
93 | - protected $max_per_file = 1; |
|
94 | - |
|
95 | - protected $doc_folders = array(); |
|
96 | - |
|
97 | - protected $formatters = array(); |
|
98 | - |
|
99 | - //-------------------------------------------------------------------- |
|
100 | - |
|
101 | - /** |
|
102 | - * The entry point for performing a search of the documentation. |
|
103 | - * |
|
104 | - * @param null $terms |
|
105 | - * @param array $folders |
|
106 | - * |
|
107 | - * @return array|null |
|
108 | - */ |
|
109 | - public function search($terms = null, $folders = []) |
|
110 | - { |
|
111 | - if (empty($terms) || empty($folders)) { |
|
112 | - return null; |
|
113 | - } |
|
114 | - |
|
115 | - $results = []; |
|
116 | - $this->doc_folders = $folders; |
|
117 | - |
|
118 | - foreach ($folders as $group => $folder) { |
|
119 | - $results = array_merge($results, $this->searchFolder($terms, $folder, $group)); |
|
120 | - } |
|
121 | - |
|
122 | - return $results; |
|
123 | - } |
|
124 | - |
|
125 | - //-------------------------------------------------------------------- |
|
126 | - |
|
127 | - //-------------------------------------------------------------------- |
|
128 | - // Private Methods |
|
129 | - //-------------------------------------------------------------------- |
|
130 | - |
|
131 | - |
|
132 | - /** |
|
133 | - * Searches a single directory worth of files. |
|
134 | - * |
|
135 | - * @param $term |
|
136 | - * @param $folder |
|
137 | - * @param $group_name |
|
138 | - * |
|
139 | - * @return array The results. |
|
140 | - */ |
|
141 | - protected function searchFolder($term, $folder, $group_name) |
|
142 | - { |
|
143 | - $results = []; |
|
144 | - |
|
145 | - $map = $this->directory_map($folder, 2); |
|
146 | - |
|
147 | - $map = $this->flattenMap($map); |
|
148 | - |
|
149 | - // Make sure we have something to work with. |
|
150 | - if (! is_array($map) || (is_array($map) && ! count($map))) { |
|
151 | - return []; |
|
152 | - } |
|
153 | - |
|
154 | - // Loop over each file and search the contents for our term. |
|
155 | - foreach ($map as $dir => $file) { |
|
156 | - $file_count = 0; |
|
157 | - |
|
158 | - if (in_array($file, $this->skip_files)) { |
|
159 | - continue; |
|
160 | - } |
|
161 | - |
|
162 | - // Is it a folder? |
|
163 | - if (is_array($file) && count($file)) { |
|
164 | - $results = array_merge($results, $this->searchFolder($term, $folder . '/' . $dir, $group_name)); |
|
165 | - continue; |
|
166 | - } |
|
167 | - |
|
168 | - // Make sure it's the right file type... |
|
169 | - if (! preg_match("/({$this->allowed_file_types})/i", $file)) { |
|
170 | - continue; |
|
171 | - } |
|
172 | - |
|
173 | - $path = is_string($dir) ? $folder . '/' . $dir . '/' . $file : $folder . '/' . $file; |
|
174 | - $term_html = htmlentities($term); |
|
175 | - |
|
176 | - // Read in the file text |
|
177 | - $handle = fopen($path, 'r'); |
|
178 | - $text = fread($handle, $this->byte_size); |
|
179 | - |
|
180 | - // Do we have a match in here somewhere? |
|
181 | - $found = stristr($text, $term) || stristr($text, $term_html); |
|
182 | - |
|
183 | - if (! $found) { |
|
184 | - continue; |
|
185 | - } |
|
186 | - |
|
187 | - // Escape our terms to safely use in a preg_match |
|
188 | - $excerpt = strip_tags($text); |
|
189 | - $term = preg_quote($term); |
|
190 | - $term = str_replace("/", "\/", "{$term}"); |
|
191 | - $term_html = preg_quote($term_html); |
|
192 | - $term_html = str_replace("/", "\/", "{$term_html}"); |
|
193 | - |
|
194 | - // Add the item to our results with extracts. |
|
195 | - if (preg_match_all( |
|
196 | - "/((\s\S*){0,3})($term|$term_html)((\s?\S*){0,3})/i", |
|
197 | - $excerpt, |
|
198 | - $matches, |
|
199 | - PREG_OFFSET_CAPTURE | PREG_SET_ORDER |
|
200 | - )) { |
|
201 | - foreach ($matches as $match) { |
|
202 | - if ($file_count >= $this->max_per_file) { |
|
203 | - continue; |
|
204 | - } |
|
205 | - $result_url = '/docs/' . $group_name . '/' . str_replace('.md', '', $file); |
|
206 | - |
|
207 | - foreach ($this->doc_folders as $alias => $folder) { |
|
208 | - $result_url = str_replace($folder, $alias, $result_url); |
|
209 | - } |
|
210 | - |
|
211 | - $results[] = [ |
|
212 | - 'title' => $this->extractTitle($excerpt, $file), |
|
213 | - 'file' => $folder . '/' . $file, |
|
214 | - 'url' => $result_url, |
|
215 | - 'extract' => $this->buildExtract($excerpt, $term, $match[0][0]) |
|
216 | - ]; |
|
217 | - |
|
218 | - $file_count++; |
|
219 | - } |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - return $results; |
|
224 | - } |
|
225 | - |
|
226 | - //-------------------------------------------------------------------- |
|
227 | - |
|
228 | - /** |
|
229 | - * Stores the name of the callback method to run to convert the source |
|
230 | - * files to viewable files. By default, this should be used to register |
|
231 | - * a Mardown Extended formatter with the system, but could be used to |
|
232 | - * extend the |
|
233 | - * |
|
234 | - * @param string $callback_name |
|
235 | - * @param bool $cascade // If FALSE the formatting of a component ends here. If TRUE, will be passed to next formatter. |
|
236 | - * @return $this |
|
237 | - */ |
|
238 | - public function registerFormatter($callback_name = '', $cascade = false) |
|
239 | - { |
|
240 | - if (empty($callback_name)) return; |
|
241 | - |
|
242 | - $this->formatters[] = array($callback_name => $cascade); |
|
243 | - |
|
244 | - return $this; |
|
245 | - } |
|
246 | - |
|
247 | - //-------------------------------------------------------------------- |
|
248 | - |
|
249 | - /** |
|
250 | - * Runs the text through the registered formatters. |
|
251 | - * |
|
252 | - * @param $str |
|
253 | - * @return mixed |
|
254 | - */ |
|
255 | - public function format($str) |
|
256 | - { |
|
257 | - if (! is_array($this->formatters)) return $str; |
|
258 | - |
|
259 | - foreach ($this->formatters as $formatter) { |
|
260 | - $method = key($formatter); |
|
261 | - $cascade = $formatter[$method]; |
|
262 | - |
|
263 | - $str = call_user_func($method, $str); |
|
264 | - |
|
265 | - if (! $cascade) return $str; |
|
266 | - } |
|
267 | - |
|
268 | - return $str; |
|
269 | - } |
|
270 | - |
|
271 | - //-------------------------------------------------------------------- |
|
272 | - |
|
273 | - |
|
274 | - //-------------------------------------------------------------------- |
|
275 | - // Protected Methods |
|
276 | - //-------------------------------------------------------------------- |
|
277 | - |
|
278 | - /** |
|
279 | - * Converts an array generated by directory_map into a flat array of |
|
280 | - * folders, removing any nested folders and adding them to the path. |
|
281 | - * |
|
282 | - * @param $map |
|
283 | - * @param $prefix Used to recursively add the folder name... |
|
284 | - * @return mixed |
|
285 | - */ |
|
286 | - protected function flattenMap($map, $prefix = '') |
|
287 | - { |
|
288 | - if (! is_array($map) || ! count($map)) { |
|
289 | - return $map; |
|
290 | - } |
|
291 | - |
|
292 | - $return = []; |
|
293 | - |
|
294 | - foreach ($map as $folder => $files) { |
|
295 | - |
|
296 | - // If it's a folder name and an array of files |
|
297 | - // then call this method recursively to flatten it out. |
|
298 | - if (is_array($files)) { |
|
299 | - $return = array_merge($return, $this->flattenMap($files, $prefix . $folder)); |
|
300 | - continue; |
|
301 | - } |
|
302 | - |
|
303 | - // Else, add our prefix (if any) to the filename... |
|
304 | - $return[] = $prefix . $files; |
|
305 | - } |
|
306 | - |
|
307 | - return $return; |
|
308 | - } |
|
309 | - |
|
310 | - //-------------------------------------------------------------------- |
|
311 | - |
|
312 | - /** |
|
313 | - * Handles extracting the text surrounding our match and basic match formatting. |
|
314 | - * |
|
315 | - * @param $excerpt |
|
316 | - * @param $term |
|
317 | - * @param $match_string |
|
318 | - * |
|
319 | - * @return string |
|
320 | - */ |
|
321 | - protected function buildExtract($excerpt, $term, $match_string) |
|
322 | - { |
|
323 | - // Find the character positions within the string that our match was found at. |
|
324 | - // That way we'll know from what positions before and after this we want to grab it in. |
|
325 | - $start_offset = stripos($excerpt, $match_string); |
|
326 | - |
|
327 | - // Modify the start and end positions based on $this->excerpt_length / 2. |
|
328 | - $buffer = floor($this->excerpt_length / 2); |
|
329 | - |
|
330 | - // Adjust our start position |
|
331 | - $start_offset = $start_offset - $buffer; |
|
332 | - if ($start_offset < 0) { |
|
333 | - $start_offset = 0; |
|
334 | - } |
|
335 | - |
|
336 | - $extract = substr($excerpt, $start_offset); |
|
337 | - |
|
338 | - $extract = strip_tags($this->format($extract)); |
|
339 | - |
|
340 | - $extract = $this->firstXWords($extract, $this->excerpt_length); |
|
341 | - |
|
342 | - // Wrap the search term in a span we can style. |
|
343 | - $extract = str_ireplace($term, '<span class="term-hilight">' . $term . '</span>', $extract); |
|
344 | - |
|
345 | - return $extract; |
|
346 | - } |
|
347 | - |
|
348 | - //-------------------------------------------------------------------- |
|
349 | - |
|
350 | - /** |
|
351 | - * Extracts the title from a bit of markdown formatted text. If it doesn't |
|
352 | - * have an h1 or h2, then it uses the filename. |
|
353 | - * |
|
354 | - * @param $excerpt |
|
355 | - * @param $file |
|
356 | - * @return string |
|
357 | - */ |
|
358 | - protected function extractTitle($excerpt, $file) |
|
359 | - { |
|
360 | - $title = ''; |
|
361 | - |
|
362 | - // Easiest to work if this is split into lines. |
|
363 | - $lines = explode("\n", $excerpt); |
|
364 | - |
|
365 | - if (is_array($lines) && count($lines)) { |
|
366 | - foreach ($lines as $line) { |
|
367 | - if (strpos($line, '# ') === 0 || strpos($line, '## ') === 0) { |
|
368 | - $title = trim(str_replace('#', '', $line)); |
|
369 | - break; |
|
370 | - } |
|
371 | - } |
|
372 | - } |
|
373 | - |
|
374 | - // If it's empty, we'll use the filename. |
|
375 | - if (empty($title)) { |
|
376 | - $title = str_replace('_', ' ', $file); |
|
377 | - $title = str_replace('.md', ' ', $title); |
|
378 | - $title = ucwords($title); |
|
379 | - } |
|
380 | - |
|
381 | - return $title; |
|
382 | - } |
|
383 | - //-------------------------------------------------------------------- |
|
384 | - |
|
385 | - /** |
|
386 | - * Create a Directory Map |
|
387 | - * |
|
388 | - * Reads the specified directory and builds an array |
|
389 | - * representation of it. Sub-folders contained with the |
|
390 | - * directory will be mapped as well. |
|
391 | - * |
|
392 | - * @param string $source_dir Path to source |
|
393 | - * @param int $directory_depth Depth of directories to traverse |
|
394 | - * (0 = fully recursive, 1 = current dir, etc) |
|
395 | - * @param bool $hidden Whether to show hidden files |
|
396 | - * @return array |
|
397 | - */ |
|
398 | - protected function directory_map($source_dir, $directory_depth = 0, $hidden = FALSE) |
|
399 | - { |
|
400 | - if ($fp = @opendir($source_dir)) { |
|
401 | - $filedata = array(); |
|
402 | - $new_depth = $directory_depth - 1; |
|
403 | - $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
404 | - |
|
405 | - while (FALSE !== ($file = readdir($fp))) { |
|
406 | - // Remove '.', '..', and hidden files [optional] |
|
407 | - if ($file === '.' OR $file === '..' OR ($hidden === FALSE && $file[0] === '.')) { |
|
408 | - continue; |
|
409 | - } |
|
410 | - |
|
411 | - is_dir($source_dir . $file) && $file .= DIRECTORY_SEPARATOR; |
|
412 | - |
|
413 | - if (($directory_depth < 1 OR $new_depth > 0) && is_dir($source_dir . $file)) |
|
414 | - { |
|
415 | - $filedata[$file] = $this->directory_map($source_dir . $file, $new_depth, $hidden); |
|
416 | - } else |
|
417 | - { |
|
418 | - // Replace the directory separator here with a forward slash since |
|
419 | - // Windows uses backward slashes and not all browsers will auto-replace |
|
420 | - // those slashes in URLs. |
|
421 | - $filedata[] = str_replace(DIRECTORY_SEPARATOR, '/', $file); |
|
422 | - } |
|
423 | - } |
|
424 | - |
|
425 | - closedir($fp); |
|
426 | - return $filedata; |
|
427 | - } |
|
428 | - |
|
429 | - return FALSE; |
|
430 | - } |
|
431 | - |
|
432 | - //-------------------------------------------------------------------- |
|
433 | - |
|
434 | - /** |
|
435 | - * Gets the first 'X' words of a string. |
|
436 | - * |
|
437 | - * @param $str |
|
438 | - * @param int $wordCount |
|
439 | - * @return string |
|
440 | - */ |
|
441 | - protected function firstXWords($str, $wordCount = 10) |
|
442 | - { |
|
443 | - return implode( |
|
444 | - '', |
|
445 | - array_slice( |
|
446 | - preg_split( |
|
447 | - '/([\s,\.;\?\!]+)/', |
|
448 | - $str, |
|
449 | - $wordCount * 2 + 1, |
|
450 | - PREG_SPLIT_DELIM_CAPTURE |
|
451 | - ), |
|
452 | - 0, |
|
453 | - $wordCount * 2 - 1 |
|
454 | - ) |
|
455 | - ); |
|
456 | - } |
|
457 | - |
|
458 | - //-------------------------------------------------------------------- |
|
44 | + /** |
|
45 | + * Minimum characters that can be submitted for a search. |
|
46 | + * |
|
47 | + * @var int |
|
48 | + */ |
|
49 | + protected $min_chars = 3; |
|
50 | + |
|
51 | + /** |
|
52 | + * Maximum characters that can be submitted for a search. |
|
53 | + * |
|
54 | + * @var int |
|
55 | + */ |
|
56 | + protected $max_chars = 30; |
|
57 | + |
|
58 | + /** |
|
59 | + * Valid file extensions we can search in. |
|
60 | + * |
|
61 | + * @var string |
|
62 | + */ |
|
63 | + protected $allowed_file_types = 'html|htm|php|php4|php5|txt|md'; |
|
64 | + |
|
65 | + /** |
|
66 | + * Which files should we skip over during our search? |
|
67 | + * |
|
68 | + * @var array |
|
69 | + */ |
|
70 | + protected $skip_files = ['.', '..', '_404.md', '_toc.ini']; |
|
71 | + |
|
72 | + /** |
|
73 | + * How much of each file should we read. |
|
74 | + * Use lower values for faster searches. |
|
75 | + * |
|
76 | + * @var int |
|
77 | + */ |
|
78 | + protected $byte_size = 51200; |
|
79 | + |
|
80 | + /** |
|
81 | + * Number of words long (approximately) |
|
82 | + * the result excerpt should be. |
|
83 | + * |
|
84 | + * @var int |
|
85 | + */ |
|
86 | + protected $excerpt_length = 60; |
|
87 | + |
|
88 | + /** |
|
89 | + * The maximum number of results allowed from a single file. |
|
90 | + * |
|
91 | + * @var int |
|
92 | + */ |
|
93 | + protected $max_per_file = 1; |
|
94 | + |
|
95 | + protected $doc_folders = array(); |
|
96 | + |
|
97 | + protected $formatters = array(); |
|
98 | + |
|
99 | + //-------------------------------------------------------------------- |
|
100 | + |
|
101 | + /** |
|
102 | + * The entry point for performing a search of the documentation. |
|
103 | + * |
|
104 | + * @param null $terms |
|
105 | + * @param array $folders |
|
106 | + * |
|
107 | + * @return array|null |
|
108 | + */ |
|
109 | + public function search($terms = null, $folders = []) |
|
110 | + { |
|
111 | + if (empty($terms) || empty($folders)) { |
|
112 | + return null; |
|
113 | + } |
|
114 | + |
|
115 | + $results = []; |
|
116 | + $this->doc_folders = $folders; |
|
117 | + |
|
118 | + foreach ($folders as $group => $folder) { |
|
119 | + $results = array_merge($results, $this->searchFolder($terms, $folder, $group)); |
|
120 | + } |
|
121 | + |
|
122 | + return $results; |
|
123 | + } |
|
124 | + |
|
125 | + //-------------------------------------------------------------------- |
|
126 | + |
|
127 | + //-------------------------------------------------------------------- |
|
128 | + // Private Methods |
|
129 | + //-------------------------------------------------------------------- |
|
130 | + |
|
131 | + |
|
132 | + /** |
|
133 | + * Searches a single directory worth of files. |
|
134 | + * |
|
135 | + * @param $term |
|
136 | + * @param $folder |
|
137 | + * @param $group_name |
|
138 | + * |
|
139 | + * @return array The results. |
|
140 | + */ |
|
141 | + protected function searchFolder($term, $folder, $group_name) |
|
142 | + { |
|
143 | + $results = []; |
|
144 | + |
|
145 | + $map = $this->directory_map($folder, 2); |
|
146 | + |
|
147 | + $map = $this->flattenMap($map); |
|
148 | + |
|
149 | + // Make sure we have something to work with. |
|
150 | + if (! is_array($map) || (is_array($map) && ! count($map))) { |
|
151 | + return []; |
|
152 | + } |
|
153 | + |
|
154 | + // Loop over each file and search the contents for our term. |
|
155 | + foreach ($map as $dir => $file) { |
|
156 | + $file_count = 0; |
|
157 | + |
|
158 | + if (in_array($file, $this->skip_files)) { |
|
159 | + continue; |
|
160 | + } |
|
161 | + |
|
162 | + // Is it a folder? |
|
163 | + if (is_array($file) && count($file)) { |
|
164 | + $results = array_merge($results, $this->searchFolder($term, $folder . '/' . $dir, $group_name)); |
|
165 | + continue; |
|
166 | + } |
|
167 | + |
|
168 | + // Make sure it's the right file type... |
|
169 | + if (! preg_match("/({$this->allowed_file_types})/i", $file)) { |
|
170 | + continue; |
|
171 | + } |
|
172 | + |
|
173 | + $path = is_string($dir) ? $folder . '/' . $dir . '/' . $file : $folder . '/' . $file; |
|
174 | + $term_html = htmlentities($term); |
|
175 | + |
|
176 | + // Read in the file text |
|
177 | + $handle = fopen($path, 'r'); |
|
178 | + $text = fread($handle, $this->byte_size); |
|
179 | + |
|
180 | + // Do we have a match in here somewhere? |
|
181 | + $found = stristr($text, $term) || stristr($text, $term_html); |
|
182 | + |
|
183 | + if (! $found) { |
|
184 | + continue; |
|
185 | + } |
|
186 | + |
|
187 | + // Escape our terms to safely use in a preg_match |
|
188 | + $excerpt = strip_tags($text); |
|
189 | + $term = preg_quote($term); |
|
190 | + $term = str_replace("/", "\/", "{$term}"); |
|
191 | + $term_html = preg_quote($term_html); |
|
192 | + $term_html = str_replace("/", "\/", "{$term_html}"); |
|
193 | + |
|
194 | + // Add the item to our results with extracts. |
|
195 | + if (preg_match_all( |
|
196 | + "/((\s\S*){0,3})($term|$term_html)((\s?\S*){0,3})/i", |
|
197 | + $excerpt, |
|
198 | + $matches, |
|
199 | + PREG_OFFSET_CAPTURE | PREG_SET_ORDER |
|
200 | + )) { |
|
201 | + foreach ($matches as $match) { |
|
202 | + if ($file_count >= $this->max_per_file) { |
|
203 | + continue; |
|
204 | + } |
|
205 | + $result_url = '/docs/' . $group_name . '/' . str_replace('.md', '', $file); |
|
206 | + |
|
207 | + foreach ($this->doc_folders as $alias => $folder) { |
|
208 | + $result_url = str_replace($folder, $alias, $result_url); |
|
209 | + } |
|
210 | + |
|
211 | + $results[] = [ |
|
212 | + 'title' => $this->extractTitle($excerpt, $file), |
|
213 | + 'file' => $folder . '/' . $file, |
|
214 | + 'url' => $result_url, |
|
215 | + 'extract' => $this->buildExtract($excerpt, $term, $match[0][0]) |
|
216 | + ]; |
|
217 | + |
|
218 | + $file_count++; |
|
219 | + } |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + return $results; |
|
224 | + } |
|
225 | + |
|
226 | + //-------------------------------------------------------------------- |
|
227 | + |
|
228 | + /** |
|
229 | + * Stores the name of the callback method to run to convert the source |
|
230 | + * files to viewable files. By default, this should be used to register |
|
231 | + * a Mardown Extended formatter with the system, but could be used to |
|
232 | + * extend the |
|
233 | + * |
|
234 | + * @param string $callback_name |
|
235 | + * @param bool $cascade // If FALSE the formatting of a component ends here. If TRUE, will be passed to next formatter. |
|
236 | + * @return $this |
|
237 | + */ |
|
238 | + public function registerFormatter($callback_name = '', $cascade = false) |
|
239 | + { |
|
240 | + if (empty($callback_name)) return; |
|
241 | + |
|
242 | + $this->formatters[] = array($callback_name => $cascade); |
|
243 | + |
|
244 | + return $this; |
|
245 | + } |
|
246 | + |
|
247 | + //-------------------------------------------------------------------- |
|
248 | + |
|
249 | + /** |
|
250 | + * Runs the text through the registered formatters. |
|
251 | + * |
|
252 | + * @param $str |
|
253 | + * @return mixed |
|
254 | + */ |
|
255 | + public function format($str) |
|
256 | + { |
|
257 | + if (! is_array($this->formatters)) return $str; |
|
258 | + |
|
259 | + foreach ($this->formatters as $formatter) { |
|
260 | + $method = key($formatter); |
|
261 | + $cascade = $formatter[$method]; |
|
262 | + |
|
263 | + $str = call_user_func($method, $str); |
|
264 | + |
|
265 | + if (! $cascade) return $str; |
|
266 | + } |
|
267 | + |
|
268 | + return $str; |
|
269 | + } |
|
270 | + |
|
271 | + //-------------------------------------------------------------------- |
|
272 | + |
|
273 | + |
|
274 | + //-------------------------------------------------------------------- |
|
275 | + // Protected Methods |
|
276 | + //-------------------------------------------------------------------- |
|
277 | + |
|
278 | + /** |
|
279 | + * Converts an array generated by directory_map into a flat array of |
|
280 | + * folders, removing any nested folders and adding them to the path. |
|
281 | + * |
|
282 | + * @param $map |
|
283 | + * @param $prefix Used to recursively add the folder name... |
|
284 | + * @return mixed |
|
285 | + */ |
|
286 | + protected function flattenMap($map, $prefix = '') |
|
287 | + { |
|
288 | + if (! is_array($map) || ! count($map)) { |
|
289 | + return $map; |
|
290 | + } |
|
291 | + |
|
292 | + $return = []; |
|
293 | + |
|
294 | + foreach ($map as $folder => $files) { |
|
295 | + |
|
296 | + // If it's a folder name and an array of files |
|
297 | + // then call this method recursively to flatten it out. |
|
298 | + if (is_array($files)) { |
|
299 | + $return = array_merge($return, $this->flattenMap($files, $prefix . $folder)); |
|
300 | + continue; |
|
301 | + } |
|
302 | + |
|
303 | + // Else, add our prefix (if any) to the filename... |
|
304 | + $return[] = $prefix . $files; |
|
305 | + } |
|
306 | + |
|
307 | + return $return; |
|
308 | + } |
|
309 | + |
|
310 | + //-------------------------------------------------------------------- |
|
311 | + |
|
312 | + /** |
|
313 | + * Handles extracting the text surrounding our match and basic match formatting. |
|
314 | + * |
|
315 | + * @param $excerpt |
|
316 | + * @param $term |
|
317 | + * @param $match_string |
|
318 | + * |
|
319 | + * @return string |
|
320 | + */ |
|
321 | + protected function buildExtract($excerpt, $term, $match_string) |
|
322 | + { |
|
323 | + // Find the character positions within the string that our match was found at. |
|
324 | + // That way we'll know from what positions before and after this we want to grab it in. |
|
325 | + $start_offset = stripos($excerpt, $match_string); |
|
326 | + |
|
327 | + // Modify the start and end positions based on $this->excerpt_length / 2. |
|
328 | + $buffer = floor($this->excerpt_length / 2); |
|
329 | + |
|
330 | + // Adjust our start position |
|
331 | + $start_offset = $start_offset - $buffer; |
|
332 | + if ($start_offset < 0) { |
|
333 | + $start_offset = 0; |
|
334 | + } |
|
335 | + |
|
336 | + $extract = substr($excerpt, $start_offset); |
|
337 | + |
|
338 | + $extract = strip_tags($this->format($extract)); |
|
339 | + |
|
340 | + $extract = $this->firstXWords($extract, $this->excerpt_length); |
|
341 | + |
|
342 | + // Wrap the search term in a span we can style. |
|
343 | + $extract = str_ireplace($term, '<span class="term-hilight">' . $term . '</span>', $extract); |
|
344 | + |
|
345 | + return $extract; |
|
346 | + } |
|
347 | + |
|
348 | + //-------------------------------------------------------------------- |
|
349 | + |
|
350 | + /** |
|
351 | + * Extracts the title from a bit of markdown formatted text. If it doesn't |
|
352 | + * have an h1 or h2, then it uses the filename. |
|
353 | + * |
|
354 | + * @param $excerpt |
|
355 | + * @param $file |
|
356 | + * @return string |
|
357 | + */ |
|
358 | + protected function extractTitle($excerpt, $file) |
|
359 | + { |
|
360 | + $title = ''; |
|
361 | + |
|
362 | + // Easiest to work if this is split into lines. |
|
363 | + $lines = explode("\n", $excerpt); |
|
364 | + |
|
365 | + if (is_array($lines) && count($lines)) { |
|
366 | + foreach ($lines as $line) { |
|
367 | + if (strpos($line, '# ') === 0 || strpos($line, '## ') === 0) { |
|
368 | + $title = trim(str_replace('#', '', $line)); |
|
369 | + break; |
|
370 | + } |
|
371 | + } |
|
372 | + } |
|
373 | + |
|
374 | + // If it's empty, we'll use the filename. |
|
375 | + if (empty($title)) { |
|
376 | + $title = str_replace('_', ' ', $file); |
|
377 | + $title = str_replace('.md', ' ', $title); |
|
378 | + $title = ucwords($title); |
|
379 | + } |
|
380 | + |
|
381 | + return $title; |
|
382 | + } |
|
383 | + //-------------------------------------------------------------------- |
|
384 | + |
|
385 | + /** |
|
386 | + * Create a Directory Map |
|
387 | + * |
|
388 | + * Reads the specified directory and builds an array |
|
389 | + * representation of it. Sub-folders contained with the |
|
390 | + * directory will be mapped as well. |
|
391 | + * |
|
392 | + * @param string $source_dir Path to source |
|
393 | + * @param int $directory_depth Depth of directories to traverse |
|
394 | + * (0 = fully recursive, 1 = current dir, etc) |
|
395 | + * @param bool $hidden Whether to show hidden files |
|
396 | + * @return array |
|
397 | + */ |
|
398 | + protected function directory_map($source_dir, $directory_depth = 0, $hidden = FALSE) |
|
399 | + { |
|
400 | + if ($fp = @opendir($source_dir)) { |
|
401 | + $filedata = array(); |
|
402 | + $new_depth = $directory_depth - 1; |
|
403 | + $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
404 | + |
|
405 | + while (FALSE !== ($file = readdir($fp))) { |
|
406 | + // Remove '.', '..', and hidden files [optional] |
|
407 | + if ($file === '.' OR $file === '..' OR ($hidden === FALSE && $file[0] === '.')) { |
|
408 | + continue; |
|
409 | + } |
|
410 | + |
|
411 | + is_dir($source_dir . $file) && $file .= DIRECTORY_SEPARATOR; |
|
412 | + |
|
413 | + if (($directory_depth < 1 OR $new_depth > 0) && is_dir($source_dir . $file)) |
|
414 | + { |
|
415 | + $filedata[$file] = $this->directory_map($source_dir . $file, $new_depth, $hidden); |
|
416 | + } else |
|
417 | + { |
|
418 | + // Replace the directory separator here with a forward slash since |
|
419 | + // Windows uses backward slashes and not all browsers will auto-replace |
|
420 | + // those slashes in URLs. |
|
421 | + $filedata[] = str_replace(DIRECTORY_SEPARATOR, '/', $file); |
|
422 | + } |
|
423 | + } |
|
424 | + |
|
425 | + closedir($fp); |
|
426 | + return $filedata; |
|
427 | + } |
|
428 | + |
|
429 | + return FALSE; |
|
430 | + } |
|
431 | + |
|
432 | + //-------------------------------------------------------------------- |
|
433 | + |
|
434 | + /** |
|
435 | + * Gets the first 'X' words of a string. |
|
436 | + * |
|
437 | + * @param $str |
|
438 | + * @param int $wordCount |
|
439 | + * @return string |
|
440 | + */ |
|
441 | + protected function firstXWords($str, $wordCount = 10) |
|
442 | + { |
|
443 | + return implode( |
|
444 | + '', |
|
445 | + array_slice( |
|
446 | + preg_split( |
|
447 | + '/([\s,\.;\?\!]+)/', |
|
448 | + $str, |
|
449 | + $wordCount * 2 + 1, |
|
450 | + PREG_SPLIT_DELIM_CAPTURE |
|
451 | + ), |
|
452 | + 0, |
|
453 | + $wordCount * 2 - 1 |
|
454 | + ) |
|
455 | + ); |
|
456 | + } |
|
457 | + |
|
458 | + //-------------------------------------------------------------------- |
|
459 | 459 | |
460 | 460 | } |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | $map = $this->flattenMap($map); |
148 | 148 | |
149 | 149 | // Make sure we have something to work with. |
150 | - if (! is_array($map) || (is_array($map) && ! count($map))) { |
|
150 | + if ( ! is_array($map) || (is_array($map) && ! count($map))) { |
|
151 | 151 | return []; |
152 | 152 | } |
153 | 153 | |
@@ -161,16 +161,16 @@ discard block |
||
161 | 161 | |
162 | 162 | // Is it a folder? |
163 | 163 | if (is_array($file) && count($file)) { |
164 | - $results = array_merge($results, $this->searchFolder($term, $folder . '/' . $dir, $group_name)); |
|
164 | + $results = array_merge($results, $this->searchFolder($term, $folder.'/'.$dir, $group_name)); |
|
165 | 165 | continue; |
166 | 166 | } |
167 | 167 | |
168 | 168 | // Make sure it's the right file type... |
169 | - if (! preg_match("/({$this->allowed_file_types})/i", $file)) { |
|
169 | + if ( ! preg_match("/({$this->allowed_file_types})/i", $file)) { |
|
170 | 170 | continue; |
171 | 171 | } |
172 | 172 | |
173 | - $path = is_string($dir) ? $folder . '/' . $dir . '/' . $file : $folder . '/' . $file; |
|
173 | + $path = is_string($dir) ? $folder.'/'.$dir.'/'.$file : $folder.'/'.$file; |
|
174 | 174 | $term_html = htmlentities($term); |
175 | 175 | |
176 | 176 | // Read in the file text |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | // Do we have a match in here somewhere? |
181 | 181 | $found = stristr($text, $term) || stristr($text, $term_html); |
182 | 182 | |
183 | - if (! $found) { |
|
183 | + if ( ! $found) { |
|
184 | 184 | continue; |
185 | 185 | } |
186 | 186 | |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | if ($file_count >= $this->max_per_file) { |
203 | 203 | continue; |
204 | 204 | } |
205 | - $result_url = '/docs/' . $group_name . '/' . str_replace('.md', '', $file); |
|
205 | + $result_url = '/docs/'.$group_name.'/'.str_replace('.md', '', $file); |
|
206 | 206 | |
207 | 207 | foreach ($this->doc_folders as $alias => $folder) { |
208 | 208 | $result_url = str_replace($folder, $alias, $result_url); |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | |
211 | 211 | $results[] = [ |
212 | 212 | 'title' => $this->extractTitle($excerpt, $file), |
213 | - 'file' => $folder . '/' . $file, |
|
213 | + 'file' => $folder.'/'.$file, |
|
214 | 214 | 'url' => $result_url, |
215 | 215 | 'extract' => $this->buildExtract($excerpt, $term, $match[0][0]) |
216 | 216 | ]; |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | */ |
255 | 255 | public function format($str) |
256 | 256 | { |
257 | - if (! is_array($this->formatters)) return $str; |
|
257 | + if ( ! is_array($this->formatters)) return $str; |
|
258 | 258 | |
259 | 259 | foreach ($this->formatters as $formatter) { |
260 | 260 | $method = key($formatter); |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | |
263 | 263 | $str = call_user_func($method, $str); |
264 | 264 | |
265 | - if (! $cascade) return $str; |
|
265 | + if ( ! $cascade) return $str; |
|
266 | 266 | } |
267 | 267 | |
268 | 268 | return $str; |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | */ |
286 | 286 | protected function flattenMap($map, $prefix = '') |
287 | 287 | { |
288 | - if (! is_array($map) || ! count($map)) { |
|
288 | + if ( ! is_array($map) || ! count($map)) { |
|
289 | 289 | return $map; |
290 | 290 | } |
291 | 291 | |
@@ -296,12 +296,12 @@ discard block |
||
296 | 296 | // If it's a folder name and an array of files |
297 | 297 | // then call this method recursively to flatten it out. |
298 | 298 | if (is_array($files)) { |
299 | - $return = array_merge($return, $this->flattenMap($files, $prefix . $folder)); |
|
299 | + $return = array_merge($return, $this->flattenMap($files, $prefix.$folder)); |
|
300 | 300 | continue; |
301 | 301 | } |
302 | 302 | |
303 | 303 | // Else, add our prefix (if any) to the filename... |
304 | - $return[] = $prefix . $files; |
|
304 | + $return[] = $prefix.$files; |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | return $return; |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | $extract = $this->firstXWords($extract, $this->excerpt_length); |
341 | 341 | |
342 | 342 | // Wrap the search term in a span we can style. |
343 | - $extract = str_ireplace($term, '<span class="term-hilight">' . $term . '</span>', $extract); |
|
343 | + $extract = str_ireplace($term, '<span class="term-hilight">'.$term.'</span>', $extract); |
|
344 | 344 | |
345 | 345 | return $extract; |
346 | 346 | } |
@@ -400,7 +400,7 @@ discard block |
||
400 | 400 | if ($fp = @opendir($source_dir)) { |
401 | 401 | $filedata = array(); |
402 | 402 | $new_depth = $directory_depth - 1; |
403 | - $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
403 | + $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
|
404 | 404 | |
405 | 405 | while (FALSE !== ($file = readdir($fp))) { |
406 | 406 | // Remove '.', '..', and hidden files [optional] |
@@ -408,11 +408,11 @@ discard block |
||
408 | 408 | continue; |
409 | 409 | } |
410 | 410 | |
411 | - is_dir($source_dir . $file) && $file .= DIRECTORY_SEPARATOR; |
|
411 | + is_dir($source_dir.$file) && $file .= DIRECTORY_SEPARATOR; |
|
412 | 412 | |
413 | - if (($directory_depth < 1 OR $new_depth > 0) && is_dir($source_dir . $file)) |
|
413 | + if (($directory_depth < 1 OR $new_depth > 0) && is_dir($source_dir.$file)) |
|
414 | 414 | { |
415 | - $filedata[$file] = $this->directory_map($source_dir . $file, $new_depth, $hidden); |
|
415 | + $filedata[$file] = $this->directory_map($source_dir.$file, $new_depth, $hidden); |
|
416 | 416 | } else |
417 | 417 | { |
418 | 418 | // Replace the directory separator here with a forward slash since |
@@ -237,7 +237,9 @@ discard block |
||
237 | 237 | */ |
238 | 238 | public function registerFormatter($callback_name = '', $cascade = false) |
239 | 239 | { |
240 | - if (empty($callback_name)) return; |
|
240 | + if (empty($callback_name)) { |
|
241 | + return; |
|
242 | + } |
|
241 | 243 | |
242 | 244 | $this->formatters[] = array($callback_name => $cascade); |
243 | 245 | |
@@ -254,7 +256,9 @@ discard block |
||
254 | 256 | */ |
255 | 257 | public function format($str) |
256 | 258 | { |
257 | - if (! is_array($this->formatters)) return $str; |
|
259 | + if (! is_array($this->formatters)) { |
|
260 | + return $str; |
|
261 | + } |
|
258 | 262 | |
259 | 263 | foreach ($this->formatters as $formatter) { |
260 | 264 | $method = key($formatter); |
@@ -262,7 +266,9 @@ discard block |
||
262 | 266 | |
263 | 267 | $str = call_user_func($method, $str); |
264 | 268 | |
265 | - if (! $cascade) return $str; |
|
269 | + if (! $cascade) { |
|
270 | + return $str; |
|
271 | + } |
|
266 | 272 | } |
267 | 273 | |
268 | 274 | return $str; |
@@ -36,176 +36,176 @@ |
||
36 | 36 | |
37 | 37 | class Events { |
38 | 38 | |
39 | - /** |
|
40 | - * The list of listeners. |
|
41 | - * |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - protected static $listeners = []; |
|
45 | - |
|
46 | - /** |
|
47 | - * Flag to let us know if we've read from the config file |
|
48 | - * and have all of the defined events. |
|
49 | - * |
|
50 | - * @var bool |
|
51 | - */ |
|
52 | - protected static $have_read_from_file = false; |
|
53 | - |
|
54 | - //-------------------------------------------------------------------- |
|
55 | - |
|
56 | - /** |
|
57 | - * Registers an action to happen on an event. The action can be any sort |
|
58 | - * of callable: |
|
59 | - * |
|
60 | - * Events::on('create', 'myFunction'); // procedural function |
|
61 | - * Events::on('create', ['myClass', 'myMethod']); // Class::method |
|
62 | - * Events::on('create', [$myInstance, 'myMethod']); // Method on an existing instance |
|
63 | - * Events::on('create', function() {}); // Closure |
|
64 | - * |
|
65 | - * @param $event_name |
|
66 | - * @param callable $callback |
|
67 | - * @param int $priority |
|
68 | - */ |
|
69 | - public static function on($event_name, callable $callback, $priority=EVENTS_PRIORITY_NORMAL) |
|
70 | - { |
|
71 | - if (! isset(self::$listeners[$event_name])) |
|
72 | - { |
|
73 | - self::$listeners[$event_name] = [ |
|
74 | - true, // If there's only 1 item, it's sorted. |
|
75 | - [$priority], |
|
76 | - [$callback] |
|
77 | - ]; |
|
78 | - } |
|
79 | - else |
|
80 | - { |
|
81 | - self::$listeners[$event_name][0] = false; // Not sorted |
|
82 | - self::$listeners[$event_name][1][] = $priority; |
|
83 | - self::$listeners[$event_name][2][] = $callback; |
|
84 | - } |
|
85 | - } |
|
39 | + /** |
|
40 | + * The list of listeners. |
|
41 | + * |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + protected static $listeners = []; |
|
45 | + |
|
46 | + /** |
|
47 | + * Flag to let us know if we've read from the config file |
|
48 | + * and have all of the defined events. |
|
49 | + * |
|
50 | + * @var bool |
|
51 | + */ |
|
52 | + protected static $have_read_from_file = false; |
|
53 | + |
|
54 | + //-------------------------------------------------------------------- |
|
55 | + |
|
56 | + /** |
|
57 | + * Registers an action to happen on an event. The action can be any sort |
|
58 | + * of callable: |
|
59 | + * |
|
60 | + * Events::on('create', 'myFunction'); // procedural function |
|
61 | + * Events::on('create', ['myClass', 'myMethod']); // Class::method |
|
62 | + * Events::on('create', [$myInstance, 'myMethod']); // Method on an existing instance |
|
63 | + * Events::on('create', function() {}); // Closure |
|
64 | + * |
|
65 | + * @param $event_name |
|
66 | + * @param callable $callback |
|
67 | + * @param int $priority |
|
68 | + */ |
|
69 | + public static function on($event_name, callable $callback, $priority=EVENTS_PRIORITY_NORMAL) |
|
70 | + { |
|
71 | + if (! isset(self::$listeners[$event_name])) |
|
72 | + { |
|
73 | + self::$listeners[$event_name] = [ |
|
74 | + true, // If there's only 1 item, it's sorted. |
|
75 | + [$priority], |
|
76 | + [$callback] |
|
77 | + ]; |
|
78 | + } |
|
79 | + else |
|
80 | + { |
|
81 | + self::$listeners[$event_name][0] = false; // Not sorted |
|
82 | + self::$listeners[$event_name][1][] = $priority; |
|
83 | + self::$listeners[$event_name][2][] = $callback; |
|
84 | + } |
|
85 | + } |
|
86 | 86 | |
87 | - //-------------------------------------------------------------------- |
|
88 | - |
|
89 | - /** |
|
90 | - * Runs through all subscribed methods running them one at a time, |
|
91 | - * until either: |
|
92 | - * a) All subscribers have finished or |
|
93 | - * b) a method returns false, at which point execution of subscribers stops. |
|
94 | - * |
|
95 | - * @param $event_name |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - public static function trigger($event_name, array $arguments = []) |
|
99 | - { |
|
100 | - // Read in our config/events file so that we have them all! |
|
101 | - if (! self::$have_read_from_file) |
|
102 | - { |
|
103 | - if (is_file(APPPATH .'config/events.php')) |
|
104 | - { |
|
105 | - include APPPATH .'config/events.php'; |
|
106 | - } |
|
107 | - self::$have_read_from_file = true; |
|
108 | - } |
|
109 | - |
|
110 | - foreach (self::listeners($event_name) as $listener) |
|
111 | - { |
|
112 | - $result = call_user_func_array($listener, $arguments); |
|
113 | - |
|
114 | - if ($result === false) |
|
115 | - { |
|
116 | - return false; |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - return true; |
|
121 | - } |
|
87 | + //-------------------------------------------------------------------- |
|
88 | + |
|
89 | + /** |
|
90 | + * Runs through all subscribed methods running them one at a time, |
|
91 | + * until either: |
|
92 | + * a) All subscribers have finished or |
|
93 | + * b) a method returns false, at which point execution of subscribers stops. |
|
94 | + * |
|
95 | + * @param $event_name |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + public static function trigger($event_name, array $arguments = []) |
|
99 | + { |
|
100 | + // Read in our config/events file so that we have them all! |
|
101 | + if (! self::$have_read_from_file) |
|
102 | + { |
|
103 | + if (is_file(APPPATH .'config/events.php')) |
|
104 | + { |
|
105 | + include APPPATH .'config/events.php'; |
|
106 | + } |
|
107 | + self::$have_read_from_file = true; |
|
108 | + } |
|
109 | + |
|
110 | + foreach (self::listeners($event_name) as $listener) |
|
111 | + { |
|
112 | + $result = call_user_func_array($listener, $arguments); |
|
113 | + |
|
114 | + if ($result === false) |
|
115 | + { |
|
116 | + return false; |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + return true; |
|
121 | + } |
|
122 | 122 | |
123 | - //-------------------------------------------------------------------- |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns an array of listeners for a single event. They are |
|
127 | - * sorted by priority. |
|
128 | - * |
|
129 | - * If the listener could not be found, returns FALSE, or TRUE if |
|
130 | - * it was removed. |
|
131 | - * |
|
132 | - * @param $event_name |
|
133 | - * @return array |
|
134 | - */ |
|
135 | - public static function listeners($event_name) |
|
136 | - { |
|
137 | - if (! isset(self::$listeners[$event_name])) |
|
138 | - { |
|
139 | - return []; |
|
140 | - } |
|
141 | - |
|
142 | - // The list is not sorted |
|
143 | - if (! self::$listeners[$event_name][0]) |
|
144 | - { |
|
145 | - // Sort it! |
|
146 | - array_multisort(self::$listeners[$event_name][1], SORT_NUMERIC, self::$listeners[$event_name][2]); |
|
147 | - |
|
148 | - // Mark it as sorted already! |
|
149 | - self::$listeners[$event_name][0] = true; |
|
150 | - } |
|
151 | - |
|
152 | - return self::$listeners[$event_name][2]; |
|
153 | - } |
|
154 | - |
|
155 | - //-------------------------------------------------------------------- |
|
156 | - |
|
157 | - /** |
|
158 | - * Removes a single listener from an event. |
|
159 | - * |
|
160 | - * If the listener couldn't be found, returns FALSE, else TRUE if |
|
161 | - * it was removed. |
|
162 | - * |
|
163 | - * @param $event_name |
|
164 | - * @param callable $listener |
|
165 | - * @return bool |
|
166 | - */ |
|
167 | - public static function removeListener($event_name, callable $listener) |
|
168 | - { |
|
169 | - if (! isset(self::$listeners[$event_name])) |
|
170 | - { |
|
171 | - return false; |
|
172 | - } |
|
173 | - |
|
174 | - foreach (self::$listeners[$event_name][2] as $index => $check) |
|
175 | - { |
|
176 | - if ($check === $listener) |
|
177 | - { |
|
178 | - unset(self::$listeners[$event_name][1][$index]); |
|
179 | - unset(self::$listeners[$event_name][2][$index]); |
|
180 | - |
|
181 | - return true; |
|
182 | - } |
|
183 | - } |
|
184 | - |
|
185 | - return false; |
|
186 | - } |
|
187 | - |
|
188 | - //-------------------------------------------------------------------- |
|
189 | - |
|
190 | - /** |
|
191 | - * Removes all listeners. |
|
192 | - * |
|
193 | - * If the event_name is specified, only listeners for that event will be |
|
194 | - * removed, otherwise all listeners for all events are removed. |
|
195 | - * |
|
196 | - * @param null $event_name |
|
197 | - */ |
|
198 | - public static function removeAllListeners($event_name=null) |
|
199 | - { |
|
200 | - if (! is_null($event_name)) |
|
201 | - { |
|
202 | - unset(self::$listeners[$event_name]); |
|
203 | - } |
|
204 | - else { |
|
205 | - self::$listeners = []; |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - //-------------------------------------------------------------------- |
|
123 | + //-------------------------------------------------------------------- |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns an array of listeners for a single event. They are |
|
127 | + * sorted by priority. |
|
128 | + * |
|
129 | + * If the listener could not be found, returns FALSE, or TRUE if |
|
130 | + * it was removed. |
|
131 | + * |
|
132 | + * @param $event_name |
|
133 | + * @return array |
|
134 | + */ |
|
135 | + public static function listeners($event_name) |
|
136 | + { |
|
137 | + if (! isset(self::$listeners[$event_name])) |
|
138 | + { |
|
139 | + return []; |
|
140 | + } |
|
141 | + |
|
142 | + // The list is not sorted |
|
143 | + if (! self::$listeners[$event_name][0]) |
|
144 | + { |
|
145 | + // Sort it! |
|
146 | + array_multisort(self::$listeners[$event_name][1], SORT_NUMERIC, self::$listeners[$event_name][2]); |
|
147 | + |
|
148 | + // Mark it as sorted already! |
|
149 | + self::$listeners[$event_name][0] = true; |
|
150 | + } |
|
151 | + |
|
152 | + return self::$listeners[$event_name][2]; |
|
153 | + } |
|
154 | + |
|
155 | + //-------------------------------------------------------------------- |
|
156 | + |
|
157 | + /** |
|
158 | + * Removes a single listener from an event. |
|
159 | + * |
|
160 | + * If the listener couldn't be found, returns FALSE, else TRUE if |
|
161 | + * it was removed. |
|
162 | + * |
|
163 | + * @param $event_name |
|
164 | + * @param callable $listener |
|
165 | + * @return bool |
|
166 | + */ |
|
167 | + public static function removeListener($event_name, callable $listener) |
|
168 | + { |
|
169 | + if (! isset(self::$listeners[$event_name])) |
|
170 | + { |
|
171 | + return false; |
|
172 | + } |
|
173 | + |
|
174 | + foreach (self::$listeners[$event_name][2] as $index => $check) |
|
175 | + { |
|
176 | + if ($check === $listener) |
|
177 | + { |
|
178 | + unset(self::$listeners[$event_name][1][$index]); |
|
179 | + unset(self::$listeners[$event_name][2][$index]); |
|
180 | + |
|
181 | + return true; |
|
182 | + } |
|
183 | + } |
|
184 | + |
|
185 | + return false; |
|
186 | + } |
|
187 | + |
|
188 | + //-------------------------------------------------------------------- |
|
189 | + |
|
190 | + /** |
|
191 | + * Removes all listeners. |
|
192 | + * |
|
193 | + * If the event_name is specified, only listeners for that event will be |
|
194 | + * removed, otherwise all listeners for all events are removed. |
|
195 | + * |
|
196 | + * @param null $event_name |
|
197 | + */ |
|
198 | + public static function removeAllListeners($event_name=null) |
|
199 | + { |
|
200 | + if (! is_null($event_name)) |
|
201 | + { |
|
202 | + unset(self::$listeners[$event_name]); |
|
203 | + } |
|
204 | + else { |
|
205 | + self::$listeners = []; |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + //-------------------------------------------------------------------- |
|
210 | 210 | |
211 | 211 | } |
@@ -66,12 +66,12 @@ discard block |
||
66 | 66 | * @param callable $callback |
67 | 67 | * @param int $priority |
68 | 68 | */ |
69 | - public static function on($event_name, callable $callback, $priority=EVENTS_PRIORITY_NORMAL) |
|
69 | + public static function on($event_name, callable $callback, $priority = EVENTS_PRIORITY_NORMAL) |
|
70 | 70 | { |
71 | - if (! isset(self::$listeners[$event_name])) |
|
71 | + if ( ! isset(self::$listeners[$event_name])) |
|
72 | 72 | { |
73 | 73 | self::$listeners[$event_name] = [ |
74 | - true, // If there's only 1 item, it's sorted. |
|
74 | + true, // If there's only 1 item, it's sorted. |
|
75 | 75 | [$priority], |
76 | 76 | [$callback] |
77 | 77 | ]; |
@@ -98,11 +98,11 @@ discard block |
||
98 | 98 | public static function trigger($event_name, array $arguments = []) |
99 | 99 | { |
100 | 100 | // Read in our config/events file so that we have them all! |
101 | - if (! self::$have_read_from_file) |
|
101 | + if ( ! self::$have_read_from_file) |
|
102 | 102 | { |
103 | - if (is_file(APPPATH .'config/events.php')) |
|
103 | + if (is_file(APPPATH.'config/events.php')) |
|
104 | 104 | { |
105 | - include APPPATH .'config/events.php'; |
|
105 | + include APPPATH.'config/events.php'; |
|
106 | 106 | } |
107 | 107 | self::$have_read_from_file = true; |
108 | 108 | } |
@@ -134,13 +134,13 @@ discard block |
||
134 | 134 | */ |
135 | 135 | public static function listeners($event_name) |
136 | 136 | { |
137 | - if (! isset(self::$listeners[$event_name])) |
|
137 | + if ( ! isset(self::$listeners[$event_name])) |
|
138 | 138 | { |
139 | 139 | return []; |
140 | 140 | } |
141 | 141 | |
142 | 142 | // The list is not sorted |
143 | - if (! self::$listeners[$event_name][0]) |
|
143 | + if ( ! self::$listeners[$event_name][0]) |
|
144 | 144 | { |
145 | 145 | // Sort it! |
146 | 146 | array_multisort(self::$listeners[$event_name][1], SORT_NUMERIC, self::$listeners[$event_name][2]); |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | */ |
167 | 167 | public static function removeListener($event_name, callable $listener) |
168 | 168 | { |
169 | - if (! isset(self::$listeners[$event_name])) |
|
169 | + if ( ! isset(self::$listeners[$event_name])) |
|
170 | 170 | { |
171 | 171 | return false; |
172 | 172 | } |
@@ -195,9 +195,9 @@ discard block |
||
195 | 195 | * |
196 | 196 | * @param null $event_name |
197 | 197 | */ |
198 | - public static function removeAllListeners($event_name=null) |
|
198 | + public static function removeAllListeners($event_name = null) |
|
199 | 199 | { |
200 | - if (! is_null($event_name)) |
|
200 | + if ( ! is_null($event_name)) |
|
201 | 201 | { |
202 | 202 | unset(self::$listeners[$event_name]); |
203 | 203 | } |
@@ -75,8 +75,7 @@ discard block |
||
75 | 75 | [$priority], |
76 | 76 | [$callback] |
77 | 77 | ]; |
78 | - } |
|
79 | - else |
|
78 | + } else |
|
80 | 79 | { |
81 | 80 | self::$listeners[$event_name][0] = false; // Not sorted |
82 | 81 | self::$listeners[$event_name][1][] = $priority; |
@@ -200,8 +199,7 @@ discard block |
||
200 | 199 | if (! is_null($event_name)) |
201 | 200 | { |
202 | 201 | unset(self::$listeners[$event_name]); |
203 | - } |
|
204 | - else { |
|
202 | + } else { |
|
205 | 203 | self::$listeners = []; |
206 | 204 | } |
207 | 205 | } |
@@ -167,11 +167,11 @@ |
||
167 | 167 | |
168 | 168 | public function reset() |
169 | 169 | { |
170 | - self::$logs = array( |
|
171 | - 'console' => array(), |
|
172 | - 'log_count' => 0, |
|
173 | - 'memory_count' => 0, |
|
174 | - ); |
|
170 | + self::$logs = array( |
|
171 | + 'console' => array(), |
|
172 | + 'log_count' => 0, |
|
173 | + 'memory_count' => 0, |
|
174 | + ); |
|
175 | 175 | } |
176 | 176 | |
177 | 177 | //-------------------------------------------------------------------- |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | */ |
93 | 93 | public static function init() |
94 | 94 | { |
95 | - self::$ci =& get_instance(); |
|
95 | + self::$ci = & get_instance(); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | //-------------------------------------------------------------------- |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | Parameters: |
106 | 106 | $data - The variable to log. |
107 | 107 | */ |
108 | - public static function log($data=null) |
|
108 | + public static function log($data = null) |
|
109 | 109 | { |
110 | 110 | if ($data !== 0 && empty($data)) |
111 | 111 | { |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | $object - The object to store the memory usage of. |
132 | 132 | $name - The name to be displayed in the console. |
133 | 133 | */ |
134 | - public static function logMemory($object=false, $name='PHP') |
|
134 | + public static function logMemory($object = false, $name = 'PHP') |
|
135 | 135 | { |
136 | 136 | $memory = memory_get_usage(); |
137 | 137 | |
@@ -182,15 +182,15 @@ discard block |
||
182 | 182 | // !PRIVATE METHODS |
183 | 183 | //-------------------------------------------------------------------- |
184 | 184 | |
185 | - protected static function addToConsole($log=null, $item=null) |
|
185 | + protected static function addToConsole($log = null, $item = null) |
|
186 | 186 | { |
187 | 187 | if (empty($log) || empty($item)) |
188 | 188 | { |
189 | 189 | return; |
190 | 190 | } |
191 | 191 | |
192 | - self::$logs['console'][] = $item; |
|
193 | - self::$logs[$log] += 1; |
|
192 | + self::$logs['console'][] = $item; |
|
193 | + self::$logs[$log] += 1; |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | //-------------------------------------------------------------------- |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | Load Time |
197 | 197 | </a> |
198 | 198 | <a href="#" id="ci-profiler-menu-memory" onclick="ci_profiler_bar.show('ci-profiler-memory', 'ci-profiler-menu-memory'); return false;"> |
199 | - <span><?php echo (! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).' MB' ?></span> |
|
199 | + <span><?php echo ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage() / 1024 / 1024, 2).' MB' ?></span> |
|
200 | 200 | Memory Used |
201 | 201 | </a> |
202 | 202 | <?php endif; ?> |
@@ -384,7 +384,7 @@ discard block |
||
384 | 384 | |
385 | 385 | <?php $append = ($section == 'get' || $section == 'post') ? '_data' : '' ?> |
386 | 386 | <a href="#" onclick="ci_profiler_bar.toggle_data_table('<?php echo $section ?>'); return false;"> |
387 | - <h2><?php echo lang('profiler_' . $section . $append) ?></h2> |
|
387 | + <h2><?php echo lang('profiler_'.$section.$append) ?></h2> |
|
388 | 388 | </a> |
389 | 389 | |
390 | 390 |
@@ -257,9 +257,12 @@ discard block |
||
257 | 257 | <?php endforeach; ?> |
258 | 258 | </table> |
259 | 259 | |
260 | - <?php else : ?> |
|
260 | + <?php else { |
|
261 | + : ?> |
|
261 | 262 | |
262 | - <?php echo $sections['console']; ?> |
|
263 | + <?php echo $sections['console']; |
|
264 | +} |
|
265 | +?> |
|
263 | 266 | |
264 | 267 | <?php endif; ?> |
265 | 268 | </div> |
@@ -288,9 +291,12 @@ discard block |
||
288 | 291 | <?php endforeach; ?> |
289 | 292 | </table> |
290 | 293 | |
291 | - <?php else : ?> |
|
294 | + <?php else { |
|
295 | + : ?> |
|
292 | 296 | |
293 | - <?php echo $sections['console']; ?> |
|
297 | + <?php echo $sections['console']; |
|
298 | +} |
|
299 | +?> |
|
294 | 300 | |
295 | 301 | <?php endif; ?> |
296 | 302 | </div> |
@@ -309,9 +315,12 @@ discard block |
||
309 | 315 | <?php endforeach; ?> |
310 | 316 | </table> |
311 | 317 | |
312 | - <?php else : ?> |
|
318 | + <?php else { |
|
319 | + : ?> |
|
313 | 320 | |
314 | - <?php echo $sections['benchmarks']; ?> |
|
321 | + <?php echo $sections['benchmarks']; |
|
322 | +} |
|
323 | +?> |
|
315 | 324 | |
316 | 325 | <?php endif; ?> |
317 | 326 | </div> |
@@ -332,9 +341,12 @@ discard block |
||
332 | 341 | <?php endforeach; ?> |
333 | 342 | </table> |
334 | 343 | |
335 | - <?php else : ?> |
|
344 | + <?php else { |
|
345 | + : ?> |
|
336 | 346 | |
337 | - <?php echo $sections['queries']; ?> |
|
347 | + <?php echo $sections['queries']; |
|
348 | +} |
|
349 | +?> |
|
338 | 350 | |
339 | 351 | <?php endif; ?> |
340 | 352 | </div> |
@@ -394,8 +406,11 @@ discard block |
||
394 | 406 | <?php foreach ($sections[$section] as $key => $val) : ?> |
395 | 407 | <tr><td class="hilight"><?php echo $key ?></td><td><?php echo htmlspecialchars($val) ?></td></tr> |
396 | 408 | <?php endforeach; ?> |
397 | - <?php else : ?> |
|
398 | - <tr><td><?php echo $sections[$section]; ?></td></tr> |
|
409 | + <?php else { |
|
410 | + : ?> |
|
411 | + <tr><td><?php echo $sections[$section]; |
|
412 | +} |
|
413 | +?></td></tr> |
|
399 | 414 | <?php endif; ?> |
400 | 415 | </table> |
401 | 416 | <?php endif; ?> |
@@ -422,19 +437,25 @@ discard block |
||
422 | 437 | <?php endforeach; ?> |
423 | 438 | </table> |
424 | 439 | |
425 | - <?php else : ?> |
|
440 | + <?php else { |
|
441 | + : ?> |
|
426 | 442 | |
427 | - <?php echo $sections['files']; ?> |
|
443 | + <?php echo $sections['files']; |
|
444 | +} |
|
445 | +?> |
|
428 | 446 | |
429 | 447 | <?php endif; ?> |
430 | 448 | </div> |
431 | 449 | <?php endif; ?> |
432 | 450 | |
433 | 451 | |
434 | -<?php else: ?> |
|
452 | +<?php else { |
|
453 | + : ?> |
|
435 | 454 | |
436 | 455 | <p class="ci-profiler-box"><?php echo lang('profiler_no_profiles') ?></p> |
437 | 456 | |
438 | -<?php endif; ?> |
|
457 | +<?php endif; |
|
458 | +} |
|
459 | +?> |
|
439 | 460 | |
440 | 461 | </div> <!-- /codeigniter_profiler --> |