Skip to content
Open
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: 3 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

## Extensibility
- Added `CraftCms\Cms\Support\Arr`.
- Added `CraftCms\Cms\Support\Facades\Path`.
- Added `CraftCms\Cms\Support\Path`.
- Added `CraftCms\Cms\Support\Str`.
- `craft\services\Elements::stopCollectingCacheInfo()` no longer sets the returned duration to the `cacheDuration` config setting if a duration wasn’t explicitly declared. ([#16796](https://github.com/craftcms/cms/pull/16796))
- Deprecated `craft\helpers\ArrayHelper`. `CraftCms\Cms\Support\Arr` should be used instead.
- Deprecated `craft\helpers\ConfigHelper`. `CraftCms\Cms\Support\Config` should be used instead.
- Deprecated `craft\helpers\Diff`. `CraftCms\Cms\Support\Diff` should be used instead.
- Deprecated `craft\helpers\Html`. `CraftCms\Cms\Support\Html` should be used instead.
- Deprecated `craft\services\Path`. `CraftCms\Cms\Support\Path` should be used instead.
- Deprecated `craft\helpers\SessionHelper`. `Illuminate\Support\Facades\Session` should be used instead.
- Deprecated `craft\helpers\Sequence`. `CraftCms\Cms\Support\Sequence` should be used instead.
- Deprecated `craft\helpers\StringHelper`. `CraftCms\Cms\Support\Str` should be used instead.
Expand Down
3 changes: 2 additions & 1 deletion src/Asset/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use CraftCms\Cms\Image\ImageHelper;
use CraftCms\Cms\Support\Env;
use CraftCms\Cms\Support\Facades\Filesystems;
use CraftCms\Cms\Support\Facades\Path;
use CraftCms\Cms\Support\Str;
use CraftCms\Cms\User\Elements\User;
use Illuminate\Container\Attributes\Singleton;
Expand Down Expand Up @@ -318,7 +319,7 @@ public function getTempAssetUploadDisk(): FilesystemAdapter
if (! $handle) {
return Storage::build([ // @phpstan-ignore return.type
'driver' => 'local',
'root' => Craft::$app->getPath()->getTempAssetUploadsPath(),
'root' => Path::tempAssetUploads(),
]);
}

Expand Down
15 changes: 8 additions & 7 deletions src/Asset/Elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
use CraftCms\Cms\Support\Facades\HtmlStack;
use CraftCms\Cms\Support\Facades\I18N;
use CraftCms\Cms\Support\Facades\InputNamespace;
use CraftCms\Cms\Support\Facades\Path;
use CraftCms\Cms\Support\Facades\Search;
use CraftCms\Cms\Support\Facades\Users;
use CraftCms\Cms\Support\Facades\Volumes;
Expand Down Expand Up @@ -2342,7 +2343,7 @@ public function getImageTransformSourcePath(): string
return FileHelper::normalizePath($volume->sourceDisk()->path($this->getPath()));
}

return Craft::$app->getPath()->getAssetSourcesPath().DIRECTORY_SEPARATOR.$this->id.'.'.$this->getExtension();
return Path::assetSources($this->id.'.'.$this->getExtension());
}

