Wizard Component for Symfony2
, (*1)
This is a simple component mainly developed for usage with the Symfony2 framework. And because
of that there is some references to that. Which easily can be changed., (*2)
License
See LICENSE for licensing terms., (*3)
Contributing
Everybody is welcome to send Pull Requests. But we reserve the right to reject any that is not
in line with our goals for this component., (*4)
See https://github.com/Peytz/Wizard/contributors for a list of contributors, (*5)
Installing
There is 3 ways to install this component. You can use Composer, Phar or just include the code directly in your
application., (*6)
Using phar
Theres is a ready to use phar achive in build/peytz-wizard.phar
which includes its own small autoloader that automatically
registers with php when included., (*7)
``` php
<?php, (*8)
require 'phar://build/peytz-wizard.phar';, (*9)
var_dump(class_exists('Peytz\Wizard\Wizard'));, (*10)
### Using Composer
You can also install it via composer by using something like the following require.
``` json
{
"require" : {
"peytz/wizard" : "master-dev"
}
}
Running tests
``` shell
$ phpunit, (*11)
Usage
-----
The api is very simple. You create a Report which properly should be an Doctrine Entity. The Report
holds your data and must implement `ReportInterface`
A Report is required as a contructor argument for a `Wizard`. A Wizard should be subclassed to add
custom steps or use a DependencyInjection framework to inject them into a Wizard object.
A Wizard holds a n number of Steps that implements the `StepInterface`. There is a basic `Step`
implementation of `StepInterface` availible.
``` php
<?php
namespace Vendor\Wizard;
use Peytz\Wizard\Step;
use Vendor\Wizard\Form\CustomFormType;
class CustomStep extends Step
{
public function getFormType()
{
return new CustomFormType();
}
}
``` php
<?php, (*12)
namespace Vendor\Wizard;, (*13)
use Peytz\Wizard\Wizard;
use Peytz\Wizard\ReportInterface;
use Vendor\Wizard\CustomStep;, (*14)
class CustomWizard extends Wizard
{
public function __construct(ReportInterface $report)
{
parent::__construct($report);
$this->add(new CustomStep());
}
}, (*15)
``` php
<?php
namespace Vendor\Wizard;
use Peytz\Wizard\ReportInterface;
class Report implements ReportInterface
{
}
Controller action implementation using Symfony Validator component for validation. Validation Groups
are extremely useful for this., (*16)
``` php
<?php, (*17)
namespace Vendor\Wizard;, (*18)
class Controller
{
protected $validator;, (*19)
protected $wizard;
public function myAction($stepIdentifier)
{
$step = $this->wizard->get($stepIdentifier);
$form = $this->createForm($step->getFormType(), $this->wizard->getReport(), array(
'validation_groups' => array($step->getName()),
));
if ($_POST) {
$form->bind($_POST);
if ($form->isValid()) {
$this->wizard->process($step);
// You should proberly save some stuff here? And redirect
}
}
return array(
'form' => $form,
);
}
}, (*20)
Sample Symfony2 DIC definition
------------------------------
I you want to use DependencyInjection with Symfony2 this is another way of having a Wizard and its steps associated.
The power of using a DIC is that each `StepInterface` implementation can have optional dependencies. Like building forms
programmatically instead of having a `FormType`.
``` xml
<container>
<services>
<service id="vendor.wizard.custom" class="Peytz\Wizard\Wizard">
<argument type="service" id="vendor.wizard.custom.report" />
<call method="add">
<argument type="service" id="vendor.wizard.custom.my_step" />
</call>
</service>
</services>
</container>
Updating gh-pages
branch
bash
$ ./apigen.sh
$ git push
, (*21)