Skip to content

Commit

Permalink
Refactoring Array
Browse files Browse the repository at this point in the history
  • Loading branch information
hungnguyenhp committed Dec 26, 2022
1 parent 02c0a11 commit 281fe38
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 171 deletions.
71 changes: 64 additions & 7 deletions hungng/HungNG_CI_Base_Controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,23 @@ public function __construct()
*/
protected function defaultJsonResponseInfo()
{
$response = [
$response = array(
'code' => StatusCodes::HTTP_OK,
'message' => StatusCodes::$statusTexts[StatusCodes::HTTP_OK],
'info' => [
'info' => array(
'name' => 'Nguyen An Hung',
'email' => '[email protected]',
'web' => 'https://nguyenanhung.com',
'blog' => 'https://blog.nguyenanhung.com',
'facebook' => 'https://facebook.com/nguyenanhung',
'github' => 'https://github.com/nguyenanhung',
],
'request_data' => [
'github' => 'https://github.com/nguyenanhung'
),
'request_data' => array(
'ip' => getIPAddress(),
'user_agent' => $this->input->user_agent(true),
'request_method' => $this->input->method(true)
]
];
)
);
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response, JSON_PRETTY_PRINT))->_display();
exit;
}
Expand Down Expand Up @@ -372,5 +372,62 @@ protected function log($name = '', $message = '', $context = array(), $inputLeve
log_message('error', $exception->getTraceAsString());
}
}

/**
* Function opcache_flush_reset
*
* @author : 713uk13m <[email protected]>
* @copyright: 713uk13m <[email protected]>
* @time : 26/12/2022 01:36
*/
protected function opcache_flush_reset()
{
if (function_exists('opcache_reset') && is_cli()) {
opcache_reset();
} else {
show_404();
}
}

/**
* Function default_base_flush_logs
*
* @author : 713uk13m <[email protected]>
* @copyright: 713uk13m <[email protected]>
* @time : 26/12/2022 04:29
*/
protected function default_base_flush_logs()
{
if (is_cli()) {
try {
$file = new \nguyenanhung\MyDebug\Manager\File();
$file->setInclude(array('*.log', '*.txt', 'log-*.php'));
$response = array(
'status' => 'OK',
'time' => date('Y-m-d H:i:s'),
'data' => array(
'logs' => $file->cleanLog(dirname(__DIR__) . '/logs', 7),
'logs-data' => $file->cleanLog(dirname(__DIR__) . '/logs-data', 7)
)
);
log_message('debug', 'Clean Log Result: ' . json_encode($response));
$output = defined('JSON_PRETTY_PRINT') ? json_encode($response, JSON_PRETTY_PRINT) : json_encode($response);
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output($output . PHP_EOL)->_display();
exit;
} catch (Exception $e) {
$message = 'Code: ' . $e->getCode() . ' - File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Message: ' . $e->getMessage();
log_message('error', $message);
}
} else {
$info = array(
'method' => $this->input->method(true),
'ip_address' => $this->input->ip_address(),
'user_agent' => $this->input->user_agent(true),
'request_headers' => $this->input->request_headers(true)
);
log_message('error', json_encode($info));
show_404();
}
}
}
}
55 changes: 35 additions & 20 deletions hungng/HungNG_CI_Base_Lib_ElasticSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ public function exists_index($index)
{
$status = 404;
if ($index) {
$params = ['index' => $index];
$params = array('index' => $index);
$response = $this->client->indices()->exists($params);
$status = $response->getStatusCode();
$status = $response->getStatusCode();
}

return $status;
}

public function create_index($index, $body)
{
$params = ['index' => $index, 'body' => $body];
$params = array('index' => $index, 'body' => $body);
$response = $this->client->indices()->create($params);
}

public function refresh_index($index)
{
$params = ['index' => $index];
$params = array('index' => $index);
$response = $this->client->indices()->refresh($params);
}

Expand All @@ -70,7 +70,7 @@ public function delete_index($index)
if (!$index) {
return false;
}
$params = ['index' => $index];
$params = array('index' => $index);
try {
$response = $this->client->indices()->delete($params);
} catch (Exception $e) {
Expand All @@ -82,68 +82,82 @@ public function delete_index($index)
public function get_found_document($index, $type, $id)
{
//db - collection - id
$params = ['index' => $index, 'type' => $type, 'id' => $id];
$params = array(
'index' => $index,
'type' => $type,
'id' => $id
);
$results = $this->client->get($params);

return $results['found'];
}

public function exists_document($index, $type, $id)
{
$params = ['index' => $index, 'type' => $type, 'id' => $id];
$params = array(
'index' => $index,
'type' => $type,
'id' => $id
);

return $this->client->exists($params);
}

public function create_document($index, $type, $id, $body)
{
$params = [
$params = array(
'index' => $index,
'type' => $type,
'id' => $id,
'body' => $body];
'body' => $body
);

return $this->client->index($params);
}

public function update_document($index, $type, $id, $body)
{
$params = [
$params = array(
'index' => $index,
'type' => $type,
'id' => $id,
'body' => $body];
'body' => $body
);

return $this->client->update($params);
}

public function delete_document($index, $type, $id)
{
$params = [
$params = array(
'index' => $index,
'type' => $type,
'refresh' => 'wait_for', //or true
'id' => $id];
'id' => $id
);

return $this->client->delete($params);
}

public function search_document($index, $type, $from, $size, $query, $all)
{
$params = [
$params = array(
'index' => $index,
'type' => $type,
'body' => [
'body' => array(
'from' => $from,
'size' => $size,
'query' => $query]];
'query' => $query
)
);
if ($all) {
$params = [
$params = array(
'index' => $index,
'type' => $type,
'from' => $from,
'size' => $size,
'body' => ['query' => $query]];
'body' => array('query' => $query)
);
}

return $this->client->search($params);
Expand All @@ -153,10 +167,11 @@ public function count_documents($index, $type, $query)
{
$exists_index = $this->exists_index($index);
if ($exists_index === 200) {
$params = [
$params = array(
'index' => $index,
'type' => $type,
'body' => ['query' => $query]];
'body' => array('query' => $query)
);
$response = $this->client->count($params);
} else {
$response['count'] = 0;
Expand Down
Loading

0 comments on commit 281fe38

Please sign in to comment.