1: <?php
2: /**
3: * Pastie_Driver:: defines an API for implementing storage backends for
4: * Pastie.
5: *
6: * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (GPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/gpl.
10: *
11: * @author Ben Klang <ben@alkaloid.net>
12: * @package Pastie
13: */
14: class Pastie_Driver
15: {
16: /**
17: * Attempts to return a concrete instance based on $driver.
18: *
19: * @param string $driver The type of the concrete subclass to return.
20: * The class name is based on the storage driver
21: * ($driver). The code is dynamically included.
22: *
23: * @param array $params A hash containing any additional configuration
24: * or connection parameters a subclass might need.
25: *
26: * @return Pastie_Driver The newly created concrete instance.
27: * @throws Horde_Exception
28: */
29: static public function factory($driver = null, $params = null)
30: {
31: if (is_null($driver)) {
32: $driver = $GLOBALS['conf']['storage']['params']['driver'];
33: }
34:
35: if (is_null($params)) {
36: $params = Horde::getDriverConfig('storage', $driver);
37: }
38:
39: $driver = ucfirst(basename($driver));
40: $class = 'Pastie_Driver_' . $driver;
41: if (class_exists($class)) {
42: return new $class($params);
43: }
44:
45: throw new Horde_Exception('Could not find driver ' . $class);
46: }
47:
48: /**
49: * Attempts to return a reference to a concrete Pastie_Driver instance based
50: * on $driver.
51: *
52: * It will only create a new instance if no Pastie_Driver instance with the
53: * same parameters currently exists.
54: *
55: * This should be used if multiple storage sources are required.
56: *
57: * This method must be invoked as: $var = &Pastie_Driver::singleton()
58: *
59: * @param string $notepad The name of the current notepad.
60: *
61: * @param string $driver The type of concrete Pastie_Driver subclass
62: * to return. The is based on the storage
63: * driver ($driver). The code is dynamically
64: * included.
65: *
66: * @param array $params (optional) A hash containing any additional
67: * configuration or connection parameters a
68: * subclass might need.
69: *
70: * @return mixed The created concrete Pastie_Driver instance, or false
71: * on error.
72: */
73: function &singleton($bin = '', $driver = null, $params = null)
74: {
75: static $instances = array();
76: if (is_null($driver)) {
77: $driver = $GLOBALS['conf']['storage']['driver'];
78: }
79:
80: if (is_null($params)) {
81: $params = Horde::getDriverConfig('storage', $driver);
82: }
83:
84: $signature = serialize(array($notepad, $driver, $params));
85: if (!isset($instances[$signature])) {
86: $instances[$signature] = &Pastie_Driver::factory($notepad, $driver, $params);
87: }
88:
89: return $instances[$signature];
90: }
91:
92: }
93: