Read configuration file line by line

@param   string $filename
@return  array Array with count of total and read lines
12162
public function read(string $filename): array
    {
        $lines = ['total' => 0, 'read' => 0];

        // Open file as SPL object
        $file = new \SplFileObject($filename);

        // Read line by line
        while (!$file->eof()) {
            $line = $file->fgets();
            // Save line only of not empty
            if ($this->isLine($line) && \strlen($line) > 1) {
                $line = trim(preg_replace('/\s+/', ' ', $line));
                $this->_lines[] = $line;
                $lines['read']++;
            }
            $lines['total']++;
        }
        return $lines;
    }
30296
static function read($filename) {
        if (file_exists($filename) && self::isReadable($filename)) {
            return file_get_contents($filename);
        }

        return NULL;
    }
1353449
public static function getConfigFile($filename)
    {
        $filename = addslashes($filename);
        if (is_file($filename)) {
            $data = str_replace("\\", "\\\\", file_get_contents($filename));
            $json = json_decode($data, true);

            if (empty($json)) {
                throw new \Exception("Config file has a json parse error, $filename");
            }

        } else {
            throw new \Exception("Config file not found, $filename");
        }
        
        return $json;
    }
1227169
public function write( $fileName )
	{
		$lines	= array();
		if( $this->useSections )
		{
			foreach( $this->data as $section => $sectionPairs )
			{
				$lines[]	= "[".$section."]";
				foreach ( $sectionPairs as $key => $data )
				{
					$value		= $data['value'];
					$comment	= $data['comment'];
					$lines[]	= $this->buildLine( $key, $value, $comment);
				}
				$lines[]	= "";
			}
		}
		else
		{
			foreach( $this->data as $key => $data )
			{
				$value		= $data['value'];
				$comment	= $data['comment'];
				$lines[]	= $this->buildLine( $key, $value, $comment);
			}
			$lines[]	= "";
		}
		$file		= new FS_File_Writer( $fileName, 0664 );
		return $file->writeArray( $lines );
	}
456169
protected function readTar( $fileName )
	{
 		$file	= new FS_File_Reader( $fileName );
		$this->content = $file->readString(); 		
		return $this->parseTar();																					// Parse the TAR file
	}
226997
public function readFile($fileName)
	{
		if (!isset($this->format))
			$this->format = \PhpOffice\PhpSpreadsheet\IOFactory::identify($fileName);
		$objectreader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($this->format);
		if ($this->format == "Csv") {
			$objectreader->setDelimiter($this->CSVDelimiter);
			$objectreader->setInputEncoding($this->CSVEncoding);
		}
		$objectPhpExcel = $objectreader->load($fileName);

		$sheetCount = $objectPhpExcel->getSheetCount();

		$sheetDatas = [];

		if ($sheetCount > 1) {
			foreach ($objectPhpExcel->getSheetNames() as $sheetIndex => $sheetName) {
				if (isset($this->getOnlySheet) && $this->getOnlySheet != null) {
					if(!$objectPhpExcel->getSheetByName($this->getOnlySheet)) {
						return $sheetDatas;
					}
					$objectPhpExcel->setActiveSheetIndexByName($this->getOnlySheet);
					$indexed = $this->getOnlySheet;
					$sheetDatas[$indexed] = $objectPhpExcel->getActiveSheet()->toArray(null, true, true, true);
					if ($this->setFirstRecordAsKeys) {
						$sheetDatas[$indexed] = $this->executeArrayLabel($sheetDatas[$indexed]);
					}
					if (!empty($this->getOnlyRecordByIndex)) {
						$sheetDatas[$indexed] = $this->executeGetOnlyRecords($sheetDatas[$indexed], $this->getOnlyRecordByIndex);
					}
					if (!empty($this->leaveRecordByIndex)) {
						$sheetDatas[$indexed] = $this->executeLeaveRecords($sheetDatas[$indexed], $this->leaveRecordByIndex);
					}
					return $sheetDatas[$indexed];
				} else {
					$objectPhpExcel->setActiveSheetIndexByName($sheetName);
					$indexed = $this->setIndexSheetByName==true ? $sheetName : $sheetIndex;
					$sheetDatas[$indexed] = $objectPhpExcel->getActiveSheet()->toArray(null, true, true, true);
					if ($this->setFirstRecordAsKeys) {
						$sheetDatas[$indexed] = $this->executeArrayLabel($sheetDatas[$indexed]);
					}
					if (!empty($this->getOnlyRecordByIndex) && isset($this->getOnlyRecordByIndex[$indexed]) && is_array($this->getOnlyRecordByIndex[$indexed])) {
						$sheetDatas = $this->executeGetOnlyRecords($sheetDatas, $this->getOnlyRecordByIndex[$indexed]);
					}
					if (!empty($this->leaveRecordByIndex) && isset($this->leaveRecordByIndex[$indexed]) && is_array($this->leaveRecordByIndex[$indexed])) {
						$sheetDatas[$indexed] = $this->executeLeaveRecords($sheetDatas[$indexed], $this->leaveRecordByIndex[$indexed]);
					}
				}
			}
		} else {
			$sheetDatas = $objectPhpExcel->getActiveSheet()->toArray(null, true, true, true);
			if ($this->setFirstRecordAsKeys) {
				$sheetDatas = $this->executeArrayLabel($sheetDatas);
			}
			if (!empty($this->getOnlyRecordByIndex)) {
				$sheetDatas = $this->executeGetOnlyRecords($sheetDatas, $this->getOnlyRecordByIndex);
			}
			if (!empty($this->leaveRecordByIndex)) {
				$sheetDatas = $this->executeLeaveRecords($sheetDatas, $this->leaveRecordByIndex);
			}
		}

		return $sheetDatas;
	}
