mulertech / file-manipulation
此类用于操作和组织文件及路径
Requires
- php: ^8.3
Requires (Dev)
- phpunit/phpunit: ^10
This package is auto-updated.
Last update: 2024-09-29 18:00:30 UTC
README
此类用于操作文件和路径
安装
使用composer安装FileManipulation包的两种方法
将以下内容添加到您的"composer.json"文件的require部分
"mulertech/file-manipulation": "^1.0"
并运行以下命令
php composer.phar update
运行以下命令
php composer.phar require mulertech/file-manipulation "^1.0"
用法
打开env文件:(返回文件内容)
$envFile = new Env('path/to/envFile');
$content = $envFile->open();
// key1=value1
解析env文件:(返回文件解析后的内容)
$envFile = new Env('path/to/envFile');
$content = $envFile->parseFile();
// ['key' => 'value', 'key2' => 'value2']
加载env文件:(将env文件加载到环境变量中)
$envFile = new Env('path/to/envFile');
$content = $envFile->parseFile();
打开json文件:(返回文件内容)
$jsonFile = new Json('path/to/file.json');
$content = $jsonFile->open();
// ['key' => 'value', 'key2' => 'value2']
打开php文件:(返回文件内容)
$phpFile = new Php('path/to/file.php');
$content = $phpFile->open();
// ['key' => 'value', 'key2' => 'value2']
打开yaml文件:(返回文件内容)
$yamlFile = new Yaml('path/to/file.yaml'); // or .yml
$content = $yamlFile->open();
// ['key' => 'value', 'key2' => 'value2']
打开其他文件:(返回文件内容)
$otherFile = new FileManipulation('path/to/file.other');
$content = $otherFile->open();
// ['key' => 'value', 'key2' => 'value2']
保存env/json/php/yaml文件
$envFile = new Env('path/to/envFile');
$content = $envFile->saveFile('content to save');
保存其他文件
$otherFile = new FileManipulation('path/to/file.other');
$content = $otherFile->saveFile('content to save');
Php文件类名
$phpFile = new Php('path/to/file.php');
$className = $phpFile->getClassName();
// ClassName
Php获取类名
$phpFile = new Php('path/to/file.php');
$classNames = $phpFile->getClassNames();
// ['ClassName', 'ClassName2']
Php获取名为"Attribute::class"的类属性
$phpFile = new Php('path/to/file.php');
$attribute = Php::getClassAttributeNamed(Class::class, Attribute::class);
// 返回Attribute::class
的ReflectionAttribute
Php获取名为"Attribute::class"的类属性实例
$phpFile = new Php('path/to/file.php');
$attribute = Php::getInstanceOfClassAttributeNamed(Class::class, Attribute::class);
// 返回Attribute::class
的实例
Php获取属性属性
$phpFile = new Php('path/to/file.php');
$propertiesAttributes = Php::getPropertiesAttributes(Class::class);
// 返回属性ReflectionProperty的数组
Php获取名为"Attribute::class"的属性属性实例
$phpFile = new Php('path/to/file.php');
$propertiesAttributes = Php::getInstanceOfPropertiesAttributesNamed(Class::class, Attribute::class);
// 返回属性名 => Attribute::class
实例的数组
Php获取方法属性
$phpFile = new Php('path/to/file.php');
$methodsAttributes = Php::getMethodsAttributes(Class::class);
// 返回方法ReflectionMethod的数组
Php获取名为"Attribute::class"的方法属性实例
$phpFile = new Php('path/to/file.php');
$methodsAttributes = Php::getInstanceOfMethodsAttributesNamed(Class::class, Attribute::class);
// 返回方法名 => Attribute::class
实例的数组
获取文件中字符串的第一个出现位置
$file = new FileManipulation('path/to/file'); // if for example php file : new Php('path/to/file.php')
$firstOccurrence = $file->getFirstOccurrence('string');
// 返回第一个出现位置的行号(int)
获取文件中字符串的最后一个出现位置
$file = new FileManipulation('path/to/file'); // if for example php file : new Php('path/to/file.php')
$lastOccurrence = $file->getLastOccurrence('string');
// 返回最后一个出现位置的行号(int)
获取行号
$file = new FileManipulation('path/to/file'); // if for example php file : new Php('path/to/file.php')
$lineNumber = $file->getLine(number);
// 返回行的内容(string)
转换文件
$jsonFile = new Json('path/to/file.json');
$yamlFile = new Yaml('path/to/file.yaml');
$jsonFile->convertFile($yamlFile);
// 将json文件转换为yaml文件
计算行数
$file = new FileManipulation('path/to/file'); // if for example php file : new Php('path/to/file.php')
$lines = $file->countLines();
// 返回文件行数(int)
在行号处插入内容
$file = new FileManipulation('path/to/file'); // if for example php file : new Php('path/to/file.php')
$file->insertContentAtLineNumber('content', 2);
// 在行号2处插入内容(一行或多行),并移动后面的行
日期路径
$dateStorage = new DateStorage('path');
$datePath = $dateStorage->datePath();
// 返回路径/year/month的路径(例如:path/2022/02)
日期文件名
DateStorage::dateFilename('suffix');
// 返回带有日期的文件名(例如:20220201-suffix)
日期时间文件名
DateStorage::dateTimeFilename('suffix');
// 返回带有日期和时间的文件名(例如:20220201-1200-suffix)