136 function toTgz2(&$packager, &$pf1, $compress =
true, $where =
null)
138 require_once
'Archive/Tar.php';
139 if (!$this->_packagefile->isEquivalent($pf1)) {
140 return PEAR::raiseError(
'PEAR_Packagefile_v2::toTgz: "' .
141 basename($pf1->getPackageFile()) .
142 '" is not equivalent to "' . basename($this->_packagefile->getPackageFile())
146 if ($where ===
null) {
147 if (!($where = System::mktemp(array(
'-d')))) {
148 return PEAR::raiseError(
'PEAR_Packagefile_v2::toTgz: mktemp failed');
150 } elseif (!@System::mkDir(array(
'-p', $where))) {
151 return PEAR::raiseError(
'PEAR_Packagefile_v2::toTgz: "' . $where .
'" could' .
155 $file = $where . DIRECTORY_SEPARATOR .
'package.xml';
156 if (file_exists($file) && !is_file($file)) {
157 return PEAR::raiseError(
'PEAR_Packagefile_v2::toTgz: unable to save package.xml as' .
162 return PEAR::raiseError(
'PEAR_Packagefile_v2::toTgz: invalid package.xml');
165 $ext = $compress ?
'.tgz' :
'.tar';
166 $pkgver = $this->_packagefile->getPackage() .
'-' . $this->_packagefile->getVersion();
167 $dest_package = getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext;
168 if (file_exists($dest_package) && !is_file($dest_package)) {
169 return PEAR::raiseError(
'PEAR_Packagefile_v2::toTgz: cannot create tgz file "' .
170 $dest_package .
'"');
173 $pkgfile = $this->_packagefile->getPackageFile();
175 return PEAR::raiseError(
'PEAR_Packagefile_v2::toTgz: package file object must ' .
176 'be created from a real file');
179 $pkgdir = dirname(realpath($pkgfile));
180 $pkgfile = basename($pkgfile);
185 $this->_packagefile->flattenFilelist();
186 $contents = $this->_packagefile->getContents();
187 if (isset($contents[
'bundledpackage'])) {
188 $contents = $contents[
'bundledpackage'];
189 if (!isset($contents[0])) {
190 $contents = array($contents);
193 $packageDir = $where;
194 foreach ($contents as $i => $package) {
196 $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
197 if (!file_exists($file)) {
198 return $packager->raiseError(
"File does not exist: $fname");
201 $tfile = $packageDir . DIRECTORY_SEPARATOR . $fname;
202 System::mkdir(array(
'-p', dirname($tfile)));
204 $filelist[$i++] = $tfile;
205 $packager->log(2,
"Adding package $fname");
208 $contents = $contents[
'dir'][
'file'];
209 if (!isset($contents[0])) {
210 $contents = array($contents);
213 $packageDir = $where;
214 foreach ($contents as $i => $file) {
215 $fname = $file[
'attribs'][
'name'];
216 $atts = $file[
'attribs'];
218 $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
219 if (!file_exists($file)) {
220 return $packager->raiseError(
"File does not exist: $fname");
223 $origperms = fileperms($file);
224 $tfile = $packageDir . DIRECTORY_SEPARATOR . $fname;
225 unset($orig[
'attribs']);
228 $contents = file_get_contents($file);
229 foreach ($orig as $tag =>
$raw) {
231 array($this->_packagefile->getTasksNs() .
':',
'-'),
232 array(
'',
'_'), $tag);
233 $task =
"PEAR_Task_$tag";
234 $task =
new $task($this->_packagefile->_config,
235 $this->_packagefile->_logger,
237 $task->init(
$raw, $atts,
null);
238 $res = $task->startSession($this->_packagefile, $contents, $tfile);
243 if (PEAR::isError($res)) {
248 System::mkdir(array(
'-p', dirname($tfile)));
249 $wp = fopen($tfile,
"wb");
250 fwrite($wp, $contents);
255 if (!file_exists($tfile)) {
256 System::mkdir(array(
'-p', dirname($tfile)));
260 chmod($tfile, $origperms);
261 $filelist[$i++] = $tfile;
262 $this->_packagefile->setFileAttribute($fname,
'md5sum', md5_file($tfile), $i - 1);
263 $packager->log(2,
"Adding file $fname");
268 $name = $pf1 !==
null ?
'package2.xml' :
'package.xml';
274 $ok = $tar->createModify(array($packagexml),
'', $where);
275 if (PEAR::isError($ok)) {
276 return $packager->raiseError($ok);
278 return $packager->raiseError(
'PEAR_Packagefile_v2::toTgz(): adding ' . $name .
283 if (!$tar->addModify($filelist, $pkgver, $where)) {
284 return $packager->raiseError(
285 'PEAR_Packagefile_v2::toTgz(): tarball creation failed');
290 $pfgen = &$pf1->getDefaultGenerator();
292 if (!$tar->addModify(array($packagexml1),
'', $where)) {
293 return $packager->raiseError(
294 'PEAR_Packagefile_v2::toTgz(): adding package.xml failed');
298 return $dest_package;
553 if (isset(
$options[
'overrideOptions']) &&
$options[
'overrideOptions'] ==
true) {
554 $this->options = array_merge($this->_defaultOptions,
$options);
556 $this->options = array_merge($this->options,
$options);
563 $this->_tagDepth = 0;
564 $this->_serializedData =
'';
566 if (is_array($data)) {
567 $tagName = isset($this->options[
'rootName']) ? $this->options[
'rootName'] :
'array';
568 $this->_serializedData .= $this->
_serializeArray($data, $tagName, $this->options[
'rootAttributes']);
572 if ($this->options[
'addDoctype'] ===
true) {
573 $this->_serializedData = XML_Util::getDoctypeDeclaration($tagName, $this->options[
'doctype'])
574 . $this->options[
'linebreak']
579 if ($this->options[
'addDecl']) {
581 $encoding = isset($this->options[
'encoding']) ? $this->options[
'encoding'] :
null;
582 $this->_serializedData = XML_Util::getXMLDeclaration(
'1.0', $encoding)
583 . $this->options[
'linebreak']
588 if ($optionsBak !==
null) {
589 $this->options = $optionsBak;
654 if ($this->options[
'attributesArray'] !==
null) {
655 if (isset($array[$this->options[
'attributesArray']])) {
656 $attributes = $array[$this->options[
'attributesArray']];
657 unset($array[$this->options[
'attributesArray']]);
662 if ($this->options[
'contentName'] !==
null) {
663 if (isset($array[$this->options[
'contentName']])) {
664 $_content = $array[$this->options[
'contentName']];
665 unset($array[$this->options[
'contentName']]);
674 if (is_array($array) && $this->options[
'mode'] ==
'simplexml') {
676 if (!count($array)) {
679 foreach ($array as $key => $val) {
686 if ($indexed && $this->options[
'mode'] ==
'simplexml') {
688 foreach ($array as $key => $val) {
689 if ($this->options[
'beautifyFilelist'] && $tagName ==
'dir') {
690 if (!isset($this->_curdir)) {
693 $savedir = $this->_curdir;
694 if (isset($val[
'attribs'])) {
695 if ($val[
'attribs'][
'name'] ==
'/') {
696 $this->_curdir =
'/';
698 if ($this->_curdir ==
'/') {
701 $this->_curdir .=
'/' . $val[
'attribs'][
'name'];
706 if ($this->options[
'beautifyFilelist'] && $tagName ==
'dir') {
707 $string .=
' <!-- ' . $this->_curdir .
' -->';
708 if (empty($savedir)) {
709 unset($this->_curdir);
711 $this->_curdir = $savedir;
715 $string .= $this->options[
'linebreak'];
717 if ($this->options[
'indent'] !==
null && $this->_tagDepth > 0) {
718 $string .= str_repeat($this->options[
'indent'], $this->_tagDepth);
721 return rtrim($string);
725 if ($this->options[
'scalarAsAttributes'] ===
true) {
726 foreach ($array as $key => $value) {
727 if (is_scalar($value) && (XML_Util::isValidName($key) ===
true)) {
729 $attributes[$this->options[
'prependAttributes'].$key] = $value;
738 'content' => $_content,
739 'attributes' => $attributes
744 $tmp = $this->options[
'linebreak'];
745 foreach ($array as $key => $value) {
747 if ($this->options[
'indent'] !==
null && $this->_tagDepth > 0) {
748 $tmp .= str_repeat($this->options[
'indent'], $this->_tagDepth);
754 $valid = XML_Util::isValidName($key);
755 if (PEAR::isError($valid)) {
756 if ($this->options[
'classAsTagName'] && is_object($value)) {
757 $key = get_class($value);
759 $key = $this->options[
'defaultTagName'];
763 if ($this->options[
'typeHints'] ===
true) {
764 $atts[$this->options[
'typeAttribute']] = gettype($value);
765 if ($key !== $origKey) {
766 $atts[$this->options[
'keyAttribute']] = (string)$origKey;
770 if ($this->options[
'beautifyFilelist'] && $key ==
'dir') {
771 if (!isset($this->_curdir)) {
774 $savedir = $this->_curdir;
775 if (isset($value[
'attribs'])) {
776 if ($value[
'attribs'][
'name'] ==
'/') {
777 $this->_curdir =
'/';
779 $this->_curdir .=
'/' . $value[
'attribs'][
'name'];
784 if (is_string($value) && $value && ($value[strlen($value) - 1] ==
"\n")) {
785 $value .= str_repeat($this->options[
'indent'], $this->_tagDepth);
789 'attributes' => $atts,
790 'content' => $value )
792 if ($this->options[
'beautifyFilelist'] && $key ==
'dir') {
793 if (isset($value[
'attribs'])) {
794 $tmp .=
' <!-- ' . $this->_curdir .
' -->';
795 if (empty($savedir)) {
796 unset($this->_curdir);
798 $this->_curdir = $savedir;
802 $tmp .= $this->options[
'linebreak'];
806 if ($this->options[
'indent']!==
null && $this->_tagDepth>0) {
807 $tmp .= str_repeat($this->options[
'indent'], $this->_tagDepth);
810 if (trim($tmp) ===
'') {
817 'attributes' => $attributes
820 if ($this->options[
'typeHints'] ===
true) {
821 if (!isset($tag[
'attributes'][$this->options[
'typeAttribute']])) {
822 $tag[
'attributes'][$this->options[
'typeAttribute']] =
'array';
848 if ($this->options[
'indentAttributes'] !==
false) {
850 $indent = str_repeat($this->options[
'indent'], $this->_tagDepth);
852 if ($this->options[
'indentAttributes'] ==
'_auto') {
853 $indent .= str_repeat(
' ', (strlen($tag[
'qname'])+2));
856 $indent .= $this->options[
'indentAttributes'];
859 $indent = $multiline =
false;
862 if (is_array($tag[
'content'])) {
863 if (empty($tag[
'content'])) {
864 $tag[
'content'] =
'';
866 } elseif(is_scalar($tag[
'content']) && (
string)$tag[
'content'] ==
'') {
867 $tag[
'content'] =
'';
870 if (is_scalar($tag[
'content']) || is_null($tag[
'content'])) {
871 if ($replaceEntities ===
true) {
875 $tag = XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options[
'linebreak']);
876 } elseif (is_array($tag[
'content'])) {
877 $tag = $this->
_serializeArray($tag[
'content'], $tag[
'qname'], $tag[
'attributes']);
878 } elseif (is_object($tag[
'content'])) {
879 $tag = $this->_serializeObject($tag[
'content'], $tag[
'qname'], $tag[
'attributes']);
880 } elseif (is_resource($tag[
'content'])) {
881 settype($tag[
'content'],
'string');
882 $tag = XML_Util::createTagFromArray($tag, $replaceEntities);