Skip to content

Commit fd7ed2c

Browse files
committed
Add artisan commands
1 parent b749628 commit fd7ed2c

12 files changed

+464
-4
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,28 @@ Enable StageFront and choose a login and password:
7474
| `STAGEFRONT_PASSWORD` | `string` | `stagefront` |
7575
| `STAGEFRONT_ENCRYPTED` | `bool` | `false` |
7676

77-
By default StageFront is disabled and uses a plain text password when it's enabled.
77+
By default StageFront is disabled and uses a plain text password when it's enabled. If you set `STAGEFRONT_ENCRYPTED` to `true` the password should be a hashed value. You can generate this using Laravel's `\Hash::make('your password')` function.
7878

79-
If you set `STAGEFRONT_ENCRYPTED` to `true` the password should be a hashed value.
79+
##### Artisan Commands for Quick Setup
8080

81-
You can generate this using Laravel's `\Hash::make('your password')` function.
81+
You can also update the credentials in the `.env` file with our `artisan` command:
82+
83+
```bash
84+
php artisan stagefront:credentials <username> <password> --encrypt
85+
```
86+
87+
If you don't enter a username or password, the command will ask for your input step by step:
88+
89+
```bash
90+
php artisan stagefront:credentials
91+
```
92+
93+
Next, you can enable or disable StageFront:
94+
95+
```bash
96+
php artisan stagefront:enable
97+
php artisan stagefront:disable
98+
```
8299

83100
## 👥 Database Logins
84101

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
}
1616
],
1717
"require": {
18-
"php": ">=7.1"
18+
"php": ">=7.1",
19+
"codezero/dotenv-updater": "^1.1"
1920
},
2021
"require-dev": {
2122
"orchestra/testbench": ">=3.6",

src/Commands/DisableStageFront.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace CodeZero\StageFront\Commands;
4+
5+
use CodeZero\DotEnvUpdater\DotEnvUpdater;
6+
use Illuminate\Console\Command;
7+
8+
class DisableStageFront extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'stagefront:disable';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Update the .env file to disable StageFront.';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @param \CodeZero\DotEnvUpdater\DotEnvUpdater $updater
28+
*
29+
* @return void
30+
*/
31+
public function handle(DotEnvUpdater $updater)
32+
{
33+
$updater->set('STAGEFRONT_ENABLED', false);
34+
35+
$this->info("StageFront has been disabled.");
36+
}
37+
}

src/Commands/EnableStageFront.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace CodeZero\StageFront\Commands;
4+
5+
use CodeZero\DotEnvUpdater\DotEnvUpdater;
6+
use Illuminate\Console\Command;
7+
8+
class EnableStageFront extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'stagefront:enable';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Update the .env file to enable StageFront.';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @param \CodeZero\DotEnvUpdater\DotEnvUpdater $updater
28+
*
29+
* @return void
30+
*/
31+
public function handle(DotEnvUpdater $updater)
32+
{
33+
$updater->set('STAGEFRONT_ENABLED', true);
34+
35+
$this->info("StageFront has been enabled.");
36+
}
37+
}

src/Commands/SetCredentials.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace CodeZero\StageFront\Commands;
4+
5+
use CodeZero\DotEnvUpdater\DotEnvUpdater;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\Hash;
8+
9+
class SetCredentials extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'stagefront:credentials {username?} {password?} {--encrypt : Encrypt the password}';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Set a username and password to login to the staging site.';
24+
25+
/**
26+
* Execute the console command.
27+
*
28+
* @param \CodeZero\DotEnvUpdater\DotEnvUpdater $updater
29+
*
30+
* @return void
31+
*/
32+
public function handle(DotEnvUpdater $updater)
33+
{
34+
$username = $this->argument('username') ?: $this->askUsername();
35+
$password = $this->argument('password') ?: $this->askPassword();
36+
$encrypt = $this->option('encrypt') ?: $this->askForEncryption();
37+
38+
if ($encrypt) {
39+
$password = Hash::make($password);
40+
}
41+
42+
$updater->set('STAGEFRONT_LOGIN', $username);
43+
$updater->set('STAGEFRONT_PASSWORD', $password);
44+
$updater->set('STAGEFRONT_ENCRYPT', $encrypt);
45+
46+
$this->info("StageFront credentials were written to [.env].");
47+
}
48+
49+
/**
50+
* Ask for a username.
51+
*
52+
* @return string
53+
*/
54+
protected function askUsername()
55+
{
56+
$username = trim($this->ask('Choose a username:', 'stagefront'));
57+
58+
if ($username) {
59+
return $username;
60+
}
61+
62+
$this->error('Username can not be empty. Try again...');
63+
64+
return $this->askUsername();
65+
}
66+
67+
/**
68+
* Ask for a password and confirmation.
69+
*
70+
* @return string
71+
*/
72+
protected function askPassword()
73+
{
74+
$password = $this->secret('Choose a password:');
75+
76+
if (trim($password)) {
77+
return $this->askPasswordConfirmation($password);
78+
}
79+
80+
$this->error('Password can not be empty. Try again...');
81+
82+
return $this->askPassword();
83+
}
84+
85+
/**
86+
* Ask for a password confirmation.
87+
*
88+
* @param string $password
89+
*
90+
* @return string
91+
*/
92+
protected function askPasswordConfirmation($password)
93+
{
94+
$passwordConfirmation = $this->secret('Retype password:');
95+
96+
if ($password === $passwordConfirmation) {
97+
return $password;
98+
}
99+
100+
$this->error('Password did not match. Try again...');
101+
102+
return $this->askPassword();
103+
}
104+
105+
/**
106+
* Ask if the password should be encrypted.
107+
*
108+
* @return bool
109+
*/
110+
protected function askForEncryption()
111+
{
112+
if ($this->argument('password')) {
113+
return false;
114+
}
115+
116+
return $this->confirm('Encrypt password?');
117+
}
118+
}

