Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle folder var/cache in module #306

Merged
merged 4 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion classes/TaskRunner/Upgrade/AllUpgradeTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
class AllUpgradeTasks extends ChainedTasks
{
const initialTask = 'upgradeNow';
const TASKS_WITH_RESTART = ['upgradeFiles', 'upgradeDb'];

protected $step = self::initialTask;

Expand Down Expand Up @@ -83,7 +84,7 @@ protected function checkIfRestartRequested(AjaxResponse $response)
return false;
}

if (!in_array($this->step, array('upgradeFiles'))) {
if (!in_array($this->step, self::TASKS_WITH_RESTART)) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions classes/TaskRunner/Upgrade/UpgradeDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public function run()
$this->next = 'upgradeModules';
$this->logger->info($this->translator->trans('Database upgraded. Now upgrading your Addons modules...', array(), 'Modules.Autoupgrade.Admin'));

$this->container->getSymfonyAdapter()->clearMigrationCache();

return true;
}

Expand All @@ -69,6 +67,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
2 changes: 0 additions & 2 deletions classes/TaskRunner/Upgrade/UpgradeFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public function run()
if (count($filesToUpgrade) > 0) {
$this->logger->info($this->translator->trans('%s files left to upgrade.', array(count($filesToUpgrade)), 'Modules.Autoupgrade.Admin'));
$this->stepDone = false;
@unlink(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'dev' . DIRECTORY_SEPARATOR . 'class_index.php');
@unlink(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'prod' . DIRECTORY_SEPARATOR . 'class_index.php');
}

return true;
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'));
}
}
}
}
77 changes: 2 additions & 75 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,45 +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/',
);

$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 @@ -680,6 +638,8 @@ protected function cleanXmlFiles()
_PS_ROOT_DIR_ . '/config/xml/tab_modules_list.xml',
_PS_ROOT_DIR_ . '/config/xml/trusted_modules_list.xml',
_PS_ROOT_DIR_ . '/config/xml/untrusted_modules_list.xml',
_PS_ROOT_DIR_ . '/var/cache/dev/class_index.php',
_PS_ROOT_DIR_ . '/var/cache/prod/class_index.php',
);
foreach ($files as $path) {
if (file_exists($path)) {
Expand Down Expand Up @@ -727,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);
}
}
}
}
39 changes: 39 additions & 0 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader16.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PrestaShop\Module\AutoUpgrade\Tools14;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeException;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\FilesystemAdapter;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\SettingsFileWriter;

class CoreUpgrader16 extends CoreUpgrader
Expand Down Expand Up @@ -144,4 +145,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 classes/UpgradeTools/FileFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function getFilesToIgnoreOnBackup()
'/cache/smarty/cache',
'/cache/tcpdf',
'/cache/cachefs',
'/var/cache',

// do not care about the two autoupgrade dir we use;
'/modules/autoupgrade',
Expand Down
19 changes: 0 additions & 19 deletions classes/UpgradeTools/SymfonyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,6 @@ public function __construct($destinationPsVersion)
$this->destinationPsVersion = $destinationPsVersion;
}

/**
* Can be called only on PS 1.7.
*/
public function clearMigrationCache()
{
if (version_compare($this->destinationPsVersion, '1.7.0.0', '<')) {
return;
}

\Tools::clearCache();
\Tools::clearXMLCache();
\Media::clearCache();
\Tools::generateIndex();

$sf2Refresh = new \PrestaShopBundle\Service\Cache\Refresh();
$sf2Refresh->addCacheClear();
$sf2Refresh->execute();
}

public function runSchemaUpgradeCommand()
{
if (version_compare($this->destinationPsVersion, '1.7.1.1', '>=')) {
Expand Down
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