ServiceFactory.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright 2012-2017 Anthon Pang. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * @package WebDriver
  18. *
  19. * @author Anthon Pang <apang@softwaredevelopment.ca>
  20. */
  21. namespace WebDriver;
  22. /**
  23. * WebDriver\ServiceFactory class
  24. *
  25. * A service factory
  26. *
  27. * @package WebDriver
  28. */
  29. final class ServiceFactory
  30. {
  31. /**
  32. * singleton
  33. *
  34. * @var \WebDriver\ServiceFactory
  35. */
  36. private static $instance;
  37. /**
  38. * @var array
  39. */
  40. protected $services;
  41. /**
  42. * @var array
  43. */
  44. protected $serviceClasses;
  45. /**
  46. * Private constructor
  47. */
  48. private function __construct()
  49. {
  50. $this->services = array();
  51. $this->serviceClasses = array(
  52. 'service.curl' => '\\WebDriver\\Service\\CurlService',
  53. );
  54. }
  55. /**
  56. * Get singleton instance
  57. *
  58. * @return \WebDriver\ServiceFactory
  59. */
  60. public static function getInstance()
  61. {
  62. if (!self::$instance) {
  63. self::$instance = new self;
  64. }
  65. return self::$instance;
  66. }
  67. /**
  68. * Get service
  69. *
  70. * @param string $serviceName Name of service
  71. *
  72. * @return object
  73. */
  74. public function getService($serviceName)
  75. {
  76. if (!isset($this->services[$serviceName])) {
  77. $className = $this->serviceClasses[$serviceName];
  78. $this->services[$serviceName] = new $className;
  79. }
  80. return $this->services[$serviceName];
  81. }
  82. /**
  83. * Set service
  84. *
  85. * @param string $serviceName Name of service
  86. * @param object $service Service instance
  87. */
  88. public function setService($serviceName, $service)
  89. {
  90. $this->services[$serviceName] = $service;
  91. }
  92. /**
  93. * Override default service class
  94. *
  95. * @param string $serviceName Name of service
  96. * @param string $className Name of service class
  97. */
  98. public function setServiceClass($serviceName, $className)
  99. {
  100. if (substr($className, 0, 1) !== '\\') {
  101. $className = '\\' . $className;
  102. }
  103. $this->serviceClasses[$serviceName] = $className;
  104. $this->services[$serviceName] = null;
  105. }
  106. }