1: <?php
2:
3: namespace Alpha\Util\Search;
4:
5: use Alpha\Exception\IllegalArguementException;
6: use Alpha\Util\Logging\Logger;
7:
8: /**
9: * A factory for creating search provider implementations that implement the
10: * SearchProviderInterface interface.
11: *
12: * @since 1.2.3
13: *
14: * @author John Collins <dev@alphaframework.org>
15: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
16: * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
17: * All rights reserved.
18: *
19: * <pre>
20: * Redistribution and use in source and binary forms, with or
21: * without modification, are permitted provided that the
22: * following conditions are met:
23: *
24: * * Redistributions of source code must retain the above
25: * copyright notice, this list of conditions and the
26: * following disclaimer.
27: * * Redistributions in binary form must reproduce the above
28: * copyright notice, this list of conditions and the
29: * following disclaimer in the documentation and/or other
30: * materials provided with the distribution.
31: * * Neither the name of the Alpha Framework nor the names
32: * of its contributors may be used to endorse or promote
33: * products derived from this software without specific
34: * prior written permission.
35: *
36: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
37: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
38: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
39: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
41: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
47: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
48: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49: * </pre>
50: */
51: class SearchProviderFactory
52: {
53: /**
54: * Trace logger.
55: *
56: * @var Alpha\Util\Logging\Logger;
57: *
58: * @since 1.2.3
59: */
60: private static $logger = null;
61:
62: /**
63: * A static method that attempts to return a SearchProviderInterface instance
64: * based on the name of the provider class supplied.
65: *
66: * @param $providerName The class name of the provider class, should be fully-qualified.
67: *
68: * @throws Alpha\Exception\IllegalArguementException;
69: *
70: * @return Alpha\Util\Search\SearchProviderInterface
71: *
72: * @since 1.2.3
73: */
74: public static function getInstance($providerName)
75: {
76: if (self::$logger == null) {
77: self::$logger = new Logger('SearchProviderFactory');
78: }
79:
80: self::$logger->debug('>>getInstance(providerName=['.$providerName.'])');
81:
82: global $config;
83:
84: if (class_exists($providerName)) {
85: $instance = new $providerName();
86:
87: if (!$instance instanceof SearchProviderInterface) {
88: throw new IllegalArguementException('The class ['.$providerName.'] does not implement the expected SearchProviderInterface interface!');
89: }
90:
91: self::$logger->debug('<<getInstance: [Object '.$providerName.']');
92:
93: return $instance;
94: } else {
95: throw new IllegalArguementException('The class ['.$providerName.'] is not defined anywhere!');
96: }
97:
98: self::$logger->debug('<<getInstance');
99: }
100: }
101: