* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ namespace PrestaShop\Module\AutoUpgrade\Parameters; use PrestaShop\Module\AutoUpgrade\Tools14; use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Filesystem; /** * Class used for management of to do files for upgrade tasks. * Load / Save / Delete etc. */ class FileConfigurationStorage { /** * @var string Location where all the configuration files are stored */ private $configPath; /** * @var Filesystem */ private $filesystem; public function __construct(string $path) { $this->configPath = $path; $this->filesystem = new Filesystem(); } /** * UpgradeConfiguration loader. * * @param string $fileName File name to load * * @return mixed or array() as default value */ public function load(string $fileName = '') { $configFilePath = $this->configPath . $fileName; $config = []; if (file_exists($configFilePath)) { $config = @unserialize(base64_decode(Tools14::file_get_contents($configFilePath))); } return $config; } /** * @param mixed $config * @param string $fileName Destination name of the config file */ public function save($config, string $fileName): bool { $configFilePath = $this->configPath . $fileName; try { $this->filesystem->dumpFile($configFilePath, base64_encode(serialize($config))); return true; } catch (IOException $e) { // TODO: $e needs to be logged return false; } } /** * @return array Temporary files path & name */ public function getFilesList(): array { $files = []; foreach (UpgradeFileNames::$tmp_files as $file) { $files[$file] = $this->getFilePath(constant('PrestaShop\\Module\\AutoUpgrade\\Parameters\\UpgradeFileNames::' . $file)); } return $files; } /** * Delete all temporary files in the config folder. */ public function cleanAll(): void { $this->filesystem->remove(self::getFilesList()); } /** * Delete a file from the filesystem. * * @param string $fileName */ public function clean(string $fileName): void { $this->filesystem->remove($this->getFilePath($fileName)); } /** * Check if a file exists on the filesystem. * * @param string $fileName */ public function exists(string $fileName): bool { return $this->filesystem->exists($this->getFilePath($fileName)); } /** * Generate the complete path to a given file. */ private function getFilePath(string $file): string { return $this->configPath . $file; } }