Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored install command #111

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "^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
160 changes: 147 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->call('statamic:install:eloquent-driver');
$this->setupStatamic();
$this->setupUser();
$this->setupEloquentDriver();

$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,142 @@ 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)) {
$authFile = Str::of(File::get($authConfig));
$authFile = $authFile->replace(
'App\Models\User::class',
'\Rapidez\Statamic\Models\User::class'
);

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

$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()
);
}

$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()
);
}

$this->call('statamic:install:eloquent-driver');
}
}
37 changes: 37 additions & 0 deletions src/Models/User.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels weird to have this file here with a namespace that doesn't match. Maybe create a stubs directory just like Laravel handles that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the namespace matches and we dont actually publish it to the project, we load it from this repository.
See src/Commands/InstallCommand.php:85

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, but it's likely that the end user want to edit the user model.

  1. Maybe keep it as a stub and publish it? But what if there is already a model?
  2. Keep it here and extend it within the project?
  3. If there is already a user model; that file still needs this changes.

So maybe option 2 so we can simply change the "extends ..." in case 3 so it will extend the model from this package instead of the framework? project -> package -> framework

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes 2 seems fine to me as well.
But during the install command do we just keep it in the package?
So when a user wants to extend it into their project, they can do it themselves.

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 User 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));
}
}