Skip to content

Commit 9e75199

Browse files
authored
Merge pull request #6 from cherry-framework/features/maker
Features/maker
2 parents a4803e3 + d487550 commit 9e75199

File tree

13 files changed

+253
-20
lines changed

13 files changed

+253
-20
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.7.0](https://github.com/cherry-framework/console/releases/tag/v1.6.0 "v1.7.0") (2019-04-23)
4+
5+
- Added Cherry features maker (See `./console maker`)
6+
- Make controller (`./console make controller`)
7+
38
## [v1.6.0](https://github.com/cherry-framework/console/releases/tag/v1.6.0 "v1.6.0") (2019-04-23)
49

510
- Added Cherry features debugger (See `./console debug`)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"require": {
1818
"php": ">=5.6.0",
1919
"cherry-project/core": "^1.5",
20-
"ext-json": "*"
20+
"ext-json": "*",
21+
"ext-readline": "*"
2122
},
2223
"autoload": {
2324
"psr-4": {

examples/config/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"ROUTES_FILE": "config/routes.json",
3-
"CONTROLLERS_PATH": "",
4-
"TEMPLATES_PATH": "",
3+
"CONTROLLERS_PATH": "controllers",
4+
"TEMPLATES_PATH": "templates",
55
"LOGS_PATH": "",
66
"WEB_ROOT": "/"
77
}

examples/config/routes.json

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
{
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-
}
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+
"hello": {
18+
"path": "/hello",
19+
"method": "GET",
20+
"action": "Cherry\\Controller\\HelloController::hello"
21+
}
1722
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Cherry\Controller;
4+
5+
class HelloController
6+
{
7+
use ControllerTrait;
8+
9+
public function hello()
10+
{
11+
$this->render('hello/index');
12+
}
13+
}

examples/index.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
<?php
22

3-
echo "Hello, World!";
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use Cherry\Kernel;
6+
use Cherry\Routing\Router;
7+
8+
$kernel = new Kernel(__DIR__);
9+
10+
$router = new Router();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Welcome to HelloController!

src/Console/Command/Maker.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
/**
3+
* The file contains Maker 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+
* Maker 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 Maker
29+
{
30+
private $_templatesPath = __DIR__.'/Maker/Templates';
31+
32+
/**
33+
* Run Cherry Features maker.
34+
*
35+
* @param ArgvInput $input CLI Input interface
36+
* @param Output $output CLI Output interface
37+
*
38+
* @return void
39+
*/
40+
private function _make(ArgvInput $input, Output $output)
41+
{
42+
$argv = $input->getArgv();
43+
44+
if ($input->getArgvCount() == 1) {
45+
$this->_makeHelp($output);
46+
} else {
47+
$this->_callMakerMethod($argv[1], $input, $output);
48+
}
49+
}
50+
51+
/**
52+
* Call maker by argument.
53+
*
54+
* @param string $method Method for calling
55+
* @param ArgvInput $input CLI Input interface
56+
* @param Output $output CLI Output interface
57+
*
58+
* @return void
59+
*/
60+
private function _callMakerMethod($method, ArgvInput $input, Output $output)
61+
{
62+
$method = '_make'.ucfirst($method);
63+
64+
if (method_exists($this, $method)) {
65+
$this->{$method}($input, $output);
66+
} else {
67+
$this->_makeHelp($output);
68+
}
69+
}
70+
71+
/**
72+
* Get Maker help
73+
*
74+
* @param Output $output CLI Output interface
75+
*
76+
* @return void
77+
*/
78+
private function _makeHelp(Output $output)
79+
{
80+
$help = file_get_contents(__DIR__.'/Maker/Docs/help.txt');
81+
print $output->text($help);
82+
}
83+
84+
/**
85+
* Make new Cherry Controller.
86+
*
87+
* @param ArgvInput $input CLI Input interface
88+
* @param Output $output CLI Output interface
89+
*
90+
* @return void
91+
*/
92+
private function _makeController(ArgvInput $input, Output $output)
93+
{
94+
print $output->success('Make Controller.')."\n";
95+
print $output->text('Enter Controller Name [hello]:');
96+
97+
//Get controller name from stdin
98+
$controllerTitle = readline() ?: 'hello';
99+
100+
// Remove spaces
101+
$controllerTitle = trim($controllerTitle);
102+
103+
// If empty, set default value
104+
$controllerTitle = $controllerTitle == '' ? 'hello' : $controllerTitle;
105+
106+
// Generate Controller name
107+
$controllerName = ucfirst($controllerTitle).'Controller';
108+
109+
// Check if controller exists
110+
if (file_exists(CONTROLLERS_PATH.'/'.$controllerName.'.php')) {
111+
print "\n".$output
112+
->warning("Controller {$controllerTitle} already exists!");
113+
} else {
114+
$templatesPath = $this->_templatesPath . '/Controller/';
115+
116+
// Get templates
117+
$controllerTemplate = file_get_contents(
118+
$templatesPath . '/controller.txt'
119+
);
120+
$templateTemplate = file_get_contents($templatesPath . '/template.txt');
121+
122+
// Replace controller name in templates
123+
$controllerTemplate = str_replace(
124+
['{controllerName}', '{controllerTitle}'],
125+
[$controllerName, $controllerTitle],
126+
$controllerTemplate
127+
);
128+
$templateTemplate = str_replace(
129+
'{controllerName}',
130+
$controllerName,
131+
$templateTemplate
132+
);
133+
134+
// Create directories if they not found
135+
if (!file_exists(CONTROLLERS_PATH)) {
136+
mkdir(CONTROLLERS_PATH, 0755, true);
137+
}
138+
if (!file_exists(TEMPLATES_PATH . '/' . $controllerTitle)) {
139+
mkdir(TEMPLATES_PATH . '/' . $controllerTitle, 0755, true);
140+
}
141+
142+
// Write to files
143+
file_put_contents(
144+
CONTROLLERS_PATH . '/' . $controllerName . '.php',
145+
$controllerTemplate
146+
);
147+
file_put_contents(
148+
TEMPLATES_PATH . '/' . $controllerTitle . '/index.templater.php',
149+
$templateTemplate
150+
);
151+
152+
// Add route for new controller
153+
154+
// Get old routes
155+
$routes = file_get_contents(ROUTES_FILE);
156+
$routes = json_decode($routes, 1);
157+
158+
// Add new route
159+
$routes[$controllerTitle] = array(
160+
'path' => '/' . $controllerTitle,
161+
'method' => 'GET',
162+
'action' => 'Cherry\Controller\\' . $controllerName . '::hello'
163+
);
164+
165+
// Save routes
166+
file_put_contents(
167+
ROUTES_FILE,
168+
json_encode($routes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
169+
);
170+
171+
print "\n" . $output
172+
->success("Controller {$controllerTitle} created successfully!");
173+
}
174+
}
175+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Cherry Maker.
2+
3+
run ./console make {argument} for creating {argument} feature.
4+
5+
{argument} values:
6+
7+
controller - Create new Cherry controller.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Cherry\Controller;
4+
5+
class {controllerName}
6+
{
7+
use ControllerTrait;
8+
9+
public function hello()
10+
{
11+
$this->render('{controllerTitle}/index');
12+
}
13+
}

0 commit comments

Comments
 (0)