| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
abstract class CJTServicesInstaller extends CJTServicesModel { |
| 10 |
|
| 11 |
// INSTALLER STATES |
| 12 |
const STATE_DOWNGRADE = -1; |
| 13 |
const STATE_FRESH_INSTALL = 2; |
| 14 |
const STATE_INSTALLED = 0; |
| 15 |
const STATE_UPGRADE = 1; |
| 16 |
|
| 17 |
/** |
| 18 |
* put your comment there... |
| 19 |
* |
| 20 |
* @var mixed |
| 21 |
*/ |
| 22 |
protected $_currentVersion; |
| 23 |
|
| 24 |
/** |
| 25 |
* put your comment there... |
| 26 |
* |
| 27 |
* @var mixed |
| 28 |
*/ |
| 29 |
protected $_upgraders = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* put your comment there... |
| 33 |
* |
| 34 |
* @var mixed |
| 35 |
*/ |
| 36 |
protected $installedVersion = null; |
| 37 |
|
| 38 |
/** |
| 39 |
* put your comment there... |
| 40 |
* |
| 41 |
*/ |
| 42 |
public function __construct( ) |
| 43 |
{ |
| 44 |
|
| 45 |
parent::__construct(); |
| 46 |
|
| 47 |
// Current version is the latest version on the upgraders array |
| 48 |
$this->_currentVersion = end( $this->_upgraders ); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* put your comment there... |
| 54 |
* |
| 55 |
*/ |
| 56 |
public function getInstalledVersion() |
| 57 |
{ |
| 58 |
return $this->installedVersion; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* put your comment there... |
| 63 |
* |
| 64 |
*/ |
| 65 |
public function getState() |
| 66 |
{ |
| 67 |
|
| 68 |
$installedVersion = $this->getInstalledVersion(); |
| 69 |
|
| 70 |
return ! $installedVersion ? |
| 71 |
self::STATE_FRESH_INSTALL : // Never installed |
| 72 |
version_compare( $this->_currentVersion, $installedVersion ); // Upgrade, Installed, Downgrade |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* put your comment there... |
| 77 |
* |
| 78 |
* @param mixed $version |
| 79 |
*/ |
| 80 |
public function getUpgraderIndex( $version ) |
| 81 |
{ |
| 82 |
return array_search( $version, $this->_upgraders ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* put your comment there... |
| 87 |
* |
| 88 |
* @param mixed $version |
| 89 |
*/ |
| 90 |
public function getUpgraderName( $version ) |
| 91 |
{ |
| 92 |
return str_replace( array( '.', '-' ), '', $version ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* put your comment there... |
| 97 |
* |
| 98 |
*/ |
| 99 |
public function install() |
| 100 |
{ |
| 101 |
return $this->processUpgraders( 0 ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* put your comment there... |
| 106 |
* |
| 107 |
* @param mixed $version |
| 108 |
*/ |
| 109 |
public function processUpgraders( $upgraderIndex ) |
| 110 |
{ |
| 111 |
|
| 112 |
# Call all exists upgraders start from $version upgrader |
| 113 |
# to the end of tehe upgraders array |
| 114 |
for ( ; $upgraderIndex < count( $this->_upgraders ); $upgraderIndex ++ ) |
| 115 |
{ |
| 116 |
|
| 117 |
$upgraderName = $this->getUpgraderName( $this->_upgraders[ $upgraderIndex ] ); |
| 118 |
|
| 119 |
$upgraderMethodName = "_upgrade{$upgraderName}"; |
| 120 |
|
| 121 |
if ( method_exists( $this, $upgraderMethodName ) ) |
| 122 |
{ |
| 123 |
if ( ! $this->$upgraderMethodName() ) |
| 124 |
{ |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
$this->installedVersion = $this->_currentVersion; |
| 133 |
|
| 134 |
$this->saveState(); |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* put your comment there... |
| 141 |
* |
| 142 |
*/ |
| 143 |
public function upgrade() |
| 144 |
{ |
| 145 |
|
| 146 |
$upgraderIndex = $this->getUpgraderIndex( $this->_currentVersion ); |
| 147 |
|
| 148 |
if ( $upgraderIndex === FALSE ) |
| 149 |
{ |
| 150 |
throw new Exception( "Upgrader does not exists!!!" ); |
| 151 |
} |
| 152 |
|
| 153 |
return $this->processUpgraders( $upgraderIndex ); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
} |