Welcome Guest, Not a member yet? Register   Sign In
Proposal for Garbage Collector Feature in CodeIgniter 4
#1
Lightbulb 

Hello,



I wanted to share a new feature that I've developed for CodeIgniter 4, and I believe it could be a valuable addition to the framework. I've implemented a Garbage Collector for Models, providing an automated way to clean up unnecessary data. If this is the wrong section for this, please let me know. (I'm new to that forum, but I'm working with Codeigniter 3+4 since 2017)



Features:
  • Flexible GC modes for different use cases.
  • Customizable settings such as the deleted field name and expiration periods.
  • Automatic cleanup execution with the garbageCleaner method automatically for every model. (and with that for every db-table)
  • Small and efficient code
  • Of course not perfect
Usage:
PHP Code:
<?php 

// Example usage in a model
class YourModel extends BaseModel {
    // ... your model attributes and settings ...

    /* ----------------------------------*
    * GARBAGE COLLECTOR VARIABLES
    * --------------------------------- */
    /**
    * * GC-MODES:
    * 
    * * Mode 1: default "deleted_at" Column from CI4-Models
    * * Mode 2: Custom Delted_at-Column ($gc_deletedField)
    * * Mode 3: Custom Deleted_at-Column, ($gc_deletedField) is timestamp (no Y-m-d H:i:s)
    */

    protected $gc_name                          "myCollectorName";
    protected $gc_enabled                      true;
    protected $gc_mode                          1;
    protected $gc_deletedField                  'model';
    protected $gc_setDeletedOnExpired          true;
    protected $gc_deleteExpiredAfterXDays      14;
    protected $gc_deleteAfterXDays              60;

    // ... other settings ...

    // ... your model code ...
}

class 
BaseModel {
    // ... your model attributes and settings ...

    // ... your base model code ...

    /* ----------------------------------*
    * GARBAGE COLLECTOR FUNCTIONS
    * --------------------------------- */
    public function garbageCleaner(){
        if($this->gc_enabled){
            if($this->gc_setDeletedOnExpired){
                $this->prepareForCleanUp();
            }
            
            
return $this->executeCleanUp($this->gc_mode);
        }

        return true;
    }

    public function executeCleanUp($gc_mode){
        if($gc_mode === 1){
            $rowsToDelete $this->getDeletedEntries($this->gc_deleteAfterXDays);
        }else if($gc_mode === 2){
            $rowsToDelete $this->getDeletedEntries($this->gc_deleteAfterXDays$this->gc_deletedField);
        }else if($gc_mode === 3){
            $rowsToDelete $this->getDeletedEntries($this->gc_deleteAfterXDays$this->gc_deletedFieldtrue);
        }else{
            return false;
        }

        $count        count($rowsToDelete);

        if($count 0){
            $idsToDelete array_column($rowsToDelete$this->primaryKey);
            if($this->delete($idsToDeletetrue)){
                /* TODO: Insert your Log-Routine */
                writeLog("$this->gc_name.$this->table""deleted data | $count rows deleted");
            }
        }

        return true;
    }

    public function prepareForCleanUp() {
        $today          date($this->defaultTimeFormat);
        $expiredRows    $this->getExpiredEntries($this->gc_deleteExpiredAfterXDays);
        $count          count($expiredRows);

        if($count 0){
            foreach($expiredRows as $key => $value){
                $expiredRows[$key]->{$this->deletedField} = $today;
            }

            $this->updateBatch(
                $expiredRows,
                $this->primaryKey
            
);

            /* TODO: Insert your Log-Routine */
            writeLog("$this->gc_name.$this->table""prepared expired data for delete | $count rows prepared");
        }

        return true;
    }

    protected function getDeletedEntries(int $olderThanXDays 0string $deletedField 'model'bool $useTimestamp false) {
        if ($deletedField == 'model') {
            $deletedField $this->deletedField;
        }
    
        
if ($olderThanXDays 0) {
            if ($useTimestamp) {
                // Working with timestamps
                $this->where("$deletedField + ($olderThanXDays * 24 * 3600) <= UNIX_TIMESTAMP(NOW())"nullfalse);
            } else {
                // Working with datetime
                $this->where("DATE_ADD($deletedField, INTERVAL $olderThanXDays DAY) <= NOW()");
            }
        }
    
        
return $this->where($deletedField ' IS NOT NULL')
                    ->withDeleted()
                    ->findAll();
    }

    protected function getExpiredEntries(int $olderThanXDays 0string $expiredField 'model'){
        if($expiredField == 'model'){
            $expiredField $this->expiredField;
        }

        if($olderThanXDays 0){
            $expirationDate date($this->defaultTimeFormatstrtotime("+" $olderThanXDays " days"));
            $this->where($expiredField ' <'$expirationDate);
        }

        return $this->where($expiredField ' IS NOT NULL')->findAll();
    }



I'm reaching out to gather feedback and discuss the possibility of integrating this feature into the main CodeIgniter 4 repository. Your thoughts, suggestions, and critiques are highly appreciated.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB