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

Allow swapping built-in renderers without using their fully qualified names #84

Merged
merged 3 commits into from
Mar 20, 2015

Conversation

nicolus
Copy link

@nicolus nicolus commented Mar 12, 2015

The documentation implies that the main purpose of Environment::addBlockRenderer() and Environment::addInlineRenderer() are to override the default renderers ("When registering these with the environment, you must tell it which block/inline classes it should handle. This allows you to essentially “swap out” built-in renderers with your own."), and the examples suggest that it can be done like this :

$environment->addBlockRenderer('FencedCode', $myRenderer);
$environment->addBlockRenderer('IndentedCode', $myRenderer);

The problem is this doesn't work, as you would actually have to do this in order to replace the built-in block renderers :

$environment->addBlockRenderer('League\CommonMark\Block\Element\FencedCode', $myRenderer);
$environment->addBlockRenderer('League\CommonMark\Block\Element\IndentedCode', $myRenderer);

The proposed pull requests adds tests that failed for both block and inline renderers being swapped as in the documentation, as well as a fix that makes it work, without breaking the existing tests which add other renderers.

It may not be the best way to work around that (I'm not even sure we should work around it), but at least the documentation should be consistent with the actual behavior.

Thanks.

@colinodell
Copy link
Member

Thanks for submitting this! I agree, the documentation should absolutely be consistent with the library's behavior.

Let me think on this and I'll get back to you :)

@colinodell colinodell self-assigned this Mar 12, 2015
@colinodell
Copy link
Member

I'm wondering if we should switch to using simple class names instead (no namespaces). I haven't really thought through the potential consequences of that, but it would make registration easier.

If we don't go that route, then I think your idea makes sense. I wonder if we should use class_exists to check for CommonMark classes instead of hard-coding them. Thoughts?

@nicolus
Copy link
Author

nicolus commented Mar 14, 2015

Thanks for taking time to review this.

I'm not sure how using class names without namespaces would work, but I'm afraid it might backfire. I think it would break code currently using the full namespace, and not allow things like this (with PHP 5.5+) :

use League\CommonMark\Block\Element\FencedCode;
//[...]
$environment->addBlockRenderer(FencedCode::class, $myRenderer);

Using class_exists sounds way better than my hard-coded array.

@colinodell
Copy link
Member

I think that makes sense, especially the 5.5+ usage.

So in that case, if you want to modify your code to use class_exists instead then I'll go ahead and merge this in :)

@colinodell colinodell added the enhancement New functionality or behavior label Mar 16, 2015
@colinodell colinodell added this to the Version 0.8 milestone Mar 16, 2015
@nicolus
Copy link
Author

nicolus commented Mar 20, 2015

OK, I'm not sure why scrutinizer is yelling at me :-/

Anyway, feel free to make the change yourself if you need.

@colinodell
Copy link
Member

Yeah, Scrutinizer has been doing that lately, not sure why. It looks good to me though :)

Thanks so much for implementing this!

colinodell added a commit that referenced this pull request Mar 20, 2015
Allow swapping built-in renderers without using their fully qualified names
@colinodell colinodell merged commit ec2a073 into thephpleague:master Mar 20, 2015
colinodell added a commit that referenced this pull request Mar 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New functionality or behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants