DarkGhostHunter/Preloader

Excluding folder

FabianKoestring opened this issue · 19 comments

Hey there,

first of all - nice work!
I have a problem excluding a complete path. It would be nice to do something like this to exclude all files from /var/www/stack/src/www/module/ and /var/www/stack/src/service/.

Preloader::make()
        ->whenHits(2000000)
        ->memory(125)
        ->autoload(__DIR__ . '/../vendor/autoload.php')
        ->exclude(
            [
                '/var/www/stack/src/www/module/*',
                '/var/www/stack/src/service/*'
            ]
        )
        ->output(__DIR__ . '/preloader.php')
        ->generate();

I could implement something like a glob recursive., but that would mean breaking the current behaviour of the exclude(), so this would be on version 2.0.

I could add a excludeRecursive() allowing the exclusion of all files inside the directory in a recursive manner.

I could implement something like a glob recursive., but that would mean breaking the current behaviour of the exclude(), so this would be on version 2.0.

I could add a excludeRecursive() allowing the exclusion of all files inside the directory in a recursive manner.

That would be nice. In my opinion this should be default so i would suggest a version 2.0. 😁

A better approach would be just to ignore adding files that should be excluded. Like if ($file != $excluded) {//add}

Any ideas? I could create a pull request.

Any ideas? I could create a pull request.

Put it up. We can discuss further on the PR itself.

I can second the need for this. It's a HUGE issue.

Hands tied right now, please PR a good solution while I'm working (on updating multiple libraries to Laravel 7).

I may have time this weekend now that I upgraded my packages to Laravel 7. I think recursive methods for adding and excluding should be enough.

@DarkGhostHunter if you're not opposed to including an external lib, I'd just use:

https://github.com/symfony/finder

I just opened the 2.0 branch. The README lays out what the plan is.

I mean... it works... It's not very elegant. Are you intending on this lib having a bright future, or just trying to get something working?

I mean... it works... It's not very elegant. Are you intending on this lib having a bright future, or just trying to get something working?

Option 2, sir.

I'd basically just replace your glob with Symfony's Finder path processing. I'd have to dig into the code to determine exactly how, but the idea would be to entirely replace the glob string argument in the current exclude method with the upgraded one that supports Symfony's glob syntax.

Preloader::make()->exclude('/this/glob/that/is/commonly/supported/**/*.php');

I'd basically just replace your glob with finder path processing. I'd have to dig into the code to determine exactly how, but the idea would be to entirely replace the glob string in the current exclude method with the upgraded one that supports Symfony's glob syntax.

Preloader::make()->exclude('/this/glob/that/is/commonly/supported/**/*.php');

If is a string: it will pass it to Finder:

    $finder->in('/this/glob/that/is/commonly/supported/**/*.php')->files();

If is an array, the same:

    $finder->in([
        '/this/glob/that/is/commonly/supported/**/*.php',
        '/this/glob/that/is/commonly/supported/**/*.twig',
    ])->files();

If its a Closure, it will pass the finder instance as argument to it.

    $path($finder);

Personally, I'd fully abstract away the dependency. It could be swapped for another lib in the future, anything, even a custom implemented one. This lib would only be tied to the string syntax that Symfony's Finder originally supported. You'd basically be signing onto that string syntax dependency, which is a full upgrade with full BC with the syntax currently used.

I see no need to introduce another dependency with the Finder object itself. Stick with a strictly typed argument (string) on the exclude and just "upgrade" the current glob syntax with the Symfony Finder glob syntax.

Problem: Symfony Finder works with directories only. If you do '/this/glob/that/is/commonly/supported/**/*.php' it will treat it as directory and won't work:

$preloader
            ->autoload($autoload = $this->workdir . '/autoload.php')
            ->memory(10)
            ->append([
                $this->workdir . '/test_a/foo.php',
                $this->workdir . '/test_b/*.php',
            ])
            ->output($this->workdir . '/preload.php');

Symfony\Component\Finder\Exception\DirectoryNotFoundException : The "/home/dgh/development/composer/preloader/tests/workdir/test_a/foo.php" directory does not exist.
~~
Symfony\Component\Finder\Exception\DirectoryNotFoundException : The "/home/dgh/development/composer/preloader/tests/workdir/test_b/*.php" directory does not exist.

Introducing it would be BC. I think the safest option is to pass it to 2.0, unless I check for each file and check if it is a file or a directory, and pass that to Symfony Finder to find and get the file and put it into the final array of files.

This is straight from the docs:

$finder->path('data')->name('*.xml');

Can you not strip off the file and use the name method with path?

I was trying to remember when I've seen this syntax used before as just a string. So, Symfony's DI container configs use this syntax/pattern directly.

https://symfony.com/doc/current/service_container/3.3-di-changes.html

I was trying to remember when I've seen this syntax used before as just a string. So, Symfony's DI container configs use this syntax/pattern directly.

https://symfony.com/doc/current/service_container/3.3-di-changes.html

Whatever they do, is not on Symfony Finder. This will have to be put in 2.0.