Last active
February 20, 2019 08:43
-
-
Save sachinkiranti/62be5a3722a865a3e419f48843f4d09b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Composer; | |
/** | |
* Class CleanUpCommand | |
* Article : https://medium.com/@sachinkiranti/writing-your-own-custom-laravel-artisan-command-571f823cf42 | |
* @package App\Console\Commands | |
*/ | |
class CleanUpCommand extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'kiranti:cleanup'; | |
/** | |
* The Composer instance. | |
* | |
* @var \Illuminate\Support\Composer | |
*/ | |
protected $composer; | |
/** | |
* Hit all the below artisan commands at once | |
* | |
* @var array | |
*/ | |
protected $commands = [ 'view:clear', 'cache:clear', 'config:clear', 'route:clear', 'clear-compiled' ]; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Clean up all views, cache and composer'; | |
/** | |
* Create a new command instance. | |
* | |
* @param Composer $composer | |
*/ | |
public function __construct(Composer $composer) | |
{ | |
parent::__construct(); | |
$this->composer = $composer; | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return void | |
*/ | |
public function handle() | |
{ | |
$this->composer->dumpAutoloads(); | |
$this->composer->dumpOptimized(); | |
$outputs = []; | |
foreach ($this->commands as $command) : | |
$this->call($command); | |
endforeach; | |
if (function_exists('exec')) : | |
exec('truncate -s 0 storage/logs/laravel*', $output, $result); | |
if (!$result) { | |
$outputs[] = 'Log cleared successfully!'; | |
} else { | |
$outputs[] = 'Log clear failed!'; | |
} | |
else : | |
$outputs[] = 'Log clear command cannot be init! exec is disabled!'; | |
endif; | |
$this->info(implode('', $outputs)); | |
$this->info('All done successfully ! Love kiranti :) '); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment