Skip to content

Commit

Permalink
Merge pull request #5552 from Laravel-Backpack/save-to-mobile-without…
Browse files Browse the repository at this point in the history
…-address-bar

Add header metas publish command
  • Loading branch information
pxpm authored Jul 15, 2024
2 parents ded2a09 + a5e3103 commit ec78467
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/BackpackServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ class BackpackServiceProvider extends ServiceProvider
use Stats;

protected $commands = [
\Backpack\CRUD\app\Console\Commands\Install::class,
\Backpack\CRUD\app\Console\Commands\AddMenuContent::class,
\Backpack\CRUD\app\Console\Commands\AddCustomRouteContent::class,
\Backpack\CRUD\app\Console\Commands\Version::class,
\Backpack\CRUD\app\Console\Commands\CreateUser::class,
\Backpack\CRUD\app\Console\Commands\PublishBackpackMiddleware::class,
\Backpack\CRUD\app\Console\Commands\PublishView::class,
\Backpack\CRUD\app\Console\Commands\Addons\RequireDevTools::class,
\Backpack\CRUD\app\Console\Commands\Addons\RequireEditableColumns::class,
\Backpack\CRUD\app\Console\Commands\Addons\RequirePro::class,
\Backpack\CRUD\app\Console\Commands\Themes\RequireThemeTabler::class,
\Backpack\CRUD\app\Console\Commands\Themes\RequireThemeCoreuiv2::class,
\Backpack\CRUD\app\Console\Commands\Themes\RequireThemeCoreuiv4::class,
\Backpack\CRUD\app\Console\Commands\Fix::class,
app\Console\Commands\Install::class,
app\Console\Commands\AddMenuContent::class,
app\Console\Commands\AddCustomRouteContent::class,
app\Console\Commands\Version::class,
app\Console\Commands\CreateUser::class,
app\Console\Commands\PublishBackpackMiddleware::class,
app\Console\Commands\PublishView::class,
app\Console\Commands\Addons\RequireDevTools::class,
app\Console\Commands\Addons\RequireEditableColumns::class,
app\Console\Commands\Addons\RequirePro::class,
app\Console\Commands\Themes\RequireThemeTabler::class,
app\Console\Commands\Themes\RequireThemeCoreuiv2::class,
app\Console\Commands\Themes\RequireThemeCoreuiv4::class,
app\Console\Commands\Fix::class,
app\Console\Commands\PublishHeaderMetas::class,
];

// Indicates if loading of the provider is deferred.
Expand Down
109 changes: 109 additions & 0 deletions src/app/Console/Commands/PublishHeaderMetas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Backpack\CRUD\app\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class PublishHeaderMetas extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'backpack:publish-header-metas';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Publishes the header metas and favicon assets.';

