/** * Create the XML object from a string, fFile or URL * * The `$default_namespace` will be used for any sort of methods calls, * member access or array access when the element or attribute name does * not include a `:`. * * @throws fValidationException When the source XML is invalid or does not exist * * @param fFile|string $source The source of the XML, either an fFile object, a string of XML, a file path or a URL * @param numeric $http_timeout The timeout to use in seconds when requesting an XML file from a URL * @return fXML */ public function __construct($source, $http_timeout = NULL) { // Prevent spitting out errors to we can throw exceptions $old_setting = libxml_use_internal_errors(TRUE); $exception_message = NULL; try { if ($source instanceof DOMElement) { $this->__dom = $source; $xml = TRUE; } elseif ($source instanceof fFile) { $xml = simplexml_load_file($source->getPath()); // This handles URLs specially by adding a reasonable timeout } elseif (preg_match('#^(?P<protocol>http(s)?)://#', $source, $matches)) { if ($http_timeout === NULL) { $http_timeout = ini_get('default_socket_timeout'); } // We use the appropriate protocol here so PHP can supress IIS https:// warnings $context = stream_context_create(array($matches['protocol'] => array('timeout' => $http_timeout))); // If the URL is not loaded in time, this supresses the file_get_contents() warning $old_level = error_reporting(error_reporting() & ~E_WARNING); $xml = file_get_contents($source, 0, $context); error_reporting($old_level); if (!$xml) { throw new fExpectedException('The URL specified, %s, could not be loaded', $source); } $xml = new SimpleXMLElement($xml); } else { $is_path = $source && !preg_match('#^\\s*<#', $source); $xml = new SimpleXMLElement($source, 0, $is_path); } } catch (Exception $e) { $exception_message = $e->getMessage(); $xml = FALSE; } // We want it to be clear when XML parsing issues occur if ($xml === FALSE) { $errors = libxml_get_errors(); foreach ($errors as $error) { $exception_message .= "\n" . $error->message; } // If internal errors were off before, turn them back off if (!$old_setting) { libxml_use_internal_errors(FALSE); } throw new fValidationException(str_replace('%', '%%', $exception_message)); } if (!$old_setting) { libxml_use_internal_errors(FALSE); } if (!$this->__dom) { $this->__dom = dom_import_simplexml($xml); } }
/** * Keeps backup copies of files so they can be restored if there is a rollback * * @internal * * @param fFile $file The file that is being written to * @return void */ public static function recordWrite($file) { self::$rollback_operations[] = array('action' => 'write', 'filename' => $file->getPath(), 'old_data' => file_get_contents($file->getPath())); }
public function testGetPath() { $file = new fFile('output/fFile/one.txt'); $this->assertEquals(str_replace('/', DIRECTORY_SEPARATOR, '/output/fFile/one.txt'), str_replace($_SERVER['DOCUMENT_ROOT'], '', $file->getPath())); }