PageRenderTime 132ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/OData Producer for PHP/library/ODataProducer/Common/ServiceConfig.php

#
PHP | 92 lines | 48 code | 5 blank | 39 comment | 14 complexity | bb698214a4dc34aca40cef10deed5bf8 MD5 | raw file
  1. <?php
  2. /**
  3. * Defines the ServiceConfig class
  4. *
  5. * PHP version 5.3
  6. *
  7. * @category ODataProducer
  8. * @package ODataProducer_Common
  9. * @author Anu Chandy <odataphpproducer_alias@microsoft.com>
  10. * @author Neelesh Vijaivargia <odataphpproducer_alias@microsoft.com>
  11. * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
  12. * @license New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
  13. * @version SVN: 1.0
  14. * @link http://odataphpproducer.codeplex.com
  15. *
  16. */
  17. namespace ODataProducer\Common;
  18. /**
  19. * Helper class to read and velidate the service config file
  20. *
  21. * @category ODataProducer
  22. * @package ODataProducer_Common
  23. * @author Anu Chandy <odataphpproducer_alias@microsoft.com>
  24. * @author Neelesh Vijaivargia <odataphpproducer_alias@microsoft.com>
  25. * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
  26. * @license New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
  27. * @version Release: 1.0
  28. * @link http://odataphpproducer.codeplex.com
  29. */
  30. class ServiceConfig
  31. {
  32. /**
  33. * Read and validates the configuration for the given service.
  34. *
  35. * @param string $serviceName requested service name
  36. * @param string &$serviceInfo service info
  37. * @param string $configFile config filename for all the services
  38. *
  39. * @return void
  40. *
  41. * @throws ODataException If configuration file
  42. * does not exists or malformed.
  43. */
  44. public static function validateAndGetsServiceInfo($serviceName, &$serviceInfo, $configFile = '../../../services/service.config.xml')
  45. {
  46. $xml = simplexml_load_file(dirname(__FILE__)."/".$configFile, null, LIBXML_NOCDATA);
  47. if (!$xml) {
  48. ODataException::createInternalServerError('service.config file is not in proper XML format');
  49. }
  50. if (count($xml->children()) != 1) {
  51. ODataException::createInternalServerError("Config file has more than one root entries");
  52. }
  53. $pathResult = $xml->xpath("/configuration/services/service[@name=\"$serviceName\"]");
  54. if (empty($pathResult)) {
  55. ODataException::createBadRequestError("No configuration info found for $serviceName");
  56. }
  57. $pathResult = $xml->xpath("/configuration/services/service[@name=\"$serviceName\"]/path");
  58. if (empty($pathResult)) {
  59. ODataException::createInternalServerError("One of the mendatory configuration info were missing in the config file");
  60. } else {
  61. $serviceInfo['SERVICE_PATH'] = strval($pathResult[0]);
  62. if (empty($serviceInfo['SERVICE_PATH'])) {
  63. ODataException::createInternalServerError("One of the mendatory configuration info were missing in the config file or config file is mail formed");
  64. }
  65. }
  66. unset($pathResult);
  67. $pathResult = $xml->xpath("/configuration/services/service[@name=\"$serviceName\"]/classname");
  68. if (empty($pathResult)) {
  69. ODataException::createInternalServerError("One of the mendatory configuration info were missing in the config file");
  70. } else {
  71. $serviceInfo['SERVICE_CLASS'] = strval($pathResult[0]);
  72. if (empty($serviceInfo['SERVICE_CLASS'])) {
  73. ODataException::createInternalServerError("One of the mendatory configuration info were missing in the config file or config file is mail formed");
  74. }
  75. }
  76. unset($pathResult);
  77. $pathResult = $xml->xpath("/configuration/services/service[@name=\"$serviceName\"]/baseURL");
  78. if (empty($pathResult)) {
  79. ODataException::createInternalServerError("One of the mendatory configuration info were missing in the config file");
  80. } else {
  81. $serviceInfo['SERVICE_BASEURL'] = strval($pathResult[0]);
  82. if (empty($serviceInfo['SERVICE_BASEURL'])) {
  83. ODataException::createInternalServerError("One of the mendatory configuration info were missing in the config file or config file is mail formed");
  84. }
  85. }
  86. }
  87. }