AgentSkillsCN

symfony-7-4-filesystem

Symfony 7.4 Filesystem组件参考文档,用于文件与目录的操作。在使用文件系统实用工具、执行文件操作、创建目录、复制、删除、修改权限、更改所有者、创建软链接、镜像复制、生成临时文件名、转储文件、追加文件内容、读取文件,或进行路径操作(规范化、拼接、转为绝对路径、转为相对路径)时,均可参考该文档。触发器包括:Filesystem、文件操作、目录创建、复制、删除、修改权限、软链接、Path、转储文件、追加文件内容、读取文件、镜像复制、使路径相对。

SKILL.md
--- frontmatter
name: "symfony-7-4-filesystem"
description: "Symfony 7.4 Filesystem component reference for file and directory operations. Use when working with filesystem utilities, file operations, directory creation, copy, remove, chmod, chown, symlink, mirror, tempnam, dumpFile, appendToFile, readFile, or Path manipulation (canonicalize, join, makeAbsolute, makeRelative). Triggers on: Filesystem, file operations, directory creation, copy, remove, chmod, symlink, Path, dumpFile, appendToFile, readFile, mirror, makePathRelative."

Symfony 7.4 Filesystem Component

GitHub: https://github.com/symfony/filesystem Docs: https://symfony.com/doc/7.4/components/filesystem.html

Quick Reference

Installation

bash
composer require symfony/filesystem

Filesystem Operations

php
use Symfony\Component\Filesystem\Filesystem;

$filesystem = new Filesystem();

// Create directory
$filesystem->mkdir('/tmp/photos', 0700);

// Check existence
$filesystem->exists('/tmp/photos');

// Copy file (use mirror() for directories)
$filesystem->copy('source.jpg', 'dest.jpg', true); // true = force override

// Remove files/directories/symlinks
$filesystem->remove(['symlink', '/path/to/dir', 'file.log']);

// Rename
$filesystem->rename('/tmp/old.txt', '/path/new.txt', true); // true = overwrite

// Symlink
$filesystem->symlink('/path/source', '/path/dest');

// Permissions & ownership
$filesystem->chmod('file.txt', 0600);
$filesystem->chown('file.txt', 'www-data', true); // recursive
$filesystem->chgrp('file.txt', 'nginx', true);

// Read/write files
$contents = $filesystem->readFile('/path/to/file.txt');
$filesystem->dumpFile('file.txt', 'Hello World'); // atomic write
$filesystem->appendToFile('logs.txt', 'New log entry');

// Mirror entire directory
$filesystem->mirror('/path/source', '/path/target');

// Temp file
$tmpFile = $filesystem->tempnam('/tmp', 'prefix_', '.png');

Path Manipulation

php
use Symfony\Component\Filesystem\Path;

// Canonicalize
Path::canonicalize('/var/www/../config.ini');
// => /var/config.ini

// Join paths
Path::join('/var/www', 'vhost', 'config.ini');
// => /var/www/vhost/config.ini

// Absolute/relative conversion
Path::makeAbsolute('config/config.yaml', '/var/www/project');
Path::makeRelative('/var/www/project/config.yaml', '/var/www/project');

// Check path type
Path::isAbsolute('/tmp'); // true
Path::isRelative('tmp'); // true

// Common base path
Path::getLongestCommonBasePath('/var/www/a.txt', '/var/www/b.txt');
// => /var/www

// Directory and root
Path::getDirectory('/var/www/file.txt'); // /var/www
Path::getRoot('/etc/apache2'); // /

Error Handling

php
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

try {
    $filesystem->mkdir('/path/to/dir');
} catch (IOExceptionInterface $exception) {
    echo "Error at ".$exception->getPath();
}

Full Documentation

For complete details on all methods, parameters, edge cases, and the Path utility class, see references/filesystem.md.