Skip to content

Commit

Permalink
Add options to the wizard
Browse files Browse the repository at this point in the history
You can use "cache", "driver", "connection", "table" to override configuration.
  • Loading branch information
ycs77 committed Jun 14, 2019
1 parent 400e7bb commit 570f8dc
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 31 deletions.
19 changes: 18 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This command generate the `UserSetupWizardController`, `NameStep`, `EmailStep` c
```php
...

Wizard::routes('wizard/user', 'UserWizardController', 'wizard.user');
Wizard::routes('wizard/user', 'UserSetupWizardController', 'wizard.user');
```

> If you can't use auto append route, you can set `config/wizard.php` attribute `append_route` to `false`.
Expand All @@ -56,6 +56,7 @@ Wizard::routes('wizard/user', 'UserWizardController', 'wizard.user');

This is generated NameStep class, you can to `rules` method set form validation, and save `$data` to your database via the `saveData` method:

*app/Steps/User/NameStep.php*
```php
<?php

Expand Down Expand Up @@ -168,6 +169,22 @@ Or use yarn:
yarn add bootstrap-steps
```

### Override wizard configuration on wizard controller

Add `wizardOptions` property to `controller`, you can use `cache`, `driver`, `connection`, `table` options to override configuration.

*app/Http/Controllers/UserSetupWizardController.php*
```php
/**
* The wizard options.
*
* @var array
*/
protected $wizardOptions = [
'cache' => false,
];
```

## Commands

**Make controller**:
Expand Down
18 changes: 14 additions & 4 deletions src/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
namespace Ycs77\LaravelWizard;

use Illuminate\Support\Manager;
use Ycs77\LaravelWizard\Wizard;

class CacheManager extends Manager
{
/**
* The wizard instance.
*
* @var \Ycs77\LaravelWizard\Wizard
*/
protected $wizard;

/**
* The wizard name.
*
Expand All @@ -16,12 +24,14 @@ class CacheManager extends Manager
/**
* Create a new Wizard Cache manager instance.
*
* @param \Ycs77\LaravelWizard\Wizard $wizard
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function __construct($app)
public function __construct(Wizard $wizard, $app)
{
$this->app = $app;
$this->wizard = $wizard;
}

/**
Expand All @@ -31,7 +41,7 @@ public function __construct($app)
*/
public function getDefaultDriver()
{
return $this->app['config']['wizard.driver'];
return $this->wizard->option('driver');
}

/**
Expand All @@ -54,7 +64,7 @@ protected function createSessionDriver()
*/
protected function createDatabaseDriver()
{
$table = $this->app['config']['wizard.table'];
$table = $this->wizard->option('table');

return new DatabaseStore($this->getDatabaseConnection(), $table, $this->app);
}
Expand Down Expand Up @@ -89,7 +99,7 @@ protected function getSessionKey()
protected function getDatabaseConnection()
{
return $this->app['db']->connection(
$this->app['config']['wizard.connection']
$this->wizard->option('connection')
);
}
}
9 changes: 9 additions & 0 deletions src/Console/stubs/controller.wizard.stub
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class DummyClass extends WizardController
*/
protected $wizardTitle = 'DummyWizardTitle';

/**
* The wizard options.
*
* Available options reference from Ycs77\LaravelWizard\Wizard::$optionsKeys.
*
* @var array
*/
protected $wizardOptions = [];

/**
* The wizard steps instance.
*
Expand Down
15 changes: 12 additions & 3 deletions src/Http/Controllers/WizardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ class WizardController extends Controller
*/
protected $wizardTitle = '';

/**
* The wizard options.
*
* Available options reference from Ycs77\LaravelWizard\Wizard::$optionsKeys.
*
* @var array
*/
protected $wizardOptions = [];

/**
* The wizard steps instance.
*
Expand All @@ -59,7 +68,7 @@ class WizardController extends Controller
*/
public function __construct(Wizard $wizard)
{
$this->wizard = $wizard->make($this->wizardName, $this->steps);
$this->wizard = $wizard->make($this->wizardName, $this->steps, $this->wizardOptions);
}

/**
Expand Down Expand Up @@ -112,15 +121,15 @@ public function store(Request $request, string $step)

$this->validate($request, $step->rules($request));

if (config('wizard.cache')) {
if ($this->wizard()->option('cache')) {
$step->cacheProgress($request);
} else {
$step->saveData($step->getRequestData($request), $step->model());
}

if (!$this->getNextStepSlug()) {
// Wizard done...
$data = config('wizard.cache') ? $this->save($request) : null;
$data = $this->wizard()->option('cache') ? $this->save($request) : null;

return $this->doneRedirectTo($data);
}
Expand Down
67 changes: 64 additions & 3 deletions src/Wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ycs77\LaravelWizard;

use Illuminate\Foundation\Application;
use Illuminate\Support\Arr;
use Ycs77\LaravelWizard\Exceptions\StepNotFoundException;

class Wizard
Expand All @@ -28,6 +29,25 @@ class Wizard
*/
protected $stepRepo;

/**
* The wizard options.
*
* @var array
*/
protected $options = [];

/**
* The wizard options extract key from config.
*
* @var array
*/
protected $optionsKeys = [
'cache',
'driver',
'connection',
'table',
];

/**
* Create a new Wizard instance.
*
Expand All @@ -44,16 +64,19 @@ public function __construct(Application $app)
*
* @param string $name
* @param mixed $steps
* @param array $options
* @return self
*/
public function make($name, $steps)
public function make($name, $steps, $options = [])
{
$this->setCache();
$this->setStepRepo();

$this->cache->setWizardName($name);
$this->stepRepo->make($steps);

$this->setOptions($options);

return $this;
}

Expand Down Expand Up @@ -86,7 +109,7 @@ public function getStep($slug = null)
*/
public function getLastProcessedStepIndex()
{
if ($this->app['config']['wizard.cache']) {
if ($this->option('cache')) {
return $this->cache->getLastProcessedIndex() ?? 0;
}

Expand Down Expand Up @@ -139,7 +162,7 @@ public function cache()
*/
public function setCache($cache = null)
{
$this->cache = $cache ?? new CacheManager($this->app);
$this->cache = $cache ?? new CacheManager($this, $this->app);
return $this;
}

Expand All @@ -165,6 +188,44 @@ public function setStepRepo($stepRepo = null)
return $this;
}

/**
* Get the wizard options.
*
* @return array
*/
public function options()
{
return $this->options;
}

/**
* Get the wizard option.
*
* @return mixed
*/
public function option($key)
{
return $this->options[$key] ?? null;
}

/**
* Get the wizard options.
*
* @param array $options
* @return self
*/
public function setOptions(array $options = [])
{
$config = Arr::only(
$this->app['config']['wizard'],
$this->optionsKeys
);

$this->options = array_merge($config, $options);

return $this;
}

/**
* Get the application instance.
*
Expand Down
12 changes: 0 additions & 12 deletions src/WizardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ class WizardServiceProvider extends ServiceProvider
*/
public function register()
{
$this->app->bind('wizard', function ($app) {
return new Wizard($app);
});

$this->app->singleton('wizard.cache', function ($app) {
return new CacheManager($app);
});

$this->app->singleton('wizard.cache.store', function ($app) {
return $app['wizard.cache']->driver();
});

$this->mergeConfigFrom(__DIR__ . '/../config/wizard.php', 'wizard');
}

Expand Down
30 changes: 26 additions & 4 deletions tests/Feature/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ public function setUp()
{
parent::setUp();

$controllerClass = '\Ycs77\LaravelWizard\Test\Stubs\WizardControllerStub';
$this->setWizardRoutes(
'/wizard/test',
'\Ycs77\LaravelWizard\Test\Stubs\WizardControllerStub',
'wizard.test'
);
}

/** @var \Illuminate\Routing\Router $router */
protected function setWizardRoutes($uri, $controllerClass, $name)
{
$this->app['router']
->middleware('web')
->group(function ($router) use ($controllerClass) {
Wizard::routes('/wizard/test', $controllerClass, 'wizard.test');
->group(function () use ($uri, $controllerClass, $name) {
Wizard::routes($uri, $controllerClass, $name);
});
}

Expand Down Expand Up @@ -131,4 +137,20 @@ public function testWizardNoCacheNowRunStepSaveData()
'first' => true,
], $this->app['session']->get('test-steps-queue'));
}

public function testWizardSetNoCacheFromControllerNowRunStepSaveData()
{
$this->setWizardRoutes(
'/wizard/no-cache',
'\Ycs77\LaravelWizard\Test\Stubs\WizardControllerOptionsStub',
'wizard.no-cache'
);

$response = $this->post('/wizard/no-cache/step-first-stub');
$response->assertRedirect('/wizard/no-cache/step-second-stub');

$this->assertEquals([
'first' => true,
], $this->app['session']->get('test-steps-queue'));
}
}
46 changes: 46 additions & 0 deletions tests/Stubs/WizardControllerOptionsStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Ycs77\LaravelWizard\Test\Stubs;

use Ycs77\LaravelWizard\Http\Controllers\WizardController;

class WizardControllerOptionsStub extends WizardController
{
/**
* The wizard name.
*
* @var string
*/
protected $wizardName = 'test';

/**
* The wizard options.
*
* Options reference in Ycs77\LaravelWizard\Wizard::$optionsKeys.
*
* @var array
*/
protected $wizardOptions = [
'cache' => false,
];

/**
* The wizard steps instance.
*
* @var array
*/
protected $steps = [
StepFirstStub::class,
StepSecondStub::class,
];

/**
* Get controller main class name.
*
* @return string
*/
public function getControllerClass()
{
return static::class;
}
}
Loading

0 comments on commit 570f8dc

Please sign in to comment.