DEV Community

Imam Ali Mustofa
Imam Ali Mustofa

Posted on

Sending CodeIgniter Profiling To Gitlab Repository instead of Displaying Template.

Today I learned how to make profiler in CodeIgniter 3 (that's old by the way) much pretty and easy to monitoring when you have many projects in many clients. So you don't have to go to your cPanel or WHCMS or whatever. Just open your Gitlab Repository or Sub-group Project Repository.

Ok, let's do it!

Requirements:

  • Flysystem Library
  • CodeIgniter Project (Maybe, HaHa)
  • Gitlab Repository / Gitlab Group Repository (Private)
  • Gitlab Personal Access Token

Enabling Composer Autoload

open your config file inside folder application/config/config.php

$config['composer_autoload'] = 'vendor/autoload.php';
Enter fullscreen mode Exit fullscreen mode

Download Flysystem Gitlab Storage using composer

Make sure your terminal/cmd in the root project directory

$ composer require royvoetman/flysystem-gitlab-storage
Enter fullscreen mode Exit fullscreen mode

Create Flysystem Configuration

Now, create a config file named flysystem.php inside application/config/flysystem.php then copy and paste code below:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * @package Flysystem Library
 * @author Imam Ali Mustofa <iam@bettadevindonesia.com>
 * @version 1.0
 */
$config['personal_access_token']    = "your-access-token";
$config['project_id']               = "your-project-id";
$config['branch']                   = "branch-name";
$config['cloud_url']                = "https://gitlab.com"; // leave it to default or change if use other server.
Enter fullscreen mode Exit fullscreen mode
  1. Create Gitlab Project
  2. Generate Personal Access Token : How To
  3. Find Gitlab Project ID: Gitlab ID

Create Custom Library

The custom library used to create profiler and override default profiler from CodeIgniter. Create a library file and name whatever you want inside application/libraries/your-library.php, and make sure you have the same name for file and class name.

<?php
require_once FCPATH . 'system/libraries/Profiler.php';
class Prof extends CI_Profiler
{
    protected $ci;
    public function __construct()
    {
        parent::__construct();
        $this->ci =& get_instance();
    }

    public function display()
    {
        $output = "-------------------------------[ Start " .date('d, F Y H:i:s'). " tratS ]-------------------------------" . PHP_EOL;
        $output .= $this->_cleanText($this->run()) . PHP_EOL;
        $output .= "-------------------------------[ Stop " .date('d, F Y H:i:s'). " potS ]-------------------------------" . PHP_EOL;
        return $output;
    }

    public function _cleanText($html_text)
    {
        $text = strip_tags($html_text);
        $text = str_replace("&rsquo;","'", $text); 
        $content = preg_replace("/&#?[a-z0-9]{2,8};/i","",$text );
        return $content;
    }
}

Enter fullscreen mode Exit fullscreen mode

The Last Step!

In my cases, I have put the custom library inside Core Custom Controller it's because I need to record any log on my website. Here the example:

<?php

use League\Flysystem\Filesystem;
use RoyVoetman\FlysystemGitlab\Client;
use RoyVoetman\FlysystemGitlab\GitlabAdapter;
/**
 * @package Betta_Controller Load default configuration of application
 * @author Imam Ali Mustofa <iam@bettadevindonesia.com>
 * @version 1.0
 */
class Betta_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->config('flysystem', TRUE); // Don't forget to load flysystem configuration file
        $this->load->library(['prof']); // Load custom library
        /**
         * Your other load configurations
         */

        // Log System Monitoring if in production
        if ($_SERVER['SERVER_NAME'] != 'my-website.app') {
            $this->_recording_log($this->prof->display());
        }
    }

    public function _recording_log($log)
    {
        $client = new Client(
            $this->config->item('personal_access_token', 'flysystem'), 
            $this->config->item('project_id', 'flysystem'), 
            $this->config->item('branch', 'flysystem'), 
            $this->config->item('cloud_url', 'flysystem')
        );
        $adapter = new GitlabAdapter($client);
        $filesystem = new Filesystem($adapter);
        $filesystem->write('system_log-'.date('d-F-Y-H-i-s').'.log', $log);
    }
}

Enter fullscreen mode Exit fullscreen mode

That's it, I hope you enjoy my code and don't forget to share if you think "It's shareable!" HaHa... if don't "Ok, no problem :("

Top comments (0)