Skip to content

Commit

Permalink
Improved install command (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmeijer97 authored Jan 29, 2025
1 parent 1e56fd6 commit 4d571f4
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"justbetter/statamic-glide-directive": "^2.1",
"rapidez/blade-directives": "^0.6|^1.0",
"rapidez/core": "^2.13|^3.0",
"rapidez/sitemap": "^1.1",
"rapidez/sitemap": "^3.0",
"spatie/once": "*",
"statamic-rad-pack/runway": "^7.6",
"statamic/cms": "^5.29",
Expand Down
181 changes: 168 additions & 13 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Rapidez\Statamic\Commands;

use Exception;
use Statamic\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;

class InstallCommand extends Command
{
Expand All @@ -12,20 +16,12 @@ class InstallCommand extends Command

public function handle(): void
{
$this->call('vendor:publish', [
'--tag' => 'runway-config',
]);

$this->call('vendor:publish', [
'--provider' => 'Statamic\Eloquent\ServiceProvider',
'--tag' => 'statamic-eloquent-config',
]);

$this->confirm('Did you set the table_prefix in config/statamic/eloquent-driver.php to "statamic_"?', 1);
$this->setupStatamic();
$this->setupUser();
$this->setupEloquentDriver();

$this->call('statamic:install:eloquent-driver');

$this->info('If you would like to store users in the database please make sure to follow this guide: https://statamic.dev/tips/storing-users-in-a-database#from-a-fresh-statamic-project');
$this->info('Running migrations...');
$this->call('migrate');

if ($this->confirm('Would you like to configure a multisite? Note: Configuring a multisite requires a Statamic PRO license.')) {
$this->call('statamic:multisite');
Expand All @@ -49,4 +45,163 @@ public function handle(): void

$this->info('Done 🚀');
}

protected function setupStatamic(): void
{
$this->info('Starting Statamic Setup...');
$this->call('statamic:install');

$composerFile = base_path('composer.json');

if (file_exists($composerFile)) {
$composerFileContents = File::get($composerFile);
$composer = json_decode($composerFileContents, true);

if (!isset($composer['scripts'])) {
$composer['scripts'] = [];
}

if (!isset($composer['scripts']['post-autoload-dump'])) {
$composer['scripts']['post-autoload-dump'] = [];
}

if (!in_array("@php artisan statamic:install --ansi", $composer['scripts']['post-autoload-dump'])) {
$composer['scripts']['post-autoload-dump'][] = "@php artisan statamic:install --ansi";
}

File::put($composerFile, json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
}

protected function setupUser(): void
{
$this->info('Starting User Setup...');
$authConfig = config_path('auth.php');

if (!class_exists('\App\Models\User') && file_exists($authConfig)) {
$this->call('vendor:publish', [
'--provider' => 'Rapidez\Statamic\RapidezStatamicServiceProvider',
'--tag' => 'rapidez-user-model',
]);
}

$migrationPath = database_path('migrations/0001_01_01_000000_create_users_table.php');

if (!file_exists($migrationPath)) {
try {
$userTable = file_get_contents('https://raw.githubusercontent.com/laravel/laravel/refs/heads/11.x/database/migrations/0001_01_01_000000_create_users_table.php');

if (!$userTable) {
throw new Exception('Failed to download the migration file.');
}

file_put_contents($migrationPath, $userTable);
} catch (Exception $e) {
Log::error('Error downloading migration file: ' . $e->getMessage());
}
}

if(!config('auth.passwords.activations') && file_exists($authConfig)) {
$authFile = Str::of(File::get($authConfig));
$authFile = $authFile->replace(
"'passwords' => [\n",
"'passwords' => [
'activations' => [
'provider' => 'users',
'table' => 'password_activation_tokens',
'expire' => 4320,
'throttle' => 60,
],\n"
);

File::put(
$authConfig,
$authFile->__toString()
);
}

$usersConfig = config_path('statamic/users.php');

if ((config('statamic.users.passwords.resets') !== 'users' || config('statamic.users.passwords.activations') !== 'activations') && file_exists($usersConfig)) {
$usersFile = Str::of(File::get($usersConfig));

if(config('statamic.users.passwords.resets') !== 'users') {
$usersFile = $usersFile->replace(
"'resets' => config('auth.defaults.passwords'),",
"'resets' => 'users',"
);
}

if(config('statamic.users.passwords.activations') !== 'activations') {
$usersFile = $usersFile->replace(
"'activations' => config('auth.defaults.passwords'),",
"'activations' => 'activations',"
);
}

File::put(
$usersConfig,
$usersFile->__toString()
);
}

$files = glob(database_path('migrations/*_statamic_auth_tables.php'));

if (!$files) {
$this->call('statamic:auth:migration');
}
}

protected function setupEloquentDriver(): void
{
$this->info('Starting Eloquent Driver Setup...');

$this->call('vendor:publish', [
'--tag' => 'runway-config',
]);

$this->call('vendor:publish', [
'--provider' => 'Statamic\Eloquent\ServiceProvider',
'--tag' => 'statamic-eloquent-config',
]);

$eloquentConfig = config_path('statamic/eloquent-driver.php');

if (file_exists($eloquentConfig)) {
File::put(
$eloquentConfig,
Str::of(File::get($eloquentConfig))
->replace(
"'table_prefix' => env('STATAMIC_ELOQUENT_PREFIX', ''),",
"'table_prefix' => env('STATAMIC_ELOQUENT_PREFIX', 'statamic_'),"
)
->__toString()
);
}

if (!$this->isEloquentDriverConfigured()) {
$this->call('statamic:install:eloquent-driver', [
'--repositories' => 'assets,collection_trees,entries,forms,form_submissions,global_variables,nav_trees,terms,tokens'
]);
}
}

protected function isEloquentDriverConfigured(): bool
{
$eloquentRepositories = collect([
'assets',
'collection_trees',
'entries',
'forms',
'form_submissions',
'global_set_variables',
'navigation_trees',
'terms',
'tokens',
]);

$remainingRepositories = $eloquentRepositories->filter(fn($repository) => config("statamic.eloquent-driver.{$repository}.driver") !== 'eloquent');

return $remainingRepositories->count() === 0;
}
}
37 changes: 37 additions & 0 deletions src/Models/BaseUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rapidez\Statamic\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Statamic\Notifications\PasswordReset;

class BaseUser extends Authenticatable
{
use HasFactory, Notifiable;

protected $fillable = [
'name',
'email',
'password',
];

protected $hidden = [
'password',
'remember_token',
];

protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}

public function sendPasswordResetNotification($token): void
{
$this->notify(new PasswordReset($token));
}
}
10 changes: 10 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Models;

use Rapidez\Statamic\Models\BaseUser;

class User extends BaseUser
{

}
4 changes: 4 additions & 0 deletions src/RapidezStatamicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ public function bootPublishables() : self
__DIR__ . '/../config/rapidez/statamic/builder.php' => config_path('rapidez/statamic/builder.php'),
], 'config');

$this->publishes([
__DIR__.'/../src/Models/User.php' => app_path('Models/User.php'),
], 'rapidez-user-model');

return $this;
}

Expand Down

0 comments on commit 4d571f4

Please sign in to comment.