Common.php 6.0 KB

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