@@ -2,7 +2,7 @@ |
||
| 2 | 2 | <h1><?php echo lang('docs_search_results') ?></h1> |
| 3 | 3 | |
| 4 | 4 | <div class="well"> |
| 5 | - <?php echo form_open( current_url(), 'class="form-inline"'); ?> |
|
| 5 | + <?php echo form_open(current_url(), 'class="form-inline"'); ?> |
|
| 6 | 6 | <input type="text" name="search_terms" class="form-control" style="width: 85%" value="<?php echo set_value('search_terms', $search_terms) ?>" /> |
| 7 | 7 | <input type="submit" name="submit" class="btn btn-primary" value="<?php echo lang('docs_search'); ?>"/> |
| 8 | 8 | <?php echo form_close(); ?> |
@@ -24,10 +24,13 @@ |
||
| 24 | 24 | </div> |
| 25 | 25 | <?php endforeach; ?> |
| 26 | 26 | |
| 27 | -<?php else: ?> |
|
| 27 | +<?php else { |
|
| 28 | + : ?> |
|
| 28 | 29 | |
| 29 | 30 | <div class="alert alert-info"> |
| 30 | - <?php echo sprintf(lang('docs_no_results'), $search_terms); ?> |
|
| 31 | + <?php echo sprintf(lang('docs_no_results'), $search_terms); |
|
| 32 | +} |
|
| 33 | +?> |
|
| 31 | 34 | </div> |
| 32 | 35 | |
| 33 | 36 | <?php endif; ?> |
@@ -34,133 +34,133 @@ discard block |
||
| 34 | 34 | |
| 35 | 35 | class Forge extends \Myth\Controllers\CLIController { |
| 36 | 36 | |
| 37 | - public function __construct() |
|
| 38 | - { |
|
| 39 | - parent::__construct(); |
|
| 37 | + public function __construct() |
|
| 38 | + { |
|
| 39 | + parent::__construct(); |
|
| 40 | 40 | |
| 41 | - $this->load->config('forge'); |
|
| 42 | - } |
|
| 41 | + $this->load->config('forge'); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - //-------------------------------------------------------------------- |
|
| 44 | + //-------------------------------------------------------------------- |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | - public function _remap($method, $params) |
|
| 48 | - { |
|
| 49 | - if (method_exists($this, $method)) |
|
| 50 | - { |
|
| 51 | - call_user_func_array( [$this, $method], $params); |
|
| 52 | - } |
|
| 53 | - else |
|
| 54 | - { |
|
| 55 | - $params = array_merge([CLI::cli_string()], $params); |
|
| 47 | + public function _remap($method, $params) |
|
| 48 | + { |
|
| 49 | + if (method_exists($this, $method)) |
|
| 50 | + { |
|
| 51 | + call_user_func_array( [$this, $method], $params); |
|
| 52 | + } |
|
| 53 | + else |
|
| 54 | + { |
|
| 55 | + $params = array_merge([CLI::cli_string()], $params); |
|
| 56 | 56 | |
| 57 | - call_user_func_array( [$this, 'run'], $params); |
|
| 58 | - } |
|
| 59 | - } |
|
| 57 | + call_user_func_array( [$this, 'run'], $params); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - //-------------------------------------------------------------------- |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Overrides to implement dynamic description building based on |
|
| 65 | - * scanning the collections and grabbing the information from |
|
| 66 | - * 'forge.php' files. |
|
| 67 | - */ |
|
| 68 | - public function index() |
|
| 69 | - { |
|
| 70 | - $collections = config_item('forge.collections'); |
|
| 71 | - |
|
| 72 | - if (! is_array($collections) || ! count($collections) ) |
|
| 73 | - { |
|
| 74 | - return CLI::error('No generator collections found.'); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - // We loop through each collection scanning |
|
| 78 | - // for any generator folders that have a |
|
| 79 | - // 'forge.php' file. For each one found |
|
| 80 | - // we build out another section in our help commands |
|
| 81 | - foreach ($collections as $alias => $path) |
|
| 82 | - { |
|
| 83 | - $path = rtrim($path, '/ ') .'/'; |
|
| 84 | - $folders = scandir($path); |
|
| 85 | - |
|
| 86 | - $_descriptions = []; |
|
| 87 | - |
|
| 88 | - foreach ($folders as $dir) |
|
| 89 | - { |
|
| 90 | - if ($dir == '.' || $dir == '..' || ! is_file($path . $dir .'/forge.php')) |
|
| 91 | - { |
|
| 92 | - continue; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - include $path . $dir .'/forge.php'; |
|
| 96 | - |
|
| 97 | - // Don't have valid arrays to work with? Move along... |
|
| 98 | - if (! isset($descriptions)) |
|
| 99 | - { |
|
| 100 | - log_message('debug', '[Forge] Invalid forge.php file at: '. $path . $dir .'/forge.php'); |
|
| 101 | - continue; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - $_descriptions = array_merge($descriptions, $_descriptions); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - ksort($_descriptions); |
|
| 108 | - |
|
| 109 | - CLI::new_line(); |
|
| 110 | - CLI::write(ucwords( str_replace('_', ' ', $alias)) .' Collection'); |
|
| 111 | - $this->sayDescriptions($_descriptions); |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - //-------------------------------------------------------------------- |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * The primary method that calls the correct generator and |
|
| 119 | - * makes it run. |
|
| 120 | - */ |
|
| 121 | - public function run($command) |
|
| 122 | - { |
|
| 123 | - $quiet = false; |
|
| 124 | - |
|
| 125 | - $segments = explode(" ", $command); |
|
| 126 | - |
|
| 127 | - // Get rid of the 'forge' command |
|
| 128 | - if ($segments[0] == 'forge') { |
|
| 129 | - array_shift( $segments ); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - $command = trim(str_ireplace("forge", '', array_shift($segments))); |
|
| 61 | + //-------------------------------------------------------------------- |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Overrides to implement dynamic description building based on |
|
| 65 | + * scanning the collections and grabbing the information from |
|
| 66 | + * 'forge.php' files. |
|
| 67 | + */ |
|
| 68 | + public function index() |
|
| 69 | + { |
|
| 70 | + $collections = config_item('forge.collections'); |
|
| 71 | + |
|
| 72 | + if (! is_array($collections) || ! count($collections) ) |
|
| 73 | + { |
|
| 74 | + return CLI::error('No generator collections found.'); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + // We loop through each collection scanning |
|
| 78 | + // for any generator folders that have a |
|
| 79 | + // 'forge.php' file. For each one found |
|
| 80 | + // we build out another section in our help commands |
|
| 81 | + foreach ($collections as $alias => $path) |
|
| 82 | + { |
|
| 83 | + $path = rtrim($path, '/ ') .'/'; |
|
| 84 | + $folders = scandir($path); |
|
| 85 | + |
|
| 86 | + $_descriptions = []; |
|
| 87 | + |
|
| 88 | + foreach ($folders as $dir) |
|
| 89 | + { |
|
| 90 | + if ($dir == '.' || $dir == '..' || ! is_file($path . $dir .'/forge.php')) |
|
| 91 | + { |
|
| 92 | + continue; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + include $path . $dir .'/forge.php'; |
|
| 96 | + |
|
| 97 | + // Don't have valid arrays to work with? Move along... |
|
| 98 | + if (! isset($descriptions)) |
|
| 99 | + { |
|
| 100 | + log_message('debug', '[Forge] Invalid forge.php file at: '. $path . $dir .'/forge.php'); |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + $_descriptions = array_merge($descriptions, $_descriptions); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + ksort($_descriptions); |
|
| 108 | + |
|
| 109 | + CLI::new_line(); |
|
| 110 | + CLI::write(ucwords( str_replace('_', ' ', $alias)) .' Collection'); |
|
| 111 | + $this->sayDescriptions($_descriptions); |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + //-------------------------------------------------------------------- |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * The primary method that calls the correct generator and |
|
| 119 | + * makes it run. |
|
| 120 | + */ |
|
| 121 | + public function run($command) |
|
| 122 | + { |
|
| 123 | + $quiet = false; |
|
| 124 | + |
|
| 125 | + $segments = explode(" ", $command); |
|
| 126 | + |
|
| 127 | + // Get rid of the 'forge' command |
|
| 128 | + if ($segments[0] == 'forge') { |
|
| 129 | + array_shift( $segments ); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + $command = trim(str_ireplace("forge", '', array_shift($segments))); |
|
| 133 | 133 | |
| 134 | 134 | $dir = $this->locateGenerator($command); |
| 135 | 135 | |
| 136 | 136 | $class_name = ucfirst($command) .'Generator'; |
| 137 | 137 | |
| 138 | - if (! file_exists($dir . $class_name .'.php')) |
|
| 139 | - { |
|
| 140 | - return CLI::error("Generator file not found for: {$class_name}"); |
|
| 141 | - } |
|
| 138 | + if (! file_exists($dir . $class_name .'.php')) |
|
| 139 | + { |
|
| 140 | + return CLI::error("Generator file not found for: {$class_name}"); |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | 143 | require_once $dir . $class_name .'.php'; |
| 144 | 144 | |
| 145 | - if (! class_exists($class_name, false)) |
|
| 146 | - { |
|
| 147 | - return CLI::error("No class `{$class_name}` found in generator file."); |
|
| 148 | - } |
|
| 145 | + if (! class_exists($class_name, false)) |
|
| 146 | + { |
|
| 147 | + return CLI::error("No class `{$class_name}` found in generator file."); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - // Should we run the process quietly? |
|
| 151 | - if ( (CLI::option('q') || CLI::option('quiet'))) |
|
| 152 | - { |
|
| 153 | - $quiet = true; |
|
| 154 | - } |
|
| 150 | + // Should we run the process quietly? |
|
| 151 | + if ( (CLI::option('q') || CLI::option('quiet'))) |
|
| 152 | + { |
|
| 153 | + $quiet = true; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - CLI::write('Invoked '. CLI::color($class_name, 'yellow')); |
|
| 156 | + CLI::write('Invoked '. CLI::color($class_name, 'yellow')); |
|
| 157 | 157 | |
| 158 | 158 | $class = new $class_name(); |
| 159 | 159 | |
| 160 | - $class->run( $segments, $quiet ); |
|
| 161 | - } |
|
| 160 | + $class->run( $segments, $quiet ); |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - //-------------------------------------------------------------------- |
|
| 163 | + //-------------------------------------------------------------------- |
|
| 164 | 164 | |
| 165 | 165 | /** |
| 166 | 166 | * Displays the readme file for a generator if it exists. |
@@ -205,67 +205,67 @@ discard block |
||
| 205 | 205 | //-------------------------------------------------------------------- |
| 206 | 206 | |
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * Overrides CLIController's version to support searching our |
|
| 210 | - * collections for the help description. |
|
| 211 | - * |
|
| 212 | - * @param null $method |
|
| 213 | - */ |
|
| 214 | - public function longDescribeMethod($method=null) |
|
| 215 | - { |
|
| 216 | - $collections = config_item('forge.collections'); |
|
| 217 | - |
|
| 218 | - if (! is_array($collections) || ! count($collections) ) |
|
| 219 | - { |
|
| 220 | - return CLI::error('No generator collections found.'); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - // We loop through each collection scanning |
|
| 224 | - // for any generator folders that have a |
|
| 225 | - // 'forge.php' file. For each one found |
|
| 226 | - // we build out another section in our help commands |
|
| 227 | - foreach ($collections as $alias => $path) |
|
| 228 | - { |
|
| 229 | - |
|
| 230 | - $path = rtrim($path, '/ ') .'/'; |
|
| 231 | - $folders = scandir($path); |
|
| 232 | - |
|
| 233 | - if (! $i = array_search(ucfirst($method), $folders)) |
|
| 234 | - { |
|
| 235 | - continue; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $dir = $path . $folders[$i] .'/'; |
|
| 239 | - |
|
| 240 | - if (! is_file($dir .'/forge.php')) |
|
| 241 | - { |
|
| 242 | - CLI::error("The {$method} command does not have any cli help available."); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - include $dir .'/forge.php'; |
|
| 246 | - |
|
| 247 | - // Don't have valid arrays to work with? Move along... |
|
| 248 | - if (! isset($long_description)) |
|
| 249 | - { |
|
| 250 | - log_message('debug', '[Forge] Invalid forge.php file at: '. $dir .'/forge.php'); |
|
| 251 | - continue; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if (empty($long_description)) |
|
| 255 | - { |
|
| 256 | - return CLI::error("The {$method} command does not have an cli help available."); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - CLI::new_line(); |
|
| 260 | - CLI::write( CLI::color(ucfirst($method) .' Help', 'yellow') ); |
|
| 261 | - return CLI::write( CLI::wrap($long_description, CLI::getWidth()) ); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - // Still here? |
|
| 265 | - CLI::error("No help found for command: {$method}"); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - //-------------------------------------------------------------------- |
|
| 208 | + /** |
|
| 209 | + * Overrides CLIController's version to support searching our |
|
| 210 | + * collections for the help description. |
|
| 211 | + * |
|
| 212 | + * @param null $method |
|
| 213 | + */ |
|
| 214 | + public function longDescribeMethod($method=null) |
|
| 215 | + { |
|
| 216 | + $collections = config_item('forge.collections'); |
|
| 217 | + |
|
| 218 | + if (! is_array($collections) || ! count($collections) ) |
|
| 219 | + { |
|
| 220 | + return CLI::error('No generator collections found.'); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + // We loop through each collection scanning |
|
| 224 | + // for any generator folders that have a |
|
| 225 | + // 'forge.php' file. For each one found |
|
| 226 | + // we build out another section in our help commands |
|
| 227 | + foreach ($collections as $alias => $path) |
|
| 228 | + { |
|
| 229 | + |
|
| 230 | + $path = rtrim($path, '/ ') .'/'; |
|
| 231 | + $folders = scandir($path); |
|
| 232 | + |
|
| 233 | + if (! $i = array_search(ucfirst($method), $folders)) |
|
| 234 | + { |
|
| 235 | + continue; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $dir = $path . $folders[$i] .'/'; |
|
| 239 | + |
|
| 240 | + if (! is_file($dir .'/forge.php')) |
|
| 241 | + { |
|
| 242 | + CLI::error("The {$method} command does not have any cli help available."); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + include $dir .'/forge.php'; |
|
| 246 | + |
|
| 247 | + // Don't have valid arrays to work with? Move along... |
|
| 248 | + if (! isset($long_description)) |
|
| 249 | + { |
|
| 250 | + log_message('debug', '[Forge] Invalid forge.php file at: '. $dir .'/forge.php'); |
|
| 251 | + continue; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if (empty($long_description)) |
|
| 255 | + { |
|
| 256 | + return CLI::error("The {$method} command does not have an cli help available."); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + CLI::new_line(); |
|
| 260 | + CLI::write( CLI::color(ucfirst($method) .' Help', 'yellow') ); |
|
| 261 | + return CLI::write( CLI::wrap($long_description, CLI::getWidth()) ); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + // Still here? |
|
| 265 | + CLI::error("No help found for command: {$method}"); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + //-------------------------------------------------------------------- |
|
| 269 | 269 | |
| 270 | 270 | /** |
| 271 | 271 | * Scans through the collections for the folder for this generator. |
@@ -48,13 +48,13 @@ discard block |
||
| 48 | 48 | { |
| 49 | 49 | if (method_exists($this, $method)) |
| 50 | 50 | { |
| 51 | - call_user_func_array( [$this, $method], $params); |
|
| 51 | + call_user_func_array([$this, $method], $params); |
|
| 52 | 52 | } |
| 53 | 53 | else |
| 54 | 54 | { |
| 55 | 55 | $params = array_merge([CLI::cli_string()], $params); |
| 56 | 56 | |
| 57 | - call_user_func_array( [$this, 'run'], $params); |
|
| 57 | + call_user_func_array([$this, 'run'], $params); |
|
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | { |
| 70 | 70 | $collections = config_item('forge.collections'); |
| 71 | 71 | |
| 72 | - if (! is_array($collections) || ! count($collections) ) |
|
| 72 | + if ( ! is_array($collections) || ! count($collections)) |
|
| 73 | 73 | { |
| 74 | 74 | return CLI::error('No generator collections found.'); |
| 75 | 75 | } |
@@ -80,24 +80,24 @@ discard block |
||
| 80 | 80 | // we build out another section in our help commands |
| 81 | 81 | foreach ($collections as $alias => $path) |
| 82 | 82 | { |
| 83 | - $path = rtrim($path, '/ ') .'/'; |
|
| 83 | + $path = rtrim($path, '/ ').'/'; |
|
| 84 | 84 | $folders = scandir($path); |
| 85 | 85 | |
| 86 | 86 | $_descriptions = []; |
| 87 | 87 | |
| 88 | 88 | foreach ($folders as $dir) |
| 89 | 89 | { |
| 90 | - if ($dir == '.' || $dir == '..' || ! is_file($path . $dir .'/forge.php')) |
|
| 90 | + if ($dir == '.' || $dir == '..' || ! is_file($path.$dir.'/forge.php')) |
|
| 91 | 91 | { |
| 92 | 92 | continue; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - include $path . $dir .'/forge.php'; |
|
| 95 | + include $path.$dir.'/forge.php'; |
|
| 96 | 96 | |
| 97 | 97 | // Don't have valid arrays to work with? Move along... |
| 98 | - if (! isset($descriptions)) |
|
| 98 | + if ( ! isset($descriptions)) |
|
| 99 | 99 | { |
| 100 | - log_message('debug', '[Forge] Invalid forge.php file at: '. $path . $dir .'/forge.php'); |
|
| 100 | + log_message('debug', '[Forge] Invalid forge.php file at: '.$path.$dir.'/forge.php'); |
|
| 101 | 101 | continue; |
| 102 | 102 | } |
| 103 | 103 | |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | ksort($_descriptions); |
| 108 | 108 | |
| 109 | 109 | CLI::new_line(); |
| 110 | - CLI::write(ucwords( str_replace('_', ' ', $alias)) .' Collection'); |
|
| 110 | + CLI::write(ucwords(str_replace('_', ' ', $alias)).' Collection'); |
|
| 111 | 111 | $this->sayDescriptions($_descriptions); |
| 112 | 112 | } |
| 113 | 113 | } |
@@ -126,38 +126,38 @@ discard block |
||
| 126 | 126 | |
| 127 | 127 | // Get rid of the 'forge' command |
| 128 | 128 | if ($segments[0] == 'forge') { |
| 129 | - array_shift( $segments ); |
|
| 129 | + array_shift($segments); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | $command = trim(str_ireplace("forge", '', array_shift($segments))); |
| 133 | 133 | |
| 134 | 134 | $dir = $this->locateGenerator($command); |
| 135 | 135 | |
| 136 | - $class_name = ucfirst($command) .'Generator'; |
|
| 136 | + $class_name = ucfirst($command).'Generator'; |
|
| 137 | 137 | |
| 138 | - if (! file_exists($dir . $class_name .'.php')) |
|
| 138 | + if ( ! file_exists($dir.$class_name.'.php')) |
|
| 139 | 139 | { |
| 140 | 140 | return CLI::error("Generator file not found for: {$class_name}"); |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - require_once $dir . $class_name .'.php'; |
|
| 143 | + require_once $dir.$class_name.'.php'; |
|
| 144 | 144 | |
| 145 | - if (! class_exists($class_name, false)) |
|
| 145 | + if ( ! class_exists($class_name, false)) |
|
| 146 | 146 | { |
| 147 | 147 | return CLI::error("No class `{$class_name}` found in generator file."); |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | // Should we run the process quietly? |
| 151 | - if ( (CLI::option('q') || CLI::option('quiet'))) |
|
| 151 | + if ((CLI::option('q') || CLI::option('quiet'))) |
|
| 152 | 152 | { |
| 153 | 153 | $quiet = true; |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - CLI::write('Invoked '. CLI::color($class_name, 'yellow')); |
|
| 156 | + CLI::write('Invoked '.CLI::color($class_name, 'yellow')); |
|
| 157 | 157 | |
| 158 | 158 | $class = new $class_name(); |
| 159 | 159 | |
| 160 | - $class->run( $segments, $quiet ); |
|
| 160 | + $class->run($segments, $quiet); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | //-------------------------------------------------------------------- |
@@ -171,20 +171,20 @@ discard block |
||
| 171 | 171 | { |
| 172 | 172 | $dir = $this->locateGenerator($command); |
| 173 | 173 | |
| 174 | - if (! is_file($dir .'readme.txt')) |
|
| 174 | + if ( ! is_file($dir.'readme.txt')) |
|
| 175 | 175 | { |
| 176 | 176 | return CLI::error('Unable to locate the readme.txt file.'); |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - $lines = file($dir .'readme.txt'); |
|
| 179 | + $lines = file($dir.'readme.txt'); |
|
| 180 | 180 | |
| 181 | - if (! is_array($lines) || ! count($lines)) |
|
| 181 | + if ( ! is_array($lines) || ! count($lines)) |
|
| 182 | 182 | { |
| 183 | 183 | return CLI::error('The readme file does not have anything to display.'); |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | $line_count = 0; // Total we're currently viewing. |
| 187 | - $max_rows = CLI::getHeight() -3; |
|
| 187 | + $max_rows = CLI::getHeight() - 3; |
|
| 188 | 188 | |
| 189 | 189 | foreach ($lines as $line) |
| 190 | 190 | { |
@@ -211,11 +211,11 @@ discard block |
||
| 211 | 211 | * |
| 212 | 212 | * @param null $method |
| 213 | 213 | */ |
| 214 | - public function longDescribeMethod($method=null) |
|
| 214 | + public function longDescribeMethod($method = null) |
|
| 215 | 215 | { |
| 216 | 216 | $collections = config_item('forge.collections'); |
| 217 | 217 | |
| 218 | - if (! is_array($collections) || ! count($collections) ) |
|
| 218 | + if ( ! is_array($collections) || ! count($collections)) |
|
| 219 | 219 | { |
| 220 | 220 | return CLI::error('No generator collections found.'); |
| 221 | 221 | } |
@@ -227,27 +227,27 @@ discard block |
||
| 227 | 227 | foreach ($collections as $alias => $path) |
| 228 | 228 | { |
| 229 | 229 | |
| 230 | - $path = rtrim($path, '/ ') .'/'; |
|
| 230 | + $path = rtrim($path, '/ ').'/'; |
|
| 231 | 231 | $folders = scandir($path); |
| 232 | 232 | |
| 233 | - if (! $i = array_search(ucfirst($method), $folders)) |
|
| 233 | + if ( ! $i = array_search(ucfirst($method), $folders)) |
|
| 234 | 234 | { |
| 235 | 235 | continue; |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | - $dir = $path . $folders[$i] .'/'; |
|
| 238 | + $dir = $path.$folders[$i].'/'; |
|
| 239 | 239 | |
| 240 | - if (! is_file($dir .'/forge.php')) |
|
| 240 | + if ( ! is_file($dir.'/forge.php')) |
|
| 241 | 241 | { |
| 242 | 242 | CLI::error("The {$method} command does not have any cli help available."); |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | - include $dir .'/forge.php'; |
|
| 245 | + include $dir.'/forge.php'; |
|
| 246 | 246 | |
| 247 | 247 | // Don't have valid arrays to work with? Move along... |
| 248 | - if (! isset($long_description)) |
|
| 248 | + if ( ! isset($long_description)) |
|
| 249 | 249 | { |
| 250 | - log_message('debug', '[Forge] Invalid forge.php file at: '. $dir .'/forge.php'); |
|
| 250 | + log_message('debug', '[Forge] Invalid forge.php file at: '.$dir.'/forge.php'); |
|
| 251 | 251 | continue; |
| 252 | 252 | } |
| 253 | 253 | |
@@ -257,8 +257,8 @@ discard block |
||
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | CLI::new_line(); |
| 260 | - CLI::write( CLI::color(ucfirst($method) .' Help', 'yellow') ); |
|
| 261 | - return CLI::write( CLI::wrap($long_description, CLI::getWidth()) ); |
|
| 260 | + CLI::write(CLI::color(ucfirst($method).' Help', 'yellow')); |
|
| 261 | + return CLI::write(CLI::wrap($long_description, CLI::getWidth())); |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | // Still here? |
@@ -278,22 +278,22 @@ discard block |
||
| 278 | 278 | { |
| 279 | 279 | $collections = config_item('forge.collections'); |
| 280 | 280 | |
| 281 | - if (! is_array($collections) || ! count($collections) ) |
|
| 281 | + if ( ! is_array($collections) || ! count($collections)) |
|
| 282 | 282 | { |
| 283 | 283 | return CLI::error('No generator collections found.'); |
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | foreach ($collections as $alias => $path) |
| 287 | 287 | { |
| 288 | - $path = rtrim($path, '/ ') .'/'; |
|
| 288 | + $path = rtrim($path, '/ ').'/'; |
|
| 289 | 289 | $folders = scandir($path); |
| 290 | 290 | |
| 291 | - if (! $i = array_search(ucfirst($name), $folders)) |
|
| 291 | + if ( ! $i = array_search(ucfirst($name), $folders)) |
|
| 292 | 292 | { |
| 293 | 293 | continue; |
| 294 | 294 | } |
| 295 | 295 | |
| 296 | - return $path . $folders[$i] .'/'; |
|
| 296 | + return $path.$folders[$i].'/'; |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | return null; |
@@ -253,8 +253,7 @@ |
||
| 253 | 253 | { |
| 254 | 254 | $this->vars[$k] = $escape ? esc($v, $context, $this->escaper) : $v; |
| 255 | 255 | } |
| 256 | - } |
|
| 257 | - else |
|
| 256 | + } else |
|
| 258 | 257 | { |
| 259 | 258 | $this->vars[$name] = $escape ? esc($value, $context, $this->escaper) : $value; |
| 260 | 259 | } |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | { |
| 101 | 101 | parent::__construct(); |
| 102 | 102 | |
| 103 | - $this->load->library('session'); |
|
| 103 | + $this->load->library('session'); |
|
| 104 | 104 | |
| 105 | 105 | $this->setupCache(); |
| 106 | 106 | |
@@ -236,8 +236,8 @@ discard block |
||
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | $this->output->enable_profiler( FALSE ) |
| 239 | - ->set_content_type( 'text/plain' ) |
|
| 240 | - ->set_output( $text ); |
|
| 239 | + ->set_content_type( 'text/plain' ) |
|
| 240 | + ->set_output( $text ); |
|
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | //-------------------------------------------------------------------- |
@@ -267,8 +267,8 @@ discard block |
||
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | $this->output->enable_profiler( FALSE ) |
| 270 | - ->set_content_type( 'application/json' ) |
|
| 271 | - ->set_output( json_encode( $json ) ); |
|
| 270 | + ->set_content_type( 'application/json' ) |
|
| 271 | + ->set_output( json_encode( $json ) ); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | //-------------------------------------------------------------------- |
@@ -292,8 +292,8 @@ discard block |
||
| 292 | 292 | } |
| 293 | 293 | |
| 294 | 294 | $this->output->enable_profiler( FALSE ) |
| 295 | - ->set_content_type( 'application/x-javascript' ) |
|
| 296 | - ->set_output( $js ); |
|
| 295 | + ->set_content_type( 'application/x-javascript' ) |
|
| 296 | + ->set_output( $js ); |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | //-------------------------------------------------------------------- |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | */ |
| 32 | 32 | |
| 33 | 33 | /* PHP5 spl_autoload */ |
| 34 | -spl_autoload_register( '\Myth\Modules::autoload' ); |
|
| 34 | +spl_autoload_register('\Myth\Modules::autoload'); |
|
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * The following properties are used to provide autocomplete for IDE's. |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | |
| 111 | 111 | $this->setupProfiler(); |
| 112 | 112 | |
| 113 | - log_message( 'debug', get_class( $this ) . ' controller loaded.' ); |
|
| 113 | + log_message('debug', get_class($this).' controller loaded.'); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | //-------------------------------------------------------------------- |
@@ -131,18 +131,18 @@ discard block |
||
| 131 | 131 | { |
| 132 | 132 | // If the controller doesn't override cache type, grab the values from |
| 133 | 133 | // the defaults set in the start file. |
| 134 | - if ( empty( $this->cache_type ) ) |
|
| 134 | + if (empty($this->cache_type)) |
|
| 135 | 135 | { |
| 136 | - $this->cache_type = $this->config->item( 'cache_type' ); |
|
| 136 | + $this->cache_type = $this->config->item('cache_type'); |
|
| 137 | 137 | } |
| 138 | - if ( empty( $this->backup_cache ) ) |
|
| 138 | + if (empty($this->backup_cache)) |
|
| 139 | 139 | { |
| 140 | - $this->backup_cache = $this->config->item( 'backup_cache_type' ); |
|
| 140 | + $this->backup_cache = $this->config->item('backup_cache_type'); |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | // Make sure that caching is ALWAYS available throughout the app |
| 144 | 144 | // though it defaults to 'dummy' which won't actually cache. |
| 145 | - $this->load->driver( 'cache', array( 'adapter' => $this->cache_type, 'backup' => $this->backup_cache ) ); |
|
| 145 | + $this->load->driver('cache', array('adapter' => $this->cache_type, 'backup' => $this->backup_cache)); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | //-------------------------------------------------------------------- |
@@ -153,15 +153,15 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | protected function autoload() |
| 155 | 155 | { |
| 156 | - if ( ! is_null( $this->language_file ) ) |
|
| 156 | + if ( ! is_null($this->language_file)) |
|
| 157 | 157 | { |
| 158 | - $this->lang->load( $this->language_file ); |
|
| 158 | + $this->lang->load($this->language_file); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - if ( ! is_null( $this->model_file ) ) |
|
| 161 | + if ( ! is_null($this->model_file)) |
|
| 162 | 162 | { |
| 163 | 163 | $this->load->database(); |
| 164 | - $this->load->model( $this->model_file ); |
|
| 164 | + $this->load->model($this->model_file); |
|
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | |
@@ -173,19 +173,19 @@ discard block |
||
| 173 | 173 | */ |
| 174 | 174 | protected function autoMigrate() |
| 175 | 175 | { |
| 176 | - $migrations = config_item( 'auto_migrate' ); |
|
| 176 | + $migrations = config_item('auto_migrate'); |
|
| 177 | 177 | |
| 178 | - if ( ! is_array( $migrations ) || ! count( $migrations ) ) |
|
| 178 | + if ( ! is_array($migrations) || ! count($migrations)) |
|
| 179 | 179 | { |
| 180 | 180 | return; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - $this->load->library( 'migration' ); |
|
| 183 | + $this->load->library('migration'); |
|
| 184 | 184 | |
| 185 | 185 | // Run all of our migrations for each group. |
| 186 | - foreach ( $migrations as $group ) |
|
| 186 | + foreach ($migrations as $group) |
|
| 187 | 187 | { |
| 188 | - $this->migration->latest( $group ); |
|
| 188 | + $this->migration->latest($group); |
|
| 189 | 189 | } |
| 190 | 190 | } |
| 191 | 191 | |
@@ -199,9 +199,9 @@ discard block |
||
| 199 | 199 | // The profiler is dealt with twice so that we can set |
| 200 | 200 | // things up to work correctly in AJAX methods using $this->render_json |
| 201 | 201 | // and it's cousins. |
| 202 | - if ( $this->config->item( 'show_profiler' ) == TRUE ) |
|
| 202 | + if ($this->config->item('show_profiler') == TRUE) |
|
| 203 | 203 | { |
| 204 | - $this->output->enable_profiler( TRUE ); |
|
| 204 | + $this->output->enable_profiler(TRUE); |
|
| 205 | 205 | } |
| 206 | 206 | } |
| 207 | 207 | |
@@ -223,21 +223,21 @@ discard block |
||
| 223 | 223 | * |
| 224 | 224 | * @return void [type] [description] |
| 225 | 225 | */ |
| 226 | - public function renderText( $text, $typography = FALSE ) |
|
| 226 | + public function renderText($text, $typography = FALSE) |
|
| 227 | 227 | { |
| 228 | 228 | // Note that, for now anyway, we don't do any cleaning of the text |
| 229 | 229 | // and leave that up to the client to take care of. |
| 230 | 230 | |
| 231 | 231 | // However, we can auto_typogrify the text if we're asked nicely. |
| 232 | - if ( $typography === TRUE ) |
|
| 232 | + if ($typography === TRUE) |
|
| 233 | 233 | { |
| 234 | - $this->load->helper( 'typography' ); |
|
| 235 | - $text = auto_typography( $text ); |
|
| 234 | + $this->load->helper('typography'); |
|
| 235 | + $text = auto_typography($text); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | - $this->output->enable_profiler( FALSE ) |
|
| 239 | - ->set_content_type( 'text/plain' ) |
|
| 240 | - ->set_output( $text ); |
|
| 238 | + $this->output->enable_profiler(FALSE) |
|
| 239 | + ->set_content_type('text/plain') |
|
| 240 | + ->set_output($text); |
|
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | //-------------------------------------------------------------------- |
@@ -253,22 +253,22 @@ discard block |
||
| 253 | 253 | * @throws RenderException |
| 254 | 254 | * @return void |
| 255 | 255 | */ |
| 256 | - public function renderJSON( $json ) |
|
| 256 | + public function renderJSON($json) |
|
| 257 | 257 | { |
| 258 | - if ( is_resource( $json ) ) |
|
| 258 | + if (is_resource($json)) |
|
| 259 | 259 | { |
| 260 | - throw new \RuntimeException( lang('bad_json_encode') ); |
|
| 260 | + throw new \RuntimeException(lang('bad_json_encode')); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | - if ( $this->config->item( 'show_profiler' ) ) |
|
| 263 | + if ($this->config->item('show_profiler')) |
|
| 264 | 264 | { |
| 265 | - $this->load->library( 'profiler' ); |
|
| 265 | + $this->load->library('profiler'); |
|
| 266 | 266 | $json['#sprint-profiler'] = $this->profiler->run(); |
| 267 | 267 | } |
| 268 | 268 | |
| 269 | - $this->output->enable_profiler( FALSE ) |
|
| 270 | - ->set_content_type( 'application/json' ) |
|
| 271 | - ->set_output( json_encode( $json ) ); |
|
| 269 | + $this->output->enable_profiler(FALSE) |
|
| 270 | + ->set_content_type('application/json') |
|
| 271 | + ->set_output(json_encode($json)); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | //-------------------------------------------------------------------- |
@@ -284,16 +284,16 @@ discard block |
||
| 284 | 284 | * @throws RenderException |
| 285 | 285 | * @return void |
| 286 | 286 | */ |
| 287 | - public function renderJS( $js = NULL ) |
|
| 287 | + public function renderJS($js = NULL) |
|
| 288 | 288 | { |
| 289 | - if ( ! is_string( $js ) ) |
|
| 289 | + if ( ! is_string($js)) |
|
| 290 | 290 | { |
| 291 | - throw new \RuntimeException( lang('bad_javascript') ); |
|
| 291 | + throw new \RuntimeException(lang('bad_javascript')); |
|
| 292 | 292 | } |
| 293 | 293 | |
| 294 | - $this->output->enable_profiler( FALSE ) |
|
| 295 | - ->set_content_type( 'application/x-javascript' ) |
|
| 296 | - ->set_output( $js ); |
|
| 294 | + $this->output->enable_profiler(FALSE) |
|
| 295 | + ->set_content_type('application/x-javascript') |
|
| 296 | + ->set_output($js); |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | //-------------------------------------------------------------------- |
@@ -308,11 +308,11 @@ discard block |
||
| 308 | 308 | */ |
| 309 | 309 | public function renderRealtime() |
| 310 | 310 | { |
| 311 | - if ( ob_get_level() > 0 ) |
|
| 311 | + if (ob_get_level() > 0) |
|
| 312 | 312 | { |
| 313 | 313 | end_end_flush(); |
| 314 | 314 | } |
| 315 | - ob_implicit_flush( TRUE ); |
|
| 315 | + ob_implicit_flush(TRUE); |
|
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | //-------------------------------------------------------------------- |
@@ -326,21 +326,21 @@ discard block |
||
| 326 | 326 | * |
| 327 | 327 | * @param string $location [description] |
| 328 | 328 | */ |
| 329 | - public function ajaxRedirect( $location = '' ) |
|
| 329 | + public function ajaxRedirect($location = '') |
|
| 330 | 330 | { |
| 331 | - $location = empty( $location ) ? '/' : $location; |
|
| 331 | + $location = empty($location) ? '/' : $location; |
|
| 332 | 332 | |
| 333 | - if ( strpos( $location, '/' ) !== 0 || strpos( $location, '://' ) !== FALSE ) |
|
| 333 | + if (strpos($location, '/') !== 0 || strpos($location, '://') !== FALSE) |
|
| 334 | 334 | { |
| 335 | - if ( ! function_exists( 'site_url' ) ) |
|
| 335 | + if ( ! function_exists('site_url')) |
|
| 336 | 336 | { |
| 337 | - $this->load->helper( 'url' ); |
|
| 337 | + $this->load->helper('url'); |
|
| 338 | 338 | } |
| 339 | 339 | |
| 340 | - $location = site_url( $location ); |
|
| 340 | + $location = site_url($location); |
|
| 341 | 341 | } |
| 342 | 342 | |
| 343 | - $this->render_json( array( 'location' => $location ) ); |
|
| 343 | + $this->render_json(array('location' => $location)); |
|
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | //-------------------------------------------------------------------- |
@@ -355,11 +355,11 @@ discard block |
||
| 355 | 355 | * |
| 356 | 356 | * @return mixed The formatted JSON data, or NULL. |
| 357 | 357 | */ |
| 358 | - public function getJSON( $format = 'object', $depth = 512 ) |
|
| 358 | + public function getJSON($format = 'object', $depth = 512) |
|
| 359 | 359 | { |
| 360 | 360 | $as_array = $format == 'array' ? TRUE : FALSE; |
| 361 | 361 | |
| 362 | - return json_decode( file_get_contents( 'php://input' ), $as_array, $depth ); |
|
| 362 | + return json_decode(file_get_contents('php://input'), $as_array, $depth); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | //-------------------------------------------------------------------- |
@@ -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 | } |
@@ -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 | } |
@@ -1,71 +1,71 @@ |
||
| 1 | 1 | <?php namespace Myth\Docs; |
| 2 | 2 | /** |
| 3 | - * Sprint |
|
| 4 | - * |
|
| 5 | - * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
| 6 | - * |
|
| 7 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
| 8 | - * of this software and associated documentation files (the "Software"), to deal |
|
| 9 | - * in the Software without restriction, including without limitation the rights |
|
| 10 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
| 11 | - * copies of the Software, and to permit persons to whom the Software is |
|
| 12 | - * furnished to do so, subject to the following conditions: |
|
| 13 | - * |
|
| 14 | - * The above copyright notice and this permission notice shall be included in |
|
| 15 | - * all copies or substantial portions of the Software. |
|
| 16 | - * |
|
| 17 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
| 18 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
| 19 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
| 20 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
| 21 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
| 22 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
| 23 | - * THE SOFTWARE. |
|
| 24 | - * |
|
| 25 | - * @package Sprint |
|
| 26 | - * @author Lonnie Ezell |
|
| 27 | - * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
| 28 | - * @license http://opensource.org/licenses/MIT (MIT) |
|
| 29 | - * @link http://sprintphp.com |
|
| 30 | - * @since Version 1.0 |
|
| 31 | - */ |
|
| 3 | + * Sprint |
|
| 4 | + * |
|
| 5 | + * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
| 6 | + * |
|
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
|
| 9 | + * in the Software without restriction, including without limitation the rights |
|
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
|
| 12 | + * furnished to do so, subject to the following conditions: |
|
| 13 | + * |
|
| 14 | + * The above copyright notice and this permission notice shall be included in |
|
| 15 | + * all copies or substantial portions of the Software. |
|
| 16 | + * |
|
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
| 23 | + * THE SOFTWARE. |
|
| 24 | + * |
|
| 25 | + * @package Sprint |
|
| 26 | + * @author Lonnie Ezell |
|
| 27 | + * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
| 28 | + * @license http://opensource.org/licenses/MIT (MIT) |
|
| 29 | + * @link http://sprintphp.com |
|
| 30 | + * @since Version 1.0 |
|
| 31 | + */ |
|
| 32 | 32 | |
| 33 | 33 | 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 | } |
@@ -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 | |
@@ -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 | } |