xpdoscriptvehicle.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 executable PHP script within a transportable package.
  22. *
  23. * @package xpdo
  24. * @subpackage transport
  25. */
  26. /**
  27. * Represents an executable PHP script within an {@link xPDOTransport} package.
  28. *
  29. * @package xpdo
  30. * @subpackage transport
  31. */
  32. class xPDOScriptVehicle extends xPDOVehicle {
  33. public $class = 'xPDOScriptVehicle';
  34. /**
  35. * Execute a PHP script represented by and stored in this vehicle.
  36. */
  37. public function install(& $transport, $options) {
  38. return $this->_executeScript($transport, $options);
  39. }
  40. /**
  41. * Execute a PHP script represented by and stored in this vehicle.
  42. */
  43. public function uninstall(& $transport, $options) {
  44. return $this->_executeScript($transport, $options);
  45. }
  46. /**
  47. * Execute the script represented by and stored in this vehicle.
  48. *
  49. * @access protected
  50. * @param xPDOTransport &$transport A reference the transport this vehicle is stored in.
  51. * @param array $options Optional attributes that can be applied to vehicle install process.
  52. * @return boolean True if the scripts are executed successfully.
  53. */
  54. protected function _executeScript(& $transport, $options) {
  55. $installed = false;
  56. $vOptions = $this->get($transport, $options);
  57. if (isset ($vOptions['object']) && isset ($vOptions['object']['source'])) {
  58. $object = $vOptions['object'];
  59. if ($this->validate($transport, $object, $vOptions)) {
  60. $fileSource = $transport->path . $object['source'];
  61. if (!$installed = include ($fileSource)) {
  62. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOScriptVehicle execution failed: ({$fileSource})");
  63. } elseif (!$this->resolve($transport, $object, $vOptions)) {
  64. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not resolve vehicle for object: ' . print_r($object, true));
  65. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not resolve vehicle: ' . print_r($vOptions, true));
  66. }
  67. } else {
  68. //$transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not validate vehicle for object: ' . print_r($object, true));
  69. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not validate vehicle: ' . print_r($vOptions, true));
  70. }
  71. }
  72. return $installed;
  73. }
  74. /**
  75. * Adds the file definition object to the payload.
  76. */
  77. public function put(& $transport, & $object, $attributes = array ()) {
  78. if (!isset ($this->payload['class'])) {
  79. $this->payload['class'] = 'xPDOScriptVehicle';
  80. }
  81. if (is_array($object) && isset ($object['source'])) {
  82. $this->payload['object'] = $object;
  83. }
  84. parent :: put($transport, $object, $attributes);
  85. }
  86. /**
  87. * Copies the files into the vehicle and transforms the payload for storage.
  88. */
  89. protected function _compilePayload(& $transport) {
  90. parent :: _compilePayload($transport);
  91. $body = array ();
  92. $cacheManager = $transport->xpdo->getCacheManager();
  93. if ($cacheManager) {
  94. if (isset($this->payload['object'])) {
  95. $object = $this->payload['object'];
  96. $fileSource = $object['source'];
  97. $scriptName = basename($fileSource, '.php');
  98. $body['source'] = $transport->signature . '/' . $this->payload['class'] . '/' . $this->payload['signature'] . '.' . $scriptName . '.script';
  99. $fileTarget = $transport->path . $body['source'];
  100. $body = array_merge($object, $body);
  101. if (!$cacheManager->copyFile($fileSource, $fileTarget)) {
  102. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Source file {$fileSource} is missing or {$fileTarget} could not be written");
  103. $body = null;
  104. }
  105. }
  106. }
  107. if (!empty($body)) {
  108. $this->payload['object'] = $body;
  109. }
  110. }
  111. }