Skip to content

Commit 60fcdd0

Browse files
author
Samuel Akopyan
committed
Console command changes, implemented IConsoleCommand interfaces
1 parent e76f979 commit 60fcdd0

File tree

10 files changed

+147
-56
lines changed

10 files changed

+147
-56
lines changed

bin/aii.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
require_once($apphp);
1515
A::init($config)->run();
1616

17-
18-
$console = new CConsole($argv);
17+
// We get automatically $argv and $argc, as we run command line command
18+
$console = new CConsole($argv);
1919
$consoleCommand = new CConsoleCommand(
2020
$console->getCommand(),
2121
$console->getParams()

bin/protected/tmp/logs/error.log

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

framework/Apphp.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ class A
9797
);
9898
/** @var array */
9999
private static $_coreConsoleClasses = array(
100-
'CConsole' => 'console/CConsole.php',
101-
'CConsoleCommand' => 'console/CConsoleCommand.php',
102-
'CHelpCommand' => 'console/CHelpCommand.php',
103-
'CVersionCommand' => 'console/CVersionCommand.php',
100+
'CConsole' => 'console/CConsole.php',
101+
'CConsoleCommand' => 'console/CConsoleCommand.php',
102+
'CHelpCommand' => 'console/CHelpCommand.php',
103+
'CVersionCommand' => 'console/CVersionCommand.php',
104+
'CCacheClearCommand' => 'console/CCacheClearCommand.php',
105+
'CMakeControllerCommand' => 'console/CMakeControllerCommand.php',
104106
);
105107
/** @var array */
106108
private static $_coreComponents = array(
Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,72 @@
11
<?php
22

3-
var_dump($argv);
4-
var_dump($argc);
3+
/**
4+
* CCacheClearCommand console core class file
5+
*
6+
* @project ApPHP Framework
7+
* @author ApPHP <info@apphp.com>
8+
* @link http://www.apphpframework.com/
9+
* @copyright Copyright (c) 2012 - 2020 ApPHP Framework
10+
* @license http://www.apphpframework.com/license/
11+
*
12+
* PUBLIC (static): PROTECTED: PRIVATE (static):
13+
* --------------- --------------- ---------------
14+
* handle (static)
15+
*/
16+
17+
class CCacheClearCommand implements IConsoleCommand
18+
{
19+
20+
/**
21+
* Handle specific console command
22+
* @param string $param
23+
* @return string
24+
*/
25+
public static function handle($param = '')
26+
{
27+
$output = '';
28+
29+
//var_dump($argv);
30+
//var_dump($argc);
31+
32+
if (empty($param)) {
33+
$output .= CConsole::redbg("No cache type for deleting is defined. Type cache:clear -help") . PHP_EOL;
34+
// } elseif ($param === '-h' || $param === '-help') {
35+
// $output .= CConsole::yellow("Usage:") . PHP_EOL;
36+
// $output .= " cache:clear-all\tFlush all application cache". PHP_EOL;
37+
// $output .= " cache:clear [type]\tFlush specific application cache". PHP_EOL;
38+
// $output .= " \t\t\t[type] - the type of cache to be removed: 'db', 'css', 'js' or 'all'". PHP_EOL;
39+
// } elseif (in_array($param, array('db', 'css', 'js', 'all'))) {
40+
// if($param == 'db' || $param == 'all'){
41+
// if (CConfig::get('cache.db.path') == '') {
42+
// $output .= CConsole::redbg("Config value 'cache.db.path' is not defined. Check your configuration file.") . PHP_EOL;
43+
// }else{
44+
// $result = CFile::emptyDirectory(CConfig::get('cache.db.path'), array('index.html'));
45+
// $output .= 'DB cache ' . ($result ? 'successfully cleaned' : 'error');
46+
// }
47+
// }
48+
// if($param == 'css' || $param == 'all'){
49+
// if (CConfig::get('compression.css.path') == '') {
50+
// $output .= CConsole::redbg("Config value 'compression.css.path' is not defined. Check your configuration file.") . PHP_EOL;
51+
// }else{
52+
// $result = CFile::emptyDirectory(CConfig::get('compression.css.path'), array('index.html'));
53+
// $output .= 'CSS cache ' . ($result ? 'successfully cleaned' : 'error');
54+
// }
55+
// }
56+
// if($param == 'js' || $param == 'all'){
57+
// if (CConfig::get('compression.js.path') == '') {
58+
// $output .= CConsole::redbg("Config value 'compression.js.path' is not defined. Check your configuration file.") . PHP_EOL;
59+
// }else{
60+
// $result = CFile::emptyDirectory(CConfig::get('compression.js.path'), array('index.html'));
61+
// $output .= 'JS cache ' . ($result ? 'successfully cleaned' : 'error');
62+
// }
63+
// }
64+
}
65+
66+
67+
return $output;
68+
}
69+
70+
}
71+
572

framework/console/CConsole.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class CConsole
2727

2828
/**
2929
* Class constructor
30+
*
31+
* @param array $argv
3032
*/
3133
public function __construct($argv = array())
3234
{

framework/console/CConsoleCommand.php

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,64 +46,31 @@ public function run($return = false)
4646

4747
switch ($this->command) {
4848

49-
case '':
5049
case '-h':
5150
case '--help':
52-
51+
case 'help':
5352
$output .= CHelpCommand::handle();
5453
break;
5554

5655
case '-v':
5756
case '--version':
58-
5957
$output .= CVersionCommand::handle();
6058
break;
6159

6260
case 'cache:clear':
6361
case 'cache:clear-all':
64-
6562
if ($this->command === 'cache:clear-all') {
6663
$this->param = 'all';
6764
}
6865

69-
if (empty($this->param)) {
70-
$output .= CConsole::redbg("No cache type for deleting is defined. Type cache:clear -help") . PHP_EOL;
71-
} elseif ($this->param === '-h' || $this->param === '-help') {
72-
$output .= CConsole::yellow("Usage:") . PHP_EOL;
73-
$output .= " cache:clear-all\tFlush all application cache". PHP_EOL;
74-
$output .= " cache:clear [type]\tFlush specific application cache". PHP_EOL;
75-
$output .= " \t\t\t[type] - the type of cache to be removed: 'db', 'css', 'js' or 'all'". PHP_EOL;
76-
} elseif (in_array($this->param, array('db', 'css', 'js', 'all'))) {
77-
if($this->param == 'db' || $this->param == 'all'){
78-
if (CConfig::get('cache.db.path') == '') {
79-
$output .= CConsole::redbg("Config value 'cache.db.path' is not defined. Check your configuration file.") . PHP_EOL;
80-
}else{
81-
$result = CFile::emptyDirectory(CConfig::get('cache.db.path'), array('index.html'));
82-
$output .= 'DB cache ' . ($result ? 'successfully cleaned' : 'error');
83-
}
84-
}
85-
if($this->param == 'css' || $this->param == 'all'){
86-
if (CConfig::get('compression.css.path') == '') {
87-
$output .= CConsole::redbg("Config value 'compression.css.path' is not defined. Check your configuration file.") . PHP_EOL;
88-
}else{
89-
$result = CFile::emptyDirectory(CConfig::get('compression.css.path'), array('index.html'));
90-
$output .= 'CSS cache ' . ($result ? 'successfully cleaned' : 'error');
91-
}
92-
}
93-
if($this->param == 'js' || $this->param == 'all'){
94-
if (CConfig::get('compression.js.path') == '') {
95-
$output .= CConsole::redbg("Config value 'compression.js.path' is not defined. Check your configuration file.") . PHP_EOL;
96-
}else{
97-
$result = CFile::emptyDirectory(CConfig::get('compression.js.path'), array('index.html'));
98-
$output .= 'JS cache ' . ($result ? 'successfully cleaned' : 'error');
99-
}
100-
}
101-
}
66+
$output .= CCacheClearCommand::handle($this->param);
67+
break;
10268

69+
case 'make:controller':
70+
$output .= CMakeControllerCommand::handle($this->param);
10371
break;
10472

10573
default:
106-
10774
$output .= PHP_EOL;
10875
$output .= CConsole::redbg("Command '".$this->command."' is not defined.") . PHP_EOL;
10976
$output .= 'Type "bin/aii --help" to check all commands and options.';

framework/console/CHelpCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
* handle (static)
1515
*/
1616

17-
class CHelpCommand
17+
class CHelpCommand implements IConsoleCommand
1818
{
1919

20+
/**
21+
* Handle specific console command
22+
* @return string
23+
*/
2024
public static function handle()
2125
{
2226
$output = '';
@@ -35,9 +39,16 @@ public static function handle()
3539
$output .= PHP_EOL;
3640

3741
$output .= CConsole::yellow("Available commands:") . PHP_EOL;
42+
43+
$output .= " ".CConsole::green("help")."\t\t\tHelp on console commands". PHP_EOL;
44+
45+
$output .= CConsole::yellow("cache") . PHP_EOL;
3846
$output .= " ".CConsole::green("cache:clear")."\t\tFlush specific application cache". PHP_EOL;
3947
$output .= " ".CConsole::green("cache:clear-all")."\tFlush all application cache". PHP_EOL;
4048

49+
$output .= CConsole::yellow("make") . PHP_EOL;
50+
$output .= " ".CConsole::green("make:controller")."\tCreate controller". PHP_EOL;
51+
4152
return $output;
4253
}
4354

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* CMakeControllerCommand console core class file
5+
*
6+
* @project ApPHP Framework
7+
* @author ApPHP <info@apphp.com>
8+
* @link http://www.apphpframework.com/
9+
* @copyright Copyright (c) 2012 - 2020 ApPHP Framework
10+
* @license http://www.apphpframework.com/license/
11+
*
12+
* PUBLIC (static): PROTECTED: PRIVATE (static):
13+
* --------------- --------------- ---------------
14+
* handle (static)
15+
*/
16+
17+
class CMakeControllerCommand implements IConsoleCommand
18+
{
19+
20+
/**
21+
* Handle specific console command
22+
*
23+
* @param string $param
24+
*
25+
* @return string
26+
*/
27+
public static function handle($param = '')
28+
{
29+
$output = 'Controller created: ' . $param;
30+
31+
return $output;
32+
}
33+
34+
}

framework/console/CVersionCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
* handle (static)
1515
*/
1616

17-
class CVersionCommand
17+
class CVersionCommand implements IConsoleCommand
1818
{
1919

20+
/**
21+
* Handle specific console command
22+
* @return string
23+
*/
2024
public static function handle()
2125
{
2226
$output = 'ApPHP Framework ' . CConsole::green(A::version());

framework/core/interfaces.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ interface IActiveRecord
2121
*/
2222
public static function model();
2323
}
24+
25+
26+
/**
27+
* IConsoleCommand is the interface that must be implemented by console command classes
28+
*/
29+
interface IConsoleCommand
30+
{
31+
/**
32+
* Handle specific console command
33+
*/
34+
public static function handle();
35+
}

0 commit comments

Comments
 (0)