1: <?php
2:
3: namespace Alpha\Util\Cache;
4:
5: use Alpha\Exception\IllegalArguementException;
6: use Alpha\Util\Logging\Logger;
7:
8: /**
9: * A factory for creating cache provider implementations that implement the
10: * CacheProviderInterface interface.
11: *
12: * @since 1.1
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 CacheProviderFactory
52: {
53: /**
54: * Trace logger.
55: *
56: * @var Alpha\Util\Logging\Logger
57: *
58: * @since 1.1
59: */
60: private static $logger = null;
61:
62: /**
63: * A static method that attempts to return a CacheProviderInterface instance
64: * based on the name of the provider class supplied.
65: *
66: * @param $providerName The class name of the provider class (fully qualified).
67: *
68: * @throws Alpha\Exception\IllegalArguementException
69: *
70: * @return Alpha\Util\Cache\CacheProviderInterface
71: *
72: * @since 1.1
73: */
74: public static function getInstance($providerName)
75: {
76: if (self::$logger == null) {
77: self::$logger = new Logger('CacheProviderFactory');
78: }
79:
80: self::$logger->debug('>>getInstance(providerName=['.$providerName.'])');
81:
82: if (class_exists($providerName)) {
83: $instance = new $providerName();
84:
85: if (!$instance instanceof CacheProviderInterface) {
86: throw new IllegalArguementException('The class ['.$providerName.'] does not implement the expected CacheProviderInterface intwerface!');
87: }
88:
89: self::$logger->debug('<<getInstance: [Object '.$providerName.']');
90:
91: return $instance;
92: } else {
93: throw new IllegalArguementException('The class ['.$providerName.'] is not defined anywhere!');
94: }
95:
96: self::$logger->debug('<<getInstance');
97: }
98: }
99: