Skip to content

Commit

Permalink
Move cache folders cleanup in dedicated class
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed May 7, 2019
1 parent 81ae146 commit 5a31334
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 76 deletions.
2 changes: 2 additions & 0 deletions classes/TaskRunner/Upgrade/UpgradeDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +63,11 @@ class UpgradeContainer
const ARCHIVE_FILEPATH = 'destDownloadFilepath';
const PS_VERSION = 'version';

/**
* @var CacheCleaner
*/
private $cacheCleaner;

/**
* @var Cookie
*/
Expand Down Expand Up @@ -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) {
Expand Down
90 changes: 90 additions & 0 deletions classes/UpgradeTools/CacheCleaner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
* 2007-2019 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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'));
}
}
}
}
76 changes: 0 additions & 76 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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_')) {
Expand Down Expand Up @@ -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);
}
}
}
}
38 changes: 38 additions & 0 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader16.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
1 change: 1 addition & 0 deletions tests/UpgradeContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 5a31334

Please sign in to comment.