40851
public function readFile($filename)
    {
        if (!is_string($filename))
            throw new IniReadingException('The expected type is string');

        if (!file_exists($filename) || !is_readable($filename))
            throw new IniReadingException(sprintf("The file %s doesn't exist or is not readable", $filename));

        if (function_exists('file_get_contents')) {
            $ini = file_get_contents($filename);
        } elseif (function_exists('file')) {
            $ini = file($filename);
            if ($ini !== false)
                $ini = implode("\n", $ini);
        } elseif (function_exists('fopen') && function_exists('fread')) {
            $handle = fopen($filename, 'r');
            if (!$handle)
                return false;
            $ini = fread($handle, filesize($filename));
            fclose($handle);
        } else {
            return false;
        }

        if ($ini === false)
            throw new IniReadingException(sprintf('Impossible to read the file %s', $filename));

        return $this->readString($ini);
    }
1056751
public static function load( $fileName )
	{
		$file	= new FS_File_Reader( $fileName );
		if( !$file->exists() )
			throw new Exception( "File '".$fileName."' is not existing." );
		$xml	= $file->readString();
		$parser	= new XML_OPML_Parser();
		return $parser->parse( $xml );
	}
753230
private function extractConfigFromMainFile(string $filename): string
  {
    $main = file_get_contents($filename);
    if ($main===false) $this->logError("Unable to read file '%s'.", $filename);

    preg_match('/^(.*requirejs.config)(.*}\))(.*)$/sm', $main, $matches);
    if (!isset($matches[2])) $this->logError("Unable to fine 'requirejs.config' in file '%s'.", $filename);

    return $matches[2];
  }
989559
public function get_config_json( $filename ) {
		if ( isset( $this->loaded[ $filename ] ) ) {
			return $this->loaded[ $filename ];
		}

		$config = $this->path . 'config/' . $filename . '.json';

		if ( ! file_exists( $config ) ) {
			return null;
		}

		$contents = file_get_contents( $config );

		if ( false === $contents ) {
			return null;
		}

		return $this->loaded[ $filename ] = json_decode( $contents, true );
	}
690661
public function readByFilename($path, $filename)
    {
        $config = false;
        $configFilePath = rtrim($path, '/') . '/' . $filename;

        if (is_file($configFilePath)) {
            $yamlParser = new YamlParser();
            $config = $yamlParser->parse(file_get_contents($configFilePath));
        }

        return $config;
    }
913123
public function read($fileName)
    {
        $parsedYaml = $this->readFromCache($fileName);
        if ($parsedYaml !== null) {
            return $parsedYaml;
        }

        try {
            $fileContent = file_get_contents($fileName);
            $parsedYaml  = Yaml::parse($fileContent);
            if (!is_array($parsedYaml)) {
                $parsedYaml = [];
            }
            $this->writeToCache($fileName, $parsedYaml);
            return $parsedYaml;
        } catch (\Exception $e) {
            throw new \RuntimeException('Could not read Yaml file '.$fileName, $e->getCode(), $e);
        }
    }
395295
public static function read( $fileName, $reverse = FALSE, $limit = 0 )
	{
		$data	= array();
		if( !file_exists( $fileName ) )
			throw new Exception( 'Log File "'.$fileName.'" is not existing.' );
		$lines		= file( $fileName );
		foreach( $lines as $line )
		{
			$line	= trim( $line );
			if( !$line )
				continue;
			$data[]	= json_decode( $line, TRUE );
		}
		if( $reverse )
			$data	= array_reverse( $data );
		if( $limit )
			$data	= array_slice( $data, 0, $limit );
		return $data;
	}
