Sending CodeIgniter Profiling To Gitlab Repository instead of Displaying Template.
Tags: todayilearned, php, codeigniter
Published: 2020-04-17 15:03:49

---
title: Sending CodeIgniter Profiling To Gitlab Repository instead of Displaying Template.
published: true
description: #todayilearned 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.
tags: todayilearned, php, codeigniter
---

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`

```php
$config['composer_autoload'] = 'vendor/autoload.php';
```

### Download `Flysystem Gitlab Storage` using composer
Make sure your terminal/cmd in the root project directory

```bash
$ composer require royvoetman/flysystem-gitlab-storage
```

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

```php
<?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.
```
1. Create Gitlab Project
2. Generate Personal Access Token : [How To](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html)
3. Find Gitlab Project ID:
![Gitlab ID](https://i.stack.imgur.com/u0K4w.png)

### 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
<?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;
    }
}

```

### 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
<?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);
    }
}

```

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