ibrostudio/laravel-file-data-manager

Laravel package to manage files data

v1.0.0 2022-06-19 05:41 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status

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

  1. Load
  • load(string $file)
  1. 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)
  1. 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.