Source for file Services.class.php

Documentation is available at Services.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."services/Services.abstract.php");
  4. require_once(HARMONI."utilities/FieldSetValidator/rules/inc.php");
  5.  
  6. /**
  7. * The Services class handles starting, stopping, registering, etc of any available services.
  8. * @version $Id: Services.class.php,v 1.24 2007/09/10 20:52:31 adamfranco Exp $
  9. *
  10. * @package harmoni.services
  11. *
  12. * @copyright Copyright &copy; 2005, Middlebury College
  13. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  14. *
  15. * @version $Id: Services.class.php,v 1.24 2007/09/10 20:52:31 adamfranco Exp $
  16. */
  17. class Services extends ServicesAbstract {
  18. /**
  19. * A private array of services: array("key1"=>ServiceObject1, ...)
  20. *
  21. * @var array $_services The array of services.
  22. ***/
  23. var $_services;
  24. /**
  25. * A private array of registerd services: array("key1"=>classname, ...)
  26. *
  27. * @var array $_registeredServices The array of registered services.
  28. ***/
  29. var $_registeredServices;
  30. /**
  31. * The constructor.
  32. * @access public
  33. * @return void
  34. ***/
  35. function Services() {
  36. $this->_services = array();
  37. $this->_aliases = array();
  38. }
  39. /**
  40. * @return array
  41. * Returns a backtrace string to prepend onto error messages.
  42. */
  43. function _getBacktrace() {
  44. $bt = debug_backtrace();
  45. $str = "<pre>".print_r($bt,true) . "</pre><br /><br />";
  46. // $str = $bt[2]['file'].":".$bt[2]['line']."<br /><br />";
  47. return $str;
  48. }
  49. /**
  50. * Registers a new service named $name. Upon starting, a new class of type $class will be instantiated.
  51. * @param string $name The reference name of the service.
  52. * @param string $class The class name to be instantiated.
  53. * @access public
  54. * @return boolean True on success. False if service is registered and running or an error occurs.
  55. ***/
  56. function register( $name, $class ) {
  57. // if its registered and started, throw fatal error return false.
  58. if ($this->running($name)) {
  59. // if we have the error Handler, throw a pretty error with that,
  60. // otherwise, use the die() function.
  61. throwError(new Error("Services::registerService('$name') - can not
  62. register service '$name' because it is already registered AND
  63. running.", "Services", 1));
  64. return false;
  65. }
  66. if (!class_exists($class)) {
  67. // if we have the error Handler, throw a pretty error with that,
  68. // otherwise, use the die() function.
  69. throwError(new Error("Services::registerService('$name') - can not
  70. register service '$name' because the class '$class' does not exist!"
  71. , "Services", 1));
  72. return false;
  73. }
  74.  
  75. $this->_registeredServices[$name] = $class;
  76. return true;
  77. }
  78. /**
  79. * Register's an object as a service.
  80. * @param string $name The name of the new service.
  81. * @param ref object $object The object.
  82. * @access public
  83. * @return void
  84. ***/
  85. function registerObject($name ,$object) {
  86. $class = get_class($object);
  87.  
  88. $this->_registeredServices[$name] = $class;
  89. $this->_services[$name] =$object;
  90. return true;
  91. }
  92. /**
  93. * Create an alias from one service name to another.
  94. *
  95. * @param string $registeredName
  96. * @param string $aliasName
  97. * @return void
  98. * @access public
  99. * @since 3/24/05
  100. */
  101. function createAlias ($registeredName, $aliasName) {
  102. if (!$this->_registeredServices[$registeredName]) {
  103. // if we have the error Handler, throw a pretty error with that,
  104. // otherwise, use the die() function.
  105. throwError(new Error("Services::createAlias('$registeredName', '$aliasName') - failed because an '$registeredName' wasn't registered yet."
  106. , "Services", 1));
  107. }
  108. if (isset($this->_aliases[$aliasName]) || isset($this->_registeredServices[$aliasName])) {
  109. // if we have the error Handler, throw a pretty error with that,
  110. // otherwise, use the die() function.
  111. throwError(new Error("Services::createAlias('$registeredName', '$aliasName') - failed because an '$aliasName' is already in use."
  112. , "Services", 1));
  113. }
  114. $this->_aliases[$aliasName] = $registeredName;
  115. }
  116. /**
  117. * Attempts to stop all running services.
  118. * @access public
  119. * @return void
  120. * @deprecated 2005/04/04 Use {@link startManagerAsService()} and {@link getService()} instead.
  121. ***/
  122. function stopAll() {
  123. foreach (array_keys($this->_registeredServices) as $name)
  124. $this->stop($name);
  125. }
  126. /**
  127. * Returns the service object associated with reference name $name.
  128. * Returns the service object associated with reference name $name.
  129. * @param string $name The reference name of the service.
  130. * @access public
  131. * @return object Object|falseThe service object.
  132. ***/
  133. function get( $name ) {
  134. $name = $this->_getServiceName($name);
  135. if (!$this->running($name)) {
  136. // if we have the error Handler, throw a pretty error with that,
  137. // otherwise, use the die() function.
  138. throwError(new Error("Services::getService('$name') - can not get service
  139. '$name' because it is not yet started.", "Services", 1));
  140. return false;
  141. }
  142. return $this->_services[$name];
  143. }
  144. /**
  145. * Attempts to start the service referenced by $name.
  146. * @param string $name The service name.
  147. * @param optional mixed $args,... Optional arguments to pass to the constructor of the service class.
  148. * @access public
  149. * @static
  150. * @return boolean True on success.
  151. * @deprecated 2005/04/04 Use {@link startManagerAsService()} and {@link getService()} instead.
  152. ***/
  153. function start( $name, $args=null ) {
  154. $name = $this->_getServiceName($name);
  155. // make sure that the service is not currently running.
  156. if ($this->running($name))
  157. return true;
  158. $classname = $this->_registeredServices[$name];
  159. // print "<pre>".print_r($this->_registeredServices, TRUE)."</pre><br />$classname<br />";
  160. $argList=array();
  161. if (func_num_args() > 1) {
  162. for ($i=1; $i<func_num_args(); $i++) {
  163. $var = "arg$i";
  164. $$var = func_get_arg($i);
  165. $argList[] = '$arg'.$i;
  166. }
  167. }
  168. $str = '$this->_services[$name] = new '.$classname.'('.implode(', ', $argList).');';
  169. // print "<br />$str";
  170.  
  171. if (!$classname)
  172. throwError(new Error("Services::startService('$name') - could not
  173. start service - A classname was not registered for this service
  174. correctly", "Services", 1));
  175. eval($str);
  176. // $this->_services[$name] = new $classname;
  177. // make sure the service was instantiated properly
  178. if (!is_object($this->_services[$name])
  179. || strtolower(get_class($this->_services[$name])) != strtolower($classname))
  180. {
  181. // if we have the error Handler, throw a pretty error with that,
  182. // otherwise, use the die() function.
  183. throwError(new Error("Services::startService('$name') - could not
  184. start service - the object of class $classname was not instantiated
  185. correctly", "Services", 1));
  186. return false;
  187. }
  188. return true;
  189. }
  190. /**
  191. * Attempts to start the manager referenced by $name. The manager must
  192. * extend OsidManager And assign its context and configuration.
  193. *
  194. * @param string $name
  195. * @param object OsidContext $context
  196. * @param object Properties $configuration
  197. * @return boolean true on success
  198. * @access public
  199. * @since 3/24/05
  200. */
  201. function startManager ( $name, $context, $configuration ) {
  202. $name = $this->_getServiceName($name);
  203. // make sure that the service is not currently running.
  204. if ($this->running($name))
  205. return true;
  206. $classname = $this->_registeredServices[$name];
  207. if (!$classname)
  208. throwError(new Error("Services::startService('$name') - could not
  209. start service - A classname was not registered for this service
  210. correctly", "Services", 1));
  211. $this->_services[$name] = new $classname;
  212. $this->_services[$name]->assignOsidContext($context);
  213. $this->_services[$name]->assignConfiguration($configuration);
  214. // make sure the service was instantiated properly
  215. if (!is_object($this->_services[$name])
  216. || strtolower(get_class($this->_services[$name])) != strtolower($classname))
  217. {
  218. // if we have the error Handler, throw a pretty error with that,
  219. // otherwise, use the die() function.
  220. throwError(new Error("Services::startService('$name') - could not
  221. start service - the object of class $classname was not instantiated
  222. correctly", "Services", 1));
  223. return false;
  224. }
  225. return true;
  226. }
  227. /**
  228. * Cycles through all registered services and attempts to start them.
  229. * @access public
  230. * @return void
  231. * @deprecated 2005/04/04 Use {@link startManagerAsService()} and {@link getService()} instead.
  232. ***/
  233. function startAll() {
  234. foreach (array_keys($this->_registeredServices) as $service) {
  235. if (!$this->running($service)) $this->start($service);
  236. }
  237. }
  238. /**
  239. * Attempts to stop the service reference by $name.
  240. * @param string $name The service name.
  241. * @access public
  242. * @return boolean True on success.
  243. * @deprecated 2005/04/04 Use {@link startManagerAsService()} and {@link getService()} instead.
  244. ***/
  245. function stop( $name ) {
  246. $name = $this->_getServiceName($name);
  247. unset($this->_services[$name]);
  248. return true;
  249. }
  250. /**
  251. * Attempts to restart the service reference by $name.
  252. * @param string $name The service name.
  253. * @access public
  254. * @return boolean True on success.
  255. * @deprecated 2005/04/04 Use {@link startManagerAsService()} and {@link getService()} instead.
  256. ***/
  257. function restart( $name ) {
  258. if (!$this->stop($name))
  259. return false;
  260. if (!$this->start($name))
  261. return false;
  262. return true;
  263. }
  264. /**
  265. * Checks if the service referenced by $name is available for use.
  266. * @param string $name The service name.*
  267. * @access public
  268. * @return boolean True if the service is available, false otherwise.
  269. ***/
  270. function available( $name ) {
  271. $name = $this->_getServiceName($name);
  272. return (isset($this->_registeredServices[$name]))?true:false;
  273. }
  274. /**
  275. * Checks if the service referenced by $name has been started.
  276. * @access public
  277. * @param string $name The service name.
  278. * @return boolean True if the service is running, false otherwise.
  279. ***/
  280. function running ( $name ) {
  281. $name = $this->_getServiceName($name);
  282. if (isset($this->_services[$name]) && is_object($this->_services[$name])
  283. && strtolower(get_class($this->_services[$name])) == strtolower($this->_registeredServices[$name]))
  284. {
  285. return true;
  286. }
  287. return false;
  288. }
  289. /**
  290. * Stops all the services -- used internally by PHP.
  291. * @access private
  292. * @return void
  293. ***/
  294. function __sleep() {
  295. foreach ($this->_services as $service) $this->stop($service);
  296. }
  297. /**
  298. * Get service name. Gets the name of a service or its alias.
  299. *
  300. * @param string $name
  301. * @return string
  302. * @access public
  303. * @since 3/24/05
  304. */
  305. function _getServiceName ($name) {
  306. if (isset($this->_aliases[$name]))
  307. return $this->_aliases[$name];
  308. else
  309. return $name;
  310. }
  311. }
  312. ?>

Documentation generated on Wed, 19 Sep 2007 10:26:26 -0400 by phpDocumentor 1.3.0RC3