godotengine / godot-asset-library

PHP frontend for Godot Engine's asset library
https://godotengine.org/asset-library
MIT License
294 stars 98 forks source link

Code standards, etc. #82

Open merumelu opened 8 years ago

merumelu commented 8 years ago

Okay, now for something important :D

At the moment, the code uses a mixture of different styles. For example, while most of the files use two spaces for indentation, some use four and others go a different way altogether and use tabs.

Another example: in templates there are a few occasions where alternate sytax is used for control structures (like if (foo): ... endif;) which do not appear in the majority of the code.

It might be good to decide on some standards while the codebase is still smallish, so changing it doesn't become such a behemoth as godotengine/godot#3916 :stuck_out_tongue:

merumelu commented 8 years ago

Btw, PSR-1 and PSR-2 are pretty much the standard in PHP nowadays.

bojidar-bg commented 8 years ago

Ok, here are some things that are (I think) already most-used:

All of this allows us to call utility functions as:

  $error = $this->utils->error_reponse_if_missing_or_not_string(false, $response, $body, 'username');
  $error = $this->utils->error_reponse_if_missing_or_not_string($error, $response, $body, 'email');
  $error = $this->utils->error_reponse_if_missing_or_not_string($error, $response, $body, 'password');
  if($error) return $response;
TomWor commented 8 years ago

I'd strongly suggest going with PSR-1 & PSR-2. https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md

Some important points:

PSR-2

PSR-1

So it should be e.g.

$error = $this->utils->errorReponseIfMissingOrNotString(false, $response, $body, 'username');

I'd also recommend using editorconfig.org to easily set editor preferences to those styles. Added a pull request for a basic editorconfig file #87.

TomWor commented 8 years ago

It might be related, so I'm posting this here: After browsing the code a bit I noticed a couple of odd design decisions.

bojidar-bg commented 8 years ago

I agree for all except the last point -- I'm not switching away from any technology since people found it "uncool" (also, bower has pretty nice decentralization I personally like). If we would switch away from uncool technologies, we are going to have to run assetlib on a Go/Rust server (which is obviously not on tuxfamily..).

TomWor commented 8 years ago

@bojidar-bg I completely get this - I also stay with some older technologies, because I'm more productive in them; and a project maintainer / main dev should always feel comfortable with the tech stack they are using. I guess I made a bad argument for chosing NPM over bower. Let's see if I might convince you further down the road for a better reason. ;)

TomWor commented 8 years ago

btw.: What about using a template engine? (Smarty/Twig) I know all the arguments around it (and been through the holy wars) - but I'm generally in favor of template engines, because they add a distinct interface between logic and presentation and make writing view helpers very modular.

(I'm also more of a frontend guy and thought I might be of use there a bit down the road)

merumelu commented 8 years ago

There is a template engine used, PHPView. However, it's pretty bare-bones.

merumelu commented 8 years ago

If we want to keep templates as PHP instead of some custom templating language, there's also Plates.

bojidar-bg commented 8 years ago

Ok... seems like we are deploying beta soon, so, we should start thinking about this issue, before 1.0 is started.

In other words, I would like if everyone agrees on some plan of refactoring action, and then we (or I) might do it. :smiley: (not implying that we aren't agreeing on something in any way)

merumelu commented 8 years ago

So does the merging of #87 mean ok to four spaces or PSR1/2 as a whole? If it's the latter, let's do that first, might be enough to just run everything through php-cs-fixer. :smile:

If we are going to replace PHP-View with something else, I'd vote for Twig. It has nice stuff like template inheritance, and unlike templates written in vanilla PHP, there's also autoescaping available as well as specific escaping strategies for JavaScript or HTML attributes which lessens the chance for accidental XSS quite a bit. Smarty might have this as well but I've never used it.

For auth stuff, I'd suggest adding a middleware which adds a user to $request using $requst->addAttribute(), which can then be checked in route middleware and is also accessible in routes themself if needed. Maybe add a "guest" level, so is_logged_in + has_level_higher_than(x) can be combined.

akien-mga commented 8 years ago

Regarding applying PSR1/2, please do it in two commits:

merumelu commented 8 years ago

Hm, PSR-1 states:

Code written for PHP 5.3 and after MUST use formal namespaces.

What namespace should we use? Godot\AssetLibrary?

bojidar-bg commented 8 years ago

@merumelu sounds good... :smile:

TomWor commented 8 years ago

I'd volunteer for integrating the template engine if you like. I'm a bit more in favor of Smarty, because I've used it extensively in the past, it's said to be faster, and extending it with custom functions is more straightforward IMO.

Just check the docs: http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension vs. http://www.smarty.net/docs/en/plugins.tpl

I know and recognize that Twig has (for some reason unknown to me) more momentum right now, so if you like to use that, I can do that.

Plates looks also nice I gotta say, but never used it.

I also have a bit of experience with Slim middleware, so if I should look into that, just ask.

bojidar-bg commented 8 years ago

Ok, I'll try to summarise everything decided on, plus some things that are under consideration:

TomWor commented 8 years ago

The PHP short tag syntax <?= is not related to PHP being old, it's been around for awhile, but it might be disabled on the host as you say - have you tested it? And yes, without it, it's really no fun to use a PHP based template language and would be an argument for one of the other solutions.

bojidar-bg commented 8 years ago

@TomWor Ups, my bad, it is my configuration that's wrong, not theirs :laughing: But, that being said, php-based templating might lead to putting too much things in the templates, which shouldn't be there (as mentioned by the Smarty site).

vnen commented 8 years ago

I remember having performance issues with Twig in the past, though it was a distant past and the problems went away once I rewrote my code to better use the caching it offers. Syntax-wise it is my favorite.

TomWor commented 7 years ago

As I'm said I'm rather agnostic about it, but I prefer Smarty syntax. Small example:

Twig

{% for color in myColors %}
    <li>{{ color }}</li>
{% endfor %}

Smarty

{foreach $myColors as $color}
    <li>{$color}</li>
{/foreach}

And writing plugins, filters, etc. is more straightforward IMO, linked it above. Anyway, let's just decide and move on, the template migration would be a good issue to move out of this thread and just implement it, it's very straightforward.

bojidar-bg commented 7 years ago

@TomWor Ok, I'm for Smarty in this case, since it is really troublesome to write all those % signs (though I dislike the ancient look of their doc :laughing: )

TomWor commented 7 years ago

@bojidar-bg Yeah, those % signs make my eyes bleed and I don't like them for readability. As for Smarty Docs: Yes, we are definitely not in cool-town here, but to their defense: The documentation is really good and clear. :)

TomWor commented 7 years ago

I might have a bit of time next week to work on the template engine integration. Can I proceed on this and start creating a template branch on my fork?

bojidar-bg commented 7 years ago

Well, would be nice if @merumelu's PSR PR is merged first, but it has some conflicts... So, I guess, go for it, then if some merge conflicts occur, we would fix them by hand :smiley:

bojidar-bg commented 6 years ago

@TomWor How is it the template engine integration going along? I would be glad to help somehow...

bojidar-bg commented 6 years ago

Just an update:

cmfcmf commented 6 years ago

That is great news! I would be willing to integrate the Doctrine ORM, if you like.

bojidar-bg commented 6 years ago

@cmfcmf Well, I checked various ORMs available, and I am not sure about using Doctrine (feel free to correct me on any of the points, as I haven't used PHP much).

Here are the various points I think are important to have (in no particular order):

  1. Clean and well-written documentation. A few ORMs have hard to understand docs, and will thus be harder to maintain in the long run (since maintainers change).
  2. Easy to develop without any tools installed, though it might generate cache files at runtime. The rationale here is that it is a bit hard to install composer or other tools on our current hosting provider, as well as maintainability (since contributors won't have to install the tools or wade through configs).
  3. Supports MySQL, and ideally also supports other databases. Otherwise, it might be harder to migrate away from MySQL if needed one day.
  4. Written with some performance in mind. Since you never know when a bottleneck will be hit.
  5. (bonus) Does as little destructive things to the database as possible. This usually means less black magic and potentially easier migrations.
  6. (bonus) Supports migrations out of the box. Not required, as there are libraries for migrations.

Taking a list of ORMs from awesome-php:

cmfcmf commented 6 years ago

You are right, I should've looked at other options before suggesting Doctrine (I suggested it because it is the only ORM I have used so far). Thank you for your comprehensive list of requirements and alternatives! I've looked into the different options myself now. Here are my two cents in addition to your own evaluation:

šŸ‘Ž

šŸ‘

To me it seems like Doctrine, Spot2 or Eloquent may be the way to go.

Calinou commented 6 years ago

I would personally go for Doctrine as I'm most familiar with that ORM.

rluders commented 6 years ago

Great! Let me point here something.

Actually the Godot's website is using OctoberCMS that is developed over the Laravel Framework. That is amazing. In fact, the big discussion, IMHO, should be if the Asset's Library desires to be close to the technology adopt on the website, allowing both to get easily connected.

So, my suggestion is to migrate the application to Laravel, using Eloquent and Blade/Twig (I really don't care about the template engine at this point) and Bulma CSS.

I started something on my fork, just for testing. Can be cloned from here: https://github.com/rluders/godot-asset-library/tree/feature/laravel

bojidar-bg commented 6 years ago

@rluders Sorry, I think I will have to stop your enthusiasm a bit.

My main concern, as discussed on other channels, is that there is little point in switching the whole codebase unless there are unsolveable problems with the current one. This is commonly known as "don't fix what's not broken."

About the big discussion of whether the Asset Library should become an OctoberCMS plugin, we have never planned for such an inclusion. Actually, part of the original plan was to provide code which would be used to create other asset libraries easily.

A final concern is that the changes when switching to another framework are huge, and learning to maintain the new code would take some time.

TomWor commented 6 years ago

I think a change to Laravel would make a lot of sense.

Sadly I never came around to contribute much to the project since my first involvement 2 years ago (!), and looking at the commit graph, there was really not much happening here by other actors as well. (no offense) Part of the reason I think might actually be the non-inclusive nature of the codebase, which is a bit of a hack based on the Slim Framework without any regards to current PHP code styleguides and best practices. What Laravel would provide is a stringent framework philosophy that a lot more developers are already familiar with and would provide a codebase that could be more easily picked up and worked on in peoples spare time. Please don't discard the idea for a framework switch and effective rewrite so easily. I think the whole asset library could be rewritten by someone with Laravel experience in about a day or two, that's how efficient the Laravel workflow is (I'm getting into it right now too, work-related). Also want to point out @rluders didn't propose making the asset library an October CMS plugin, just aligning the technologies with another, which would be another slam-dunk strategy-wise in my books. Just my 2 cents.

rluders commented 6 years ago

@bojidar-bg so, I totally disagree with the ā€œdonā€™t fix whatā€™s not brokenā€ principle. And Iā€™ll explain the main reason for that in one second.

@TomWor you came with a lot of points that is the reason to move to Laravel. And when you said that non-inclusive nature of the code is reducing the number of contribution, I canā€™t agree more. And this was one of the main reasons for me to propose the Laravel.

My first idea was exactly to help the project has it is right now, but It was so confusing and difficult to find out where to start. And I consider myself an experienced PHP Developer and Software Engineer (you can check my LinkedIn if you want to). And at the first moment I was just reading the code and trying to find something to improve, and my second thought was: OK, maybe the way that I can help is to make it an familiar codebase and some design principles, that could make it grows SOLID.

The code right now is pretty small, and most part of then could be easily moved out to Laravel. Iā€™m working with the rewrite right now, on my fork, and Iā€™ll probably work hard this weekend to see how much of the concepts can I delivery.

I know, Slim is great, in fact, thereā€™s a lot of nice frameworks that could be used. But, my big concert about using micro-frameworks is that you need to spend too much time focus in ā€œdesigning principlesā€ that an Laravel already provides, you donā€™t need to research libraries, see how they work together, design your abstracts, your folder structure, and more. You already have the basic job done, you could just keep the focus in your code business.

I know that we already have some work going out, to using some ORM and also moving the template engine to Twig. And I feel like we donā€™t need to discard this jobs, ā€˜cause we could use Twig instead of Blade with Laravel, and also using Repository Pattern to embrace the non-eloquent ORM for know, or even move it to Eloquent will be not be THAT painful.

And yes, @TomWor, align the technologies will give us a lot of benefits, ā€˜cause we could have people working on the website that will also be able to help on the asset store. So, instead having two teams, we will have ONE big and integrated team. So we could easily integrate system and contributors.

rluders commented 6 years ago

So... Here... As I said before, I'm working in an complete (?) rewrite of the asset store using Laravel. You can check an preview here: http://godot.luders.com.br/

menip commented 5 years ago

@rluders Your ui seems clean. What is status of project?

rluders commented 5 years ago

Hi, @menip. I just didn't have time to work on it right now (too many things happening). I'll get it back after August. Any help to work on it will be appreciated. Feel free to ping me on IRC (at freenode: rluders) or even on Godot official Discord (rluders#5401).