59878
public static function parseFromFile($filename)
    {
        if (file_exists($filename) && is_readable($filename)) {
            return new self(file_get_contents($filename));
        } else {
            throw new \RuntimeException(sprintf("File %s is not readable, or doesn't exist.", $filename));
        }
    }
1245365
public function read($filename)
    {
        $file = fopen($filename, 'r');
        $json = fread($file, filesize($filename));
        fclose($file);
        $array = (array)json_decode($json);
        foreach ($array as $key => $value) {
            $this[$key] = $value;
        }
        unset($file);
        unset($json);
        unset($array);
    }
1172500
public static function getTokens(string $filename): array
    {
        $tokens = token_get_all(file_get_contents($filename));

        $line = 0;
        foreach ($tokens as &$token) {
            if (isset($token[self::LINE])) {
                $line = $token[self::LINE];
            }

            if (!is_array($token)) {
                $token = [$token, $token, $line];
            }

            unset($token);
        }

        return $tokens;
    }
122268
public function readdata($filename) {
        global $CFG;

        // Find if we are importing a .dat file.
        if (strtolower(pathinfo($filename, PATHINFO_EXTENSION)) == 'dat') {
            if (!is_readable($filename)) {
                $this->error(get_string('filenotreadable', 'error'));
                return false;
            }

            $fileobj = new qformat_blackboard_six_file();

            // As we are not importing a .zip file,
            // there is no imsmanifest, and it is not possible
            // to parse it to find the file type.
            // So we need to guess the file type by looking at the content.
            // For now we will do that searching for a required tag.
            // This is certainly not bullet-proof but works for all usual files.
            $fileobj->text = file_get_contents($filename);
            if (strpos($fileobj->text, '<questestinterop>')) {
                $fileobj->filetype = self::FILETYPE_QTI;
            }
            if (strpos($fileobj->text, '<POOL>')) {
                $fileobj->filetype = self::FILETYPE_POOL;
            }
            // In all other cases we are not able to handle this question file.

            // Readquestions is now expecting an array of strings.
            return array($fileobj);
        }
        // We are importing a zip file.
        // Create name for temporary directory.
        $uniquecode = time();
        $this->tempdir = make_temp_directory('bbquiz_import/' . $uniquecode);
        if (is_readable($filename)) {
            if (!copy($filename, $this->tempdir . '/bboard.zip')) {
                $this->error(get_string('cannotcopybackup', 'question'));
                fulldelete($this->tempdir);
                return false;
            }
            $packer = get_file_packer('application/zip');
            if ($packer->extract_to_pathname($this->tempdir . '/bboard.zip', $this->tempdir)) {
                $dom = new DomDocument();

                if (!$dom->load($this->tempdir . '/imsmanifest.xml')) {
                    $this->error(get_string('errormanifest', 'qformat_blackboard_six'));
                    fulldelete($this->tempdir);
                    return false;
                }

                $xpath = new DOMXPath($dom);

                // We starts from the root element.
                $query = '//resources/resource';
                $qfile = array();

                $examfiles = $xpath->query($query);
                foreach ($examfiles as $examfile) {
                    $fileobj = new qformat_blackboard_six_file();

                    if ($examfile->getAttribute('type') == 'assessment/x-bb-qti-test'
                            || $examfile->getAttribute('type') == 'assessment/x-bb-qti-pool') {

                        if ($content = $this->get_filecontent($examfile->getAttribute('bb:file'))) {
                            $fileobj->filetype = self::FILETYPE_QTI;
                            $fileobj->filebase = $this->tempdir;
                            $fileobj->text = $content;
                            $qfile[] = $fileobj;
                        }
                    }
                    if ($examfile->getAttribute('type') == 'assessment/x-bb-pool') {
                        if ($examfile->getAttribute('baseurl')) {
                            $fileobj->filebase = $this->tempdir. '/' . $examfile->getAttribute('baseurl');
                        }
                        if ($content = $this->get_filecontent($examfile->getAttribute('file'))) {
                            $fileobj->filetype = self::FILETYPE_POOL;
                            $fileobj->text = $content;
                            $qfile[] = $fileobj;
                        }
                    }
                }

                if ($qfile) {
                    return $qfile;
                } else {
                    $this->error(get_string('cannotfindquestionfile', 'question'));
                    fulldelete($this->tempdir);
                }
            } else {
                $this->error(get_string('cannotunzip', 'question'));
                fulldelete($this->temp_dir);
            }
        } else {
            $this->error(get_string('cannotreaduploadfile', 'error'));
            fulldelete($this->tempdir);
        }
        return false;
    }