bf03708c7d065995e3e57ff2bc441dd3a8ca59cc
[yaffs-website] / vendor / instaclick / php-webdriver / lib / WebDriver / ServiceFactory.php
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
22 namespace WebDriver;
23
24 /**
25  * WebDriver\ServiceFactory class
26  *
27  * A service factory
28  *
29  * @package WebDriver
30  */
31 final class ServiceFactory
32 {
33     /**
34      * singleton
35      *
36      * @var \WebDriver\ServiceFactory
37      */
38     private static $instance;
39
40     /**
41      * @var array
42      */
43     protected $services;
44
45     /**
46      * @var array
47      */
48     protected $serviceClasses;
49
50     /**
51      * Private constructor
52      */
53     private function __construct()
54     {
55         $this->services = array();
56
57         $this->serviceClasses = array(
58             'service.curl' => '\\WebDriver\\Service\\CurlService',
59         );
60     }
61
62     /**
63      * Get singleton instance
64      *
65      * @return \WebDriver\ServiceFactory
66      */
67     public static function getInstance()
68     {
69         if (!self::$instance) {
70             self::$instance = new self;
71         }
72
73         return self::$instance;
74     }
75
76     /**
77      * Get service
78      *
79      * @param string $serviceName Name of service
80      *
81      * @return object
82      */
83     public function getService($serviceName)
84     {
85         if (!isset($this->services[$serviceName])) {
86             $className = $this->serviceClasses[$serviceName];
87
88             $this->services[$serviceName] = new $className;
89         }
90
91         return $this->services[$serviceName];
92     }
93
94     /**
95      * Set service
96      *
97      * @param string $serviceName Name of service
98      * @param object $service     Service instance
99      */
100     public function setService($serviceName, $service)
101     {
102         $this->services[$serviceName] = $service;
103     }
104
105     /**
106      * Override default service class
107      *
108      * @param string $serviceName Name of service
109      * @param string $className   Name of service class
110      */
111     public function setServiceClass($serviceName, $className)
112     {
113         if (substr($className, 0, 1) !== '\\') {
114             $className = '\\' . $className;
115         }
116
117         $this->serviceClasses[$serviceName] = $className;
118         $this->services[$serviceName] = null;
119     }
120 }