/**
Expand All @@ -2354,7 +2355,7 @@ public function getImageTransformSourcePath(): string
public function getCopyOfFile(): string
{
$tempFilename = FileHelper::uniqueName($this->_filename);
$tempPath = Craft::$app->getPath()->getTempPath().DIRECTORY_SEPARATOR.$tempFilename;
$tempPath = Path::temp($tempFilename);
Assets::downloadFile($this->getVolume()->sourceDisk(), $this->getPath(), $tempPath);

return $tempPath;
Expand Down Expand Up @@ -3255,7 +3256,7 @@ private function _relocateFile(): void
}

$tempFilename = FileHelper::uniqueName($filename);
$tempPath = Craft::$app->getPath()->getTempPath().DIRECTORY_SEPARATOR.$tempFilename;
$tempPath = Path::temp($tempFilename);
Assets::downloadFile($oldVolume->sourceDisk(), $oldPath, $tempPath);
}

Expand Down Expand Up @@ -3344,10 +3345,10 @@ private function _validateTempFilePath(): bool
$tempFilePath = FileHelper::normalizePath($tempFilePath);

// Make sure it's within a known temp path, the project root, or storage/ folder
$pathService = Craft::$app->getPath();
$pathService = app(\CraftCms\Cms\Support\Path::class);
$allowedRoots = [
[$pathService->getTempPath(), true],
[$pathService->getTempAssetUploadsPath(), true],
[$pathService->temp(), true],
[$pathService->tempAssetUploads(), true],
[sys_get_temp_dir(), true],
[Aliases::get('@root', false), false],
[Aliases::get('@storage', false), false],
Expand All @@ -3370,7 +3371,7 @@ private function _validateTempFilePath(): bool
}

// Make sure it's *not* within a system directory though
$systemDirs = $pathService->getSystemPaths();
$systemDirs = $pathService->system();
$systemDirs = array_map($this->_normalizeTempPath(...), $systemDirs);
$systemDirs = array_filter($systemDirs, fn ($value) => $value !== false);

Expand Down
5 changes: 0 additions & 5 deletions src/Cp/Rebrand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace CraftCms\Cms\Cp;

use craft\services\Path;
use CraftCms\Cms\Image\ImageHelper;
use CraftCms\Cms\Support\Str;
use Illuminate\Support\Facades\Storage;
Expand All @@ -16,10 +15,6 @@ class Rebrand
*/
private array $_imageVariables = [];

public function __construct(
public Path $path
) {}

