-
Notifications
You must be signed in to change notification settings - Fork 385
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 namespaced classes and PSR-4 support #711
Conversation
Any thoughts or questions? Is there anything I could improve? |
hi @Art4 I don't mind this patch, only hesitation would be that SimplePie focuses on remaining compatible with other projects so it would be good to hear how that will happen. I don't think issuing warnings for not using new namespaced classes is a good idea if that's what you meant. |
I would recommend to set silenced deprecation errors. They could look like this: @trigger_error('The `SimpelPie` class is deprecated and will be removed in 2.0. Use `SimplePie\SimplePie` instead.', \E_USER_DEPRECATED); Without the @-silencing operator users would see the errors as you noted. But with the @-silencing operator users would need to opt-in for deprecation notices. set_error_handler(function ($errno, $errstr, $errline, $errfile) {
// handle silenced errors
if (0 === error_reporting()) {
// throw Exception or show warning
}
}); Silencing allows users to opt-in when they are ready to cope with the warnings and test the new classes. Then, when they no longer receive warnings, they know their code is ready for SimplePie 2.0. However, setting the warnings is only optional and can also be omitted. But by doing so, we miss a good opportunity to inform the users about the planned changes and they would have a more difficult job to prepare early for the code changes with v2.0. This forward compatibility concept has also been used by Symfony for many years and has proven very effective. |
yeah I'm not sure. Maybe if enough people are keen on this then we could create a version 2.0 starting with this patch? I don't have the time to maintain it at the moment as it seems like there's quite a bit of refactoring to make it worthwhile. |
This change is specifically designed not to introduce a BC break so bump of minor version should be fine per semver. We could probably introduce something like https://github.com/Roave/BackwardCompatibilityCheck to CI to confirm compability. |
I would strongly advise against introducing namespaces in a v2 branch. As you said, I also suspect that this feature will require a lot of refactoring. If this happens in a dedicated branch, then it becomes necessary to regularly merge the changes from the v1 branch into the v2 branch. This is even more time consuming and I personally don't feel like doing this extra work. However, I am willing to continue working on refactoring from time to time, as I described in the plan in my first post. This way we are not dependent on someone having time, but can do the refactoring piece by piece and release it regulary. The namespaces in this branch could also be declared experimental for my sake. This way, users will always have the opportunity to use and test the namespaced classes and give us feedback if something is not working yet. You also have to keep in mind that the introduction of the namespaces will bring other changes that are hard to estimate now. e.g. the constants, building with But when we are sure that the namespaced classes are ready, we can mark the non-namespace classes as deprecated and remove them with v2. This makes the most sense, especially considering that this project is very much about stability and compatibility. |
@mblaney I've done the most of the refactoring that would be needed in #722. I've also created a more specific roadmap, how the changes of this PR could released. |
Hi @Art4 thanks for the update and thanks @jtojnar for commenting. I agree with what you're saying and don't want to stand in the way of the improvements you're making just because I've been too busy to think about it! :-) I will leave this open for a few more days in case there are any other opinions about such a big change, but otherwise happy to merge. |
Thank you @mblaney. I'm fully aware that this changes should be discussed by a wider range of the community. As @jtojnar already stated is this PR specially designed not to introduce a BC break. By releasing this PR as a new minor version (v1.6.0) we will hopefully make more people aware of the upcoming changes from #722 so they could share their opinions. |
ok thanks for your work on this @Art4 merging now |
I would like to have support for PSR-4. I saw in this issue that this change would be appreciated, so I looked into it.
If I understand PSR-4 correctly, there MUST be a namespace:
However, SimplePie does not provide a namespace at the moment. This PR creates a class alias for each class, which is inside the namespace
SimplePie
. (ForSimplePie_Parse_Date
you can useSimplePie\Parse\Date
). There are some exceptions to this:SimplePie
has the aliasSimplePie\SimplePie
.SimplePie_gzdecode
has aliasSimplePie\Gzdecode
(with capital G).SimplePie_Core
is deprecated and has not received an aliasSimplePie_Decode_HTML_Entities
is deprecated and has not received an aliasThen I created a new
src
folder and created pseudo-classes in it that extend the old classes. The reason for this is that incomposer.json
you have to specify a folder for PSR-4 where the classes are located in the correct file structure. If one request theSimplePie\SimplePie
class, the filesrc/SimplePie.php
will be loaded. Thanks toclass_exists('SimplePie');
the filelibrary/SimplePie.php
gets loaded (via psr-0) and there the class alias will be set. (The procedure is the same that was used for Twig, see twigphp/Twig#2484).Additionally, I created simple tests in the
tests\Unit
folder that verify that the classes/interfaces are available.What can the future look like?
The new
src
folder will allow us to gradually move all non-deprecated code into the namespace in the future. The plan for this might look something like this.src
folder and create aliases in thelibrary
folder. (This PR)class_alias('SimplePie_Cache', 'SimplePie\Cache', false);
library
classes to the namespace classessrc
folder:class_alias('SimplePie\Cache', 'SimplePie_Cache');
library
folderlibrary
folder, remove aliasesWhat do you think of this?
Automation script
For the modification and creation of the classes and tests I created a script based on the script from Twig: