Skip to content

Commit 9833057

Browse files
authored
[5.2] Refactor all instances of File to use framework (joomla#43362)
* Refactor all occurences of File to use framework * Refactoring some error handling * Codestyle * Fix error handling * Fix FormattedtextLogger.php * Chaining exceptions
1 parent 321b153 commit 9833057

File tree

3 files changed

+62
-34
lines changed

3 files changed

+62
-34
lines changed

administrator/components/com_installer/src/Model/InstallModel.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Joomla\CMS\Event\Installer\BeforeInstallationEvent;
1515
use Joomla\CMS\Event\Installer\BeforeInstallerEvent;
1616
use Joomla\CMS\Factory;
17-
use Joomla\CMS\Filesystem\File;
1817
use Joomla\CMS\Installer\Installer;
1918
use Joomla\CMS\Installer\InstallerHelper;
2019
use Joomla\CMS\Language\Text;
@@ -23,6 +22,8 @@
2322
use Joomla\CMS\Router\Route;
2423
use Joomla\CMS\Updater\Update;
2524
use Joomla\CMS\Uri\Uri;
25+
use Joomla\Filesystem\Exception\FilesystemException;
26+
use Joomla\Filesystem\File;
2627
use Joomla\Filesystem\Path;
2728

2829
// phpcs:disable PSR1.Files.SideEffects
@@ -327,7 +328,13 @@ protected function _getPackageFromUpload()
327328
$tmp_src = $userfile['tmp_name'];
328329

329330
// Move uploaded file.
330-
File::upload($tmp_src, $tmp_dest, false, true);
331+
try {
332+
File::upload($tmp_src, $tmp_dest, false, true);
333+
} catch (FilesystemException $exception) {
334+
Factory::getApplication()->enqueueMessage(Text::_('COM_INSTALLER_MSG_INSTALL_WARNINSTALLUPLOADERROR'), 'error');
335+
336+
return false;
337+
}
331338

332339
// Unpack the downloaded package file.
333340
$package = InstallerHelper::unpack($tmp_dest, true);

administrator/components/com_joomlaupdate/src/Model/UpdateModel.php

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Joomla\CMS\Component\ComponentHelper;
1515
use Joomla\CMS\Extension\ExtensionHelper;
1616
use Joomla\CMS\Factory;
17-
use Joomla\CMS\Filesystem\File as FileCMS;
1817
use Joomla\CMS\Filter\InputFilter;
1918
use Joomla\CMS\Http\Http;
2019
use Joomla\CMS\Http\HttpFactory;
@@ -30,6 +29,7 @@
3029
use Joomla\CMS\User\UserHelper;
3130
use Joomla\CMS\Version;
3231
use Joomla\Database\ParameterType;
32+
use Joomla\Filesystem\Exception\FilesystemException;
3333
use Joomla\Filesystem\File;
3434
use Joomla\Registry\Registry;
3535
use Joomla\Utilities\ArrayHelper;
@@ -508,7 +508,11 @@ protected function downloadPackage($url, $target)
508508

509509
// Make sure the target does not exist.
510510
if (is_file($target)) {
511-
File::delete($target);
511+
try {
512+
File::delete($target);
513+
} catch (FilesystemException $exception) {
514+
return false;
515+
}
512516
}
513517

514518
// Download the package
@@ -526,9 +530,9 @@ protected function downloadPackage($url, $target)
526530
$body = $result->body;
527531

528532
// Write the file to disk
529-
$result = File::write($target, $body);
530-
531-
if (!$result) {
533+
try {
534+
File::write($target, $body);
535+
} catch (FilesystemException $exception) {
532536
return false;
533537
}
534538

@@ -611,14 +615,18 @@ public function createUpdateFile($basename = null): bool
611615
$configpath = JPATH_COMPONENT_ADMINISTRATOR . '/update.php';
612616

613617
if (is_file($configpath)) {
614-
File::delete($configpath);
618+
try {
619+
File::delete($configpath);
620+
} catch (FilesystemException $exception) {
621+
return false;
622+
}
615623
}
616624

617625
// Write new file. First try with File.
618-
$result = File::write($configpath, $data);
619-
620-
// In case File failed but direct access could help.
621-
if (!$result) {
626+
try {
627+
$result = File::write($configpath, $data);
628+
} catch (FilesystemException $exception) {
629+
// In case File failed but direct access could help.
622630
$fp = @fopen($configpath, 'wt');
623631

624632
if ($fp !== false) {
@@ -889,18 +897,21 @@ public function cleanUp()
889897

890898
$file = $app->getUserState('com_joomlaupdate.file', null);
891899

892-
if (is_file($tempdir . '/' . $file)) {
893-
File::delete($tempdir . '/' . $file);
894-
}
900+
try {
901+
if (is_file($tempdir . '/' . $file)) {
902+
File::delete($tempdir . '/' . $file);
903+
}
895904

896-
// Remove the update.php file used in Joomla 4.0.3 and later.
897-
if (is_file(JPATH_COMPONENT_ADMINISTRATOR . '/update.php')) {
898-
File::delete(JPATH_COMPONENT_ADMINISTRATOR . '/update.php');
899-
}
905+
// Remove the update.php file used in Joomla 4.0.3 and later.
906+
if (is_file(JPATH_COMPONENT_ADMINISTRATOR . '/update.php')) {
907+
File::delete(JPATH_COMPONENT_ADMINISTRATOR . '/update.php');
908+
}
900909

901-
// Remove joomla.xml from the site's root.
902-
if (is_file(JPATH_ROOT . '/joomla.xml')) {
903-
File::delete(JPATH_ROOT . '/joomla.xml');
910+
// Remove joomla.xml from the site's root.
911+
if (is_file(JPATH_ROOT . '/joomla.xml')) {
912+
File::delete(JPATH_ROOT . '/joomla.xml');
913+
}
914+
} catch (FilesystemException $exception) {
904915
}
905916

906917
// Unset the update filename from the session.
@@ -983,10 +994,10 @@ public function upload()
983994
$tmp_src = $userfile['tmp_name'];
984995

985996
// Move uploaded file.
986-
$result = FileCMS::upload($tmp_src, $tmp_dest, false, true);
987-
988-
if (!$result) {
989-
throw new \RuntimeException(Text::_('COM_INSTALLER_MSG_INSTALL_WARNINSTALLUPLOADERROR'), 500);
997+
try {
998+
File::upload($tmp_src, $tmp_dest, false);
999+
} catch (FilesystemException $exception) {
1000+
throw new \RuntimeException(Text::_('COM_INSTALLER_MSG_INSTALL_WARNINSTALLUPLOADERROR'), 500, $exception);
9901001
}
9911002

9921003
Factory::getApplication()->setUserState('com_joomlaupdate.temp_file', $tmp_dest);
@@ -1061,7 +1072,10 @@ public function removePackageFiles()
10611072

10621073
foreach ($files as $file) {
10631074
if ($file !== null && is_file($file)) {
1064-
File::delete($file);
1075+
try {
1076+
File::delete($file);
1077+
} catch (FilesystemException $exception) {
1078+
}
10651079
}
10661080
}
10671081
}

libraries/src/Log/Logger/FormattedtextLogger.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
namespace Joomla\CMS\Log\Logger;
1111

1212
use Joomla\CMS\Factory;
13-
use Joomla\CMS\Filesystem\File;
1413
use Joomla\CMS\Filesystem\Folder;
1514
use Joomla\CMS\Log\LogEntry;
1615
use Joomla\CMS\Log\Logger;
1716
use Joomla\CMS\Version;
17+
use Joomla\Filesystem\Exception\FilesystemException;
18+
use Joomla\Filesystem\File;
1819
use Joomla\Utilities\IpHelper;
1920

2021
// phpcs:disable PSR1.Files.SideEffects
@@ -136,8 +137,10 @@ public function __destruct()
136137
// Format all lines and write to file.
137138
$lines = array_map([$this, 'formatLine'], $this->deferredEntries);
138139

139-
if (!File::append($this->path, implode("\n", $lines) . "\n")) {
140-
throw new \RuntimeException('Cannot write to log file.');
140+
try {
141+
File::write($this->path, implode("\n", $lines) . "\n", false, true);
142+
} catch (FilesystemException $exception) {
143+
throw new \RuntimeException('Cannot write to log file.', 500, $exception);
141144
}
142145
}
143146

@@ -165,8 +168,10 @@ public function addEntry(LogEntry $entry)
165168
$line = $this->formatLine($entry);
166169
$line .= "\n";
167170

168-
if (!File::append($this->path, $line)) {
169-
throw new \RuntimeException('Cannot write to log file.');
171+
try {
172+
File::write($this->path, $line, false, true);
173+
} catch (FilesystemException $exception) {
174+
throw new \RuntimeException('Cannot write to log file.', 500, $exception);
170175
}
171176
}
172177
}
@@ -269,8 +274,10 @@ protected function initFile()
269274
// Build the log file header.
270275
$head = $this->generateFileHeader();
271276

272-
if (!File::write($this->path, $head)) {
273-
throw new \RuntimeException('Cannot write to log file.');
277+
try {
278+
File::write($this->path, $head);
279+
} catch (FilesystemException $exception) {
280+
throw new \RuntimeException('Cannot write to log file.', 500, $exception);
274281
}
275282
}
276283

0 commit comments

Comments
 (0)