Skip to content

Commit a38c381

Browse files
authored
Merge pull request #1 from cherry-framework/develop
Develop
2 parents 1fa9106 + 3a7e541 commit a38c381

File tree

8 files changed

+588
-31
lines changed

8 files changed

+588
-31
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Cherry-Console Changelog
2+
3+
## [v1.0.0](https://github.com/cherry-framework/logs-viewer/releases/tag/v1.0.0 "v1.0.0") (2019-04-8)
4+
#### The first stable version
5+
6+
- Package available on: `composer require cherry-project/console`
7+
- Output text styling
8+
- Available commands:
9+
10+
- server - Run PHP Development server
11+

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
11
# Cherry-Console
22
CLI Console for Cherry-project.
33

4+
5+
[![GitHub license](https://img.shields.io/github/license/cherry-framework/console.svg)](https://github.com/cherry-framework/console/blob/master/LICENSE)
6+
7+
[![GitHub release](https://img.shields.io/github/release/cherry-framework/console.svg)](https://github.com/console/logs-viewer/releases)
8+
9+
[![Packagist Version](https://img.shields.io/packagist/v/cherry-project/console.svg "Packagist Version")](https://packagist.org/packages/cherry-project/console "Packagist Version")
10+
11+
------------
12+
13+
## Including
14+
**Install from composer** `composer require cherry-project/console`
15+
16+
Next you must create executable PHP script (Ex.: console).
17+
18+
**Note** Add this line at top of your console for executing
19+
it in PHP interpreter: `#!/usr/bin/env php`
20+
21+
**Include Autoloader in console**
22+
```php
23+
require_once __DIR__ . '/vendor/autoload.php';
24+
```
25+
26+
## Usage
27+
28+
Import class
29+
30+
```php
31+
use Cherry\Console\Console;
32+
```
33+
34+
Create new console instance
35+
```php
36+
$console = new Console();
37+
```
38+
39+
Now you can get console help message:
40+
41+
`php console --help`
42+
43+
or
44+
45+
`./console --help`
46+
447
**2019 © Cherry-project**

examples/console

100644100755
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
#!/usr/bin/env php
22
<?php
33

4-
set_time_limit(0);
5-
64
require __DIR__ . '/../vendor/autoload.php';
75

8-
use Cherry\Console\ArgvInput;
96
use Cherry\Console\Console;
107

11-
$console = new Console(new ArgvInput());
8+
$console = new Console();

src/Console/ArgvInput.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Console/Console.php

Lines changed: 147 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,160 @@
11
<?php
2+
/**
3+
* The file contains Console class
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+
*/
213

314
namespace Cherry\Console;
415

16+
use Cherry\Console\Input\ArgvInput;
17+
use Cherry\Console\Output\Output;
18+
19+
/**
20+
* CLI Console for Cherry-project
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+
*/
528
class Console
629
{
730
private $_argvInput;
31+
private $_output;
32+
833
private $_argv;
34+
private $_argvParsed;
35+
36+
/**
37+
* Console constructor.
38+
*/
39+
public function __construct()
40+
{
41+
$this->_argvInput = new ArgvInput();
42+
$this->_output = new Output();
43+
44+
$this->_argv = $this->_argvInput->getArgv();
45+
$this->_argvParsed = $this->_argvInput->getArgvParsed();
46+
47+
$this->_run();
48+
}
49+
50+
/**
51+
* Run the Console.
52+
*
53+
* @return void
54+
*/
55+
private function _run()
56+
{
57+
$argvInput = $this->_argvInput;
58+
59+
$argv = $this->_argv;
60+
61+
if ($argvInput->getArgvCount() == 0
62+
|| $argvInput->getBoolean('help')
63+
|| $argvInput->getBoolean('h')
64+
) {
65+
$this->_printHelp();
66+
return;
67+
}
68+
69+
//Call action
70+
$this->_call($argv[0]);
71+
}
972

10-
public function __construct(ArgvInput $argvInput)
73+
/**
74+
* Call method of this class.
75+
*
76+
* @param string $method Method name
77+
*
78+
* @return void
79+
*/
80+
private function _call($method)
1181
{
12-
$this->_argvInput = $argvInput;
13-
$this->_argv = $argvInput->getArgv();
82+
$method = "_{$method}";
83+
84+
if (method_exists($this, $method)) {
85+
$this->{$method}();
86+
} else {
87+
$this->_printHelp();
88+
}
89+
}
90+
91+
/**
92+
* Print console help message.
93+
*
94+
* @return void
95+
*/
96+
private function _printHelp()
97+
{
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;
154+
155+
print $this->_output
156+
->text($info);
14157

15-
var_dump($this->_argv);
158+
echo exec("php -S {$server} -t " . dirname(dirname(__DIR__)));
16159
}
17160
}

0 commit comments

Comments
 (0)