Skip to content

Commit

Permalink
feat: Add method to enable disable log generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Chance-fyi committed Aug 7, 2023
1 parent 7fb34e0 commit 8f5549f
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 100 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ class User extends BaseModel
\Chance\Log\facades\OperationLog::clearLog();
```

### 启用禁用

```php
# 启用 (默认)
\Chance\Log\facades\OperationLog::enable();
# 禁用
\Chance\Log\facades\OperationLog::disable();
```

### 效果图

![image](https://user-images.githubusercontent.com/37658940/215932487-9c923053-1bdb-4198-a13e-3ca7d668d65c.png)
Expand Down
50 changes: 50 additions & 0 deletions src/OperationLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class OperationLog
public const DELETED = 'deleted';
public const BATCH_DELETED = 'batch_deleted';
private const CONTEXT_LOG = 'context_operation_log';
private const CONTEXT_STATUS = 'context_operation_log_status';

protected array $tableComment;

Expand All @@ -41,6 +42,8 @@ class OperationLog

protected array $tableModelMapping = [];

protected bool $status = true;

public function __construct()
{
if (Facade::getResolvedInstance(self::class)) {
Expand Down Expand Up @@ -207,6 +210,53 @@ public function getTableModelMapping(): array
return $this->tableModelMapping;
}

public function status(): bool
{
if (extension_loaded('swoole') && class_exists(HyperfContext::class)) {
return HyperfContext::get(self::CONTEXT_STATUS, true);
}

if (class_exists(WebmanContext::class)) {
return WebmanContext::get(self::CONTEXT_STATUS) ?? true;
}

return $this->status;
}

public function enable(): void
{
if (extension_loaded('swoole') && class_exists(HyperfContext::class)) {
HyperfContext::set(self::CONTEXT_STATUS, true);

return;
}

if (class_exists(WebmanContext::class)) {
WebmanContext::set(self::CONTEXT_STATUS, true);

return;
}

$this->status = true;
}

public function disable(): void
{
if (extension_loaded('swoole') && class_exists(HyperfContext::class)) {
HyperfContext::set(self::CONTEXT_STATUS, false);

return;
}

if (class_exists(WebmanContext::class)) {
WebmanContext::set(self::CONTEXT_STATUS, false);

return;
}

$this->status = false;
}

private function getRawLog()
{
if (extension_loaded('swoole') && class_exists(HyperfContext::class)) {
Expand Down
1 change: 1 addition & 0 deletions src/facades/HyperfOrmLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @method static batchDeleted(Model $model, array $data)
* @method static beginTransaction()
* @method static rollBackTransaction(int $toLevel)
* @method static status()
*/
class HyperfOrmLog extends Facade
{
Expand Down
1 change: 1 addition & 0 deletions src/facades/IlluminateOrmLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @method static batchDeleted(Model $model, array $data)
* @method static beginTransaction()
* @method static rollBackTransaction(int $toLevel)
* @method static status()
*/
class IlluminateOrmLog extends Facade
{
Expand Down
2 changes: 2 additions & 0 deletions src/facades/OperationLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @method static clearLog()
* @method static setTableModelMapping(array $map)
* @method static getTableModelMapping()
* @method static enable()
* @method static disable()
*/
class OperationLog extends Facade
{
Expand Down
1 change: 1 addition & 0 deletions src/facades/ThinkOrmLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @method static batchDeleted(Model $model, array $data)
* @method static beginTransaction()
* @method static rollBackTransaction(int $toLevel)
* @method static status()
*/
class ThinkOrmLog extends Facade
{
Expand Down
62 changes: 34 additions & 28 deletions src/orm/hyperf/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ public function insertOrIgnore(array $values): int

public function update(array $values): int
{
$oldData = $this->get()->toArray();
if (!empty($oldData)) {
$model = $this->generateModel();
if (count($oldData) > 1) {
HyperfOrmLog::batchUpdated($model, $oldData, $values);
} else {
HyperfOrmLog::updated($model, (array) $oldData[0], $values);
if (HyperfOrmLog::status()) {
$oldData = $this->get()->toArray();
if (!empty($oldData)) {
$model = $this->generateModel();
if (count($oldData) > 1) {
HyperfOrmLog::batchUpdated($model, $oldData, $values);
} else {
HyperfOrmLog::updated($model, (array) $oldData[0], $values);
}
}
}

Expand Down Expand Up @@ -107,33 +109,37 @@ private function generateModel(): Model

private function insertLog(array $values): void
{
$model = $this->generateModel();
if (is_array(reset($values))) {
HyperfOrmLog::batchCreated($model, $values);
} else {
/** @var Connection $connection */
$connection = $this->getConnection();
$id = $connection->getPdo()->lastInsertId();
$pk = $model->getKeyName();
$values[$pk] = $id;
HyperfOrmLog::created($model, $values);
if (HyperfOrmLog::status()) {
$model = $this->generateModel();
if (is_array(reset($values))) {
HyperfOrmLog::batchCreated($model, $values);
} else {
/** @var Connection $connection */
$connection = $this->getConnection();
$id = $connection->getPdo()->lastInsertId();
$pk = $model->getKeyName();
$values[$pk] = $id;
HyperfOrmLog::created($model, $values);
}
}
}

private function deleteLog($id = null): void
{
if (!empty($id)) {
$data = [(array) $this->find($id)];
} else {
$data = $this->get()->toArray();
}

if (!empty($data)) {
$model = $this->generateModel();
if (count($data) > 1) {
HyperfOrmLog::batchDeleted($model, $data);
if (HyperfOrmLog::status()) {
if (!empty($id)) {
$data = [(array) $this->find($id)];
} else {
HyperfOrmLog::deleted($model, (array) $data[0]);
$data = $this->get()->toArray();
}

if (!empty($data)) {
$model = $this->generateModel();
if (count($data) > 1) {
HyperfOrmLog::batchDeleted($model, $data);
} else {
HyperfOrmLog::deleted($model, (array) $data[0]);
}
}
}
}
Expand Down
62 changes: 34 additions & 28 deletions src/orm/illuminate/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ public function insertOrIgnore(array $values): int

public function update(array $values): int
{
$oldData = $this->get()->toArray();
if (!empty($oldData)) {
$model = $this->generateModel();
if (count($oldData) > 1) {
IlluminateOrmLog::batchUpdated($model, $oldData, $values);
} else {
IlluminateOrmLog::updated($model, (array) $oldData[0], $values);
if (IlluminateOrmLog::status()) {
$oldData = $this->get()->toArray();
if (!empty($oldData)) {
$model = $this->generateModel();
if (count($oldData) > 1) {
IlluminateOrmLog::batchUpdated($model, $oldData, $values);
} else {
IlluminateOrmLog::updated($model, (array) $oldData[0], $values);
}
}
}

Expand Down Expand Up @@ -107,33 +109,37 @@ private function generateModel(): Model

private function insertLog(array $values): void
{
$model = $this->generateModel();
if (is_array(reset($values))) {
IlluminateOrmLog::batchCreated($model, $values);
} else {
/** @var Connection $connection */
$connection = $this->getConnection();
$id = $connection->getPdo()->lastInsertId();
$pk = $model->getKeyName();
$values[$pk] = $id;
IlluminateOrmLog::created($model, $values);
if (IlluminateOrmLog::status()) {
$model = $this->generateModel();
if (is_array(reset($values))) {
IlluminateOrmLog::batchCreated($model, $values);
} else {
/** @var Connection $connection */
$connection = $this->getConnection();
$id = $connection->getPdo()->lastInsertId();
$pk = $model->getKeyName();
$values[$pk] = $id;
IlluminateOrmLog::created($model, $values);
}
}
}

private function deleteLog($id = null): void
{
if (!empty($id)) {
$data = [(array) $this->find($id)];
} else {
$data = $this->get()->toArray();
}

if (!empty($data)) {
$model = $this->generateModel();
if (count($data) > 1) {
IlluminateOrmLog::batchDeleted($model, $data);
if (IlluminateOrmLog::status()) {
if (!empty($id)) {
$data = [(array) $this->find($id)];
} else {
IlluminateOrmLog::deleted($model, (array) $data[0]);
$data = $this->get()->toArray();
}

if (!empty($data)) {
$model = $this->generateModel();
if (count($data) > 1) {
IlluminateOrmLog::batchDeleted($model, $data);
} else {
IlluminateOrmLog::deleted($model, (array) $data[0]);
}
}
}
}
Expand Down
Loading

0 comments on commit 8f5549f

Please sign in to comment.