Created
December 5, 2016 05:37
-
-
Save artttj/632472a9e054ca8f0911404be4a2e597 to your computer and use it in GitHub Desktop.
Create Theme Data Upgrade SP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Versions { | |
public function __construct() { | |
} | |
public static function create() { | |
return new self(); | |
} | |
public function compareVersions($a, $b) | |
{ | |
global $dir; | |
$nameA = basename("$dir/$a"); | |
$nameB = basename("$dir/$b"); | |
$versionA = self::getLastVersionFromFile($nameA); | |
$versionB = self::getLastVersionFromFile($nameB); | |
return version_compare($versionA, $versionB) < 1; | |
} | |
public function getLastVersionFromFile($fileName) | |
{ | |
$version = explode('.php', $fileName); | |
$version = explode('-', $version[0]); | |
$version = $version[count($version) - 1]; | |
return $version; | |
} | |
public function getVersionsFromFile($fileName) | |
{ | |
$version = explode('.php', $fileName); | |
$version = explode('-', $version[0]); | |
$newVersion = $version[count($version) - 1]; | |
$oldVersion = $version[count($version) - 2]; | |
$prefix = explode($oldVersion, $fileName)[0]; | |
return array( | |
'prefix' => $prefix, | |
'newVersion' => $newVersion, | |
'oldVersion' => $oldVersion | |
); | |
} | |
public function incrementFileVersions($versions = array()) | |
{ | |
$oldVersion = self::updateVersion($versions['oldVersion']); | |
$newVersion = self::updateVersion($versions['newVersion']); | |
return array($oldVersion, $newVersion); | |
} | |
public static function updateVersion($i, $inc_dec = 1) | |
{ | |
$a = explode('.', $i); | |
if($inc_dec > 0){ | |
$a[count($a)-1]++; | |
} elseif ($inc_dec < 0){ | |
$a[count($a)-1]--; | |
} | |
return implode('.', $a); | |
} | |
public function getOldScriptsVersionsInDirectory($dir) | |
{ | |
$scripts = scandir($dir); | |
usort($scripts, array($this, 'compareVersions')); | |
return self::getVersionsFromFile($scripts[0]); | |
} | |
public function createNewScript($dir, $content = '') | |
{ | |
$lastVersions = self::getOldScriptsVersionsInDirectory($dir); | |
$newVersions = self::incrementFileVersions($lastVersions); | |
$newFileName = $lastVersions['prefix'] . implode('-', $newVersions) . '.php'; | |
$content = empty($content) ? '<?php' . PHP_EOL : $content; | |
$fp = fopen($dir . $newFileName, "w"); | |
$fw = fwrite($fp, $content); | |
fclose($fp); | |
return array( | |
'filename' => $newFileName, | |
'success' => $fw > 0 | |
); | |
} | |
} | |
if (empty($argv[1])) { | |
//return printf("You should pass dir for scripts updating, ie 'app/code/local/ModuleName/PathToScriptsFolder'%s", PHP_EOL); | |
$configFilePath = './app/code/local/Shirtpunch/Theme/etc/config.xml'; | |
$dir1 = './app/code/local/Shirtpunch/Theme/data/shirtpunch_theme_setup/'; | |
$dir2 = './app/code/local/Shirtpunch/Theme/sql/shirtpunch_theme_setup/'; | |
$content = <<<CONTENT | |
<?php | |
\$installer = \$this; | |
\$installer->startSetup(); | |
\$installer->endSetup(); | |
CONTENT; | |
$updatedDataScript = Versions::create()->createNewScript($dir1, $content); | |
$updatedSqlScript = Versions::create()->createNewScript($dir2); | |
if ($updatedDataScript['success']) { | |
printf('Data upgrade was successfully created: ' . $updatedDataScript['filename'] . '%s', PHP_EOL); | |
}; | |
if ($updatedSqlScript['success']) { | |
printf('SQL upgrade was successfully created: ' . $updatedSqlScript['filename'] . '%s', PHP_EOL); | |
} | |
$config = simplexml_load_file($configFilePath); | |
$configVersion = $config->modules->Shirtpunch_Theme->version; | |
$newConfigVersion = Versions::updateVersion($configVersion); | |
$config->modules->Shirtpunch_Theme->version = $newConfigVersion; | |
$config->asXml($configFilePath); | |
printf('Theme config was updated: ' . $newConfigVersion . '%s', PHP_EOL); | |
} else { | |
$dir = './' . $argv[1]; | |
$updatedDataScript = Versions::create()->createNewScript($dir); | |
if ($updatedDataScript['success']) { | |
printf('Upgrade script was successfully created: ' . $updatedDataScript['filename'] . '%s', PHP_EOL); | |
}; | |
} | |
return PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment