Instantly share code, notes, and snippets.
Last active
January 11, 2023 16:56
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save AnandPilania/9a35ce1ed513354c9434e264b98291a3 to your computer and use it in GitHub Desktop.
Laravel Artisan Command for Shared-Hosting Compatibility (support HTTPS option)
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\Filesystem\Filesystem; | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Support\Str; | |
class SharedHostingCompatibilityCommand extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'shared-hosting {--https : HTTPS support in Shared-Hosting.}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Add Shared-Hosting Compatiblity.'; | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
if(file_exists($publicDir = base_path('public'))) { | |
(new Filesystem)->moveDirectory($publicDir, base_path('public_html')); | |
$this->info('public to public_html'); | |
} | |
if(file_exists($serverFile = base_path('server.php'))) { | |
$serverContent = file_get_contents($serverFile); | |
$serverUpdated = false; | |
if(Str::contains($serverContent, "'/public'")) { | |
$serverUpdated = true; | |
$this->replaceInFile("'/public'", "'/public_html'", $serverFile, $serverContent); | |
} | |
if(Str::contains($serverContent, "'/public/")) { | |
$serverUpdated = true; | |
$this->replaceInFile("'/public/", "'/public_html/", $serverFile, $serverContent); | |
} | |
if($serverUpdated) { | |
$this->info('server.php updated'); | |
} | |
} | |
$bootstrapFile = base_path('bootstrap/app.php'); | |
if(file_exists($bootstrapFile) | |
&& !Str::contains( | |
$bootstrapContent = file_get_contents($bootstrapFile), | |
'public_html')) { | |
$this->replaceInFile( | |
"_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)\n);", | |
"_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)\n);\n\n/*\n|--------------------------------------------------------------------------\n| Changing PUBLIC path to PUBLIC_HTML\n|--------------------------------------------------------------------------\n*/\n\$app->bind('path.public', function() {\n\treturn base_path() . '/public_html';\n});", | |
$bootstrapFile, | |
$bootstrapContent | |
); | |
$this->info('public.path added to bootstrap/app.php'); | |
} | |
$webpackFile = base_path('webpack.mix.js'); | |
if(file_exists($webpackFile) | |
&& !Str::contains( | |
$webpackContent = file_get_contents($webpackFile), | |
"mix.setPublicPath('public_html')")) { | |
$this->replaceInFile( | |
"const mix = require('laravel-mix');", | |
"const mix = require('laravel-mix');\n\nmix.setPublicPath('public_html');", | |
$webpackFile, | |
$webpackContent); | |
$this->info('webpack.mix.js updated'); | |
if(Str::contains($webpackContent, 'public/')) { | |
$this->comment('Please manually remove \'public/\' from mix.js/css/postCss/sass...'); | |
} | |
} | |
$indexFile = base_path('index.php'); | |
if(!file_exists($indexFile)) { | |
file_put_contents($indexFile, "<?php\n\nrequire __DIR__.'/server.php';\n"); | |
$this->info('index.php added'); | |
} | |
if($this->option('https') | |
&& !Str::contains( | |
$appProviderContent = file_get_contents( | |
$appProviderFile = app_path('Providers/AppServiceProvider.php') | |
), "URL::forceScheme('https')")) { | |
$this->replaceInFile( | |
[ | |
"public function boot(){", | |
"public function boot() {", | |
"public function boot()\n {", | |
"public function boot()\n\t{" | |
], | |
"public function boot()\n {\n\t\t\Illuminate\Support\Facades\URL::forceScheme('https');", | |
$appProviderFile, | |
$appProviderContent); | |
$this->info('HTTPS option enabled.'); | |
} | |
$this->line(''); | |
$this->info('Shared-Hosting comptaiblity succeed!'); | |
} | |
protected function replaceInFile($search, $replace, $path, $content = null) | |
{ | |
file_put_contents($path, str_replace($search, $replace, $content ?? file_get_contents($path))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment