Skip to content

Commit

Permalink
feat(global): New release 2.0.1
Browse files Browse the repository at this point in the history
- Improve code style with Laravel Pint!
- Upgrade php version from 8.1 to 8.2 in GitHub Action for run tests!
- Upgrade phpunit configuration
  • Loading branch information
gnovaro committed Nov 30, 2023
1 parent 1be5276 commit 6f1a799
Show file tree
Hide file tree
Showing 30 changed files with 145 additions and 162 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:

runs-on: ubuntu-latest

steps:
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
extensions: mbstring, pdo, pdo_sqlite, xml, json, gd
- uses: actions/checkout@v3
- name: Copy .env
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ Homestead.yaml
npm-debug.log
yarn-error.log
.idea
etc/infrastructure/nginx/conf.d/
.phpunit.cache/
7 changes: 4 additions & 3 deletions app/Console/Commands/DatabaseCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function handle()
{
$status = 0;
$schema = $this->argument('name');
$charset = config("database.connections.mysql.charset",'utf8mb4');
$collation = config("database.connections.mysql.collation",'utf8mb4_unicode_ci');
$charset = config('database.connections.mysql.charset', 'utf8mb4');
$collation = config('database.connections.mysql.collation', 'utf8mb4_unicode_ci');

config(["database.connections.mysql.database" => null]);
config(['database.connections.mysql.database' => null]);
$query = "CREATE DATABASE IF NOT EXISTS $schema CHARACTER SET $charset COLLATE $collation;";
try {
$status = DB::statement($query);
Expand All @@ -53,6 +53,7 @@ public function handle()
$this->error("An error occurred creating the DB: $schema");
Log::error($t->getMessage());
}

return $status;
}
}
39 changes: 16 additions & 23 deletions app/Console/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,31 @@ public function __construct()