public function getImage(string $type)
{
if (! in_array($type, ['icon', 'logo'], true)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Filesystem/Filesystems/Temp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CraftCms\Cms\Filesystem\Filesystems;

use Craft;
use CraftCms\Cms\Support\Facades\Path;

use function CraftCms\Cms\t;

Expand All @@ -25,7 +25,7 @@ public static function displayName(): string
public function __construct($config = [])
{
// Config normalization
$config['path'] ??= Craft::$app->getPath()->getTempAssetUploadsPath();
$config['path'] ??= Path::tempAssetUploads();
$config['name'] ??= t('Temporary Uploads');

parent::__construct($config);
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Controllers/Assets/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use CraftCms\Cms\Asset\Folders;
use CraftCms\Cms\Database\Table;
use CraftCms\Cms\Http\RespondsWithFlash;
use CraftCms\Cms\Support\Facades\Path;
use CraftCms\Cms\Support\Query;
use CraftCms\Cms\Support\Str;
use Illuminate\Http\JsonResponse;
Expand Down Expand Up @@ -158,7 +159,7 @@ public function downloadAsset(Request $request): Response
}

// Otherwise create a zip of all the selected assets
$zipPath = Craft::$app->getPath()->getTempPath().'/'.Str::uuid()->toString().'.zip';
$zipPath = Path::temp(Str::uuid()->toString().'.zip');
$zip = new ZipArchive;

abort_if($zip->open($zipPath, ZipArchive::CREATE) !== true, 500, 'Cannot create zip at '.$zipPath);
Expand Down
10 changes: 4 additions & 6 deletions src/Http/Controllers/Assets/TransformController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace CraftCms\Cms\Http\Controllers\Assets;

use Craft;
use craft\helpers\FileHelper;
use CraftCms\Aliases\Aliases;
use CraftCms\Cms\Asset\Elements\Asset;
Expand All @@ -13,6 +12,7 @@
use CraftCms\Cms\Image\Data\ImageTransform;
use CraftCms\Cms\Image\ImageTransformer;
use CraftCms\Cms\Image\ImageTransformHelper;
use CraftCms\Cms\Support\Facades\Path;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Filesystem\LocalFilesystemAdapter;
use Illuminate\Http\JsonResponse;
Expand All @@ -22,6 +22,8 @@
use Symfony\Component\HttpKernel\Exception\HttpException;
use Throwable;

use function Illuminate\Filesystem\join_paths;

final readonly class TransformController
{
use EnforcesPermissions;
Expand Down Expand Up @@ -112,11 +114,7 @@ public function generateFallback(Request $request): Response
}

$filename = sprintf('%s.%s', $asset->id, $ext);
$path = implode(DIRECTORY_SEPARATOR, [
Craft::$app->getPath()->getImageTransformsPath(),
$transformString,
$filename,
]);
$path = Path::imageTransforms(join_paths($transformString, $filename));

if (! file_exists($path) || filemtime($path) < ($asset->dateModified?->getTimestamp() ?? 0)) {
if ($useOriginal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use CraftCms\Cms\ProjectConfig\ProjectConfig;
use CraftCms\Cms\Support\Api;
use CraftCms\Cms\Support\Composer;
use CraftCms\Cms\Support\Facades\Path;
use CraftCms\Cms\Support\Facades\Security;
use CraftCms\Cms\Support\Facades\Sites;
use CraftCms\Cms\Support\Str;
Expand Down Expand Up @@ -201,7 +202,7 @@ public function createZip(
bool $attachTemplates,
?UploadedFile $attachment
): array {
$zipPath = Craft::$app->getPath()->getTempPath().'/'.Str::uuid()->toString().'.zip';
$zipPath = Path::temp(Str::uuid()->toString().'.zip');
$message = '';

// Create the zip
Expand Down Expand Up @@ -232,15 +233,15 @@ public function createZip(
$zip->addFromString('project.yaml', Yaml::dump($projectConfig, 20, 2));

// project.yaml backups
$configBackupPath = Craft::$app->getPath()->getConfigBackupPath(false);
$configBackupPath = Path::configBackup(create: false);
$zip->addGlob($configBackupPath.'/*', 0, [
'remove_all_path' => true,
'add_path' => 'config-backups/',
]);

// Logs
if ($attachLogs) {
$logPath = Craft::$app->getPath()->getLogPath();
$logPath = Path::logs();
FileHelper::addFilesToZip($zip, $logPath, 'logs', [
'only' => ['*.log'],
'except' => ['web-404s.log'],
Expand All @@ -263,7 +264,7 @@ public function createZip(

// Templates
if ($attachTemplates) {
$templatesPath = Craft::$app->getPath()->getSiteTemplatesPath();
$templatesPath = Path::siteTemplates();
FileHelper::addFilesToZip($zip, $templatesPath, 'templates');
}

Expand Down
6 changes: 3 additions & 3 deletions src/Http/Middleware/CheckForUpdates.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace CraftCms\Cms\Http\Middleware;

use Closure;
use Craft;
use CraftCms\Cms\Cms;
use CraftCms\Cms\Support\Arr;
use CraftCms\Cms\Support\Facades\Path;
use CraftCms\Cms\Updates\Updates;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
Expand Down Expand Up @@ -36,7 +36,7 @@ public function handle(Request $request, Closure $next): mixed
if ($this->updates->hasCraftVersionChanged()) {
$this->updates->updateCraftVersionInfo();

if (! File::cleanDirectory(Craft::$app->getPath()->getCompiledTemplatesPath(false))) {
if (! File::cleanDirectory(Path::compiledTemplates(create: false))) {
Log::error('Could not delete compiled templates');
}
}
Expand All @@ -58,7 +58,7 @@ private function processUpdate(Request $request, Closure $next): mixed
]));
}

File::cleanDirectory(Craft::$app->getPath()->getCompiledTemplatesPath(false));
File::cleanDirectory(Path::compiledTemplates(create: false));

return response()->view('_special/dbupdate');
}
Expand Down
8 changes: 4 additions & 4 deletions src/Image/ImageTransformHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace CraftCms\Cms\Image;

use Craft;
use craft\helpers\Assets;
use craft\helpers\FileHelper;
use CraftCms\Cms\Asset\Elements\Asset;
Expand All @@ -17,6 +16,7 @@
use CraftCms\Cms\Filesystem\Exceptions\FsObjectNotFoundException;
use CraftCms\Cms\Image\Data\ImageTransform;
use CraftCms\Cms\Support\Arr;
use CraftCms\Cms\Support\Facades\Path;
use CraftCms\Cms\Support\Str;
use CraftCms\Cms\Validation\Rules\ColorRule;
use Illuminate\Filesystem\LocalFilesystemAdapter;
Expand Down Expand Up @@ -145,8 +145,8 @@ public static function getLocalImageSource(Asset $asset): string
$prefix = pathinfo($asset->getFilename(), PATHINFO_FILENAME).'.delimiter.';
$extension = $asset->getExtension();
$tempFilename = uniqid($prefix, true).'.'.$extension;
$tempPath = Craft::$app->getPath()->getTempPath();
$tempFilePath = $tempPath.DIRECTORY_SEPARATOR.$tempFilename;
$tempPath = Path::temp();
$tempFilePath = Path::temp($tempFilename);

// Fetch a list of existing temp files for this image.
$files = FileHelper::findFiles($tempPath, [
Expand Down Expand Up @@ -446,7 +446,7 @@ public static function generateTransform(
// It's important that the temp filename has the target file extension, as CraftCms\Cms\Image\Raster::saveAs() uses it
// to determine the options that should be passed to Imagine\Image\ManipulatorInterface::save().
$tempFilename = FileHelper::uniqueName(sprintf('%s.%s', $asset->getFilename(false), $format));
$tempPath = Craft::$app->getPath()->getTempPath().DIRECTORY_SEPARATOR.$tempFilename;
$tempPath = Path::temp($tempFilename);
$image->saveAs($tempPath);
clearstatcache(true, $tempPath);

Expand Down
5 changes: 3 additions & 2 deletions src/Image/ImageTransforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use CraftCms\Cms\ProjectConfig\Events\ConfigEvent;
use CraftCms\Cms\ProjectConfig\ProjectConfig;
use CraftCms\Cms\Support\Arr;
use CraftCms\Cms\Support\Facades\Path;
use CraftCms\Cms\Support\Query;
use CraftCms\Cms\Support\Str;
use DateTime;
Expand Down Expand Up @@ -322,7 +323,7 @@ public function deleteAllTransformData(Asset $asset): void
$this->deleteResizedAssetVersion($asset);
$this->deleteCreatedTransformsForAsset($asset);

$file = Craft::$app->getPath()->getAssetSourcesPath().DIRECTORY_SEPARATOR.$asset->id.'.'.pathinfo($asset->getFilename(), PATHINFO_EXTENSION);
$file = Path::assetSources($asset->id.'.'.pathinfo($asset->getFilename(), PATHINFO_EXTENSION));

if (file_exists($file)) {
FileHelper::unlink($file);
Expand All @@ -332,7 +333,7 @@ public function deleteAllTransformData(Asset $asset): void
public function deleteResizedAssetVersion(Asset $asset): void
{
$dirs = [
Craft::$app->getPath()->getImageEditorSourcesPath().'/'.$asset->id,
Path::imageEditorSources((string) $asset->id),
];

foreach ($dirs as $dir) {
Expand Down
8 changes: 6 additions & 2 deletions src/ProjectConfig/Commands/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Console\Command;
use Symfony\Component\Yaml\Yaml;

use function Illuminate\Filesystem\join_paths;
use function Laravel\Prompts\confirm;

/**
Expand Down Expand Up @@ -49,7 +50,7 @@ public function handle(ProjectConfig $projectConfig): int
if ($path !== null) {
// Prefix with the working directory if a relative path or no path is given
if (str_starts_with($path, '.') || ! str_contains(FileHelper::normalizePath($path, '/'), '/')) {
$path = getcwd().DIRECTORY_SEPARATOR.$path;
$path = join_paths(getcwd(), $path);
}

$path = FileHelper::normalizePath($path);
Expand All @@ -59,8 +60,11 @@ public function handle(ProjectConfig $projectConfig): int

if (is_dir($path)) {
$i = 0;
$scope = $this->option('external') ? 'external' : 'internal';
$date = date('Y-m-d');
do {
$testPath = $path.DIRECTORY_SEPARATOR.'project-config-'.($this->option('external') ? 'external' : 'internal').'--'.date('Y-m-d').($i ? "--$i" : '').'.yaml';
$suffix = $i ? "--$i" : '';
$testPath = join_paths($path, "project-config-$scope--$date$suffix.yaml");
$i++;
} while (file_exists($testPath));
$path = $testPath;
Expand Down
Loading
Loading