Skip to content

Latest commit

 

History

History
66 lines (53 loc) · 1.66 KB

processors.md

File metadata and controls

66 lines (53 loc) · 1.66 KB

Processors

Processors allow you to process objects before and/or after they are persisted. Processors must implement the Nelmio\Alice\ProcessorInterface.

Here is an example where we may use this feature to make sure passwords are properly hashed on a User:

namespace MyApp\DataFixtures\Processor;

use Nelmio\Alice\ProcessorInterface;
use MyApp\Hasher\PasswordHashInterface;
use User;

class UserProcessor implements ProcessorInterface
{
    /**
     * @var PasswordHashInterface
     */
    protected $passwordHasher;

    /**
     * @param PasswordHashInterface $passwordHasher
     */
    public function __construct(PasswordHashInterface $passwordHasher)
    {
        $this->passwordHasher = $passwordHasher;
    }

    /**
     * {@inheritdoc}
     */
    public function preProcess($object)
    {
        if (false === $object instanceof User) {
            return;
        }

        $object->password = $this->passwordHasher->hash($object->password);
    }

    /**
     * {@inheritdoc}
     */
    public function postProcess($object)
    {
    }
}

You can add a list of processors in the load method, e.g.

$objects = \Nelmio\Alice\Fixtures::load(__DIR__.'/fixtures.yml', $objectManager, $options, $processors);

Or, you can add them to your loader using the ::addProcessor() method, e.g.

$loader = new \Nelmio\Alice\Fixtures($objectManager, $options);
$loader->addProcessor($processor);
$objects = $loader->loadFiles(__DIR__.'/fixtures.yml');

Previous chapter: Customize Data Generation
Got back to Table of Contents