|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Core.php |
| 4 | + * |
| 5 | + * PHP Version 5 |
| 6 | + * |
| 7 | + * @category Brandung |
| 8 | + * @package Brandung_magento-composer-installer |
| 9 | + * @author David Verholen <[email protected]> |
| 10 | + * @copyright 2014 Brandung GmbH & Co Kg |
| 11 | + * @license http://opensource.org/licenses/OSL-3.0 OSL-3.0 |
| 12 | + * @link http://www.brandung.de |
| 13 | + */ |
| 14 | + |
| 15 | +namespace MagentoHackathon\Composer\Magento\Deploystrategy; |
| 16 | + |
| 17 | +use Composer\Util\Filesystem; |
| 18 | + |
| 19 | + |
| 20 | +/** |
| 21 | + * Class Core |
| 22 | + * |
| 23 | + * @category Brandung |
| 24 | + * @package Brandung_magento-composer-installer |
| 25 | + * @author David Verholen <[email protected]> |
| 26 | + * @copyright 2014 Brandung GmbH & Co Kg |
| 27 | + * @license http://opensource.org/licenses/OSL-3.0 OSL-3.0 |
| 28 | + * @link http://www.brandung.de |
| 29 | + */ |
| 30 | +class Core extends Copy |
| 31 | +{ |
| 32 | + const DS = DIRECTORY_SEPARATOR; |
| 33 | + |
| 34 | + const BACKUP_DIR = 'persistent'; |
| 35 | + |
| 36 | + const DEFAULT_MAGENTO_ROOT = 'magento'; |
| 37 | + |
| 38 | + /** |
| 39 | + * Directories that need write Permissions for the Web Server |
| 40 | + * |
| 41 | + * @var array |
| 42 | + */ |
| 43 | + protected $magentoWritableDirs |
| 44 | + = array( |
| 45 | + 'app/etc', |
| 46 | + 'media', |
| 47 | + 'var' |
| 48 | + ); |
| 49 | + |
| 50 | + /** |
| 51 | + * Directories that persist between Updates |
| 52 | + * |
| 53 | + * @var array |
| 54 | + */ |
| 55 | + protected $persistentDirs |
| 56 | + = array( |
| 57 | + 'media', |
| 58 | + 'var' |
| 59 | + ); |
| 60 | + |
| 61 | + /** |
| 62 | + * fs |
| 63 | + * |
| 64 | + * @var Filesystem |
| 65 | + */ |
| 66 | + protected $fs; |
| 67 | + |
| 68 | + public function __construct($sourceDir, $destDir) |
| 69 | + { |
| 70 | + parent::__construct($sourceDir, $destDir); |
| 71 | + $this->fs = new Filesystem(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * beforeDeploy |
| 76 | + * |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + protected function beforeClean() |
| 80 | + { |
| 81 | + parent::beforeDeploy(); |
| 82 | + if (!file_exists($this->destDir) || !is_dir($this->destDir)) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + $this->fs->ensureDirectoryExists(self::BACKUP_DIR); |
| 87 | + $this->movePersistentFiles($this->destDir, self::BACKUP_DIR); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * afterClean |
| 92 | + * |
| 93 | + * @return void |
| 94 | + */ |
| 95 | + protected function afterClean() |
| 96 | + { |
| 97 | + parent::afterClean(); |
| 98 | + $this->emptyDir($this->destDir); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + /** |
| 103 | + * afterDeploy |
| 104 | + * |
| 105 | + * @return void |
| 106 | + */ |
| 107 | + protected function afterDeploy() |
| 108 | + { |
| 109 | + parent::afterDeploy(); |
| 110 | + if (!file_exists(self::BACKUP_DIR) || !is_dir(self::BACKUP_DIR)) { |
| 111 | + return; |
| 112 | + } |
| 113 | + $this->movePersistentFiles(self::BACKUP_DIR, $this->destDir); |
| 114 | + $this->rrmdir(self::BACKUP_DIR); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * getLocalXmlPath |
| 119 | + * |
| 120 | + * @return string |
| 121 | + */ |
| 122 | + protected function getLocalXmlPath() |
| 123 | + { |
| 124 | + return 'app' . self::DS . 'etc' . self::DS . 'local.xml'; |
| 125 | + } |
| 126 | + |
| 127 | + protected function movePersistentFiles($sourceDir, $targetDir) |
| 128 | + { |
| 129 | + $this->movePersistentDirectories($sourceDir, $targetDir); |
| 130 | + $this->moveLocalXml($sourceDir, $targetDir); |
| 131 | + } |
| 132 | + |
| 133 | + protected function moveLocalXml($sourceDir, $targetDir) |
| 134 | + { |
| 135 | + $source = $sourceDir . self::DS . $this->getLocalXmlPath(); |
| 136 | + $target = $targetDir . self::DS . $this->getLocalXmlPath(); |
| 137 | + if (file_exists($source)) { |
| 138 | + $this->fs->ensureDirectoryExists(dirname($target)); |
| 139 | + rename($source, $target); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + protected function movePersistentDirectories($sourceDir, $targetDir) |
| 144 | + { |
| 145 | + foreach ($this->persistentDirs as $dir) { |
| 146 | + $source = $sourceDir . self::DS . $dir; |
| 147 | + $target = $targetDir . self::DS . $dir; |
| 148 | + if (file_exists($source)) { |
| 149 | + $this->emptyDir($target); |
| 150 | + $this->fs->copyThenRemove($source, $target); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * rrmdir |
| 157 | + * |
| 158 | + * @param string $dirPath path of the dir to recursively remove |
| 159 | + * |
| 160 | + * @return bool |
| 161 | + */ |
| 162 | + protected function rrmdir($dirPath) |
| 163 | + { |
| 164 | + if (file_exists($dirPath)) { |
| 165 | + foreach ( |
| 166 | + new \RecursiveIteratorIterator( |
| 167 | + new \RecursiveDirectoryIterator( |
| 168 | + $dirPath, |
| 169 | + \FilesystemIterator::SKIP_DOTS |
| 170 | + ), |
| 171 | + \RecursiveIteratorIterator::CHILD_FIRST |
| 172 | + ) as $path |
| 173 | + ) { |
| 174 | + /* @var \SplFileInfo $path */ |
| 175 | + $path->isDir() |
| 176 | + ? rmdir($path->getPathname()) |
| 177 | + : unlink($path->getPathname()); |
| 178 | + } |
| 179 | + return rmdir($dirPath); |
| 180 | + } |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * emptyDir |
| 186 | + * |
| 187 | + * @param $dir |
| 188 | + * |
| 189 | + * @return void |
| 190 | + */ |
| 191 | + protected function emptyDir($dir) |
| 192 | + { |
| 193 | + $this->rrmdir($dir); |
| 194 | + $this->fs->ensureDirectoryExists($dir); |
| 195 | + } |
| 196 | +} |
0 commit comments