/**
* Execute the console command.
*
* @return int
*/
public function handle() :int
public function handle(): int
{
//1 - Check if .env file exist or created based in the example file
$envPath = base_path('.env');
if(File::exists($envPath))
{
if (File::exists($envPath)) {
$this->info('.env ENVIRONMENT file exist');
}

if(!File::exists($envPath))
{
if(File::copy(base_path('.env.example'), base_path('.env'))){
if (! File::exists($envPath)) {
if (File::copy(base_path('.env.example'), base_path('.env'))) {
$this->info('Copy default .env.example > .env');
}
}

//2- Generate APP_KEY
if(empty(env('APP_KEY')))
{
if (empty(env('APP_KEY'))) {
Artisan::call('key:generate');
$this->info('Application key generated');
}

//3 - Check if DB_PASSWORD is set
if(empty(env('DB_PASSWORD')))
{
if (empty(env('DB_PASSWORD'))) {
$yesno = $this->ask('DB_PASSWORD is in blank. Do you want to setup? (Y/N)');
if(strtoupper($yesno) == 'Y') {
if (strtoupper($yesno) == 'Y') {
$password = $this->ask('Setup DB_PASSWORD:');
$this->setEnv('DB_PASSWORD', $password);
}
Expand All @@ -78,33 +72,32 @@ public function handle() :int
}

/**
* @param string $key
* @param string $value
* @return void
*/
private function setEnv(string $key, string $value)
{
file_put_contents(app()->environmentFilePath(), str_replace(
$key . '=' . env($value),
$key . '=' . $value,
$key.'='.env($value),
$key.'='.$value,
file_get_contents(app()->environmentFilePath())
));
}

private function databaseExist() : bool
private function databaseExist(): bool
{
$exist = false;
try {
DB::connection()->getPdo();
if(DB::connection()->getDatabaseName()){
$this->info("Yes! Successfully connected to the DB: " . DB::connection()->getDatabaseName());
if (DB::connection()->getDatabaseName()) {
$this->info('Yes! Successfully connected to the DB: '.DB::connection()->getDatabaseName());
$exist = true;
}else{
$this->error("Could not find the database. Please check your configuration.");
} else {
$this->error('Could not find the database. Please check your configuration.');
}
} catch (\Throwable $t) {
$this->info("Could not open connection to database server. Please check your configuration.");
$this->info('Could not open connection to database server. Please check your configuration.');
}

return $exist;
}
}
5 changes: 3 additions & 2 deletions app/Console/Commands/UserCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;

class UserCreate extends Command
Expand Down Expand Up @@ -49,10 +49,11 @@ public function handle()
'name' => $name,
'email' => $email,
'password' => Hash::make($pwd),
'lang' => $lang
'lang' => $lang,
]);

$this->info('Account created for '.$name);

return 0;
}
}
15 changes: 6 additions & 9 deletions app/Console/Commands/WebsiteCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace App\Console\Commands;

use App\Models\Website;
use Illuminate\Console\Command;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use App\Models\Website;

class WebsiteCreate extends Command
{
Expand Down Expand Up @@ -51,7 +50,7 @@ public function handle()
$php_version = $this->argument('php');

$website = Website::where('url', $domain)->first();
if (!$website) {
if (! $website) {
$website = new Website();
$website->url = $domain;
$website->created_at = now();
Expand All @@ -66,13 +65,11 @@ public function handle()
}

$destination = "/etc/$server";
if (is_dir("/etc/$server/sites-available"))
{
if (is_dir("/etc/$server/sites-available")) {
$destination = "/etc/$server/sites-available/$domain.conf";
}

if (is_dir("/etc/$server/conf.d"))
{
if (is_dir("/etc/$server/conf.d")) {
$destination = "/etc/$server/conf.d/$domain.conf";
}

Expand All @@ -91,13 +88,13 @@ public function handle()
}

// Enable virtual host
if (is_dir("/etc/$server/sites-available"))
{
if (is_dir("/etc/$server/sites-available")) {
$virtual_host_path = "/etc/$server/sites-enabled/";
chdir($virtual_host_path);
$process = new Process(['ln', '-s', $destination]);
$process->run();
}

// TODO: check nginx -t
// TODO: service nginx reload
return $status;
Expand Down
1 change: 0 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Kernel extends ConsoleKernel
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
Expand Down
43 changes: 22 additions & 21 deletions app/Helpers/File.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace App\Helpers;

/**
* File Helper
*/
Expand All @@ -9,33 +10,33 @@ class File
/**
* Return human formated file size
*
* @param int $size bytes
*
* @param int $size bytes
* @return string formated size for humans
*/
public static function formatSize($size)
{
$suffix = '';
switch ($size) {
case ($size > 1099511627776):
$size /= 1099511627776;
$suffix = 'TB';
break;
case ($size > 1073741824):
$size /= 1073741824;
$suffix = 'GB';
break;
case ($size > 1048576):
$size /= 1048576;
$suffix = 'MB';
break;
case ($size > 1024):
$size /= 1024;
$suffix = 'KB';
break;
default:
$suffix = 'B';
case $size > 1099511627776:
$size /= 1099511627776;
$suffix = 'TB';
break;
case $size > 1073741824:
$size /= 1073741824;
$suffix = 'GB';
break;
case $size > 1048576:
$size /= 1048576;
$suffix = 'MB';
break;
case $size > 1024:
$size /= 1024;
$suffix = 'KB';
break;
default:
$suffix = 'B';
}
return round($size, 2) . " " . $suffix;

return round($size, 2).' '.$suffix;
}
}
34 changes: 19 additions & 15 deletions app/Helpers/Linux.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace App\Helpers;

use Symfony\Component\Process\Exception\ProcessFailedException;
Expand All @@ -9,7 +10,6 @@
*
* https://symfony.com/doc/current/components/process.html
* https://linuxize.com/post/how-to-create-users-in-linux-using-the-useradd-command/
*
*/
class Linux
{
Expand All @@ -18,38 +18,42 @@ class Linux
*
* @return string formated server uptime
*/
public static function getUptime() : string
public static function getUptime(): string
{
$str = @file_get_contents('/proc/uptime');
$num = floatval($str);
$secs = fmod($num, 60); $num = intdiv($num, 60);
$mins = $num % 60; $num = intdiv($num, 60);
$hours = $num % 24; $num = intdiv($num, 24);
$days = $num;
return sprintf("%03dd: %02dh: %02dm: %02ds", $days, $hours, $mins, $secs);
$str = @file_get_contents('/proc/uptime');
$num = floatval($str);
$secs = fmod($num, 60);
$num = intdiv($num, 60);
$mins = $num % 60;
$num = intdiv($num, 60);
$hours = $num % 24;
$num = intdiv($num, 24);
$days = $num;

return sprintf('%03dd: %02dh: %02dm: %02ds', $days, $hours, $mins, $secs);
}

/**
* Add Linux user to the system
*
* @param string $username linux username
*
* @return string
* @param string $username linux username
*/
public static function userAdd(string $username = '') : string
public static function userAdd(string $username = ''): string
{
if (!empty($username)) {
if (! empty($username)) {
// -m generate home directory
$process = new Process(['useradd', '-m', $username]);
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}

$output = $process->getOutput();

return $output;
}
return 'ERROR: username can\'t be empty';
}
}
4 changes: 1 addition & 3 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
Expand Down Expand Up @@ -44,7 +44,6 @@ public function __construct()
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
Expand All @@ -59,7 +58,6 @@ protected function validator(array $data)
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
Expand Down
Loading

0 comments on commit 6f1a799

Please sign in to comment.