Utility.php

<?php

namespace Tlf\Tester;

class Utility {

    static public function xdotoolRefreshFirefox($switchBackToWindow = false){
        $args = $switchBackToWindow ? ' y' : '';
        system(__DIR__.'/reload.sh'.$args);

        \Taeluf\Tester\Utility::xdotoolRefreshFirefox($switchBackToWindow);
    }


    static public function startOb(){
        ob_start();
        $ob_level = ob_get_level();
        return $ob_level;
    }
    static public function endOb($ob_level){
        $output = '';

        while (ob_get_level()>$ob_level){
            $output .= ob_get_clean()."\n";
        }
        $output .= ob_get_clean();
        return $output;
    }



    /**
     * Copyright 2021 Reed Sutman, Taeluf
     * MIT License
     * See tluf.me/utils
     *
     * Please retain this notice
     */
    static public function getClassFromFile($file){
        if (is_dir($file)||!is_file($file)){
            return null;
            // throw new \Exception("'$file' is not a file");
        }
        $fp = fopen($file, 'r');
        $class = $namespace = $buffer = '';
        $i = 0;
        while (!$class) {
            if (feof($fp)) break;

            $buffer .= fread($fp, 512);
            ob_start();
            $tokens = @token_get_all($buffer);
            $err = ob_get_clean();

            if (strpos($buffer, '{') === false) continue;

            for (;$i<count($tokens);$i++) {
                if ($tokens[$i][0] === T_NAMESPACE) {
                    for ($j=$i+1;$j<count($tokens); $j++) {
                        if ($tokens[$j][0] === T_STRING) {
                            $namespace .= '\\'.$tokens[$j][1];
                        } else if ($tokens[$j] === '{' || $tokens[$j] === ';') {
                            break;
                        }
                    }
                }

                if ($tokens[$i][0] === T_CLASS) {
                    for ($j=$i+1;$j<count($tokens);$j++) {
                        if ($tokens[$j] === '{') {
                            if (isset($tokens[$i+2][1]))$class = $tokens[$i+2][1];
                        }
                    }
                }
            }
        }
        if ($class=='')return '';
        return $namespace.'\\'.$class;
        
    }
/**
 * Copyright 2021 Reed Sutman, Taeluf
 * MIT License
 * See tluf.me/utils
 *
 * Please retain this notice
 *
 * This recursive file getting function actually came from tluf.me/liaison
 */
/**
 * Utility class to work with files & directories
 * @todo move to my util repo & depend upon this class.
 */

    /**
     * Get all files in a directory. Does not return directories
     *
     * @param $dir the directory to search in
     * @param $relativeTo A /root/path to remove from file paths returned
     * @param $endingWith only get files that end with the given string, like `.php` or `.md`, etc
     * @return array of files
     */
    static public function getAllFiles($dir,$relativeTo='', $endingWith=''){
        $fPathsRelTo = $relativeTo;
        if (!is_dir($dir))return [];
        $dir = str_replace(['///','//'],'/','/'.$dir.'/');
        $fPathsRelTo = $fPathsRelTo ? str_replace(['///','//'],'/','/'.$fPathsRelTo.'/') : null;
        $dh = opendir($dir);
        $allFiles = [];
        while ($file=readdir($dh)){
            if ($file=='.'||$file=='..')continue;
            if (is_dir($dir.$file)){
                $subFiles = self::getAllFiles($dir.'/'.$file,$fPathsRelTo,$endingWith);
                $allFiles = array_merge($allFiles,$subFiles);
                continue;
            }
            $path = str_replace(['///','//'],'/',$dir.'/'.$file);
            if ($fPathsRelTo!==null){
                $path = '/'.substr($path,strlen($fPathsRelTo));
            }
            if ($endingWith!=''
                &&substr($path,-strlen($endingWith))!=$endingWith)continue;
            $allFiles[] = $path;
        }
        return $allFiles;
    }

}