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

Feature/script tag #1259

Merged
merged 29 commits into from Mar 16, 2022

Conversation

robsonsobral
Copy link
Contributor

@robsonsobral robsonsobral commented Aug 7, 2021

Overview

It implements:

  • {script="group/template"}
  • {stylesheet="site:group/template"}

Resolves #1246.
Addresses #1220.

Nature of This Change

  • πŸ› Fixes a bug
  • πŸš€ Implements a new feature
  • πŸ› Refactors existing code
  • πŸ’… Fixes coding style
  • βœ… Adds tests
  • πŸ‘½ Adds new dependency
  • πŸ”₯ Removes unused files / code
  • πŸ”’ Improves security

Is this backwards compatible?

  • Yes
  • No

Documentation

User Guide Pull Request: ExpressionEngine/ExpressionEngine-User-Guide#232

@robsonsobral
Copy link
Contributor Author

robsonsobral commented Aug 8, 2021

As use the Stylesheet library to serve JavaScript feels weird, I duplicated and renamed it to Assets. I guess, the original can be converted into a proxy to the new one, to keep it available to addons developers. However, I've no idea on the better path to take.

@intoeetive
Copy link
Contributor

I don't like duplicatig the code - I think we need to be as DRY as possible.

I think it's better to have something like class Javascript extends Stylesheet - however we already have Javascript library so this might bring extra confusion.

Maybe this would be good chance to take this code out of legacy folder and make it Library or Service. And then the legacy class would just inherit the new one

@robsonsobral
Copy link
Contributor Author

@intoeetive , do you mind help me a little more with the right places and naming conventions? Should I create an Assets Library? Is that fine?

@intoeetive
Copy link
Contributor

I think making this change is combination of adding new feature with code refactoring.

We generally try to avoid adding stuff into legacy folder (we still do that for fixes though or when full component rewrite would be required otherwise). I also don't like the name Assets (that's just a personal thing; I think it brings confusion with the image management add-on), I prefer something like Resources.

So if I would be you :) I'd probably do something similar to this:

  1. Under ExpressionEngine\Libraries create Resources/Stylesheet and Resources/Javascript libraries, and have those registered in app.setup.php
  2. I would also probably create something like Resources/AbstractResource.php that the libraries could inherit or partially reuse. I'd put there stuff like sending generic headers, or adding hashsum etc.
  3. Keep the legacy EE_Stylesheet, but replace all functions code will calls to new library. Make it log deprecation message when called
  4. Update EE code to use the new library
  5. Make the other necessary changes to implement the new feature (in Template library etc)

Again, this is just how I personally view things here. I admit it can be approached differently as well

@robsonsobral
Copy link
Contributor Author

I prefer to follow your lead, as I'm a JS Developer and you'll be taking care of this code in the long run.

This way?
image

Here we go!

@intoeetive
Copy link
Contributor

Yes, something along those lines should be fine

(please also try to avoid force-pushing, that makes merging more difficult)

@robsonsobral
Copy link
Contributor Author

I'm sorry it have been taking so long, @intoeetive . Not even being an important feature for me, I've not found time enough for.

While working, I've found a bug which should not have to wait for me to be fixed! The Last-Modified header submitted along CSS requests are wrong when using template files!

/** -----------------------------------------
/**  Retrieve template file if necessary
/** -----------------------------------------*/
if (ee()->config->item('save_tmpl_files') == 'y') {
    ee()->load->helper('file');
    $basepath = PATH_TMPL . (
        isset($site) ? $site : (ee()->config->item('template_folder') ?: ee()->config->item('site_short_name'))
    ) . '/';
    $basepath .= $group . '.group/' . $row['template_name'] . '.' . $this->type;

    $str = read_file($basepath);
    $row['template_data'] = ($str !== false) ? $str : $row['template_data'];
    $row['edit_date'] = ($str !== false) ? filemtime($basepath) : $row['edit_date']; // this line is missing!
}

Without that last line, the submitted date is from database, not saved files.

@robsonsobral
Copy link
Contributor Author

I'm sorry, @intoeetive . I proved myself a total newbie PHP developer. I've saved my files in the right places, but I couldn't make them to work.

@intoeetive
Copy link
Contributor

I'll try to go with it. Can't promise when that will be happening though

@robsonsobral
Copy link
Contributor Author

I'm sorry, @intoeetive . And thank you!

@robsonsobral
Copy link
Contributor Author

I did it, @intoeetive ! It worked!

@robsonsobral
Copy link
Contributor Author

@intoeetive , πŸ˜₯πŸ˜₯πŸ˜₯?

@intoeetive
Copy link
Contributor

I looked through the code, and I think you did awesome job here.

I did not check every tiny bit, but it does look very good to me.

However we need to have some automated tests written to accept this - do you happen to have sample template code, or add-on code, that's utilizing this new code and that we can use to test?

Also in few places there need to be spaces around . to make the code PSR-12 compatible.

Thank you very much!

@intoeetive intoeetive added this to the 6.3.0 milestone Jan 19, 2022
@robsonsobral robsonsobral mentioned this pull request Jan 20, 2022
1 task
@robsonsobral
Copy link
Contributor Author

Tests added!

The same tests can be used to test the integrity attribute in the future.

@robsonsobral
Copy link
Contributor Author

I guess I should add tests for trigger words on URLs, like https://example.com/css/channel/channel_css/.

@robsonsobral
Copy link
Contributor Author

Now I've a problem. To add js as a template trigger word is a breaking change. May I ignore this? I never used this feature, so I don't know how important it is.

@intoeetive
Copy link
Contributor

Adding js as reserved word might be a problem if the system already has template or group named js. So need to think about how to handle this best - maybe have this as configurable value

@robsonsobral
Copy link
Contributor Author

robsonsobral commented Feb 8, 2022

Options I have:

  1. to not add js as a template trigger word. I don't know how important trigger words are. For example, xml is a valid template type which doesn't have trigger word on URI. Also, there's an annoying dev suggesting adding json as a valid template. Does it mean it requires a trigger word as well? Who knows?
  2. to put {script} tag behind a configuration. Which one?
  3. to put js as a trigger word behind a configuration. Which one?;
  4. to delay {script} tag until eecms v7 (and make me cry for my life).

Options 1 and 3 would make template type trigger words to be considered almost as a to-be-deprecated feature.

In any case, /css/ needs to be added to tests and I'm going to do that.

@intoeetive
Copy link
Contributor

I've never used the "trigger word" feature myself, so don't know how important it is. My vote is option 1.

@intoeetive
Copy link
Contributor

I have fixed the tests. Letting you guys handle everything else!

@matthewjohns0n matthewjohns0n merged commit 04a69ac into ExpressionEngine:6.dev Mar 16, 2022
@robsonsobral robsonsobral deleted the feature/script_tag branch March 16, 2022 19:03
@intoeetive intoeetive mentioned this pull request Aug 31, 2022
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.

Site short name on {stylesheet='template_group/css_template'}
4 participants