src/Service/GoogleApiService.php
private function locateFile($fileName, ?string $userSuppliedPath = null)
    {
        // These are the primary gApiServiceAccountCredentialsFile file locations
        $locations = [
            // First check the project file
            $this->projectDir.DIRECTORY_SEPARATOR.$fileName,
        ];

        // Then the current working directory, providing it is different from the project directory
        if (getcwd() !== $this->projectDir) {
            $locations[] = getcwd().DIRECTORY_SEPARATOR.$fileName;
        }

        $locationsFailed = [];

        // Prepend the requested location if specified
        if (null !== $userSuppliedPath) {
            array_unshift($locations, $userSuppliedPath);
        }

        // In the first loop, we go through the primary locations.
        for ($loop = 1; $loop <= 2; ++$loop) {
            while ($path = array_shift($locations)) {
                if (file_exists($path) && is_readable($path)) {
                    // When a file is found, we break free from both loops and just carry on with our life
                    return $path;
                }

                $locationsFailed[] = $path;
            }

            // If we made it here during the first loop, primary locations didn't work. We'll work out all the directories above us and try them
            if (1 === $loop) {
                // This means we didn't fin'd the file when checking the default locations, so let's try everything we can

                // Add the root directory first
                $locations[] = DIRECTORY_SEPARATOR.$fileName;

                // We'll take CWD and project directory and will try to crawl all the way up to the root to attempt to load the file
                foreach ([$this->projectDir, getcwd()] as $leafDirectory) {
                    $path = '/';
                    foreach (array_filter(explode(DIRECTORY_SEPARATOR, dirname($leafDirectory))) as $directory) {
                        $path .= $directory.DIRECTORY_SEPARATOR;

                        // Don't add one location twice
                        if (false === array_search($path.$fileName, $locationsFailed)
                            && false === array_search($path.$fileName, $locations)
                            && is_dir($path)
                            && is_readable($path)
                        ) {
                            array_unshift($locations, $path.$fileName);
                        }
                    }
                }
            }

            // If we made it here during the second loop, neither primary nor secondary locations worked. We can't continue.
        }

        return $locationsFailed;
    }
private function locateFile($fileName, ?string $userSuppliedPath = null)
    {
        // These are the primary gApiServiceAccountCredentialsFile file locations
        $locations = [
            // First check the project file
            $this->projectDir.DIRECTORY_SEPARATOR.$fileName,
        ];

        // Then the current working directory, providing it is different from the project directory
        if (getcwd() !== $this->projectDir) {
            $locations[] = getcwd().DIRECTORY_SEPARATOR.$fileName;
        }

        $locationsFailed = [];

        // Prepend the requested location if specified
        if (null !== $userSuppliedPath) {
            array_unshift($locations, $userSuppliedPath);
        }

        // In the first loop, we go through the primary locations.
        for ($loop = 1; $loop <= 2; ++$loop) {
            while ($path = array_shift($locations)) {
                if (file_exists($path) && is_readable($path)) {
                    // When a file is found, we break free from both loops and just carry on with our life
                    return $path;
                }

                $locationsFailed[] = $path;
            }

            // If we made it here during the first loop, primary locations didn't work. We'll work out all the directories above us and try them
            if (1 === $loop) {
                // This means we didn't fin'd the file when checking the default locations, so let's try everything we can

                // Add the root directory first
                $locations[] = DIRECTORY_SEPARATOR.$fileName;

                // We'll take CWD and project directory and will try to crawl all the way up to the root to attempt to load the file
                foreach ([$this->projectDir, getcwd()] as $leafDirectory) {
                    $path = '/';
                    foreach (array_filter(explode(DIRECTORY_SEPARATOR, dirname($leafDirectory))) as $directory) {
                        $path .= $directory.DIRECTORY_SEPARATOR;

                        // Don't add one location twice
                        if (false === array_search($path.$fileName, $locationsFailed)
                            && false === array_search($path.$fileName, $locations)
                            && is_dir($path)
                            && is_readable($path)
                        ) {
                            array_unshift($locations, $path.$fileName);
                        }
                    }
                }
            }

            // If we made it here during the second loop, neither primary nor secondary locations worked. We can't continue.
        }

        return $locationsFailed;
    }
private function locateFile ( $ fileName , ? string $ userSuppliedPath = null ) { // These are the primary gApiServiceAccountCredentialsFile file locations $ locations = [ // First check the project file $ this -> projectDir . DIRECTORY_SEPARATOR . $ fileName , ] ; // Then the current working directory, providing it is different from the project directory if ( getcwd ( ) !== $ this -> projectDir ) { $ locations [ ] = getcwd ( ) . DIRECTORY_SEPARATOR . $ fileName ; } $ locationsFailed = [ ] ; // Prepend the requested location if specified if ( null !== $ userSuppliedPath ) { array_unshift ( $ locations , $ userSuppliedPath ) ; } // In the first loop, we go through the primary locations. for ( $ loop = 1 ; $ loop <= 2 ; ++ $ loop ) { while ( $ path = array_shift ( $ locations ) ) { if ( file_exists ( $ path ) && is_readable ( $ path ) ) { // When a file is found, we break free from both loops and just carry on with our life return $ path ; } $ locationsFailed [ ] = $ path ; } // If we made it here during the first loop, primary locations didn't work. We'll work out all the directories above us and try them if ( 1 === $ loop ) { // This means we didn't fin'd the file when checking the default locations, so let's try everything we can // Add the root directory first $ locations [ ] = DIRECTORY_SEPARATOR . $ fileName ; // We'll take CWD and project directory and will try to crawl all the way up to the root to attempt to load the file foreach ( [ $ this -> projectDir , getcwd ( ) ] as $ leafDirectory ) { $ path = '/' ; foreach ( array_filter ( explode ( DIRECTORY_SEPARATOR , dirname ( $ leafDirectory ) ) ) as $ directory ) { $ path .= $ directory . DIRECTORY_SEPARATOR ; // Don't add one location twice if ( false === array_search ( $ path . $ fileName , $ locationsFailed ) && false === array_search ( $ path . $ fileName , $ locations ) && is_dir ( $ path ) && is_readable ( $ path ) ) { array_unshift ( $ locations , $ path . $ fileName ) ; } } } } // If we made it here during the second loop, neither primary nor secondary locations worked. We can't continue. } return $ locationsFailed ; }
Looks for a specified file name in various locations.

Returns path to the file if found, or array of tried locations if not found.

Locations checked:

- Project root dir
- Current working directory

@param $fileName
@param null|string $userSuppliedPath

@return string|array
Looks for a specified file name in various locations .