Spomky-Labs / pwa-bundle

PHP library for generating a full featured PWA manifest

Home Page:https://pwa.spomky-labs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

drop new RegExp() in favor of generating the javascript regex

tacman opened this issue · comments

Description

Instead of

// RegexMatchCallbackHandler.php

        return sprintf("new RegExp('%s')", trim(mb_substr($matchCallback, 6)));

// use 
        return trim(mb_substr($matchCallback, 6));

When the regex is wrong, you'll see the error right away. Plus, you get flags (which we don't currently support with regex: ) and it's easier for PHP programmers to understand, even though it's technically a javascript regex.

Some musings at Spomky-Labs/phpwa-doc#12

Example

No response

Hi,

If you just set a string without any recognized pattern, the raw string is used as it is.
I do not know the advantages of the new RexExp('foo') compared to just 'foo'.

I think the only advantage of creating a RegExp is when the string is dynamic;

const myRegex = 'ab+c';
new RegExp(myRegex);

but since the bundle is generating javascript, we don't need to do that, we can simply generate the regex as javascript.

One big advantage to this approach is that if the user makes an error in the pwa.yaml file, they will see the error immediately in the javascript console. Otherwise, we should compile the expression and tell the user.

I found several errors in my pwa.yaml file, mostly because I was confused about the regex pattern.

Unlike PHP, it appears that you can't change the regex markers, and since we're dealing a lot with slashes, maybe it's easier to use RegExp(). We can add something for the flags if we have a good use case (e.g. matching JPG and PNG files instead of .jpg and .png).

Instinctively, seeing regex: /article looks like a bad regex to me. What if this key were named RegExp instead? That would make it clear it was a javascript RegExp and not php.

The simpler, the better.

We can add something for the flags if we have a good use case

Nothing prevent users to directly use JS

match_callback: 'new RegExp("/foo/bar", "g")'

Maybe I should simply drop it in favor of raw values.

I'm still a bit confused, some examples will help. I've got a few projects using pwa-bundle, I'd like them all to be working by the time you do your talk.

Running a Symfony app on your phone without an internet connection and without writing a single line of javascript sounds like science fiction, but it's sooo close!

I added a section in the documentation.
I hope it will clarify the match_callback option.
Also, I removed the regex: handler as it is ultimately useless.

Running a Symfony app on your phone without an internet connection and without writing a single line of javascript sounds like science fiction, but it's sooo close!

I hope this will allow users to adopt the PWA standard. Developers will focus on the application and not on the implementation details.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.