nunomaduro / termwind

🍃 In short, it's like Tailwind CSS, but for the PHP command-line applications.
MIT License
2.29k stars 78 forks source link

Add support for custom renderers #164

Open WendellAdriel opened 1 year ago

WendellAdriel commented 1 year ago

I saw that Termwind has some default renderers for some elements:

The purpose of this PR is to allow users to register Custom Renderers if they want. This can be a nice addition for developers that want to create plugins, so they can create their own custom renderers.

The general idea is to have a new contract Renderer that must be implemented for all renderers and the actual render will be done by the new class ElementRenderer that can be used to register new renderers and to use the registered renderers to render the content.

I'm marking this as a Draft because I still need to know if this is something that you want to have in the project and if so I can work on modifying anything that's not "compatible" with what you're thinking for the project.

If this is not something you want in the project feel free to decline this, but if you think this can work, I'll be happy to work on any changes needed to have this merged. 😉 💪🏼 🔥

WendellAdriel commented 1 year ago

Thanks for the review @jdreesen! I made the changes you pointed out and also added a new test case to test that when registering an invalid renderer an exception is thrown! 💪🏼

WendellAdriel commented 1 year ago

@xiCO2k @nunomaduro I think the PR is now looking good for a final review

WendellAdriel commented 1 year ago

I didn't add in the PR but if you think it could be something useful I can create a function registerRenderer that can be imported and used like the style function that will wrap the register method from the ElementRenderer class so I can mark the ElementRenderer as an internal class.

WendellAdriel commented 1 year ago

I didn't add in the PR but if you think it could be something useful I can create a function registerRenderer that can be imported and used like the style function that will wrap the register method from the ElementRenderer class so I can mark the ElementRenderer as an internal class.

I added the registerRenderer function now, marked the ElementRenderer class as internal and updated the test suite to use the registerRenderer function.