|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * The file contains Debugger trait |
| 4 | + * |
| 5 | + * PHP version 5 |
| 6 | + * |
| 7 | + * @category Library |
| 8 | + * @package Cherry |
| 9 | + * @author Temuri Takalandze <[email protected]> |
| 10 | + * @license https://github.com/cherry-framework/console/blob/master/LICENSE MIT |
| 11 | + * @link https://github.com/cherry-framework/console |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Cherry\Console\Command; |
| 15 | + |
| 16 | +use Cherry\Console\Input\ArgvInput; |
| 17 | +use Cherry\Console\Output\Output; |
| 18 | + |
| 19 | +/** |
| 20 | + * Debugger Trait for Cherry Console. |
| 21 | + * |
| 22 | + * @category Library |
| 23 | + * @package Cherry |
| 24 | + * @author Temuri Takalandze <[email protected]> |
| 25 | + * @license https://github.com/cherry-framework/console/blob/master/LICENSE MIT |
| 26 | + * @link https://github.com/cherry-framework/console |
| 27 | + */ |
| 28 | +trait Debugger |
| 29 | +{ |
| 30 | + /** |
| 31 | + * Run Cherry Features debugger. |
| 32 | + * |
| 33 | + * @param ArgvInput $input CLI Input interface |
| 34 | + * @param Output $output CLI Output interface |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + private function _debug(ArgvInput $input, Output $output) |
| 39 | + { |
| 40 | + $argv = $input->getArgv(); |
| 41 | + |
| 42 | + if ($input->getArgvCount() == 1) { |
| 43 | + $this->_debugHelp($output); |
| 44 | + } else { |
| 45 | + $this->_callDebuggerMethod($argv[1], $input, $output); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Call debugger by argument. |
| 51 | + * |
| 52 | + * @param string $method Method for calling |
| 53 | + * @param ArgvInput $input CLI Input interface |
| 54 | + * @param Output $output CLI Output interface |
| 55 | + * |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + private function _callDebuggerMethod($method, ArgvInput $input, Output $output) |
| 59 | + { |
| 60 | + $method = '_debug'.ucfirst($method); |
| 61 | + |
| 62 | + if (method_exists($this, $method)) { |
| 63 | + $this->{$method}($input, $output); |
| 64 | + } else { |
| 65 | + $this->_debugHelp($output); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get Debugger help |
| 71 | + * |
| 72 | + * @param Output $output CLI Output interface |
| 73 | + * |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + private function _debugHelp(Output $output) |
| 77 | + { |
| 78 | + $help = file_get_contents(__DIR__.'/Debugger/Docs/help.txt'); |
| 79 | + print $output->text($help); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Debug Cherry Router. |
| 84 | + * |
| 85 | + * @param ArgvInput $input CLI Input interface |
| 86 | + * @param Output $output CLI Output interface |
| 87 | + * |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + private function _debugRouter(ArgvInput $input, Output $output) |
| 91 | + { |
| 92 | + echo $output->success('Cherry Router Debugger')."\n\n"; |
| 93 | + |
| 94 | + $routes = @file_get_contents(ROUTES_FILE); |
| 95 | + $routes = json_decode($routes, 1); |
| 96 | + |
| 97 | + foreach ($routes as $k => $v) { |
| 98 | + $desc = ''; |
| 99 | + foreach ($v as $k2 => $v2) { |
| 100 | + $desc .= <<<EOF |
| 101 | + {$k2} - {$v2} |
| 102 | +
|
| 103 | +EOF; |
| 104 | + } |
| 105 | + |
| 106 | + $full = <<<EOF |
| 107 | +{$output->info($k.':')} |
| 108 | +{$desc} |
| 109 | +EOF; |
| 110 | + |
| 111 | + echo $output->text($full); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments