DEV Community

Imam Ali Mustofa
Imam Ali Mustofa

Posted on

2 1

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 :("

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay