1: <?php
2: /**
3: * Handles a XML directory node in the contents list.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Pear
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Pear
12: */
13:
14: /**
15: * Handles a XML directory node in the contents list.
16: *
17: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Horde
23: * @package Pear
24: * @author Gunnar Wrobel <wrobel@pardus.de>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Pear
27: */
28: class Horde_Pear_Package_Xml_Element_Directory
29: {
30: /**
31: * The package.xml handler to operate on.
32: *
33: * @var Horde_Pear_Package_Xml
34: */
35: private $_xml;
36:
37: /**
38: * The directory node.
39: *
40: * @var DOMNode
41: */
42: private $_dir;
43:
44: /**
45: * The name of this directory.
46: *
47: * @var string
48: */
49: private $_name;
50:
51: /**
52: * The path to this directory.
53: *
54: * @var string
55: */
56: private $_path;
57:
58: /**
59: * The level in the tree.
60: *
61: * @var int
62: */
63: private $_level;
64:
65: /**
66: * Constructor.
67: *
68: * @param string $name The name of
69: * the directory.
70: * @param Horde_Pear_Package_Xml_Element_Directory $parent The parent
71: * directory.
72: */
73: public function __construct($name, $parent = null)
74: {
75: $this->_name = $name;
76: if ($parent instanceOf Horde_Pear_Package_Xml_Element_Directory) {
77: $this->_xml = $parent->getDocument();
78: $this->_path = $parent->getPath() . '/' . $name;
79: $this->_level = $parent->getLevel() + 1;
80: } else {
81: $this->_path = '';
82: $this->_level = 1;
83: }
84: }
85:
86: /**
87: * Set the package.xml handler to operate on.
88: *
89: * @param Horde_Pear_Package_Xml $xml The XML handler.
90: *
91: * @return NULL
92: */
93: public function setDocument(Horde_Pear_Package_Xml $xml)
94: {
95: $this->_xml = $xml;
96: }
97:
98: /**
99: * Return the package.xml handler this element belongs to.
100: *
101: * @return Horde_Pear_Package_Xml The XML handler.
102: */
103: public function getDocument()
104: {
105: if ($this->_xml === null) {
106: throw new Horde_Pear_Exception('The XML document has been left undefined!');
107: }
108: return $this->_xml;
109: }
110:
111: /**
112: * Set the DOM node of the directory entry.
113: *
114: * @param DOMNode $directory The directory node.
115: *
116: * @return NULL
117: */
118: public function setDirectoryNode(DOMNode $directory)
119: {
120: $this->_dir = $directory;
121: }
122:
123: /**
124: * Get the DOM node of the directory entry.
125: *
126: * @return DOMNode The directory node.
127: */
128: public function getDirectoryNode()
129: {
130: if ($this->_dir === null) {
131: throw new Horde_Pear_Exception('The directory node has been left undefined!');
132: }
133: return $this->_dir;
134: }
135:
136: /**
137: * Return the name of this directory.
138: *
139: * @return string The directory name.
140: */
141: public function getName()
142: {
143: return $this->getDirectoryNode()->getAttribute('name');
144: }
145:
146: /**
147: * Return the level of depth in the tree for this directory.
148: *
149: * @return int The level.
150: */
151: public function getLevel()
152: {
153: return $this->_level;
154: }
155:
156: /**
157: * Return the full path to this element.
158: *
159: * @return string The path.
160: */
161: public function getPath()
162: {
163: return $this->_path;
164: }
165:
166: /**
167: * Return the subdirectories for this directory.
168: *
169: * @return array The list of subdirectories.
170: */
171: public function getSubdirectories()
172: {
173: $result = array();
174: foreach ($this->_xml->findNodesRelativeTo('./p:dir', $this->getDirectoryNode()) as $directory) {
175: $name = $directory->getAttribute('name');
176: $result[$name] = $this->_xml->createElementDirectory($name, $this);
177: $result[$name]->setDirectoryNode($directory);
178: }
179: return $result;
180: }
181:
182: /**
183: * Return the list of files in this directory.
184: *
185: * @return array The list of files.
186: */
187: public function getFiles()
188: {
189: $result = array();
190: foreach ($this->_xml->findNodesRelativeTo('./p:file', $this->getDirectoryNode()) as $file) {
191: $name = $file->getAttribute('name');
192: $result[$name] = $this->_xml->createElementFile($name, $this);
193: $result[$name]->setFileNode($file);
194: }
195: return $result;
196: }
197:
198: /**
199: * Insert a new file entry into the XML at the given point with the
200: * specified name and file role.
201: *
202: * @param string $name The name.
203: * @param string $role The role.
204: * @param DOMNode $point Insertion point.
205: *
206: * @return Horde_Pear_Package_Xml_Element_File The inserted element.
207: */
208: public function insertFile($name, $role, DOMNode $point = null)
209: {
210: $element = $this->_xml->createElementFile($name, $this, $role);
211: $element->insert($point);
212: return $element;
213: }
214:
215: /**
216: * Insert a new directory entry into the XML at the given point with the
217: * specified name
218: *
219: * @param string $name The name.
220: * @param DOMNode $point Insertion point.
221: *
222: * @return Horde_Pear_Package_Xml_Element_Directory The inserted element.
223: */
224: public function insertSubDirectory($name, DOMNode $point = null)
225: {
226: $element = $this->_xml->createElementDirectory($name, $this);
227: $element->_insert($this, $point);
228: return $element;
229: }
230:
231: /**
232: * Insert the directory entry into the XML at the given point.
233: *
234: * @param Horde_Pear_Package_Xml_Element_Directory $parent The parent.
235: * @param DOMNode $point Insertion point.
236: *
237: * @return NULL
238: */
239: private function _insert(Horde_Pear_Package_Xml_Element_Directory $parent,
240: DOMNode $point = null)
241: {
242: if ($point === null) {
243: $point = $parent->getDirectoryNode()->lastChild;
244: } else {
245: if ($point->previousSibling) {
246: $ws = trim($point->previousSibling->textContent);
247: if (empty($ws)) {
248: $point = $point->previousSibling;
249: }
250: }
251: }
252:
253: $dir = $this->_xml->insert(
254: array(
255: "\n " . str_repeat(" ", $this->_level),
256: 'dir' => array('name' => $this->_name),
257: ' ',
258: $this->_xml->createComment(' ' . $this->_path . ' ')
259: ),
260: $point
261: );
262: $this->_xml->append(
263: "\n" . str_repeat(' ', $this->_level + 1),
264: $dir
265: );
266: $this->setDirectoryNode($dir);
267: }
268:
269: /**
270: * Remove the directory entry from the XML.
271: *
272: * @return NULL
273: */
274: public function delete()
275: {
276: $dir = $this->getDirectoryNode();
277: $this->_xml->removeWhitespace($dir->nextSibling);
278: $this->_xml->removeComment($dir->nextSibling, $this->_path);
279: $this->_xml->removeWhitespace($dir->nextSibling);
280: $dir->parentNode->removeChild($dir);
281: }
282: }