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

[Scoper] Pass directly the files to PHP-Scoper instead of finder instances #407

Closed
Konamiman opened this issue Aug 27, 2020 · 7 comments · Fixed by #768
Closed

[Scoper] Pass directly the files to PHP-Scoper instead of finder instances #407

Konamiman opened this issue Aug 27, 2020 · 7 comments · Fixed by #768

Comments

@Konamiman
Copy link
Contributor

Konamiman commented Aug 27, 2020

Feature Request

Right now there's a finders configuration key that tells how to find the files to be processed and included in the output. The problem is that there's no way to tell PHP Scoper "include these files in the output, but don't process them".

I think this could be implemented with a new output_finders key. It would work as follows:

  • Anything that is matched by either output_finders or finders is included in the output.
  • But, only what is matched by finders is processed.

Or put another way:

  • Anything that is matched by output_finders but not by finders is copied verbatim to the output.
  • Anything that is matched by finders is processed and then generated in the output (as it happens now).

That sounds complicated but it makes sense if we look at a simple example:

'output_finders' => [ Finder::create()->files()->in('src') ],
'finders' => [ Finder::create()->files()->in('src')->name('*.php') ]

So this means "I want everything from src in the output, but only PHP files should be processed".

This is something I actually need for my job so I can implement it and submit a pull request. In fact I've already been experimenting with the code and seems definitely doable.

@Konamiman Konamiman changed the title Add support for adding unprocessed files to the output Add support for including unprocessed files in the output Aug 27, 2020
@theofidry
Copy link
Member

@Konamiman
Copy link
Contributor Author

If I understood correctly that setting allows whitelisting a concrete set of files only. A mechanism based in finders would be much more flexible and powerful.

@theofidry
Copy link
Member

I'm wondering if it's worth it though. I realised quite late, but in the scoper file you already have access to the scoped Finder hence you can use it there right away and pass the result to the config which doesn't increase things in complexity for the user and simplifies things on PHP-Scoper side

@Konamiman
Copy link
Contributor Author

How would that work? Could you please provide an example for the use case I present in the issue description? (output all the files in a given directory but prefix only *.php)

@theofidry
Copy link
Member

Sure, here's one (although not using the Finder but you could totally do): https://github.com/humbug/php-scoper/blob/master/scoper.inc.php#L17

@Konamiman
Copy link
Contributor Author

Ok, got it. The following works and is equivalent to my example:

$files_to_output_untouched = (static function (): array {
    $finder = Finder::create()->files()->in('src')->notName('*.php');

    $files = [];
    foreach ($finder as $file) {
        $files[] = $file->getPathName();
    }

    return $files;
})();

return [
    'files-whitelist' => $files_to_output_untouched,
    'finders' => [ Finder::create()->files()->in('src') ],
    ...

Maybe something like this could be added to the documentation, but that's a different story 🙂

@theofidry
Copy link
Member

I think it would be clearer if the same would be done for 'finders' in which case it could be added to the config template and would be clearer for everyone

@theofidry theofidry changed the title Add support for including unprocessed files in the output [Scoper] Pass directly the files to PHP-Scoper instead of finder instances Dec 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants