1: <?php
2: /**
3: * A Horde_Injector based Horde_Vfs factory.
4: *
5: * @author Michael J. Rubinsky <mrubinsk@horde.org>
6: * @category Horde
7: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
8: * @package Core
9: */
10:
11: /**
12: * A Horde_Injector based Horde_Vfs factory.
13: *
14: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
15: *
16: * See the enclosed file COPYING for license information (LGPL). If you
17: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
18: *
19: * @author Michael J. Rubinsky <mrubinsk@horde.org>
20: * @category Horde
21: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
22: * @package Core
23: */
24: class Horde_Core_Factory_Vfs extends Horde_Core_Factory_Base
25: {
26: /**
27: * Instances.
28: *
29: * @var array
30: */
31: private $_instances = array();
32:
33: /**
34: * Returns the VFS instance.
35: *
36: * @param string $scope The vfs scope to return.
37: * @param array $params Configuration parameters. If specified this
38: * configuration is used instead of the configuration
39: * from conf.php.
40: *
41: * @return Horde_Vfs The VFS object.
42: */
43: public function create($scope = 'horde', $params = null)
44: {
45: if (empty($this->_instances[$scope])) {
46: if (!$params) {
47: $params = $this->getConfig($scope);
48: }
49:
50: $class = 'Horde_Vfs_' . basename(Horde_String::ucfirst($params['type']));
51: if (!class_exists($class)) {
52: throw new Horde_Exception('Class definition of ' . $class . ' not found.');
53: }
54:
55: $this->_instances[$scope] = new $class($params['params']);
56: }
57:
58: return $this->_instances[$scope];
59: }
60:
61: /**
62: * Returns the VFS driver parameters for the specified backend.
63: *
64: * @param string $name The VFS system name being used.
65: *
66: * @return array A hash with the VFS parameters; the VFS driver in 'type'
67: * and the connection parameters in 'params'.
68: * @throws Horde_Exception
69: */
70: public function getConfig($name = 'horde')
71: {
72: global $conf;
73:
74: if ($name !== 'horde' && !isset($conf[$name]['type'])) {
75: throw new Horde_Exception(Horde_Core_Translation::t("You must configure a VFS backend."));
76: }
77:
78: $vfs = ($name == 'horde' || $conf[$name]['type'] == 'horde')
79: ? $conf['vfs']
80: : $conf[$name];
81:
82: switch (Horde_String::lower($vfs['type'])) {
83: case 'sql':
84: case 'sqlfile':
85: case 'musql':
86: if ($name == 'horde' || $conf[$name]['type'] == 'horde') {
87: $vfs['params']['db'] = $this->_injector->getInstance('Horde_Db_Adapter');
88: } else {
89: $config = Horde::getDriverConfig('vfs', 'sql');
90: unset($config['umask'], $config['vfsroot']);
91: $vfs['params']['db'] = $this->_injector
92: ->getInstance('Horde_Core_Factory_Db')
93: ->create('horde', $config);
94: }
95: break;
96: }
97:
98: return $vfs;
99: }
100: }
101: