1: <?php
2:
3: namespace Alpha\Util\Logging;
4:
5: use Alpha\Exception\IllegalArguementException;
6: use Alpha\Util\Config\ConfigProvider;
7:
8: /**
9: * A factory for creating log provider implementations that implement the
10: * LogProviderInterface interface.
11: *
12: * @since 2.0
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 LogProviderFactory
52: {
53: /**
54: * A static method that attempts to return a LogProviderInterface instance
55: * based on the name of the provider class supplied.
56: *
57: * @param $providerName The class name of the provider class (fully qualified).
58: *
59: * @throws Alpha\Exception\IllegalArguementException
60: *
61: * @return Alpha\Util\Logging\LogProviderInterface
62: *
63: * @since 2.0
64: */
65: public static function getInstance($providerName)
66: {
67: $config = ConfigProvider::getInstance();
68:
69: if (class_exists($providerName)) {
70: $instance = new $providerName();
71:
72: if (!$instance instanceof LogProviderInterface) {
73: throw new IllegalArguementException('The class ['.$providerName.'] does not implement the expected LogProviderInterface intwerface!');
74: }
75:
76: $instance->setMaxSize($config->get('app.log.file.max.size'));
77:
78: return $instance;
79: } else {
80: throw new IllegalArguementException('The class ['.$providerName.'] is not defined anywhere!');
81: }
82: }
83: }
84: