简体   繁体   中英

Find file inside phar with symfony finder

I have the following code to find files in a directory called robofile-templates in my source tree. Now I am packaging the app into a phar file.

Can I use similar code to iterate over the files contained in the phar file or do I have to rewrite it?

protected function getTemplateRoboFiles() {
    $base = dirname( __DIR__ );

    $templates = [];

    $finder = new Finder();
    $finder->directories()->in( $base )->name( 'robofile-templates' );
    foreach ( $finder as $directory ) {
        $fileFinder = new Finder();
        $fileFinder->files()->in( $directory->getRealPath() )->name( '*.php' );
        foreach ( $fileFinder as $file ) {
            $templates[ $file->getBasename( '.php' ) ] = $file->getRealPath();
        }
    }

    return $templates;
}

EDIT: Interesting enough, opendir / readdir work inside the PHAR. I could resort to use those basic file functions, but I think Symfony file finder is built on the same basic commands?

Symfony finder is working as expected, also finding files in phars.

My problem was on another part of the process: I was building the phar using composer install and my composer.lock was not up to data. As the file I was trying to find in the archive, was in a composer module, it was just not actually included in the phar.

So who is stumbling across this: Better check if your file is really packaged into the phar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM