8000 customizable doctrine mappings by pascalwacker · Pull Request #214 · msgphp/msgphp · GitHub
[go: up one dir, main page]

Skip to content

customizable doctrine mappings #214

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

Merged
merged 9 commits into from
Oct 28, 2018
Merged

Conversation

pascalwacker
Copy link
Contributor

based on the previous pull #213 if've also integrated the key_max_length param for the *.orm.xml files as well as the Role.php file. Since I didn't know if this is the road to go down, I've decided to create a separate pull request, in case you want to merge 213 but no this one.

@@ -34,7 +36,9 @@ public function warmUp($cacheDir): void
$filesystem->mkdir($target = $cacheDir.'/'.$this->dirName);

foreach ($this->mappingFiles as $file) {
$filesystem->copy($file, $target.'/'.basename($file));
$contents = file_get_contents($file);
$contents = str_replace('%msgphp.doctrine.key_max_length%', $this->keyMaxLength, $contents);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this to Doctrine\MappingConfig e.g. MappingConfig::interpolate(string $contents) which replaces its own property placeholders.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created such a class in commit: ae59c6c. Was this what you meant?

As a side effect I had to enable the autowireing for the make command as well as the CacheWarmer. IMO this should be fine. Any objection against it?

@ro0NL ro0NL mentioned this pull request Oct 25, 2018
19 tasks
@ro0NL ro0NL changed the title Key length customizable doctrine mappings Oct 25, 2018
@pascalwacker
Copy link
Contributor Author

I thought a bit about it and I would reduce the default limit from 255 characters to 191 characters to ensure every one will have a "it works out of the box" experience. Additionally, I would suggest to add a receipt to https://github.com/symfony/recipes-contrib/tree/master/msgphp which adds the config option (with the new default 191 character limit), so people will be able to simply change it, if they like. What do you think?

$container->register(DoctrineInfra\MappingConfig::class)
->setPublic(false)
->setArgument('$keyMaxLength', '%msgphp.doctrine.mapping.key_max_length%');

if (FeatureDetection::hasFrameworkBundle($container)) {
$container->register(DoctrineInfra\MappingCacheWarmer::class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$container->autowire(...) and remove setAutowired below

public static function provideObjectFieldMappings(): iterable
private $keyMaxLength;

public function __construct(int $keyMaxLength = 255)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed right

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, I'll remove it

$this->keyMaxLength = $keyMaxLength;
}

public static function provideObjectFieldMappings(int $keyMaxLength = 255): iterable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we update the contract to ObjectFieldMappingsProviderInterface::provideObjectFieldMappings(MappingConfig $config)

i was also thinking to rename this part to simply MappingProviderInterface

@pascalwacker
Copy link
Contributor Author

@ro0NL most of the changes are done. Could you elaborate on where exactly I'll have to put

parameters:
  msgphp.doctrine.mapping_config:
    some_key: some_value

As far as I can see, so far no config file for this exists. Would I need to make the modification in https://github.com/symfony/recipes-contrib/tree/master/msgphp?

Another (related) question about: #214 (review)
Would I need to modify the MappingConfig class to store all assigned values in an array and have a set and get method for values on that array? Or did I get your intention wrong?

@pascalwacker
Copy link
Contributor Author

@ro0NL all right, if I understood all your changes correctly it should be ready now.

The changeable parameters are:

parameters:
    msgphp.doctrine.mapping_config:
        key_max_length: 191
        #mappingDir: /foo/bar/baz

Questions:

  1. In theory, also mapping_files would work, as it can be overwritten. What do you think, should we prevent this? I personally would keep it, in case the need to override it ever arises.
  2. If a mapping dir is supplied, I currently assume, it's an absolute path. Should we use relative paths, starting at the project dir, or even check if there's a leading / and if so assume an absolute path and otherwise a relative one?

@pascalwacker
Copy link
Contributor Author

About the unit tests, I think there is a caching issue or it tries to pull some things from the master branch. See: https://travis-ci.org/msgphp/msgphp/jobs/446701263

...............................PHP Fatal error:  Declaration of MsgPhp\Eav\Infra\Doctrine\ObjectFieldMappings::provideObjectFieldMappings(MsgPhp\Domain\Infra\Doctrine\MappingConfigInterface $config): iterable must be compatible with MsgPhp\Domain\Infra\Doctrine\ObjectFieldMappingsProviderInterface::provideObjectFieldMappings(): iterable in /home/travis/build/msgphp/msgphp/src/Eav/Infra/Doctrine/ObjectFieldMappings.php on line 15

As in https://github.com/pascalwacker/msgphp/blob/key_length/src/Domain/Infra/Doctrine/ObjectFieldMappingsProviderInterface.php#L18 the interface should be:

   public static function provideObjectFieldMappings(MappingConfigInterface $config): iterable;

but it seams that the unit test is not seeing the Interface correctly. Anything we can do about that?

Copy link
Contributor
@ro0NL ro0NL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pascalwacker can you rebase please? Master is green again :)

@pascalwacker
Copy link
Contributor Author
pascalwacker commented Oct 27, 2018

@ro0NL I had to move the config param to msgphp.doctrine.mapping_internal_config in ExtensionHelper. The reason is, that in DoctrineObjectFieldMappingPass it would only fetch the user defined paramets (as soon as there where any), but ignore the ones merged in ExtensionHelper (which is problematic, as we need the mapping_files which are only generated there. What do you think, is it okay this way?

And there's a new FatalErrorException thrown:

 (1/1) FatalErrorException

Compile Error: Declaration of MsgPhp\Domain\Infra\Messenger\Middleware\ConsoleMessageReceiverMiddleware::handle(Symfony\Component\Messenger\Envelope $envelope, Symfony\Component\Messenger\Middleware\StackInterface $stack): Symfony\Component\Messenger\Envelope must be compatible with Symfony\Component\Messenger\Middleware\MiddlewareInterface::handle($message, callable $next)

As far as I can tell, this exception is thrown because of the changes here: 5cd0beb#diff-aa27d06b004f798c679f353e887025c6

Copy link
Contributor
@ro0NL ro0NL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're really close now :)

/**
* @author Pascal Wacker <hello@pascalwacker.ch>
*
* @internal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should remove it, as we pass it as a service now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the @internal annotation?

@pascalwacker
Copy link
Contributor Author

last but not least we should add length=191 in the recipe entities as well

Where do I have to do this? Sorry, I've never worked on a flex receipt enabled project before.

@pascalwacker
Copy link
Contributor Author

@ro0NL like this? pascalwacker/recipes-contrib@103e4e6 (I haven't created a pull request yet)

I'm guessing, that I need to enable MsgPhpEavBundle and MsgPhpUserBundle. However I don't think that's correct, as it should be enabled for the Domain and not just these two bundles. How can I enable it for the Domain itself, or is this already the correct way?

@pascalwacker
Copy link
Contributor Author

I'm sorry, but I don't get it, what do I have to change at the annotation? Change @ORM\Column(type="msgphp_user_id") to @ORM\Column(type="msgphp_user_id", length=191)?

@pascalwacker
Copy link
Contributor Author

All right, pull request is created at symfony/recipes-contrib#550

@pascalwacker
Copy link
Contributor Author

As requested, I've moved the msgphp.doctrine.mapping_config to the BundleHelper 1c5bc52#diff-09010414274175a264e60ed1785c574f

However I'm not sure if it works as expected. As soon as I have in a symfony project a parameter in the services.yaml file, like this:

parameters:
    msgphp.doctrine.mapping_config:
        key_max_length: 123

All I get in DoctrineObjectFieldMappingPass is an array with one entry, with the key key_max_length. If I check in the BundleHelper where the parameter is set/merged, var_dump($container->hasParam('msgphp.doctrine.mapping_config')); I get a false and if I check, what's accessible in through the container, I only get the kernel namespace.

Can you confirm this behaviour? And if so, any idea, how to fix it?

Copy link
Contributor
@ro0NL ro0NL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good on 4.1 :) thanks @pascalwacker this works really nice.

@ro0NL ro0NL merged commit b5b5c78 into msgphp:master Oct 28, 2018
@pascalwacker
Copy link
Contributor Author

no problem, it was cool working on it! At times I felt like an absolute beginner having no idea what I'm actually doing :P

Do you have any reads you would recommend on domain/message driven applications, besides the ones listed in https://msgphp.github.io/ (or on the inner workings of Symfony, like all the compile pass magic going on)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants
0