ibrostudio / laravel-file-data-manager
Laravel package to manage files data
v1.0.0
2022-06-19 05:41 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-10-09 04:07:36 UTC
README
Little tool to read, change or add data in files.
Currently, it works with:
- .json files
- .php files
- .js files
Installation
You can install the package via composer:
composer require ibrostudio/laravel-file-data-manager
Usage
There is 3 types of chainable methods, callable in this order: load > manipulation > result
- Load
- load(string $file)
- Manipulation
- findValue(string $key)
- findArray(string $key)
- findRegex(string $regex)
- replaceValue(string $key, mixed $value)
- addArrayValue(string $key, mixed $value)
- addRegexValue(string $regex, string $value)
- Result
- getContent()
- getValue()
- write()
Examples
- Working with values
use IBroStudio\FileDataManager\FileDataManager; $package_name = FileDataManager::load(base_path('composer.json')) ->findValue('name') ->getValue(); // = 'vendor/currentPackageName' $package_name = FileDataManager::load(__DIR__ . '/Fixtures/Test.php') ->replaceValue('name', 'vendor/newPackageName') ->findValue('name') ->getValue(); // = 'vendor/newPackageName'
- Working with arrays
Test.php
class Test { protected array $testArray = [ SomeClass1::class, SomeClass2::class, ]; }
use IBroStudio\FileDataManager\FileDataManager; $test = FileDataManager::load('Test.php'); $test ->findArray('$testArray') ->getValue(); // = ['SomeClass1::class', 'SomeClass2::class'] $test ->addArrayValue('$testArray', 'SomeClass3::class') ->write(); $test ->findArray('$testArray') ->getValue(); // = ['SomeClass1::class', 'SomeClass2::class', 'SomeClass3::class']
- Working with regex
Test.php
use Vendor\Package\Namespace\Class1; class Test{}
use IBroStudio\FileDataManager\FileDataManager; FileDataManager::load('Test.php') ->addRegexValue('#(use\s(.*?)\;)#s', 'use Vendor\Package\Namespace\Class2;') ->write(); $imports = FileDataManager::load('Test.php') ->findRegex('#(use\s(.*?)\;)#s') ->getValue(); // = ['use Vendor\Package\Namespace\Class1;', 'use Vendor\Package\Namespace\Class2;']
It is possible to chain manipulations methods:
use IBroStudio\FileDataManager\FileDataManager; FileDataManager::load('Test.php') ->replaceValue('$testValue', 'tata') ->addArrayValue('$testArray1', "'NewValue'") ->addArrayValue('$testArray2', 'OtherNewValue::class') ->addRegexValue('#(use\s(.*?)\;)#s', 'use Vendor\Package\Namespace\Class2;') ->write();
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.