Read from a file one line at a time.

@test
@param $fileName
@param callable|null $formatter - the default is a string.
@return \Generator|string|object
546244
private function fileLineGenerator(string $fileName, callable $formatter = null)
    {
        $f = fopen($fileName, 'r');
        try {
            while ($line = fgets($f)) {
                if(!is_null($formatter)){
                    yield call_user_func($formatter, $line);
                }
                else {
                    yield $line;
                }
            }
        } finally {
            fclose($f);
        }
    }
223659
public static function parseSource($source, $filename = NULL) {
    static $tokenizer, $parser = NULL;
    if (!isset($parser)) {
      $tokenizer = new Tokenizer();
      $parser = new self();
    }
    $tokens = $tokenizer->getAll($source, $filename);
    $parser->filename = $filename;
    return $parser->buildTree(new TokenIterator($tokens));
  }
767810
public function createReader($filePath, $delimiter = ReaderInterface::DELIMITER_DEFAULT, $fileCharset = ReaderInterface::CHARSET_DEFAULT, $mode = 'r', $type = null)
    {
        if (null === $type) {
            $type = $this->getDefaultReaderType();
        }

        return $this->getReaderFactory()->createReader($type, $filePath, $delimiter, $fileCharset, $mode);
    }
10636
private function openStream($filename)
    {
        if (!file_exists($filename)) {
            throw new ExtensionException(sprintf('File path (%s) doesn\'t exist', $filename));
        }
        
        if (!is_readable($filename)) {
            throw new ExtensionException(sprintf('File path (%s) doesn\'t readable', $filename));
        }
        
        return fopen($filename, 'a+');
    }
1173598
public function addFile($filename, FormatChainInterface $formatChain = null)
    {
        if (null === $formatChain) {
            if (null === $this->formatGuesser) {
                throw new FormatGuesserRequiredException();
            }

            $formatChain = $this->formatGuesser->guess($filename);
        }

        $this->files[] = new File($filename, $formatChain);

        return $this;
    }
1123726
public static function load($filename)
    {
        if (!is_readable($filename)) {
            throw new IOError($filename, 'File not found or file is not readable');
        }
        $content = @file_get_contents($filename);
        if ($content === false) {
            self::throwNativeError($filename);
        }
        return $content;
    }
785920
public static function createParsersFromFormats($formats, UriInterface $uri, LoggerInterface $logger)
    {
        $formatBits = intval($formats);

        // Run through all registered parsers and yield the requested instances
        foreach (self::$parsers as $parserFormat => $parserClass) {
            if ($parserFormat & $formatBits) {
                yield $parserFormat => new $parserClass($uri, $logger);
            }
        }
    }
174807
public static function read($fileName)
    {
        $data = parent::read($fileName);

        $json = json_decode($data, true);

        if (json_last_error()) {
            throw new \RuntimeException(json_last_error_msg());
        }

        return $json;
    }
1319651
public function formatters($formatters = null)
    {
        if (!func_num_args()) {
            return $this->_formatters;
        }
        $this->_formatters = $formatters;
        return $this;
    }
1019131
public static function fromFile($filename)
    {
        if (!is_file($filename) || !is_readable($filename)) {
            throw new \LogicException(sprintf('The file "%s" does not exist or is not readable.', $filename));
        }

        $html = file_get_contents($filename);

        return new static($html);
    }
306011
private function readFile($filename)
    {
        if (!file_exists($filename)) {
            throw new \Exception(sprintf('File not found: %s', $filename));
        }
        if (!is_readable($filename) || ($content = file_get_contents($filename)) === false) {
            throw new \Exception(sprintf('Failed to read file: %s', $filename));
        }

        return $content;
    }
569694
protected function loadFile($filename) {
        $lines = file($filename, FILE_IGNORE_NEW_LINES);            // don't use FILE_SKIP_EMPTY_LINES to have correct line
                                                                    // numbers for error messages
        if ($lines && strStartsWith($lines[0], "\xEF\xBB\xBF")) {
            $lines[0] = substr($lines[0], 3);                       // detect and drop a possible BOM header
        }

        foreach ($lines as $i => $line) {
            $line = trim($line);
            if (!strlen($line) || $line[0]=='#')                    // skip empty and comment lines
                continue;

            $parts = explode('=', $line, 2);                        // split key/value
            if (sizeof($parts) < 2) {
                // Don't trigger a regular error as it will cause an infinite loop if the same config is used by the error handler.
                $msg = __METHOD__.'()  Skipping syntax error in "'.$filename.'", line '.($i+1).': missing key-value separator';
                stderr($msg);
                error_log($msg, ERROR_LOG_DEFAULT);
                continue;
            }
            $key      = trim($parts[0]);
            $rawValue = trim($parts[1]);

            // drop possible comments
            if (strpos($rawValue, '#')!==false && strlen($comment=$this->getLineComment($rawValue))) {
                $value = trim(strLeft($rawValue, -strlen($comment)));
            }
            else {
                $value = $rawValue;
            }

            // parse and store property value
            $this->setProperty($key, $value);
        }
        return true;
    }
539997
protected function _loadFile($filename='')
    {
        if (is_null($this->_reader)) {
            $this->_reader = new Mage_Connect_Package_Reader($filename);
        }
        $content = $this->_reader->load();
        $this->_packageXml = simplexml_load_string($content);
        return $this;
    }
958619
public function setSourceFile($filename)
    {
        if ($filename instanceof Pdf) {
            return parent::setSourceFile($filename->currentFilename);
        } else {
            return parent::setSourceFile($filename);
        }
        // FIX: tried to be more efficiend but using same parsers brings issues clonning too.
        $newPdf = $filename;
        $this->currentFilename =  $newPdf->currentFilename;

        $this->parsers[$newPdf->currentFilename] = $newPdf->parsers[$newPdf->currentFilename];
        $this->setPdfVersion(
            max($this->getPdfVersion(), $newPdf->currentParser->getPdfVersion())
        );
        $this->currentParser = $this->parsers[$newPdf->currentFilename];
        return $this->currentParser->getPageCount();
    }
744950
public static function load( $fileName )
	{
		if( !file_exists( $fileName ) )
			throw new Exception( 'File "'.$fileName.'" is not existing.' );

		$reader	= new FS_File_Reader( $fileName );
		$lines	= $reader->readArray();
		
		$list	= array();
		foreach( $lines as $line )
		{
			$line = trim( $line );
			if( !$line )
				continue;
			if( preg_match( self::$commentPattern, $line ) )
				continue;
					
			if( preg_match( self::$sectionPattern, $line ) )
			{
				$section = substr( $line, 1, -1 );
				if( !isset( $list[$section] ) )
					$list[$section]	= array();
			}
			else if( $section )
				$list[$section][]	= $line;
		}
		return $list;
	}
1371597
public static function deserialize( $fileName )
	{
		$reader	= new FS_File_Reader( $fileName );
		$xml	= $reader->readString( $fileName );
		return parent::deserialize( $xml ); 
	}
653401
public function import_from_file($filename)
    {
        $reader = new FileReader($filename);
        if (!$reader->is_resource()) {
            return false;
        }

        $this->filename = (string) $filename;

        return $this->import_from_reader($reader);
    }