Skip to content

Commit 2374eeb

Browse files
authored
Merge pull request #43 from nguyenanhung/v3.2.0-develop
Add action run via CLI
2 parents 6ed0ebc + 41365d8 commit 2374eeb

File tree

2 files changed

+242
-186
lines changed

2 files changed

+242
-186
lines changed

hungng/HungNG_CI_Base_Controllers.php

Lines changed: 121 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,50 @@
1515
* @author 713uk13m <dev@nguyenanhung.com>
1616
* @copyright 713uk13m <dev@nguyenanhung.com>
1717
*
18-
* @property CI_Benchmark $benchmark This class enables you to mark points and calculate the time difference between them. Memory consumption can also be displayed.
19-
* @property CI_Calendar $calendar This class enables the creation of calendars
20-
* @property CI_Cache $cache Caching Class
21-
* @property CI_Cart $cart Shopping Cart Class
22-
* @property CI_Config $config This class contains functions that enable config files to be managed
23-
* @property CI_Controller $controller This class object is the super class that every library in CodeIgniter will be assigned to
24-
* @property CI_DB_forge $dbforge Database Forge Class
18+
* @property CI_Benchmark $benchmark This class enables you to mark points and calculate the time difference between them. Memory consumption can also be displayed.
19+
* @property CI_Calendar $calendar This class enables the creation of calendars
20+
* @property CI_Cache $cache Caching Class
21+
* @property CI_Cart $cart Shopping Cart Class
22+
* @property CI_Config $config This class contains functions that enable config files to be managed
23+
* @property CI_Controller $controller This class object is the super class that every library in CodeIgniter will be assigned to
24+
* @property CI_DB_forge $dbforge Database Forge Class
2525
* @property CI_DB_pdo_driver|CI_DB_query_builder|CI_DB_driver $db This is the platform-independent base Query Builder implementation class
26-
* @property CI_DB_utility $dbutil Database Utility Class
27-
* @property CI_Driver_Library $driver Driver Library Class
28-
* @property CI_Email $email Permits email to be sent using Mail, Sendmail, or SMTP
29-
* @property CI_Encrypt $encrypt Provides two-way keyed encoding using Mcrypt
30-
* @property CI_Encryption $encryption Provides two-way keyed encryption via PHP's MCrypt and/or OpenSSL extensions
31-
* @property CI_Exceptions $exceptions Exceptions Class
32-
* @property CI_Form_validation $form_validation Form Validation Class
33-
* @property CI_FTP $ftp FTP Class
34-
* @property CI_Hooks $hooks Provides a mechanism to extend the base system without hacking
35-
* @property CI_Image_lib $image_lib Image Manipulation class
36-
* @property CI_Input $input Pre-processes global input data for security
37-
* @property CI_Javascript $javascript Javascript Class
38-
* @property CI_Jquery $jquery Jquery Class
39-
* @property CI_Lang $lang Language Class
40-
* @property CI_Loader $load Loads framework components
41-
* @property CI_Log $log Logging Class
42-
* @property CI_Migration $migration All migrations should implement this, forces up() and down() and gives access to the CI super-global
43-
* @property CI_Model $model CodeIgniter Model Class
44-
* @property CI_Output $output Responsible for sending final output to the browser
45-
* @property CI_Pagination $pagination Pagination Class
46-
* @property CI_Parser $parser Parser Class
47-
* @property CI_Profiler $profiler This class enables you to display benchmark, query, and other data in order to help with debugging and optimization.
48-
* @property CI_Router $router Parses URIs and determines routing
49-
* @property CI_Security $security Security Class
50-
* @property CI_Session $session Session Class
51-
* @property CI_Table $table Lets you create tables manually or from database result objects, or arrays
52-
* @property CI_Trackback $trackback Trackback Sending/Receiving Class
53-
* @property CI_Typography $typography Typography Class
54-
* @property CI_Unit_test $unit Simple testing class
55-
* @property CI_Upload $upload File Uploading Class
56-
* @property CI_URI $uri Parses URIs and determines routing
57-
* @property CI_User_agent $agent Identifies the platform, browser, robot, or mobile device of the browsing agent
58-
* @property CI_Xmlrpc $xmlrpc XML-RPC request handler class
59-
* @property CI_Xmlrpcs $xmlrpcs XML-RPC server class
60-
* @property CI_Zip $zip Zip Compression Class
61-
* @property CI_Utf8 $utf8 Provides support for UTF-8 environments
26+
* @property CI_DB_utility $dbutil Database Utility Class
27+
* @property CI_Driver_Library $driver Driver Library Class
28+
* @property CI_Email $email Permits email to be sent using Mail, Sendmail, or SMTP
29+
* @property CI_Encrypt $encrypt Provides two-way keyed encoding using Mcrypt
30+
* @property CI_Encryption $encryption Provides two-way keyed encryption via PHP's MCrypt and/or OpenSSL extensions
31+
* @property CI_Exceptions $exceptions Exceptions Class
32+
* @property CI_Form_validation $form_validation Form Validation Class
33+
* @property CI_FTP $ftp FTP Class
34+
* @property CI_Hooks $hooks Provides a mechanism to extend the base system without hacking
35+
* @property CI_Image_lib $image_lib Image Manipulation class
36+
* @property CI_Input $input Pre-processes global input data for security
37+
* @property CI_Javascript $javascript Javascript Class
38+
* @property CI_Jquery $jquery Jquery Class
39+
* @property CI_Lang $lang Language Class
40+
* @property CI_Loader $load Loads framework components
41+
* @property CI_Log $log Logging Class
42+
* @property CI_Migration $migration All migrations should implement this, forces up() and down() and gives access to the CI super-global
43+
* @property CI_Model $model CodeIgniter Model Class
44+
* @property CI_Output $output Responsible for sending final output to the browser
45+
* @property CI_Pagination $pagination Pagination Class
46+
* @property CI_Parser $parser Parser Class
47+
* @property CI_Profiler $profiler This class enables you to display benchmark, query, and other data in order to help with debugging and optimization.
48+
* @property CI_Router $router Parses URIs and determines routing
49+
* @property CI_Security $security Security Class
50+
* @property CI_Session $session Session Class
51+
* @property CI_Table $table Lets you create tables manually or from database result objects, or arrays
52+
* @property CI_Trackback $trackback Trackback Sending/Receiving Class
53+
* @property CI_Typography $typography Typography Class
54+
* @property CI_Unit_test $unit Simple testing class
55+
* @property CI_Upload $upload File Uploading Class
56+
* @property CI_URI $uri Parses URIs and determines routing
57+
* @property CI_User_agent $agent Identifies the platform, browser, robot, or mobile device of the browsing agent
58+
* @property CI_Xmlrpc $xmlrpc XML-RPC request handler class
59+
* @property CI_Xmlrpcs $xmlrpcs XML-RPC server class
60+
* @property CI_Zip $zip Zip Compression Class
61+
* @property CI_Utf8 $utf8 Provides support for UTF-8 environments
6262
*/
6363
class HungNG_CI_Base_Controllers extends CI_Controller
6464
{
@@ -85,26 +85,50 @@ public function __construct()
8585
protected function defaultJsonResponseInfo()
8686
{
8787
$response = array(
88-
'code' => StatusCodes::HTTP_OK,
89-
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_OK],
90-
'info' => array(
91-
'name' => 'Nguyen An Hung',
92-
'email' => 'dev@nguyenanhung.com',
93-
'web' => 'https://nguyenanhung.com',
94-
'blog' => 'https://blog.nguyenanhung.com',
88+
'code' => StatusCodes::HTTP_OK,
89+
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_OK],
90+
'info' => array(
91+
'name' => 'Nguyen An Hung',
92+
'email' => 'dev@nguyenanhung.com',
93+
'web' => 'https://nguyenanhung.com',
94+
'blog' => 'https://blog.nguyenanhung.com',
9595
'facebook' => 'https://facebook.com/nguyenanhung',
96-
'github' => 'https://github.com/nguyenanhung'
96+
'github' => 'https://github.com/nguyenanhung'
9797
),
98-
'request_data' => array(
99-
'ip' => getIPAddress(),
100-
'user_agent' => $this->input->user_agent(true),
98+
'request_data' => is_cli() ? 'Run from CLI' : array(
99+
'ip' => getIPAddress(),
100+
'user_agent' => $this->input->user_agent(true),
101101
'request_method' => $this->input->method(true)
102102
)
103103
);
104104
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response, JSON_PRETTY_PRINT))->_display();
105105
exit;
106106
}
107107

