Common.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * PEAR_Task_Common, base class for installer tasks
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category pear
  8. * @package PEAR
  9. * @author Greg Beaver <cellog@php.net>
  10. * @copyright 1997-2009 The Authors
  11. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  12. * @link http://pear.php.net/package/PEAR
  13. * @since File available since Release 1.4.0a1
  14. */
  15. /**#@+
  16. * Error codes for task validation routines
  17. */
  18. define('PEAR_TASK_ERROR_NOATTRIBS', 1);
  19. define('PEAR_TASK_ERROR_MISSING_ATTRIB', 2);
  20. define('PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE', 3);
  21. define('PEAR_TASK_ERROR_INVALID', 4);
  22. /**#@-*/
  23. define('PEAR_TASK_PACKAGE', 1);
  24. define('PEAR_TASK_INSTALL', 2);
  25. define('PEAR_TASK_PACKAGEANDINSTALL', 3);
  26. /**
  27. * A task is an operation that manipulates the contents of a file.
  28. *
  29. * Simple tasks operate on 1 file. Multiple tasks are executed after all files have been
  30. * processed and installed, and are designed to operate on all files containing the task.
  31. * The Post-install script task simply takes advantage of the fact that it will be run
  32. * after installation, replace is a simple task.
  33. *
  34. * Combining tasks is possible, but ordering is significant.
  35. *
  36. * <file name="test.php" role="php">
  37. * <tasks:replace from="@data-dir@" to="data_dir" type="pear-config"/>
  38. * <tasks:postinstallscript/>
  39. * </file>
  40. *
  41. * This will first replace any instance of @data-dir@ in the test.php file
  42. * with the path to the current data directory. Then, it will include the
  43. * test.php file and run the script it contains to configure the package post-installation.
  44. *
  45. * @category pear
  46. * @package PEAR
  47. * @author Greg Beaver <cellog@php.net>
  48. * @copyright 1997-2009 The Authors
  49. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  50. * @version Release: 1.10.10
  51. * @link http://pear.php.net/package/PEAR
  52. * @since Class available since Release 1.4.0a1
  53. * @abstract
  54. */
  55. class PEAR_Task_Common
  56. {
  57. /**
  58. * Valid types for this version are 'simple' and 'multiple'
  59. *
  60. * - simple tasks operate on the contents of a file and write out changes to disk
  61. * - multiple tasks operate on the contents of many files and write out the
  62. * changes directly to disk
  63. *
  64. * Child task classes must override this property.
  65. *
  66. * @access protected
  67. */
  68. protected $type = 'simple';
  69. /**
  70. * Determines which install phase this task is executed under
  71. */
  72. public $phase = PEAR_TASK_INSTALL;
  73. /**
  74. * @access protected
  75. */
  76. protected $config;
  77. /**
  78. * @access protected
  79. */
  80. protected $registry;
  81. /**
  82. * @access protected
  83. */
  84. public $logger;
  85. /**
  86. * @access protected
  87. */
  88. protected $installphase;
  89. /**
  90. * @param PEAR_Config
  91. * @param PEAR_Common
  92. */
  93. function __construct(&$config, &$logger, $phase)
  94. {
  95. $this->config = &$config;
  96. $this->registry = &$config->getRegistry();
  97. $this->logger = &$logger;
  98. $this->installphase = $phase;
  99. if ($this->type == 'multiple') {
  100. $GLOBALS['_PEAR_TASK_POSTINSTANCES'][get_class($this)][] = &$this;
  101. }
  102. }
  103. /**
  104. * Validate the basic contents of a task tag.
  105. *
  106. * @param PEAR_PackageFile_v2
  107. * @param array
  108. * @param PEAR_Config
  109. * @param array the entire parsed <file> tag
  110. *
  111. * @return true|array On error, return an array in format:
  112. * array(PEAR_TASK_ERROR_???[, param1][, param2][, ...])
  113. *
  114. * For PEAR_TASK_ERROR_MISSING_ATTRIB, pass the attribute name in
  115. * For PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, pass the attribute name and
  116. * an array of legal values in
  117. *
  118. * @abstract
  119. */
  120. public static function validateXml($pkg, $xml, $config, $fileXml)
  121. {
  122. }
  123. /**
  124. * Initialize a task instance with the parameters
  125. *
  126. * @param array raw, parsed xml
  127. * @param array attributes from the <file> tag containing this task
  128. * @param string|null last installed version of this package
  129. * @abstract
  130. */
  131. public function init($xml, $fileAttributes, $lastVersion)
  132. {
  133. }
  134. /**
  135. * Begin a task processing session. All multiple tasks will be processed
  136. * after each file has been successfully installed, all simple tasks should
  137. * perform their task here and return any errors using the custom
  138. * throwError() method to allow forward compatibility
  139. *
  140. * This method MUST NOT write out any changes to disk
  141. *
  142. * @param PEAR_PackageFile_v2
  143. * @param string file contents
  144. * @param string the eventual final file location (informational only)
  145. * @return string|false|PEAR_Error false to skip this file, PEAR_Error to fail
  146. * (use $this->throwError), otherwise return the new contents
  147. * @abstract
  148. */
  149. public function startSession($pkg, $contents, $dest)
  150. {
  151. }
  152. /**
  153. * This method is used to process each of the tasks for a particular
  154. * multiple class type. Simple tasks need not implement this method.
  155. *
  156. * @param array an array of tasks
  157. * @access protected
  158. */
  159. public static function run($tasks)
  160. {
  161. }
  162. /**
  163. * @final
  164. */
  165. public static function hasPostinstallTasks()
  166. {
  167. return isset($GLOBALS['_PEAR_TASK_POSTINSTANCES']);
  168. }
  169. /**
  170. * @final
  171. */
  172. public static function runPostinstallTasks()
  173. {
  174. foreach ($GLOBALS['_PEAR_TASK_POSTINSTANCES'] as $class => $tasks) {
  175. $err = call_user_func(
  176. array($class, 'run'),
  177. $GLOBALS['_PEAR_TASK_POSTINSTANCES'][$class]
  178. );
  179. if ($err) {
  180. return PEAR_Task_Common::throwError($err);
  181. }
  182. }
  183. unset($GLOBALS['_PEAR_TASK_POSTINSTANCES']);
  184. }
  185. /**
  186. * Determines whether a role is a script
  187. * @return bool
  188. */
  189. public function isScript()
  190. {
  191. return $this->type == 'script';
  192. }
  193. public function throwError($msg, $code = -1)
  194. {
  195. include_once 'PEAR.php';
  196. return PEAR::raiseError($msg, $code);
  197. }
  198. }