Custom Alice Fixture Processors in Symfony Bundle-less Project

Let’s talk about Alice Fixtures. Today I wanted to plug some users into my project and basically just plug a custom Processor to load users with the user manipulator like this:

<?php
 
namespace My\Resources\Fixtures;
 
use Nelmio\Alice\ProcessorInterface;
use My\Domain\Agent;
use Symfony\Component\DependencyInjection\ContainerInterface;
 
class UserProcessor implements ProcessorInterface
{
    protected $container;
 
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }
 
    public function preProcess($object)
    {
 
    }
 
    public function postProcess($object)
    {
        if (!$object instanceof Agent) {
            return;
        }
 
        $userManager = $this->container->get('fos_user.user_manager');
        $userManager->updateUser($object, true);
    }
}

Notice the injection of the container was just because I was in a hurry, but we could as well inject only the service to write a better test. However the loader is badly injected with the container so that is why we just plugged it fast like that.

Notice one more thing, the namespace has no bundle name on it. You are right, that is because there is no need for a bundle when you are building your domain logic. There should be packages, you want packages, not bundles. Bundles here are unreal and artificial making things unnecessarily nested and turning the names into what they ought not to be.

<?php
 
namespace My\Resources\Fixtures;
 
use Hautelook\AliceBundle\Alice\DataFixtureLoader;
use Nelmio\Alice\Fixtures;
use Symfony\Component\Finder\Finder;
 
class FixturesLoader extends DataFixtureLoader
{
    /**
     * {@inheritDoc}
     */
    protected function getFixtures()
    {
        $files = Finder::create()->in(__DIR__)->name('*.yml');
 
        $paths = [];
        /** @var \SplFileInfo $file */
        foreach ($files as $file) {
            $paths[] = $file->getRealPath();
        }
 
        return $paths;
    }
 
    public function getProcessors()
    {
        return [
            new UserProcessor($this->container),
            new OtherDomainProcessor($this->container),
            // ...
        ];
    }
}

And this is how you plug it from your alice loader.
Very simple huh? Then you can just have your agent be plugged when you run the doctrine:fixtures:load command pointing to that folder.

My\Domain\Agent:
    agent{1}:
        username: test
        email: test@test.com
        plainPassword: test
        enabled: true

I just wanted to share this, even if some already know about alice processors. This example was done with the hautelook/alice-bundle. And I found this https://github.com/nelmio/alice#processors after doing it the same way almost ^_^. But it also has mistakes like it shouldn’t return any objet no the post and prePersist methods.

Hope someone finds it when they google, i couldn’t find it on google even though it was on the readme.md

Encouragements!

3 thoughts on “Custom Alice Fixture Processors in Symfony Bundle-less Project

  1. It sounds like we were discovering about processors at roughly the same time!

    I quickly added those docs on processors after bemoaning the lack of docs on them when I needed to use them (https://github.com/nelmio/alice/pull/120) you’re right about the postProcess shouldn’t return an object. I also made a typo ‘Processors must implement the `ProcessInterface`.’ doh! (should be `ProcessorInterface` of course).

    I’ll do another PR correcting these, let me know if there’s anything else you think the docs could do with.

Leave a Reply to Luis Cordova Cancel reply

Your email address will not be published. Required fields are marked *