BxDolService.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolRequest');
  10. /**
  11. * Service calls to modules' methods.
  12. *
  13. * The class has one static method is needed to make service calls
  14. * to module's methods from the Dolphin's core or the other modules.
  15. *
  16. *
  17. * Example of usage:
  18. * @code
  19. * BxDolService::call('payment', 'get_add_to_cart_link', array($iVendorId, $mixedModuleId, $iItemId, $iItemCount));
  20. * @endcode
  21. *
  22. *
  23. * Memberships/ACL:
  24. * Doesn't depend on user's membership.
  25. *
  26. *
  27. * Alerts:
  28. * no alerts available
  29. *
  30. */
  31. class BxDolService extends BxDol
  32. {
  33. /**
  34. * Perform serice call
  35. * @param $mixed module name or module id
  36. * @param $sMethod service method name in format 'method_name', corresponding class metod is serviceMethodName
  37. * @param $aParams params to pass to service method
  38. * @param $sClass class to search for service method, by default it is main module class
  39. * @return service call result
  40. */
  41. public static function call($mixed, $sMethod, $aParams = array(), $sClass = 'Module')
  42. {
  43. bx_import('BxDolModuleQuery');
  44. $oDb = BxDolModuleQuery::getInstance();
  45. $aModule = array();
  46. if(is_string($mixed))
  47. $aModule = $oDb->getModuleByName($mixed);
  48. else
  49. $aModule = $oDb->getModuleById($mixed);
  50. return empty($aModule) ? '' : BxDolRequest::processAsService($aModule, $sMethod, $aParams, $sClass);
  51. }
  52. /**
  53. * Perform serice call by accepting serialized array of service call parameters:
  54. * @code
  55. * array (
  56. * 'module' => 'system', // module name
  57. * 'method' => 'test', // service method name
  58. * 'params' => array(), // array of parameters to pass to service method
  59. * 'class' => 'Module', // class to search service method in
  60. * )
  61. * @endcode
  62. *
  63. * @param $s serialized array of serice call
  64. * @param $aMarkers service method name in format 'method_name', corresponding class metod is serviceMethodName
  65. * @param $sReplaceIn params to pass to service method
  66. * @return service call result
  67. */
  68. public static function callSerialized($s, $aMarkers = array(), $sReplaceIn = 'params')
  69. {
  70. $a = @unserialize($s);
  71. if (false === $a || !is_array($a))
  72. return '';
  73. if (isset($a[$sReplaceIn]))
  74. $a[$sReplaceIn] = bx_replace_markers($a[$sReplaceIn], $aMarkers);
  75. return self::call($a['module'], $a['method'], isset($a['params']) ? $a['params'] : array(), isset($a['class']) ? $a['class'] : 'Module');
  76. }
  77. /**
  78. * Check if string is serialized array
  79. */
  80. public static function isSerializedService($s)
  81. {
  82. return preg_match('/^a:[\d+]:\{/', $s);
  83. }
  84. }
  85. /** @} */