0

so I have this function that gets the remaining memory where my Laravel project is located. The thing is there are two controllers that need to check the remaining memory.

Here is what it looks like it is just inside my controller

private function convGB($bytes, $unit = "", $decimals = 2)
{
     $units = array('B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4, 
     'PB' => 5, 'EB' => 6, 'ZB' => 7, 'YB' => 8);

     $value = 0;
     if ($bytes > 0) 
     {
         if (!array_key_exists($unit, $units)) 
         {
             $pow = floor(log($bytes)/log(1024));
             $unit = array_search($pow, $units);
         }

         $value = ($bytes/pow(1024,floor($units[$unit])));
     }

     if (!is_numeric($decimals) || $decimals < 0) {
     $decimals = 2;
     }

     return sprintf('%.' . $decimals . 'f '.$unit, $value);
}

private function getMem()
{
    $ds  = disk_total_space(substr(base_path(), 0, 2));
    $fs  = disk_free_space(substr(base_path(), 0, 2));
    $ffs = disk_free_space(substr(base_path(), 0, 2));

    if ($ds >= 1073741824)
    {
        $ds = number_format($ds / 1073741824, 2) . ' GB';
    }
    elseif ($ds >= 1048576)
    {
        $ds = number_format($ds / 1048576, 2) . ' MB';
    }
    elseif ($ds >= 1024)
    {
        $ds = number_format($ds / 1024, 2) . ' KB';
    }
    elseif ($ds > 1)
    {
        $ds = $ds . ' B';
    }
    elseif ($ds == 1)
    {
        $ds = $ds . ' B';
    }
    else
    {
        $ds = '0 size';
    }

    if ($fs >= 1073741824)
    {
        $fs = number_format($fs / 1073741824, 2) . ' GB';
    }
    elseif ($fs >= 1048576)
    {
        $fs = number_format($fs / 1048576, 2) . ' MB';
    }
    elseif ($fs >= 1024)
    {
        $fs = number_format($fs / 1024, 2) . ' KB';
    }
    elseif ($fs > 1)
    {
        $fs = $fs . ' B';
    }
    elseif ($fs == 1)
    {
        $fs = $fs . ' B';
    }
    else
    {
        $fs = '0 size';
    }

    $converted = $this->convGB($ffs);

    return array( $ds , $fs , $converted);
}

So I wanted to put the function in an external PHP so that I would just call it when I needed it. Any ideas on how to do this? Thanks so much!

2
  • So which function do you want to move? I see two in your code example. Commented Jul 7, 2016 at 6:33
  • hi i will just use the getMem function Commented Jul 7, 2016 at 8:03

1 Answer 1

1

Create a new file in your app/Helpers directory name it AnythingHelper.php An example of my helper is :

<?php
function getDomesticCities()
{
$result = \App\Package::where('type', '=', 'domestic')
    ->groupBy('from_city')
    ->get(['from_city']);

return $result;
}

generate a service provider for your helper by following command

php artisan make:provider HelperServiceProvider

in the register function of your newly generated HelperServiceProvider.php add following code

require base_path().'/app/Helpers/AnythingHelper.php';

now in your config/app.php load this service provider and you are done

'App\Providers\HelperServiceProvider',

The code was taken from here: How do I make global helper functions in laravel 5?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.