diff --git a/classes/TaskRunner/Upgrade/UpgradeDb.php b/classes/TaskRunner/Upgrade/UpgradeDb.php index f2bd1d8482..e41e6afda3 100644 --- a/classes/TaskRunner/Upgrade/UpgradeDb.php +++ b/classes/TaskRunner/Upgrade/UpgradeDb.php @@ -69,6 +69,8 @@ public function getCoreUpgrader() public function init() { + $this->container->getCacheCleaner()->cleanFolders(); + // Migrating settings file $this->container->initPrestaShopAutoloader(); (new SettingsFileWriter($this->translator))->migrateSettingsFile($this->logger); diff --git a/classes/UpgradeContainer.php b/classes/UpgradeContainer.php index b6493ed58a..4cb480b068 100644 --- a/classes/UpgradeContainer.php +++ b/classes/UpgradeContainer.php @@ -29,6 +29,7 @@ use PrestaShop\Module\AutoUpgrade\Log\LegacyLogger; use PrestaShop\Module\AutoUpgrade\Log\Logger; +use PrestaShop\Module\AutoUpgrade\UpgradeTools\CacheCleaner; use PrestaShop\Module\AutoUpgrade\UpgradeTools\FileFilter; use PrestaShop\Module\AutoUpgrade\UpgradeTools\FilesystemAdapter; use PrestaShop\Module\AutoUpgrade\UpgradeTools\ModuleAdapter; @@ -62,6 +63,11 @@ class UpgradeContainer const ARCHIVE_FILEPATH = 'destDownloadFilepath'; const PS_VERSION = 'version'; + /** + * @var CacheCleaner + */ + private $cacheCleaner; + /** * @var Cookie */ @@ -192,6 +198,20 @@ public function getProperty($property) } } + /** + * Init and return CacheCleaner + * + * @return CacheCleaner + */ + public function getCacheCleaner() + { + if (null !== $this->cacheCleaner) { + return $this->cacheCleaner; + } + + return $this->cacheCleaner = new CacheCleaner($this, $this->getLogger()); + } + public function getCookie() { if (null !== $this->cookie) { diff --git a/classes/UpgradeTools/CacheCleaner.php b/classes/UpgradeTools/CacheCleaner.php new file mode 100644 index 0000000000..bf0ac118d1 --- /dev/null +++ b/classes/UpgradeTools/CacheCleaner.php @@ -0,0 +1,90 @@ + + * @copyright 2007-2019 PrestaShop SA + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * International Registered Trademark & Property of PrestaShop SA + */ + +namespace PrestaShop\Module\AutoUpgrade\UpgradeTools; + +use PrestaShop\Module\AutoUpgrade\UpgradeContainer; +use Psr\Log\LoggerInterface; + +class CacheCleaner +{ + /** + * @var UpgradeContainer + */ + private $container; + + /** + * @var LoggerInterface + */ + private $logger; + + public function __construct(UpgradeContainer $container, LoggerInterface $logger) + { + $this->container = $container; + $this->logger = $logger; + } + + public function cleanFolders() + { + $dirsToClean = array( + $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/app/cache/', + $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/cache/smarty/cache/', + $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/cache/smarty/compile/', + $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/var/cache/', + ); + + $defaultThemeNames = array( + 'default', + 'prestashop', + 'default-boostrap', + 'classic', + ); + + if (defined('_THEME_NAME_') && $this->container->getUpgradeConfiguration()->shouldUpdateDefaultTheme() && in_array(_THEME_NAME_, $defaultThemeNames)) { + $dirsToClean[] = $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/themes/' . _THEME_NAME_ . '/cache/'; + } + + foreach ($dirsToClean as $dir) { + if (!file_exists($dir)) { + $this->logger->debug($this->container->getTranslator()->trans('[SKIP] directory "%s" does not exist and cannot be emptied.', array(str_replace($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH), '', $dir)), 'Modules.Autoupgrade.Admin')); + continue; + } + foreach (scandir($dir) as $file) { + if ($file[0] === '.' || $file === 'index.php') { + continue; + } + // ToDo: Use Filesystem instead ? + if (is_file($dir . $file)) { + unlink($dir . $file); + } elseif (is_dir($dir . $file . DIRECTORY_SEPARATOR)) { + FilesystemAdapter::deleteDirectory($dir . $file . DIRECTORY_SEPARATOR); + } + $this->logger->debug($this->container->getTranslator()->trans('[CLEANING CACHE] File %s removed', array($file), 'Modules.Autoupgrade.Admin')); + } + } + } +} diff --git a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php index c5dda77a23..11ec9cd99e 100644 --- a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php +++ b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php @@ -29,7 +29,6 @@ use PrestaShop\Module\AutoUpgrade\UpgradeContainer; use PrestaShop\Module\AutoUpgrade\UpgradeException; -use PrestaShop\Module\AutoUpgrade\UpgradeTools\FilesystemAdapter; use PrestaShop\Module\AutoUpgrade\UpgradeTools\ThemeAdapter; use Psr\Log\LoggerInterface; @@ -107,8 +106,6 @@ public function doUpgrade() $this->runRecurrentQueries(); $this->logger->debug($this->container->getTranslator()->trans('Database upgrade OK', array(), 'Modules.Autoupgrade.Admin')); // no error! - // Settings updated, compile and cache directories must be emptied - $this->cleanFolders(); $this->upgradeLanguages(); $this->generateHtaccess(); $this->cleanXmlFiles(); @@ -465,46 +462,6 @@ protected function runRecurrentQueries() $this->db->execute('UPDATE `' . _DB_PREFIX_ . 'configuration` SET value="' . $this->destinationUpgradeVersion . '" WHERE name = "PS_VERSION_DB"', false); } - protected function cleanFolders() - { - $dirsToClean = array( - $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/app/cache/', - $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/cache/smarty/cache/', - $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/cache/smarty/compile/', - $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/var/cache/', - ); - - $defaultThemeNames = array( - 'default', - 'prestashop', - 'default-boostrap', - 'classic', - ); - - if (defined('_THEME_NAME_') && $this->container->getUpgradeConfiguration()->shouldUpdateDefaultTheme() && in_array(_THEME_NAME_, $defaultThemeNames)) { - $dirsToClean[] = $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/themes/' . _THEME_NAME_ . '/cache/'; - } - - foreach ($dirsToClean as $dir) { - if (!file_exists($dir)) { - $this->logger->debug($this->container->getTranslator()->trans('[SKIP] directory "%s" does not exist and cannot be emptied.', array(str_replace($this->container->getProperty(UpgradeContainer::PS_ROOT_PATH), '', $dir)), 'Modules.Autoupgrade.Admin')); - continue; - } - foreach (scandir($dir) as $file) { - if ($file[0] === '.' || $file === 'index.php' /*|| $file === '.htaccess'*/) { - continue; - } - // ToDo: Use Filesystem instead ? - if (is_file($dir . $file)) { - unlink($dir . $file); - } elseif (is_dir($dir . $file . DIRECTORY_SEPARATOR)) { - FilesystemAdapter::deleteDirectory($dir . $file . DIRECTORY_SEPARATOR); - } - $this->logger->debug($this->container->getTranslator()->trans('[CLEANING CACHE] File %s removed', array($file), 'Modules.Autoupgrade.Admin')); - } - } - } - protected function upgradeLanguages() { if (!defined('_PS_TOOL_DIR_')) { @@ -730,38 +687,5 @@ protected function updateTheme() protected function runCoreCacheClean() { \Tools::clearCache(); - - // delete cache filesystem if activated - if (defined('_PS_CACHE_ENABLED_') && false != _PS_CACHE_ENABLED_) { - $depth = (int) $this->db->getValue('SELECT value - FROM ' . _DB_PREFIX_ . 'configuration - WHERE name = "PS_CACHEFS_DIRECTORY_DEPTH"'); - if ($depth) { - if (!defined('_PS_CACHEFS_DIRECTORY_')) { - define('_PS_CACHEFS_DIRECTORY_', $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/cache/cachefs/'); - } - FilesystemAdapter::deleteDirectory(_PS_CACHEFS_DIRECTORY_, false); - if (class_exists('CacheFs', false)) { - $this->createCacheFsDirectories((int) $depth); - } - } - } - } - - private function createCacheFsDirectories($level_depth, $directory = false) - { - if (!$directory) { - if (!defined('_PS_CACHEFS_DIRECTORY_')) { - define('_PS_CACHEFS_DIRECTORY_', $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/cache/cachefs/'); - } - $directory = _PS_CACHEFS_DIRECTORY_; - } - $chars = '0123456789abcdef'; - for ($i = 0; $i < strlen($chars); ++$i) { - $new_dir = $directory . $chars[$i] . '/'; - if (mkdir($new_dir, 0775) && chmod($new_dir, 0775) && $level_depth - 1 > 0) { - $this->createCacheFsDirectories($level_depth - 1, $new_dir); - } - } } } diff --git a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader16.php b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader16.php index b75b956797..7ed060809a 100644 --- a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader16.php +++ b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader16.php @@ -144,4 +144,42 @@ protected function loadEntityInterface() { require_once _PS_ROOT_DIR_ . '/Core/Foundation/Database/Core_Foundation_Database_EntityInterface.php'; } + + protected function runCoreCacheClean() + { + parent::runCoreCacheClean(); + + // delete cache filesystem if activated + if (defined('_PS_CACHE_ENABLED_') && false != _PS_CACHE_ENABLED_) { + $depth = (int) $this->db->getValue('SELECT value + FROM ' . _DB_PREFIX_ . 'configuration + WHERE name = "PS_CACHEFS_DIRECTORY_DEPTH"'); + if ($depth) { + if (!defined('_PS_CACHEFS_DIRECTORY_')) { + define('_PS_CACHEFS_DIRECTORY_', $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/cache/cachefs/'); + } + FilesystemAdapter::deleteDirectory(_PS_CACHEFS_DIRECTORY_, false); + if (class_exists('CacheFs', false)) { + $this->createCacheFsDirectories((int) $depth); + } + } + } + } + + private function createCacheFsDirectories($level_depth, $directory = false) + { + if (!$directory) { + if (!defined('_PS_CACHEFS_DIRECTORY_')) { + define('_PS_CACHEFS_DIRECTORY_', $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH) . '/cache/cachefs/'); + } + $directory = _PS_CACHEFS_DIRECTORY_; + } + $chars = '0123456789abcdef'; + for ($i = 0; $i < strlen($chars); ++$i) { + $new_dir = $directory . $chars[$i] . '/'; + if (mkdir($new_dir, 0775) && chmod($new_dir, 0775) && $level_depth - 1 > 0) { + $this->createCacheFsDirectories($level_depth - 1, $new_dir); + } + } + } } diff --git a/tests/UpgradeContainerTest.php b/tests/UpgradeContainerTest.php index 0060107537..f91e293e77 100644 --- a/tests/UpgradeContainerTest.php +++ b/tests/UpgradeContainerTest.php @@ -52,6 +52,7 @@ public function objectsToInstanciateProvider() { // | Function to call | Expected class | return array( + array('getCacheCleaner', PrestaShop\Module\AutoUpgrade\UpgradeTools\CacheCleaner::class), array('getCookie', PrestaShop\Module\AutoUpgrade\Cookie::class), array('getFileConfigurationStorage', PrestaShop\Module\AutoUpgrade\Parameters\FileConfigurationStorage::class), array('getFileFilter', \PrestaShop\Module\AutoUpgrade\UpgradeTools\FileFilter::class),