Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add middleware feature (extensions) #46

Closed
wants to merge 1 commit into from

Conversation

Toilal
Copy link
Contributor

@Toilal Toilal commented Aug 8, 2019

You can register middlewares to customize how automapper works internally and define
global behaviors.

The following example will set 42 to any id property that would have been null.

<?php

class AnwserToUniverseMiddleware implements PropertyMiddleware
{
    public function supportsMapProperty($propertyName,
                                        $source,
                                        $destination,
                                        MappingInterface $mapping,
                                        MappingOperationInterface $operation,
                                        array $context = [])
    {
        return $propertyName == 'id';
    }

    public function mapProperty($propertyName,
                                $source,
                                $destination,
                                MappingInterface $mapping,
                                MappingOperationInterface $operation,
                                array $context = [])
    {
        $defaultValue = $mapping->getOptions()->getPropertyReader()->getProperty($destination, $propertyName);
        if ($defaultValue === NULL) {
            $mapping->getOptions()->getPropertyWriter()->setProperty($destination, $propertyName, 42);
        }
    }
}

$config->registerMiddlewares(new AnswerToLifeMiddleware());
$config->registerMapping(Employee::class, EmployeeDto::class);
$mapper = new AutoMapper($config);

// The AutoMapper can now be used as usual, but your custom mapper class will be
// called to do the actual mapping.
$employee = new Employee(NULL, 'John', 'Doe', 1980);
$result = $mapper->map($employee, EmployeeDto::class);
//$result->id = 42;

Middleware feature open up many extension capabilities, feel free to check PHPDocs
from PropertyMiddleware and
MapperMiddleware interfaces for details.

@Toilal
Copy link
Contributor Author

Toilal commented Aug 8, 2019

I'm sorry for the big pull request, but it was in the TODO list and I really need this feature :).

Hope the implementation is what you are expecting for a middleware feature.

@Toilal Toilal force-pushed the middleware branch 3 times, most recently from b5acca5 to 445da4c Compare August 8, 2019 13:47
@BoShurik
Copy link
Contributor

BoShurik commented Aug 8, 2019

It does not look like a middleware for me. I think Extension name is more suitable here

@mark-gerarts
Copy link
Owner

Hi @Toilal, this is a big PR indeed! So as a head's up: it will take a while before I find the time to review this.

At a first glance I'll have to agree with @BoShurik. The usage of the before/after/skip/override constants seem like something that won't be needed in a middleware stack based on callables.

Still, lots of work went into this, and with a few changes I'll definitely merge this in master. I'll focus on the other PR's first though, before I start on this one.

@Toilal
Copy link
Contributor Author

Toilal commented Aug 8, 2019

I see what you mean about middleware. I could refactor to provide to the middleware function a callable parameter that would perform the default mapping operation when called, so that middleware implementation can invoke it at the start of the method, at the end of the method, or not at all.

This would then make the "before/after/override/skip" thing useless.

@Toilal Toilal mentioned this pull request Aug 9, 2019
@Toilal Toilal changed the title Add middleware feature Add middleware feature (extensions) Aug 9, 2019
@Toilal
Copy link
Contributor Author

Toilal commented Aug 9, 2019

I'm closing this pull request in favor or #47

@Toilal Toilal closed this Aug 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants