Skip to content

Instantly share code, notes, and snippets.

@PechenkiUA
Created March 18, 2024 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PechenkiUA/f8e0c0fd4d704545186b5103f0ae86de to your computer and use it in GitHub Desktop.
Save PechenkiUA/f8e0c0fd4d704545186b5103f0ae86de to your computer and use it in GitHub Desktop.
opencart clear cache
<?php
/**
*
*/
class CodCache {
/**
* @var
*/
private $registry;
/**
* @var
*/
private $db;
/**
* @var true
*/
private $isCreatedDb;
/**
* @param $registry
*/
public function __construct($registry)
{
$this->registry = $registry;
$this->db = $registry->get('db');
if (!$this->isCreatedDb){
$this->createTable();
$this->isCreatedDb = true;
}
}
/**
* @param $key
* @return void
*/
public function clear($key){
if ($key == 'system') {
$dir = DIR_CACHE;
} elseif ($key == 'modification') {
$dir = DIR_MODIFICATION;
} elseif ($key == 'image') {
$dir = DIR_IMAGE . 'cache/';
} else {
$dir = false;
}
if ($dir) {
$files = array();
// Make path into an array
$path = array($dir . '*');
// While the path array is still populated keep looping through
while (count($path) != 0) {
$next = array_shift($path);
foreach (glob($next) as $file) {
// If directory add to path array
if (is_dir($file)) {
$path[] = $file . '/*';
}
// Add the file to the files to be deleted array
$files[] = $file;
}
}
// Reverse sort the file array
rsort($files);
// Clear all files
foreach ($files as $file) {
if ($file != $dir . 'index.html' && $file != $dir . '.htaccess') {
// If file just delete
if (is_file($file)) {
unlink($file);
// If directory use the remove directory function
} elseif (is_dir($file)) {
rmdir($file);
}
}
}
}
}
/**
* @return void
*/
public function clearIfOneC(){
$sql = $this->db->query("SELECT * FROM `twu_one_c` WHERE name='is_update' LIMIT 1");
if($sql->row && $sql->row['value'] == 0){
return false;
}
$this->clear('image');
$this->clear('system');
$this->setIsUpdate();
}
/**
* @return void
*/
private function setIsUpdate(){
$this->db->query("
UPDATE `twu_one_c` SET
`name` = 'is_update',
`value` = '0'
WHERE `name` = 'is_update';
");
}
/**
* @return void
*/
private function createTable(){
$sqlIsTable = $this->db->query("SHOW TABLES LIKE 'twu_one_c'");
if ($sqlIsTable->row) return;
$sql = "
CREATE TABLE `twu_one_c` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` char(255) COLLATE 'utf8mb4_general_ci' NULL,
`value` text(1000) COLLATE 'utf8mb4_general_ci' NOT NULL,
`update_at` timestamp NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE='InnoDB' COLLATE 'utf8mb4_general_ci';
";
$this->db->query($sql);
$sqlIsTableNew = $this->db->query("SHOW TABLES LIKE 'twu_one_c2'");
if ($sqlIsTableNew->row) {
$this->db->query("
INSERT INTO `twu_one_c` (`id`, `name`, `value`)
VALUES ('1', 'is_update', '0');
");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment