Locates an entry using its name, returns the entry contents

@param string $file - path to a file
@param boolean $as_xml

@return boolean|\DOMDocument
351581
protected function getZipContent( $file, $as_xml = true ) {

		// Locates an entry using its name
		$index = $this->zip->locateName( $file );

		if ( false === $index ) {
			return false;
		}

		// returns the contents using its index
		$content = $this->zip->getFromIndex( $index );

		// if it's not xml, return
		if ( ! $as_xml ) {
			return $content;
		}

		$collapsed = preg_replace( '/\s+/', '', $content );
		if ( preg_match( '/<!DOCTYPE/i', $collapsed ) ) {
			// Invalid XML: Detected use of illegal DOCTYPE
			return false;
		}

		// trouble with simplexmlelement and elements with dashes
		// (ODT's are ripe with dashes), so giving it to the DOM
		$old_value = libxml_disable_entity_loader( true );
		$xml = new \DOMDocument();
		$xml->loadXML( $content, LIBXML_NOBLANKS | LIBXML_NOENT | LIBXML_NONET | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING );
		libxml_disable_entity_loader( $old_value );

		return $xml;
	}
888240
public function findDestination($file, $overwrite = false) {
        if ($file instanceof File) {
            $name = $file->name();
            $ext = '.' . $file->ext();

        } else {
            $name = $file;
            $ext = '';

            if ($pos = mb_strrpos($name, '.')) {
                $ext = mb_substr($name, $pos, (mb_strlen($name) - $pos));
                $name = mb_substr($name, 0, $pos);
            }
        }

        $target = $this->_directory . $name . $ext;

        if (!$overwrite) {
            $no = 1;

            while (file_exists($target)) {
                $target = sprintf('%s%s-%s%s', $this->_directory, $name, $no, $ext);
                $no++;
            }
        }

        return $target;
    }
421844
public static function getFormatedXml($string, $asDomDocument = false)
    {
        if (!is_null($string)) {
            $domDocument = self::getDOMDocument($string);
            return $asDomDocument ? $domDocument : $domDocument->saveXML();
        }
        return null;
    }
1338519
protected function findFile($file)
    {
        foreach (clone $this->paths as $path) {
            $filePath = $path . '/' . $file;

            if (is_file($filePath)) {
                return $filePath;
            }
        }

        return false;
    }
1075284
protected function locateFile($file)
    {
        $this->locateFile && $file = call_user_func($this->locateFile, $file);

        return $file;
    }
602816
private function parseFile($file)
    {
        try {
            $dom = XmlUtils::loadFile($file, __DIR__.'/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd');
        } catch (\Exception $e) {
            throw new MappingException($e->getMessage(), $e->getCode(), $e);
        }

        return simplexml_import_dom($dom);
    }
1043168
public static function getFormatedXml($_string,$_asDomDocument = false)
    {
        if(!empty($_string) && class_exists('DOMDocument'))
        {
            $dom = new DOMDocument('1.0','UTF-8');
            $dom->formatOutput = true;
            $dom->preserveWhiteSpace = false;
            $dom->resolveExternals = false;
            $dom->substituteEntities = false;
            $dom->validateOnParse = false;
            if($dom->loadXML($_string))
                return $_asDomDocument?$dom:$dom->saveXML();
        }
        return $_asDomDocument?null:$_string;
    }
285704
private static File astFile(final String uriOrFileName) {
        try {
            final String astFileName = uriOrFileName + ".xml";
            return uriOrFileName.startsWith("file:") ? new File(URI.create(astFileName)) : new File(astFileName);
        } catch (IllegalArgumentException e) {
            return null;
        }
    }
1353170
protected function parseFile($file)
    {
        try {
            $dom = XmlUtils::loadFile($file, array($this, 'validateSchema'));
        } catch (\InvalidArgumentException $e) {
            throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
        }

        return simplexml_import_dom($dom);
    }
119193
public function load($file)
    {
        $schema = array();

        $path     = $this->locator->locate($file);
        $xml      = $this->parseFile($path);
        $document = $xml->documentElement;

        /** @var \DOMElement $setting */
        $prefix = $document->hasAttribute('prefix') ? $document->getAttribute('prefix') . '.' : '';

        foreach ($document->getElementsByTagName('parameter') as $node) {
            $this->parseParameter($node, $schema, $prefix);
        }

        return $schema;
    }
1182859
public static function fromFile(string $file) : DOMDocument
    {
        if (!is_file($file)) {
            throw new InvalidArgumentException(sprintf('Path "%s" is not a file', $file));
        }

        if (!is_readable($file)) {
            throw new InvalidArgumentException(sprintf('File "%s" is not readable', $file));
        }

        // libxml_disable_entity_loader(true) disables \DOMDocument::load() method
        // so we need to read the content and use \DOMDocument::loadXML()
        $xml = file_get_contents($file);
        if ($xml === false) {
            throw new RuntimeException(sprintf(
                'Contents of readable file "%s" could not be gotten',
                $file
            ));
        }

        if (trim($xml) === '') {
            throw new RuntimeException(sprintf('File "%s" does not have content', $file));
        }

        return static::fromString($xml);
    }
