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

Refactored the Grav classes load and process methods to follow clean coding standards #745

Merged
merged 7 commits into from
Apr 13, 2016

Conversation

toovy
Copy link

@toovy toovy commented Mar 24, 2016

When we evaluated Grav the first class I've looked into was Grav/Common/Grav. I loved the container and the central process chain, but the code seemed to be "copy & paste". Nearly the same code was repeated over and over again. The open-closed-principle was not applied. I've refactored the process and load method using two arrays as basis to load the needed services dynamically into the container and to use them in the process method in a more generic way. New services and processes can now be added without modifying existing mode (only the arrays would need to be modified).

to follow clean coding standards.

Cleaned up container setup
Added comments for the new methods in Grav
Removed not needed use statements from grav file
Refactored the grav->process method to be more extendable
Cleaned up a bit, reordered class methods, used measureTime in load
$this->measureTime($processor->id, $processor->title, function($debugger) use ($processor) {
$processor->process($debugger);
});
endforeach;
Copy link
Member

Choose a reason for hiding this comment

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

We aren't in a template file here, please use { ... } instead. :)

Copy link
Author

Choose a reason for hiding this comment

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

As the other code uses the classical notation { ... } I have changed it. But I've not see any guideline that say the alternative syntax should only be used in templates. Happy to learn something. :)

// then to get it from the container all time.
$container->measureTime = function($timerId, $timerTitle, $callback) use ($debugger) {
$debugger->startTimer($timerId, $timerTitle);
$callback($debugger);
Copy link
Member

Choose a reason for hiding this comment

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

I do not like having $debugger as a parameter. The parameter seems to have no purpose, which is why I ended up using a lot of time trying to understand reasoning behind it. I see it being used in 2 processors out of 13, but then again, its already available through the container. If its just there because of optimization, there are better ways to optimize code.

Copy link
Author

Choose a reason for hiding this comment

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

You've got a point. I'll change that.

*
* @return void
*/
protected function registerServices($container) {
Copy link
Member

Choose a reason for hiding this comment

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

$container = $this :) I don't think the parameter is needed anymore.

…n thus does not need to be passed through any more
@mahagr
Copy link
Member

mahagr commented Mar 24, 2016

I cannot find anything else to be fixed. :)

I like the change, even if right now there are not real benefits of having the array except the code refactor to make it more abstract. I'm thinking if we should allow plugins to use the new protected methods you added.. Hmm.. Also at some point we might want to allow setup.php to mess up with the DI container (to make real use from having an array), but that is not in the scope of this change.

@toovy
Copy link
Author

toovy commented Mar 24, 2016

You are right, the changes will make the system more open in future, there is no immediate benefit that e.g. a feature adds. I'm off the opinion that the core also needs refactorings like this one, else Grav might end up with a codebase like other successful CMS that are nearly unmaintainable cogh Wordpress cogh. ;)

@mahagr
Copy link
Member

mahagr commented Mar 24, 2016

There are multiple classes in Grav that I would refactor right away if I had the time to do it. One of the most painful ones are Page and Pages classes, which have experienced the most changes since we first implemented them. I'm just afraid the changes I would make would also break B/C, so they just need to wait a bit longer.

Changes like this one are really welcome. Still I'd like to wait to have extra pairs of eyes checking out this code and the performance implications it may have (I doubt there are any, but its something we also need to consider).

@rhukster I know you're at your vacation, but if you have some extra time, this will be one of the most interesting things you could look into. :)

@toovy
Copy link
Author

toovy commented Mar 24, 2016

@mahagr sure, that all makes sense. As we might use Grav for more clients we also might investigate even more.

@toovy
Copy link
Author

toovy commented Mar 24, 2016

Oh I see what you mean, 2526 locs...

@rhukster
Copy link
Member

I really like the ideas behind this refactor. Definitely does improve readability. I can see that down the line having more public API methods to allow for plugins to interact directly with the processors could be very useful.

I only have a couple of days left on my vaca and i'll be back home. I'll look at this more thoroughly this weekend. Thanks!

@rhukster
Copy link
Member

rhukster commented Apr 6, 2016

I know we've made a few changes that is causing some conflicts now. Would you mind updating this PR with the latest changes in the develop branch and i'll do some testing?

Thanks!!!

@toovy
Copy link
Author

toovy commented Apr 8, 2016

@rhukster I'll try to look into the conflicts next week

@toovy
Copy link
Author

toovy commented Apr 13, 2016

@rhukster merged, tests are ok (except the 5 that did go wrong before) and the demo system runs.

@rhukster rhukster merged commit 9eeb4c1 into getgrav:develop Apr 13, 2016
@rhukster
Copy link
Member

Ok, i've tested on my local and everything seems to be fine. Performance differences are negligible which was my only real concern. Thanks for your hard work on this, i think it's going to make Grav very flexible going forward.

rhukster added a commit that referenced this pull request Apr 13, 2016
@toovy
Copy link
Author

toovy commented Apr 14, 2016

Happy to help :)

@flaviocopes
Copy link
Contributor

P.S. tests should be good now.
They were failing when run with a user/ folder that had at least one plugin (inside a site folder, for example), but working in Travis / when run inside the clean Grav git repository.

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

Successfully merging this pull request may close these issues.

4 participants