/application/libraries/Engine/Package/Installer/Abstract.php

https://github.com/shopaholiccompany/shopaholic · PHP · 332 lines · 188 code · 50 blank · 94 comment · 20 complexity · e684240d5b276b2329e8a341584cfa9d MD5 · raw file

  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_Package
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: Abstract.php 7244 2010-09-01 01:49:53Z john $
  10. * @author John Boehr <j@webligo.com>
  11. */
  12. /**
  13. * @category Engine
  14. * @package Engine_Filter
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. * @author John Boehr <j@webligo.com>
  18. */
  19. abstract class Engine_Package_Installer_Abstract
  20. {
  21. /**
  22. * The active operation
  23. *
  24. * @var Engine_Package_Manager_Operation_Abstract
  25. */
  26. protected $_operation;
  27. /**
  28. * Database adapter
  29. *
  30. * @var Zend_Db_Adapter_Abstract
  31. */
  32. protected $_db;
  33. /**
  34. * Virtual File System Adapter
  35. *
  36. * @var Engine_Vfs_Adapter_Abstract
  37. */
  38. protected $_vfs;
  39. /**
  40. * The module name
  41. *
  42. * @var string
  43. */
  44. protected $_name;
  45. protected $_classMethods;
  46. protected $_errors = array();
  47. protected $_messages = array();
  48. /**
  49. * Constructor
  50. *
  51. * @param Engine_Package_Manager_Operation_Abstract $operation
  52. * @param Zend_Db_Adapter_Abstract $db
  53. * @param array $options
  54. */
  55. final public function __construct(Engine_Package_Manager_Operation_Abstract $operation, array $options = null)
  56. {
  57. $this->_operation = $operation;
  58. $this->_name = $operation->getPackage()->getName();
  59. if( is_array($options) ) {
  60. $this->setOptions($options);
  61. }
  62. $this->init();
  63. }
  64. /**
  65. * Set options
  66. *
  67. * @param array $options
  68. * @return self
  69. */
  70. public function setOptions(array $options)
  71. {
  72. foreach( $options as $key => $value ) {
  73. $method = 'set' . ucfirst($key);
  74. if( method_exists($this, $method) ) {
  75. $this->$method($value);
  76. }
  77. }
  78. return $this;
  79. }
  80. public function setOperation(Engine_Package_Manager_Operation_Abstract $operation)
  81. {
  82. if( null !== $this->_operation ) {
  83. throw new Engine_Package_Installer_Exception('Operation already set');
  84. }
  85. $this->_operation = $operation;
  86. return $this;
  87. }
  88. /**
  89. * @return Engine_Package_Manager_Operation_Abstract
  90. */
  91. public function getOperation()
  92. {
  93. if( null === $this->_operation ) {
  94. throw new Engine_Package_Installer_Exception('Operation not set');
  95. }
  96. return $this->_operation;
  97. }
  98. public function setDb(Zend_Db_Adapter_Abstract $db = null)
  99. {
  100. if( null !== $this->_db ) {
  101. throw new Engine_Package_Installer_Exception('Database already set');
  102. }
  103. $this->_db = $db;
  104. return $this;
  105. }
  106. /**
  107. * @return Zend_Db_Adapter_Abstract
  108. */
  109. public function getDb()
  110. {
  111. if( null === $this->_db ) {
  112. throw new Engine_Package_Installer_Exception('Database not set');
  113. }
  114. return $this->_db;
  115. }
  116. public function setVfs(Engine_Vfs_Adapter_Abstract $vfs = null)
  117. {
  118. if( null !== $this->_vfs ) {
  119. throw new Engine_Package_Installer_Exception('VFS already set');
  120. }
  121. $this->_vfs = $vfs;
  122. return $this;
  123. }
  124. /**
  125. * @return Engine_Vfs_Adapter_Abstract
  126. */
  127. public function getVfs()
  128. {
  129. if( null === $this->_vfs ) {
  130. throw new Engine_Package_Installer_Exception('VFS not set');
  131. }
  132. return $this->_vfs;
  133. }
  134. // Events
  135. public function notify($type)
  136. {
  137. foreach( $this->_getClassMethods() as $method ) {
  138. if( substr($method, 0, 2) != 'on' ) continue;
  139. if( strtolower($method) === strtolower('on' . $type) ) {
  140. try {
  141. $ret = $this->$method();
  142. } catch( Exception $e ) {
  143. $this->_error('Error: ' . $e->getMessage());
  144. return false;
  145. }
  146. return true;
  147. }
  148. }
  149. return null;
  150. }
  151. /**
  152. * Internal construct hook
  153. *
  154. * @return void
  155. */
  156. public function init()
  157. {
  158. }
  159. // Messages/Errors
  160. protected function _error($error)
  161. {
  162. $this->_errors[] = $error;
  163. return $this;
  164. }
  165. protected function _message($message)
  166. {
  167. $this->_messages[] = $message;
  168. return $this;
  169. }
  170. public function hasError()
  171. {
  172. return !empty($this->_errors);
  173. }
  174. public function hasMessages()
  175. {
  176. return !empty($this->_messages);
  177. }
  178. public function getErrors()
  179. {
  180. return $this->_errors;
  181. }
  182. public function getMessages()
  183. {
  184. return $this->_messages;
  185. }
  186. public function clearErrors()
  187. {
  188. $this->_errors = array();
  189. return $this;
  190. }
  191. public function clearMessages()
  192. {
  193. $this->_messages = array();
  194. return $this;
  195. }
  196. // Utilities
  197. /**
  198. * Get the current versions. Key 0 should be the target version, Key 1 should
  199. * be the currently installed version
  200. *
  201. * @return array
  202. */
  203. protected function _getVersions()
  204. {
  205. $currentVersionPackage = $this->_getVersionPackage();
  206. //$currentVersionManifest = $this->_getVersionManifest();
  207. $currentVersionDatabase = $this->_getVersionDatabase();
  208. $targetVersion = $this->_getVersionTarget();
  209. $currentVersion = ( $currentVersionDatabase ? $currentVersionDatabase : $currentVersionPackage );
  210. return array(
  211. $targetVersion,
  212. $currentVersion
  213. );
  214. }
  215. /**
  216. * Get the target version
  217. *
  218. * @return string
  219. */
  220. protected function _getVersionTarget()
  221. {
  222. if( $this->getOperation()->getOperationType() == 'remove' ) {
  223. return null;
  224. } else {
  225. return $this->getOperation()->getPackage()->getVersion();
  226. }
  227. }
  228. /**
  229. * Get the current version as defined by the package file
  230. *
  231. * @return string
  232. */
  233. protected function _getVersionPackage()
  234. {
  235. $previousPackage = $this->getOperation()->getPreviousPackage();
  236. if( null !== $previousPackage ) {
  237. return $previousPackage->getVersion();
  238. }
  239. return null;
  240. }
  241. /**
  242. * Get the current version as defined by the manifest file
  243. *
  244. * @return string
  245. */
  246. protected function _getVersionManifest()
  247. {
  248. $package = $this->getOperation()->getPackage();
  249. $path = $package->getPath();
  250. if( $this instanceof Engine_Package_Installer_Module ) {
  251. $manifestPath = APPLICATION_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . 'settings' . DIRECTORY_SEPARATOR . 'manifest.php';
  252. } else {
  253. $manifestPath = APPLICATION_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . 'manifest.php';
  254. }
  255. if( !file_exists($manifestPath) ) {
  256. return null;
  257. }
  258. ob_start();
  259. $config = include $manifestPath;
  260. ob_end_clean();
  261. if( isset($config['package']['version']) ) {
  262. return $config['package']['version'];
  263. }
  264. return null; // hmmmm...
  265. }
  266. /**
  267. * Get the current version as defined by the database
  268. *
  269. * @return string
  270. */
  271. protected function _getVersionDatabase()
  272. {
  273. return null;
  274. }
  275. protected function _getClassMethods()
  276. {
  277. if( null === $this->_classMethods ) {
  278. $this->_classMethods = array_map('strtolower', get_class_methods(get_class($this)));
  279. }
  280. return $this->_classMethods;
  281. }
  282. }