Skip to content

Commit a4803e3

Browse files
authored
Merge pull request #5 from cherry-framework/features/router-debugger
Features/router debugger
2 parents e922a4a + 09c025f commit a4803e3

File tree

9 files changed

+244
-62
lines changed

9 files changed

+244
-62
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Cherry-Console Changelog
22

3+
## [v1.6.0](https://github.com/cherry-framework/console/releases/tag/v1.6.0 "v1.6.0") (2019-04-23)
4+
5+
- Added Cherry features debugger (See `./console debug`)
6+
- Debug router (`./console debug router`)
7+
38
## [v1.5.0](https://github.com/cherry-framework/console/releases/tag/v1.5.0 "v1.5.0") (2019-04-21)
49

510
- Use color constants for output color definition;

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"minimum-stability": "dev",
1717
"require": {
1818
"php": ">=5.6.0",
19-
"cherry-project/core": "^1.5"
19+
"cherry-project/core": "^1.5",
20+
"ext-json": "*"
2021
},
2122
"autoload": {
2223
"psr-4": {

examples/config/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"ROUTES_FILE": "",
2+
"ROUTES_FILE": "config/routes.json",
33
"CONTROLLERS_PATH": "",
44
"TEMPLATES_PATH": "",
55
"LOGS_PATH": "",

examples/config/routes.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"homepage": {
3+
"path": "/",
4+
"method": "GET",
5+
"action": "Cherry\\Controller\\DefaultController::index"
6+
},
7+
"add-new-user": {
8+
"path": "/addUser",
9+
"method": "POST",
10+
"action": "Cherry\\Controller\\DefaultController::addUser"
11+
},
12+
"remove-user": {
13+
"path": "/addUSer/{userId}",
14+
"method": "DELETE",
15+
"action": "Cherry\\Controller\\DefaultController::removeUser"
16+
}
17+
}

src/Console/Command/Debugger.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Cherry Debugger.
2+
3+
run ./console debug {argument} for debugging {argument} feature.
4+
5+
{argument} values:
6+
7+
router - Debug application all route.

src/Console/Command/Server.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* The file contains Server 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+
* Server 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 Server
29+
{
30+
/**
31+
* Run PHP Development server from CLI
32+
*
33+
* @param ArgvInput $input CLI Input interface
34+
* @param Output $output CLI Output interface
35+
*
36+
* @return void
37+
*/
38+
private function _server(ArgvInput $input, Output $output)
39+
{
40+
$argv = $input->getArgv();
41+
42+
$server = null;
43+
44+
if (isset($argv[1]) && $argv[1] == 'run') {
45+
$server = "127.0.0.1:8000";
46+
} else if (isset($argv[1]) && $argv[1] == 'start') {
47+
if (isset($argv[2])) {
48+
$server = $argv[2];
49+
} else {
50+
$this->_printHelp();
51+
return;
52+
}
53+
} else {
54+
$this->_printHelp();
55+
return;
56+
}
57+
58+
$info = <<<EOF
59+
60+
{$output->success("Started Cherry Server on http://{$server}")}
61+
// Quit the server with Ctrl + C.
62+
EOF;
63+
64+
print $output->text($info);
65+
66+
echo exec("php -S {$server} -t " . WEB_ROOT);
67+
}
68+
}

src/Console/Console.php

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace Cherry\Console;
1515

16+
use Cherry\Console\Command\Debugger;
17+
use Cherry\Console\Command\Server;
1618
use Cherry\Console\Input\ArgvInput;
1719
use Cherry\Console\Output\Output;
1820

@@ -27,6 +29,9 @@
2729
*/
2830
class Console
2931
{
32+
use Debugger;
33+
use Server;
34+
3035
private $_argvInput;
3136
private $_output;
3237

@@ -82,7 +87,7 @@ private function _call($method)
8287
$method = "_{$method}";
8388

8489
if (method_exists($this, $method)) {
85-
$this->{$method}();
90+
$this->{$method}($this->_argvInput, $this->_output);
8691
} else {
8792
$this->_printHelp();
8893
}
@@ -95,66 +100,9 @@ private function _call($method)
95100
*/
96101
private function _printHelp()
97102
{
98-
$hello = <<<EOF
99-
Welcome to
100-
____ _
101-
/ ___| |__ ___ _ __ _ __ _ _
102-
| | | '_ \ / _ \ '__| '__| | | |
103-
| |___| | | | __/ | | | | |_| |
104-
\____|_| |_|\___|_| |_| \__, |
105-
|___/
106-
107-
Console
108-
----------------------------------
109-
110-
help, --help, -h - Show help.
111-
112-
server [option] [arguments] - Start PHP Development server.
113-
[option] - Server start options
114-
run - Start server on 127.0.0.1:8000
115-
start - Start server on given address:port
116-
[argument] - Additional arguments for option "start" address:port (127.0.0.1:8000)
117-
118-
EOF;
119-
120-
print $this->_output
121-
->text($hello);
122-
}
123-
124-
/**
125-
* Run PHP Development server from CLI
126-
*
127-
* @return void
128-
*/
129-
private function _server()
130-
{
131-
$argv = $this->_argv;
132-
133-
$server = null;
134-
135-
if (isset($argv[1]) && $argv[1] == 'run') {
136-
$server = "127.0.0.1:8000";
137-
} else if (isset($argv[1]) && $argv[1] == 'start') {
138-
if (isset($argv[2])) {
139-
$server = $argv[2];
140-
} else {
141-
$this->_printHelp();
142-
return;
143-
}
144-
} else {
145-
$this->_printHelp();
146-
return;
147-
}
148-
149-
$info = <<<EOF
150-
151-
{$this->_output->success("Started Cherry Server on http://{$server}")}
152-
// Quit the server with Ctrl + C.
153-
EOF;
103+
$help = file_get_contents(__DIR__.'/Files/Docs/help.txt');
154104

155105
print $this->_output
156-
->text($info);
157-
158-
echo exec("php -S {$server} -t " . WEB_ROOT);
106+
->text($help);
159107
}
160108
}

src/Console/Files/Docs/help.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Welcome to
2+
____ _
3+
/ ___| |__ ___ _ __ _ __ _ _
4+
| | | '_ \ / _ \ '__| '__| | | |
5+
| |___| | | | __/ | | | | |_| |
6+
\____|_| |_|\___|_| |_| \__, |
7+
|___/
8+
9+
Console
10+
----------------------------------
11+
12+
help, --help, -h - Show help.
13+
14+
server [option] [arguments] - Start PHP Development server.
15+
[option] - Server start options
16+
run - Start server on 127.0.0.1:8000
17+
start - Start server on given address:port
18+
[argument] - Additional arguments for option "start" address:port (127.0.0.1:8000)
19+
20+
debug [feature] - Debug Cherry features.
21+
[feature] - Feature for debugging
22+
router - Debug application all route.

0 commit comments

Comments
 (0)