mulertech / file-manipulation
This class manipulates and organizes files and path
Requires
- php: ^8.3
- symfony/yaml: ^7
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.6
- mulertech/docker-tests: ^1
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2025-02-21 17:56:42 UTC
README
This package manipulates files and paths
Installation
Two methods to install FileManipulation package with composer :
Add to your "composer.json" file into require section :
"mulertech/file-manipulation": "^1.0"
and run the command :
php composer.phar update
Run the command :
php composer.phar require mulertech/file-manipulation "^1.0"
Usage
Open env file : (return content of file)
$envFile = new Env('path/to/envFile');
$content = $envFile->open();
// key1=value1
Parse env file : (return parsed content of file)
$envFile = new Env('path/to/envFile');
$content = $envFile->parseFile();
// ['key' => 'value', 'key2' => 'value2']
Load env file : (load env file in environment variables)
$envFile = new Env('path/to/envFile');
$content = $envFile->parseFile();
Open json file : (return content of file)
$jsonFile = new Json('path/to/file.json');
$content = $jsonFile->open();
// ['key' => 'value', 'key2' => 'value2']
Open php file : (return content of file)
$phpFile = new Php('path/to/file.php');
$content = $phpFile->open();
// ['key' => 'value', 'key2' => 'value2']
Open yaml file : (return content of file)
$yamlFile = new Yaml('path/to/file.yaml'); // or .yml
$content = $yamlFile->open();
// ['key' => 'value', 'key2' => 'value2']
Open other file : (return content of file)
$otherFile = new FileManipulation('path/to/file.other');
$content = $otherFile->open();
// ['key' => 'value', 'key2' => 'value2']
Save env/json/php/yaml file :
$envFile = new Env('path/to/envFile');
$content = $envFile->saveFile('content to save');
Save other file :
$otherFile = new FileManipulation('path/to/file.other');
$content = $otherFile->saveFile('content to save');
Php file class name :
$phpFile = new Php('path/to/file.php');
$className = $phpFile->getClassName();
// ClassName
Php get class names :
$phpFile = new Php('path/to/file.php');
$classNames = $phpFile->getClassNames();
// ['ClassName', 'ClassName2']
Php get class attribute named "Attribute::class" :
$phpFile = new Php('path/to/file.php');
$attribute = Php::getClassAttributeNamed(Class::class, Attribute::class);
// return ReflectionAttribute of Attribute::class
Php get instance of class attribute named "Attribute::class" :
$phpFile = new Php('path/to/file.php');
$attribute = Php::getInstanceOfClassAttributeNamed(Class::class, Attribute::class);
// return instance of Attribute::class
Php get properties attributes :
$phpFile = new Php('path/to/file.php');
$propertiesAttributes = Php::getPropertiesAttributes(Class::class);
// return array of ReflectionProperty of properties
Php get instance of properties attributes named "Attribute::class" :
$phpFile = new Php('path/to/file.php');
$propertiesAttributes = Php::getInstanceOfPropertiesAttributesNamed(Class::class, Attribute::class);
// return array of property name => instances of Attribute::class
Php get methods attributes :
$phpFile = new Php('path/to/file.php');
$methodsAttributes = Php::getMethodsAttributes(Class::class);
// return array of ReflectionMethod of methods
Php get instance of methods attributes named "Attribute::class" :
$phpFile = new Php('path/to/file.php');
$methodsAttributes = Php::getInstanceOfMethodsAttributesNamed(Class::class, Attribute::class);
// return array of method name => instances of Attribute::class
Get first occurrence of a string in a file :
$file = new FileManipulation('path/to/file'); // if for example php file : new Php('path/to/file.php')
$firstOccurrence = $file->getFirstOccurrence('string');
// return line of first occurrence (int)
Get last occurrence of a string in a file :
$file = new FileManipulation('path/to/file'); // if for example php file : new Php('path/to/file.php')
$lastOccurrence = $file->getLastOccurrence('string');
// return line of last occurrence (int)
Get line number :
$file = new FileManipulation('path/to/file'); // if for example php file : new Php('path/to/file.php')
$lineNumber = $file->getLine(number);
// return content of line (string)
Convert file :
$jsonFile = new Json('path/to/file.json');
$yamlFile = new Yaml('path/to/file.yaml');
$jsonFile->convertFile($yamlFile);
// convert json file to yaml file
Count lines :
$file = new FileManipulation('path/to/file'); // if for example php file : new Php('path/to/file.php')
$lines = $file->countLines();
// return number of lines of file (int)
Insert content at line number :
$file = new FileManipulation('path/to/file'); // if for example php file : new Php('path/to/file.php')
$file->insertContentAtLineNumber('content', 2);
// insert content (one or more lines) at line number 2 and move other lines after
Date path :
$dateStorage = new DateStorage('path');
$datePath = $dateStorage->datePath();
// return path of path/year/month (example : path/2022/02)
Date filename :
DateStorage::dateFilename('suffix');
// return filename with date (example : 20220201-suffix)
Date time filename :
DateStorage::dateTimeFilename('suffix');
// return filename with date and time (example : 20220201-1200-suffix)