/**
* Execute the console command.
*/
public function handle()
{
$appName = $this->ask('What is the application name ?', config('app.name').' Backoffice');
$backpackPrefix = config('backpack.base.route_prefix');
$appColor = $this->ask('What is the application color ?', '#161c2d');
$pathPrefix = $this->ask('Where should icon files be published relative to public folder?');

$pathPrefix = Str::finish($pathPrefix ?? '', '/');

// laravel adds a dummy favicon with 0 bytes. we need to remove it otherwise our script would skip publishing the favicon on new Laravel installations.
// we will check the favicon file size, to make sure it's not a "valid" favicon. we will only delete the favicon if it has 0 bytes in size.
$this->checkIfFaviconIsLaravelDefault($pathPrefix);

$stubPath = __DIR__.'/../../../resources/stubs/';

$filesToPublish = [
public_path($pathPrefix.'site.webmanifest') => $stubPath.'manifest.stub',
resource_path('views/vendor/backpack/ui/inc/header_metas.blade.php') => $stubPath.'header_metas.stub',
public_path($pathPrefix.'browserconfig.xml') => $stubPath.'browserconfig.stub',
public_path($pathPrefix.'android-chrome-192x192.png') => __DIR__.'/../../../public/android-chrome-192x192.png',
public_path($pathPrefix.'android-chrome-192x192.png'),
public_path($pathPrefix.'android-chrome-512x512.png'),
public_path($pathPrefix.'apple-touch-icon.png'),
public_path($pathPrefix.'favicon-16x16.png'),
public_path($pathPrefix.'favicon-32x32.png'),
public_path($pathPrefix.'favicon.ico'),
public_path($pathPrefix.'safari-pinned-tab.svg'),
public_path($pathPrefix.'mstile-70x70.png'),
public_path($pathPrefix.'mstile-144x144.png'),
public_path($pathPrefix.'mstile-150x150.png'),
public_path($pathPrefix.'mstile-310x150.png'),
public_path($pathPrefix.'mstile-310x310.png'),
];

foreach ($filesToPublish as $destination => $stub) {
if (! is_string($destination)) {
$destination = $stub;
$stub = null;
}

if (File::exists($destination)) {
$this->comment("The file {$destination} already exists. Skipping.");

continue;
}

if (! File::isDirectory(dirname($destination))) {
File::makeDirectory(dirname($destination), 0755, true);
}

if (! $stub) {
File::copy(__DIR__.'/../../../public/'.basename($destination), $destination);
$this->info("File {$destination} published.");
continue;
}

$stub = File::get($stub);

$stub = str_replace('__APP_NAME__', $appName, $stub);
$stub = str_replace('__APP_COLOR__', $appColor, $stub);
$stub = str_replace('__BACKPACK_PREFIX__', $backpackPrefix, $stub);
$stub = str_replace('__PATH_PREFIX__', $pathPrefix, $stub);

File::put($destination, $stub);

$this->info("File {$destination} published.");
}

$this->comment('[DONE] Metas and favicon assets published successfully.');
}

private function checkIfFaviconIsLaravelDefault(string $path)
{
if (File::exists(public_path($path.'favicon.ico'))) {
// check the file size. if it's 0 it's the laravel dummy favicon, remove it.
if (filesize(public_path($path.'favicon.ico')) === 0) {
File::delete(public_path($path.'favicon.ico'));
$this->comment('[INFO] We deleted the Laravel dummy favicon. Publishing assets now.');
}
}
}
}
Binary file added src/public/android-chrome-192x192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/public/android-chrome-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/public/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/public/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/public/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/public/favicon.ico
Binary file not shown.
Binary file added src/public/mstile-144x144.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/public/mstile-150x150.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/public/mstile-310x150.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/public/mstile-310x310.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/public/mstile-70x70.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions src/public/safari-pinned-tab.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/resources/stubs/browserconfig.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<browserconfig>
<msapplication>
<tile>
<square150x150logo src="__PATH_PREFIX__mstile-150x150.png"/>
<TileColor>__APP_COLOR__</TileColor>
</tile>
</msapplication>
</browserconfig>
13 changes: 13 additions & 0 deletions src/resources/stubs/header_metas.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes" />
<link rel="apple-touch-icon" sizes="180x180" href="{{ asset('__PATH_PREFIX__apple-touch-icon.png') }}">
<link rel="icon" type="image/png" href="{{ asset('__PATH_PREFIX__favicon-32x32.png') }}" sizes="32x32">
<link rel="icon" type="image/png" href="{{ asset('__PATH_PREFIX__favicon-16x16.png') }}" sizes="16x16">
<link rel="manifest" href="{{ asset('__PATH_PREFIX__site.webmanifest') }}">
<link rel="mask-icon" href="{{ asset('__PATH_PREFIX__safari-pinned-tab.svg') }}" color="__APP_COLOR__">
<link rel="shortcut icon" href="{{ asset('__PATH_PREFIX__favicon.ico') }}">
<meta name="theme-color" content="__APP_COLOR__">
<meta name="apple-mobile-web-app-title" content="__APP_NAME__">
<meta name="application-name" content="__APP_NAME__">
<meta name="msapplication-TileColor" content="__APP_COLOR__">
<meta name="msapplication-config" content="{{asset('__PATH_PREFIX__browserconfig.xml')}}">
19 changes: 19 additions & 0 deletions src/resources/stubs/manifest.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "__APP_NAME__",
"icons": [
{
"src": "__PATH_PREFIX__android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "__PATH_PREFIX__android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"theme_color": "__APP_COLOR__",
"background_color": "__APP_COLOR__",
"start_url": "/__BACKPACK_PREFIX__",
"display": "standalone"
}

0 comments on commit ec78467

Please sign in to comment.