108+
/**
109+
* Function defaultJsonResponseInfoFromCLI
110+
*
111+
* User: 713uk13m <dev@nguyenanhung.com>
112+
* Copyright: 713uk13m <dev@nguyenanhung.com>
113+
* @return void
114+
*/
115+
protected function defaultJsonResponseInfoFromCLI()
116+
{
117+
$response = array(
118+
'code' => StatusCodes::HTTP_OK,
119+
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_OK],
120+
'info' => array(
121+
'name' => 'Nguyen An Hung',
122+
'email' => 'dev@nguyenanhung.com',
123+
'web' => 'https://nguyenanhung.com',
124+
'github' => 'https://github.com/nguyenanhung'
125+
),
126+
'action' => 'Default Response from CLI'
127+
);
128+
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response, JSON_PRETTY_PRINT))->_display();
129+
exit;
130+
}
131+
108132
/**
109133
* Function renderOutput
110134
*
@@ -116,12 +140,14 @@ protected function defaultJsonResponseInfo()
116140
*/
117141
protected function renderOutput($response)
118142
{
119-
$method = $this->input->method(true);
120-
$ip = getIPAddress();
121-
$userAgent = $this->input->user_agent(true);
122-
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
123-
if (method_exists($this, 'log')) {
124-
$this->log('RequestAPI', $message, $response);
143+
if (!is_cli()) {
144+
$method = $this->input->method(true);
145+
$ip = getIPAddress();
146+
$userAgent = $this->input->user_agent(true);
147+
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
148+
if (method_exists($this, 'log')) {
149+
$this->log('RequestAPI', $message, $response);
150+
}
125151
}
126152
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response))->_display();
127153
exit;
@@ -138,12 +164,14 @@ protected function renderOutput($response)
138164
*/
139165
protected function renderOutputPretty($response)
140166
{
141-
$method = $this->input->method(true);
142-
$ip = getIPAddress();
143-
$userAgent = $this->input->user_agent(true);
144-
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
145-
if (method_exists($this, 'log')) {
146-
$this->log('RequestAPI', $message, $response);
167+
if (!is_cli()) {
168+
$method = $this->input->method(true);
169+
$ip = getIPAddress();
170+
$userAgent = $this->input->user_agent(true);
171+
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
172+
if (method_exists($this, 'log')) {
173+
$this->log('RequestAPI', $message, $response);
174+
}
147175
}
148176
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response, JSON_PRETTY_PRINT))->_display();
149177
exit;
@@ -160,11 +188,13 @@ protected function renderOutputPretty($response)
160188
*/
161189
protected function jsonResponse($response = array(), $status = 200)
162190
{
163-
$method = $this->input->method(true);
164-
$ip = getIPAddress();
165-
$userAgent = $this->input->user_agent(true);
166-
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
167-
log_message('debug', $message);
191+
if (!is_cli()) {
192+
$method = $this->input->method(true);
193+
$ip = getIPAddress();
194+
$userAgent = $this->input->user_agent(true);
195+
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
196+
log_message('debug', $message);
197+
}
168198
if (is_array($response) || is_object($response)) {
169199
$response = json_encode($response);
170200
}
@@ -183,11 +213,13 @@ protected function jsonResponse($response = array(), $status = 200)
183213
*/
184214
protected function jsonResponsePretty($response = array(), $status = 200)
185215
{
186-
$method = $this->input->method(true);
187-
$ip = getIPAddress();
188-
$userAgent = $this->input->user_agent(true);
189-
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
190-
log_message('debug', $message);
216+
if (!is_cli()) {
217+
$method = $this->input->method(true);
218+
$ip = getIPAddress();
219+
$userAgent = $this->input->user_agent(true);
220+
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
221+
log_message('debug', $message);
222+
}
191223
if (is_array($response) || is_object($response)) {
192224
$response = json_encode($response, JSON_PRETTY_PRINT);
193225
}
@@ -209,20 +241,18 @@ protected function errorExceptionResponse($exception)
209241
{
210242
log_message('error', __get_error_message__($exception));
211243
log_message('error', __get_error_trace__($exception));
212-
213244
$response = array();
214245
$response['code'] = StatusCodes::HTTP_BAD_REQUEST;
215246
$response['message'] = StatusCodes::$statusTexts[StatusCodes::HTTP_BAD_REQUEST];
216247
if ((defined('ENVIRONMENT') && (ENVIRONMENT === 'development' || ENVIRONMENT === 'staging' || ENVIRONMENT === 'testing')) || in_array(getIPAddress(), config_item('whitelist_ip'), true)) {
217248
$response['error'] = array(
218-
'Code' => $exception->getCode(),
219-
'File' => $exception->getFile(),
220-
'Line' => $exception->getLine(),
221-
'Message' => $exception->getMessage(),
249+
'Code' => $exception->getCode(),
250+
'File' => $exception->getFile(),
251+
'Line' => $exception->getLine(),
252+
'Message' => $exception->getMessage(),
222253
'TraceAsString' => $exception->getTraceAsString(),
223254
);
224255
}
225-
226256
return $response;
227257
}
228258

@@ -242,7 +272,6 @@ protected function errorResponse($message = '')
242272
$response['code'] = StatusCodes::HTTP_BAD_REQUEST;
243273
$response['message'] = StatusCodes::$statusTexts[StatusCodes::HTTP_BAD_REQUEST];
244274
$response['errorMessage'] = $message;
245-
246275
return $response;
247276
}
248277

@@ -257,7 +286,7 @@ protected function errorResponse($message = '')
257286
protected function errorMethodResponse()
258287
{
259288
return array(
260-
'code' => StatusCodes::HTTP_METHOD_NOT_ALLOWED,
289+
'code' => StatusCodes::HTTP_METHOD_NOT_ALLOWED,
261290
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_METHOD_NOT_ALLOWED]
262291
);
263292
}
@@ -275,8 +304,8 @@ protected function errorMethodResponse()
275304
protected function errorCredentialsResponse($message = '')
276305
{
277306
return array(
278-
'code' => StatusCodes::HTTP_FORBIDDEN,
279-
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_FORBIDDEN],
307+
'code' => StatusCodes::HTTP_FORBIDDEN,
308+
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_FORBIDDEN],
280309
'errorMessage' => $message
281310
);
282311
}
@@ -299,7 +328,6 @@ protected function errorSignatureResponse($validSignature = array())
299328
if ((defined('_PROCESS_TEST_') && _PROCESS_TEST_ === true) || in_array(getIPAddress(), config_item('whitelist_ip'), true)) {
300329
$response['validSignature'] = $validSignature;
301330
}
302-
303331
return $response;
304332
}
305333

@@ -412,9 +440,9 @@ protected function default_base_flush_logs()
412440
}
413441
$response = array(
414442
'status' => 'OK',
415-
'time' => date('Y-m-d H:i:s'),
416-
'data' => array(
417-
'logs' => $file->cleanLog($applicationPath . 'logs', 7),
443+
'time' => date('Y-m-d H:i:s'),
444+
'data' => array(
445+
'logs' => $file->cleanLog($applicationPath . 'logs', 7),
418446
'logs-data' => $file->cleanLog($applicationPath . 'logs-data', 7)
419447
)
420448
);
@@ -430,9 +458,9 @@ protected function default_base_flush_logs()
430458
}
431459
} else {
432460
$info = array(
433-
'method' => $this->input->method(true),
434-
'ip_address' => $this->input->ip_address(),
435-
'user_agent' => $this->input->user_agent(true),
461+
'method' => $this->input->method(true),
462+
'ip_address' => $this->input->ip_address(),
463+
'user_agent' => $this->input->user_agent(true),
436464
'request_headers' => $this->input->request_headers(true)
437465
);
438466
log_message('error', json_encode($info));

0 commit comments

Comments
 (0)