Loader.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Part of XML_XRD
  4. *
  5. * PHP version 5
  6. *
  7. * @category XML
  8. * @package XML_XRD
  9. * @author Christian Weiske <cweiske@php.net>
  10. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  11. * @link http://pear.php.net/package/XML_XRD
  12. */
  13. require_once 'XML/XRD/Loader/Exception.php';
  14. /**
  15. * File/string loading dispatcher.
  16. * Loads the correct loader for the type of XRD file (XML or JSON).
  17. * Also provides type auto-detection.
  18. *
  19. * @category XML
  20. * @package XML_XRD
  21. * @author Christian Weiske <cweiske@php.net>
  22. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  23. * @version Release: @package_version@
  24. * @link http://pear.php.net/package/XML_XRD
  25. */
  26. class XML_XRD_Loader
  27. {
  28. public function __construct(XML_XRD $xrd)
  29. {
  30. $this->xrd = $xrd;
  31. }
  32. /**
  33. * Loads the contents of the given file.
  34. *
  35. * Note: Only use file type auto-detection for local files.
  36. * Do not use it on remote files as the file gets requested several times.
  37. *
  38. * @param string $file Path to an XRD file
  39. * @param string $type File type: xml or json, NULL for auto-detection
  40. *
  41. * @return void
  42. *
  43. * @throws XML_XRD_Loader_Exception When the file is invalid or cannot be
  44. * loaded
  45. */
  46. public function loadFile($file, $type = null)
  47. {
  48. if ($type === null) {
  49. $type = $this->detectTypeFromFile($file);
  50. }
  51. $loader = $this->getLoader($type);
  52. $loader->loadFile($file);
  53. }
  54. /**
  55. * Loads the contents of the given string
  56. *
  57. * @param string $str XRD string
  58. * @param string $type File type: xml or json, NULL for auto-detection
  59. *
  60. * @return void
  61. *
  62. * @throws XML_XRD_Loader_Exception When the string is invalid or cannot be
  63. * loaded
  64. */
  65. public function loadString($str, $type = null)
  66. {
  67. if ($type === null) {
  68. $type = $this->detectTypeFromString($str);
  69. }
  70. $loader = $this->getLoader($type);
  71. $loader->loadString($str);
  72. }
  73. /**
  74. * Creates a XRD loader object for the given type
  75. *
  76. * @param string $type File type: xml or json
  77. *
  78. * @return XML_XRD_Loader
  79. */
  80. protected function getLoader($type)
  81. {
  82. $class = 'XML_XRD_Loader_' . strtoupper($type);
  83. $file = str_replace('_', '/', $class) . '.php';
  84. include_once $file;
  85. if (class_exists($class)) {
  86. return new $class($this->xrd);
  87. }
  88. throw new XML_XRD_Loader_Exception(
  89. 'No loader for XRD type "' . $type . '"',
  90. XML_XRD_Loader_Exception::NO_LOADER
  91. );
  92. }
  93. /**
  94. * Tries to detect the file type (xml or json) from the file content
  95. *
  96. * @param string $file File name to check
  97. *
  98. * @return string File type ('xml' or 'json')
  99. *
  100. * @throws XML_XRD_Loader_Exception When opening the file fails.
  101. */
  102. public function detectTypeFromFile($file)
  103. {
  104. if (!file_exists($file)) {
  105. throw new XML_XRD_Loader_Exception(
  106. 'Error loading XRD file: File does not exist',
  107. XML_XRD_Loader_Exception::OPEN_FILE
  108. );
  109. }
  110. $handle = fopen($file, 'r');
  111. if (!$handle) {
  112. throw new XML_XRD_Loader_Exception(
  113. 'Cannot open file to determine type',
  114. XML_XRD_Loader_Exception::OPEN_FILE
  115. );
  116. }
  117. $str = (string)fgets($handle, 10);
  118. fclose($handle);
  119. return $this->detectTypeFromString($str);
  120. }
  121. /**
  122. * Tries to detect the file type from the content of the file
  123. *
  124. * @param string $str Content of XRD file
  125. *
  126. * @return string File type ('xml' or 'json')
  127. *
  128. * @throws XML_XRD_Loader_Exception When the type cannot be detected
  129. */
  130. public function detectTypeFromString($str)
  131. {
  132. if (substr($str, 0, 1) == '{') {
  133. return 'json';
  134. } else if (substr($str, 0, 5) == '<?xml') {
  135. return 'xml';
  136. }
  137. throw new XML_XRD_Loader_Exception(
  138. 'Detecting file type failed',
  139. XML_XRD_Loader_Exception::DETECT_TYPE
  140. );
  141. }
  142. }
  143. ?>