546179
private function getFileContents($file) {
    if (file_exists($file) === FALSE) {
      return NULL;
    }

    $contents = file_get_contents($file);
    if ($contents === FALSE) {
      return NULL;
    }

    return $contents;
  }
1064119
protected function parseXml($file)
    {
        $this->xpath = new \DOMXPath($this->tryLoad($file));
        $this->xpath->registerNamespace('x', 'http://schemas.sulu.io/webspace/webspace');

        // set simple webspace properties
        $this->webspace = new Webspace();
        $this->webspace->setName($this->xpath->query('/x:webspace/x:name')->item(0)->nodeValue);
        $this->webspace->setKey($this->xpath->query('/x:webspace/x:key')->item(0)->nodeValue);
        $this->webspace->setTheme($this->generateTheme());
        $this->webspace->setNavigation($this->generateNavigation());
        $this->webspace->setResourceLocatorStrategy('tree_leaf_edit');

        $this->generateTemplates($this->webspace);
        $this->generateDefaultTemplates($this->webspace);

        // set security
        $this->generateSecurity();

        // set localizations on webspaces
        $this->generateWebspaceLocalizations();

        // set segments on webspaces
        $this->generateSegments();

        // set portals on webspaces
        $this->generatePortals();

        // validate the webspace, and throw exceptions if not valid
        $this->validate();

        return $this->webspace;
    }
1010430
protected function getFilePath(string $file, ?string $extension = null, ?string $suffix = null): string
	{
		if(strpos($file, '::') === false)
		{
			// No namespace so we'll just use the default path

			$path = $this->path;
		}
		else
		{
			// The file is namespaced so we'll use the namespace path

			[$namespace, $file] = explode('::', $file, 2);

			if(!isset($this->namespaces[$namespace]))
			{
				throw new RuntimeException(vsprintf('The [ %s ] namespace does not exist.', [$namespace]));
			}

			$path = $this->namespaces[$namespace];
		}

		// Append suffix to path if needed

		if($suffix !== null)
		{
			$path .= DIRECTORY_SEPARATOR . $suffix;
		}

		// Return full path to file

		return $path . DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR, $file) . ($extension ?? $this->extension);
	}
191212
public function load($file)
    {
        if (!is_file($file)) {
            if (!$found = $this->findFile($file)) {
                $paths = array_values(iterator_to_array(clone $this->paths));

                $paths = implode(" / ", $paths);

                throw new \RuntimeException(sprintf('Language file: %s not found. Paths in queue: %s', $file, $paths));
            }
        } else {
            $found = $file;
        }

        return file_get_contents($found);
    }
119285
public static function getFileContents(string $file) : string
    {
        if (!is_readable($file)) {
            throw new RuntimeException(sprintf(
                'File "%s" does not exist or is not readable',
                $file
            ));
        }

        $contents = file_get_contents($file);
        if ($contents === false) {
            throw new RuntimeException(sprintf(
                'Could not read from existing and readable file "%s"',
                $file
            ));
        }

        return $contents;
    }
901175
public function fromFile($file)
    {
        /** @var \DOMDocument $xmlDomDocument */
        if (!$xmlDomDocument = XmlUtils::loadFile($file, __DIR__ . '/../xsd/enums.xsd')) {
            throw new \RuntimeException(sprintf(
                'Invalid enums xml file "%s".',
                $file
            ));
        }

        /** @var array $xmlData */
        if (!$xmlData = XmlUtils::convertDomElementToArray($xmlDomDocument->firstChild)) {
            throw new \RuntimeException('Invalid enum DOM object.');
        }

        $namespace = $xmlData['enums']['namespace'];

        $filePath = substr($file, 0, -strlen(basename($file)) - 1);
        $enumsPath = str_replace(':', '/', $namespace);

        // invalid enum file location
        if (substr($filePath, -strlen($enumsPath)) !== $enumsPath) {
            throw new \RuntimeException(sprintf(
                'Invalid enums xml directory "%s". Expected sub-directory "%s".',
                $filePath,
                $enumsPath
            ));
        }

        // get language options
        $languages = [];
        foreach ($xmlData['enums'] as $key => $value) {
            if (substr($key, -8) == '-options') {
                $languages[$key] = $value;
            }
        }

        $enums = [];

        if (isset($xmlData['enums']['enum'])) {
            $xmlData['enums']['enum'] = $this->fixArray($xmlData['enums']['enum'], 'name');

            foreach ($xmlData['enums']['enum'] as $enum) {
                $enumId = EnumId::fromString(sprintf('%s:%s', $namespace, $enum['name']));

                // duplicate schema
                if (array_key_exists($enumId->toString(), $enums)) {
                    throw new \RuntimeException(sprintf(
                        'Duplicate enum "%s" in file "%s".',
                        $enumId->toString(),
                        $file
                    ));
                }

                $enums[] = $this->parse(array_merge($enum, $languages, ['namespace' => $namespace]));
            }
        }

        return $enums;
    }