Skip to content

Instantly share code, notes, and snippets.

@Z3d0X
Created April 8, 2022 19:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Z3d0X/3981de122991b0a616aa69277335569c to your computer and use it in GitHub Desktop.
Save Z3d0X/3981de122991b0a616aa69277335569c to your computer and use it in GitHub Desktop.
Quickly install and configure filament in a new laravel project

Quickly install and configure filament in a new laravel project.

image

Features

  • Install and configure filament
  • Install and configure some of first party plugins of filament
  • Install tailwind and configure custom theme setup for filament

Warning

This is only intended to be used in freshly created new laravel projects

Usage

  • copy the FilamentQuickStart.php to app/Console/Commands folder
  • run php artisan filament:quickstart
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
class FilamentQuickStart extends Command
{
protected $signature = 'filament:quickstart';
protected $description = 'Quickly add filament to a new project';
protected Filesystem $files;
public function handle(Filesystem $files)
{
$this->files = $files;
$custom_theme = $this->confirm('Would you like to install tailwind to customize your theme?', true);
$plugins = $this->choice('Which plugin(s) would you like to install?',
[
'none',
'spatie-laravel-tags-plugin',
'spatie-laravel-settings-plugin',
'spatie-laravel-media-library-plugin',
], 0, null, true);
$this->print('Installing dependencies...');
exec('composer require '.$this->getComposerPackages($plugins));
$this->configurePlugins($plugins);
if ($custom_theme) {
$this->configureCustomTheme();
}
if($this->confirm('Would like to run migrations?')) {
$this->call('migrate');
}
return 0;
}
protected function publish($provider = null, $tag = null)
{
$this->call('vendor:publish', [
'--provider' => $provider,
'--tag' => $tag
]);
}
protected function configurePlugins(array $plugins): void
{
$this->print('Configuring plugins...');
$this->publish(null, 'filament-config');
if (in_array('none', $plugins)) {
return;
}
if (in_array('spatie-laravel-tags-plugin', $plugins)) {
$this->publish('Spatie\Tags\TagsServiceProvider', 'tags-config');
$this->publish('Spatie\Tags\TagsServiceProvider', 'tags-migrations');
}
if (in_array('spatie-laravel-settings-plugin', $plugins)) {
$this->publish('Spatie\LaravelSettings\LaravelSettingsServiceProvider', 'settings');
$this->publish('Spatie\LaravelSettings\LaravelSettingsServiceProvider', 'migrations');
}
if (in_array('spatie-laravel-media-library-plugin', $plugins)) {
$this->publish('Spatie\MediaLibrary\MediaLibraryServiceProvider', 'config');
$this->publish('Spatie\MediaLibrary\MediaLibraryServiceProvider', 'migrations');
}
}
protected function getComposerPackages(array $plugins): string
{
$composer_packages = join(' ', [
'livewire/livewire',
'filament/filament',
]);
if (in_array('none', $plugins)) {
return $composer_packages;
}
foreach ($plugins as $plugin) {
$composer_packages .= ' filament/'.$plugin;
$composer_packages .= ' spatie/laravel-'.str($plugin)->after('spatie-laravel-')->before('-plugin')->replace('-', '');
}
return $composer_packages;
}
protected function configureCustomTheme()
{
$this->print('Configuring custom theme...');
exec('npm install tailwindcss @tailwindcss/forms @tailwindcss/typography --save-dev');
$this->createTailwindConfigFile();
$this->createCssFile();
$this->configureWebpackMix();
$this->registerCustomTheme();
exec('npm run dev');
}
protected function registerCustomTheme()
{
$this->files->replace(
app_path('Providers/AppServiceProvider.php'),
<<<PHP
<?php
namespace App\Providers;
use Filament\Facades\Filament;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Filament::serving(function () {
Filament::registerTheme(mix('css/admin.css'));
});
}
}
PHP
);
}
protected function createTailwindConfigFile(): void
{
$this->files->put(
base_path('tailwind.admin.config.js'),
<<<JS
const colors = require('tailwindcss/colors')
module.exports = {
content: [
'./resources/**/*.blade.php',
'./vendor/filament/**/*.blade.php',
],
darkMode: 'class',
theme: {
extend: {
colors: {
danger: colors.rose,
primary: colors.blue,
success: colors.green,
warning: colors.yellow,
},
},
},
plugins: [
require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
],
}
JS
);
}
protected function createCssFile(): void
{
$this->files->put(
resource_path('css/admin.css'),
<<<CSS
@import '../../vendor/filament/forms/dist/module.esm.css';
@tailwind base;
@tailwind components;
@tailwind utilities;
CSS
);
}
protected function configureWebpackMix(): void
{
$this->files->append(
base_path('webpack.mix.js'),
<<<JS
mix.postCss('resources/css/admin.css', 'public/css', [
require('tailwindcss')('tailwind.admin.config.js'),
])
JS
);
}
protected function print(string $message): void
{
$this->line("<bg=white;fg=black>".$message."</>");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment