xpdotransportvehicle.class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. * Copyright 2010-2015 by MODX, LLC.
  4. *
  5. * This file is part of xPDO.
  6. *
  7. * xPDO is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * xPDO is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * xPDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  18. * Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * Defines a class that represents an embedded package within a transportable package.
  22. *
  23. * @package xpdo
  24. * @subpackage transport
  25. */
  26. /**
  27. * Represents an xPDOTransport embedded within an {@link xPDOTransport} package.
  28. *
  29. * @package xpdo
  30. * @subpackage transport
  31. */
  32. class xPDOTransportVehicle extends xPDOVehicle {
  33. public $class = 'xPDOTransportVehicle';
  34. /**
  35. * Copies the transport into the vehicle and transforms the payload for storage.
  36. */
  37. protected function _compilePayload(& $transport) {
  38. parent :: _compilePayload($transport);
  39. $body = array ();
  40. $cacheManager = $transport->xpdo->getCacheManager();
  41. if ($cacheManager) {
  42. if (isset($this->payload['object'])) {
  43. $object = $this->payload['object'];
  44. $fileSource = $object['source'];
  45. $body['source'] = $transport->signature . '/' . $this->payload['class'] . '/' . $this->payload['signature'] . '/';
  46. $fileTarget = $transport->path . $body['source'];
  47. $body['target'] = $object['target'];
  48. $fileName = isset ($object['name']) ? $object['name'] : basename($fileSource);
  49. $body['name'] = $fileName;
  50. if (!is_writable($fileTarget)) {
  51. $cacheManager->writeTree($fileTarget);
  52. }
  53. if (file_exists($fileSource) && is_writable($fileTarget)) {
  54. $copied = false;
  55. if (is_file($fileSource)) {
  56. $copied = $cacheManager->copyFile($fileSource, $fileTarget . $fileName);
  57. }
  58. if (!$copied) {
  59. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not copy file from {$fileSource} to {$fileTarget}{$fileName}");
  60. $body = null;
  61. }
  62. } else {
  63. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Source file {$fileSource} is missing or {$fileTarget} is not writable");
  64. $body = null;
  65. }
  66. }
  67. }
  68. if (!empty($body)) {
  69. $this->payload['object'] = $body;
  70. }
  71. }
  72. /**
  73. * Install the xPDOTransport represented by this vehicle into the transport host.
  74. */
  75. public function install(& $transport, $options) {
  76. $installed = $this->_installTransport($transport, $options);
  77. return $installed;
  78. }
  79. /**
  80. * Install the xPDOTransport from the vehicle payload.
  81. *
  82. * @param xPDOTransport $transport The host xPDOTransport instance.
  83. * @param array $options Any optional attributes to apply to the installation.
  84. */
  85. protected function _installTransport(& $transport, $options) {
  86. $installed = false;
  87. $vOptions = $this->get($transport, $options);
  88. if (isset($vOptions['object']) && isset($vOptions['object']['source']) && isset($vOptions['object']['target']) && isset($vOptions['object']['name'])) {
  89. if ($transport->xpdo->getDebug() === true)
  90. $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Installing Vehicle: " . print_r($vOptions, true));
  91. $state = isset($vOptions['state']) ? $vOptions['state'] : xPDOTransport::STATE_PACKED;
  92. $pkgSource = $transport->path . $vOptions['object']['source'] . $vOptions['object']['name'];
  93. $pkgTarget = eval($vOptions['object']['target']);
  94. $object = xPDOTransport::retrieve($transport->xpdo, $pkgSource, $pkgTarget, $state);
  95. if ($this->validate($transport, $object, $vOptions)) {
  96. $installed = $object->install($vOptions);
  97. if (!$installed) {
  98. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Error installing vehicle: ' . print_r($vOptions, true));
  99. }
  100. elseif (!$this->resolve($transport, $object, $vOptions)) {
  101. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not resolve vehicle: ' . print_r($vOptions, true));
  102. }
  103. } else {
  104. //$transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not validate vehicle: ' . print_r($vOptions, true));
  105. }
  106. } else {
  107. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not load vehicle: ' . print_r($vOptions, true));
  108. }
  109. return $installed;
  110. }
  111. /**
  112. * Uninstalls vehicle artifacts from the transport host.
  113. */
  114. public function uninstall(& $transport, $options) {
  115. $uninstalled = false;
  116. $vOptions = $this->get($transport, $options);
  117. if (isset($vOptions['object']) && isset($vOptions['object']['source']) && isset($vOptions['object']['target']) && isset($vOptions['object']['name'])) {
  118. if ($transport->xpdo->getDebug() === true)
  119. $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Installing Vehicle: " . print_r($vOptions, true));
  120. $state = isset($vOptions['state']) ? $vOptions['state'] : xPDOTransport::STATE_UNPACKED;
  121. $pkgSource = $transport->path . $vOptions['object']['source'] . $vOptions['object']['name'];
  122. $pkgTarget = eval($vOptions['object']['target']);
  123. $object = xPDOTransport::retrieve($transport->xpdo, $pkgSource, $pkgTarget, $state);
  124. if ($this->validate($transport, $object, $vOptions)) {
  125. $uninstalled = $object->uninstall($vOptions);
  126. if (!$uninstalled) {
  127. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not uninstall vehicle: ' . print_r($vOptions, true));
  128. }
  129. elseif (!$this->resolve($transport, $object, $vOptions)) {
  130. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not resolve vehicle: ' . print_r($vOptions, true));
  131. }
  132. } else {
  133. //$transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not validate vehicle: ' . print_r($vOptions, true));
  134. }
  135. } else {
  136. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not load vehicle: ' . print_r($vOptions, true));
  137. }
  138. return $uninstalled;
  139. }
  140. /**
  141. * Put an xPDOTransport representation into a transport package.
  142. */
  143. public function put(& $transport, & $object, $attributes = array ()) {
  144. if (!isset ($this->payload['class'])) {
  145. $this->payload['class'] = 'xPDOTransportVehicle';
  146. }
  147. if (is_array($object) && isset ($object['source']) && isset ($object['target'])) {
  148. if (!isset($object['name'])) $object['name'] = basename($object['source']);
  149. $this->payload['object'] = $object;
  150. }
  151. parent :: put($transport, $object, $attributes);
  152. }
  153. }