PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/include/resource/ResourceManager.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 154 lines | 68 code | 19 blank | 67 comment | 17 complexity | c5a246f1eab1d1af90b1a5cb1b1044f5 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. /*********************************************************************************
  3. * SugarCRM Community Edition is a customer relationship management program developed by
  4. * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  24. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  25. *
  26. * The interactive user interfaces in modified source and object code versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the "Powered by
  32. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  33. * technical reasons, the Appropriate Legal Notices must display the words
  34. * "Powered by SugarCRM".
  35. ********************************************************************************/
  36. /**
  37. * ResourceManager.php
  38. * This class is responsible for resource management of SQL queries, file usage, etc.
  39. */
  40. class ResourceManager {
  41. private static $instance;
  42. private $_observers = array();
  43. /**
  44. * The constructor; declared as private
  45. */
  46. private function ResourceManager() {
  47. }
  48. /**
  49. * getInstance
  50. * Singleton method to return static instance of ResourceManager
  51. * @return The static singleton ResourceManager instance
  52. */
  53. static public function getInstance(){
  54. if (!isset(self::$instance)) {
  55. self::$instance = new ResourceManager();
  56. } // if
  57. return self::$instance;
  58. }
  59. /**
  60. * setup
  61. * Handles determining the appropriate setup based on client type.
  62. * It will create a SoapResourceObserver instance if the $module parameter is set to
  63. * 'Soap'; otherwise, it will try to create a WebResourceObserver instance.
  64. * @param $module The module value used to create the corresponding observer
  65. * @return boolean value indicating whether or not an observer was successfully setup
  66. */
  67. public function setup($module) {
  68. //Check if config.php exists
  69. if(!file_exists('config.php') || empty($module)) {
  70. return false;
  71. }
  72. if($module == 'Soap') {
  73. require_once('include/resource/Observers/SoapResourceObserver.php');
  74. $observer = new SoapResourceObserver('Soap');
  75. } else {
  76. require_once('include/resource/Observers/WebResourceObserver.php');
  77. $observer = new WebResourceObserver($module);
  78. }
  79. //Load config
  80. if(!empty($observer->module)) {
  81. $limit = 0;
  82. if(isset($GLOBALS['sugar_config']['resource_management'])) {
  83. $res = $GLOBALS['sugar_config']['resource_management'];
  84. if(!empty($res['special_query_modules']) &&
  85. in_array($observer->module, $res['special_query_modules']) &&
  86. !empty($res['special_query_limit']) &&
  87. is_int($res['special_query_limit']) &&
  88. $res['special_query_limit'] > 0) {
  89. $limit = $res['special_query_limit'];
  90. } else if(!empty($res['default_limit']) && is_int($res['default_limit']) && $res['default_limit'] > 0) {
  91. $limit = $res['default_limit'];
  92. }
  93. } //if
  94. if($limit) {
  95. $db = DBManagerFactory::getInstance();
  96. $db->setQueryLimit($limit);
  97. $observer->setLimit($limit);
  98. $this->_observers[] = $observer;
  99. }
  100. return true;
  101. }
  102. return false;
  103. }
  104. /**
  105. * notifyObservers
  106. * This method notifies the registered observers with the provided message.
  107. * @param $msg Message from language file to notify observers with
  108. */
  109. public function notifyObservers($msg) {
  110. if(empty($this->_observers)) {
  111. return;
  112. }
  113. //Notify observers limit has been reached
  114. if(empty($GLOBALS['app_strings'])) {
  115. $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
  116. }
  117. $limitMsg = $GLOBALS['app_strings'][$msg];
  118. foreach( $this->_observers as $observer) {
  119. $limit = $observer->limit;
  120. $module = $observer->module;
  121. eval("\$limitMsg = \"$limitMsg\";");
  122. $GLOBALS['log']->fatal($limitMsg);
  123. $observer->notify($limitMsg);
  124. }
  125. }
  126. /*
  127. * getObservers
  128. * Returns the observer instances that have been setup for the ResourceManager instance
  129. * @return Array of ResourceObserver(s)
  130. */
  131. function getObservers() {
  132. return $this->_observers;
  133. }
  134. }
  135. ?>