src/StageFrontServiceProvider.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace CodeZero\StageFront;
44

5+
use CodeZero\StageFront\Commands\DisableStageFront;
6+
use CodeZero\StageFront\Commands\EnableStageFront;
7+
use CodeZero\StageFront\Commands\SetCredentials;
58
use CodeZero\StageFront\Composers\ThrottleTimeRemaining;
69
use Illuminate\Support\ServiceProvider;
710

@@ -26,6 +29,7 @@ public function boot()
2629
$this->loadViewComposers();
2730
$this->loadTranslations();
2831
$this->registerPublishableFiles();
32+
$this->registerArtisanCommands();
2933
}
3034

3135
/**
@@ -108,4 +112,20 @@ protected function mergeConfig()
108112
{
109113
$this->mergeConfigFrom(__DIR__."/../config/{$this->name}.php", $this->name);
110114
}
115+
116+
/**
117+
* Register artisan commands.
118+
*
119+
* @return void
120+
*/
121+
protected function registerArtisanCommands()
122+
{
123+
if ($this->app->runningInConsole()) {
124+
$this->commands([
125+
SetCredentials::class,
126+
EnableStageFront::class,
127+
DisableStageFront::class,
128+
]);
129+
}
130+
}
111131
}

tests/Commands/CommandTestCase.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace CodeZero\StageFront\Tests\Commands;
4+
5+
use CodeZero\DotEnvUpdater\DotEnvUpdater;
6+
use CodeZero\StageFront\Tests\TestCase;
7+
use Illuminate\Support\Facades\App;
8+
use Illuminate\Support\Facades\File;
9+
10+
class CommandTestCase extends TestCase
11+
{
12+
/**
13+
* DotEnvUpdater instance.
14+
*
15+
* @var \CodeZero\DotEnvUpdater\DotEnvUpdater
16+
*/
17+
protected $updater;
18+
19+
/**
20+
* Path to the test .env file.
21+
*
22+
* @var string
23+
*/
24+
protected $envFile;
25+
26+
/**
27+
* Setup the test environment.
28+
*
29+
* @return void
30+
*/
31+
protected function setUp(): void
32+
{
33+
parent::setUp();
34+
35+
$this->envFile = __DIR__ . '/../Stubs/.env';
36+
37+
File::put($this->envFile, '');
38+
39+
$this->updater = new DotEnvUpdater($this->envFile);
40+
41+
App::instance(DotEnvUpdater::class, $this->updater);
42+
}
43+
44+
/**
45+
* Clean up the testing environment before the next test.
46+
*
47+
* @return void
48+
*/
49+
protected function tearDown(): void
50+
{
51+
if (File::exists($this->envFile)) {
52+
File::delete($this->envFile);
53+
}
54+
55+
parent::tearDown();
56+
}
57+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace CodeZero\StageFront\Tests\Commands;
4+
5+
class DisableStageFrontTest extends CommandTestCase
6+
{
7+
/** @test */
8+
public function it_sets_unencrypted_credentials()
9+
{
10+
$this->artisan('stagefront:disable')
11+
->assertExitCode(0);
12+
13+
$this->assertFalse($this->updater->get('STAGEFRONT_ENABLED'));
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace CodeZero\StageFront\Tests\Commands;
4+
5+
class EnableStageFrontTest extends CommandTestCase
6+
{
7+
/** @test */
8+
public function it_sets_unencrypted_credentials()
9+
{
10+
$this->artisan('stagefront:enable')
11+
->assertExitCode(0);
12+
13+
$this->assertTrue($this->updater->get('STAGEFRONT_ENABLED'));
14+
}
15+
}

0 commit comments

Comments
 (0)