1: <?php
2: /**
3: * Creates the Horde_Push content object.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Push
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://www.horde.org/components/Horde_Push
12: */
13:
14: /**
15: * Creates the Horde_Push content object.
16: *
17: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you did not
20: * receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Horde
23: * @package Push
24: * @author Gunnar Wrobel <wrobel@pardus.de>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://www.horde.org/components/Horde_Push
27: */
28: class Horde_Push_Factory_Push
29: {
30: /**
31: * Create the Horde_Push content element.
32: *
33: * @param array $arguments The command line arguments.
34: * @param array $options Command line options.
35: * @param array $conf The configuration.
36: *
37: * @return array The elements to be pushed.
38: */
39: public function create($arguments, $options, $conf)
40: {
41: if (empty($arguments)) {
42: return array(new Horde_Push());
43: }
44: $result = array();
45: foreach ($arguments as $argument) {
46: $result[] = $this->_parseArgument($argument, $conf);
47: }
48: return $result;
49: }
50:
51: /**
52: * Parse an argument into a Horde_Push element.
53: *
54: * @param string $argument A single command line argument.
55: * @param array $conf The configuration.
56: *
57: * @return Horde_Push The element to be pushed.
58: */
59: private function _parseArgument($argument, $conf)
60: {
61: $elements = explode('://', $argument);
62: if (isset($elements[0])) {
63: $argument = substr($argument, strlen($elements[0]) + 3);
64: if (empty($argument)) {
65: throw new Horde_Push_Exception('Missing file path!');
66: }
67: switch ($elements[0]) {
68: case 'kolab':
69: return $this->_parseKolab($argument, $conf);
70: case 'php':
71: return $this->_parsePhp($argument, $conf);
72: case 'yaml':
73: return $this->_parseYaml($argument, $conf);
74: case 'empty':
75: return new Horde_Push();
76: }
77: }
78: throw new Horde_Push_Exception(
79: sprintf('Invalid command line arguments: %s!', $argument)
80: );
81: }
82:
83: /**
84: * Parse the content of a Kolab object into a Horde_Push element.
85: *
86: * @param string $argument A single command line argument (without the scheme argument).
87: * @param array $conf The configuration.
88: *
89: * @return Horde_Push The element to be pushed.
90: */
91: private function _parseKolab($argument, $conf)
92: {
93: if (!interface_exists('Horde_Kolab_Storage')) {
94: throw new Horde_Push_Exception(
95: 'The Horde_Kolab_Storage package is missing!'
96: );
97: }
98: $elements = explode('/', $argument);
99: $id = array_pop($elements);
100: $path = join('/', $elements);
101: $factory = new Horde_Kolab_Storage_Factory($conf['kolab']);
102: return $this->_createFromData(
103: $factory->create()->getData($path, 'note')->getObject($id)
104: );
105: }
106:
107: /**
108: * Parse the content of a PHP file into a Horde_Push element.
109: *
110: * @param string $argument A single command line argument (without the scheme argument).
111: * @param array $conf The configuration.
112: *
113: * @return Horde_Push The element to be pushed.
114: */
115: private function _parsePhp($argument, $conf)
116: {
117: if (!file_exists($argument)) {
118: throw new Horde_Push_Exception(
119: sprintf('Invalid file path: "%s"!', $argument)
120: );
121: }
122:
123: global $push;
124: include $argument;
125: return $this->_createFromData($push);
126: }
127:
128: /**
129: * Parse the content of a YAML file into a Horde_Push element.
130: *
131: * @param string $argument A single command line argument (without the scheme argument).
132: * @param array $conf The configuration.
133: *
134: * @return Horde_Push The element to be pushed.
135: */
136: private function _parseYaml($argument, $conf)
137: {
138: if (!class_exists('Horde_Yaml')) {
139: throw new Horde_Push_Exception(
140: 'The Horde_Yaml package is missing!'
141: );
142: }
143: if (!file_exists($argument)) {
144: throw new Horde_Push_Exception(
145: sprintf('Invalid file path: "%s"!', $argument)
146: );
147: }
148: return $this->_createFromData(Horde_Yaml::loadFile($argument));
149: }
150:
151: /**
152: * Generate a Horde_Push element based on the provided data.
153: *
154: * @param array $data The data to be pushed.
155: *
156: * @return Horde_Push The element to be pushed.
157: */
158: private function _createFromData($data)
159: {
160: if (!isset($data['summary'])) {
161: throw new Horde_Push_Exception(
162: 'Data is lacking a summary element!'
163: );
164: }
165: $push = new Horde_Push();
166: $push->setSummary($data['summary']);
167: if (isset($data['body'])) {
168: if (htmlspecialchars($data['body']) != $data['body']) {
169: $push->addContent($data['body'], 'text/html');
170: } else {
171: $push->addContent($data['body']);
172: }
173: }
174: if (isset($data['tags'])) {
175: foreach ($data['tags'] as $tag) {
176: $push->addTag($tag);
177: }
178: }
179: if (isset($data['references'])) {
180: foreach ($data['references'] as $reference) {
181: $push->addReference($reference);
182: }
183: }
184: return $push;
185: }
186:
187: }