coenjacobs / mozart

Developers tool for WordPress plugins: Wraps all your projects dependencies in your own namespace, in order to prevent conflicts with other plugins loading the same dependencies in different versions.
https://coenjacobs.me/projects/mozart/
MIT License
426 stars 53 forks source link

Ability to define custom renames for single package #54

Open danieliser opened 4 years ago

danieliser commented 4 years ago

Would be nice if we could replace parts of a package namespace along the way.

Currently extras.mozart.packages accepts array of strings, I'm thinking it could be configured to accept objects as well

"packages": [
  "pimple/pimple",
  { 
    "name": "company/package",
    "replace_namespace: [ "\\Company\\Packages", "" ]
  }
]

In this way you can customize the handling of each package with various modifiers, such as string replacement in a namespace.

Still toying with how that gets implemented on the backend, but happy to proof of concept it.

coenjacobs commented 4 years ago

Can you give me a little more information on the use case for this? My first impression is that this is beyond the scope of what Mozart should do, but I could be wrong. Please convince me. :)

danieliser commented 4 years ago

@coenjacobs So this library does 99% of what I'm discussing already. It searches for a namespace and replaces it with a prepended namespace. That is, it does S&R very effectively on namespaces.

What I'm proposing is that you offer a little flexilibility in the search & replace, specifically the replace in my use case.

Just for example

namespace Company\Package;
class Container extends \Pimple\Container {}

Now I require that package in another project of the Company.

This project uses something like namespace Company\ProjectA;

So when mozart replaces things, it comes out as namespace Company\ProjectA\Company\Package which makes little to no sense when working with it in our own code base.

What I'd prefer is to change the Replacers replace string for this particular package with '' nothing, thus making is so I can simply call new Company\ProjectA\Container();, or alternatively shorthanding it at least to something more appropriate like 'UtilsorBootstrap` or similar.

I could come up with a few more use cases, but I quickly hit this once I started breaking our code into smaller reusable packages last week after finding mozart and deciding we should head this direction.

In general you could rename packages entirely in this way as well, while still maintaining the full link to their original source for easy updating via composer & attribution.

coenjacobs commented 4 years ago

Very valid use case, @danieliser, consider me convinced! This is actually one of the issues with Mozart I still had in the back of my mind, but hadn't connected the dots that you meant the same. I'd be very open to an implementation (or proof of concept) of this. Some things to keep in mind, though, that I think need to be accounted for:

danieliser commented 4 years ago

@coenjacobs -

Good questions.

Off top of my head: