2017 © Pedro Peláez
 

sdk edit-array-in-file

CRUD (Add, modify, list, delete) of elements from arrays in php file.

image

jetwaves/edit-array-in-file

CRUD (Add, modify, list, delete) of elements from arrays in php file.

  • Saturday, February 17, 2018
  • by jetwaves
  • Repository
  • 1 Watchers
  • 1 Stars
  • 37 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 9 % Grown

The README.md

edit-array-in-file

CRUD (create/add/insert, modify/update, list/find/search, delete/remove) of elements from arrays in php file., (*1)


CREDIT TO dingo/api and Laravel framework, (*2)

Idea: use ORM-like code to manipulate file content

<?php
$editor->where('aliases',[], Editor::TYPE_KV_PAIR)
       ->insert("'JWTAuth' => Tymon\\JWTAuth\\Facades\\JWTAuth::class".PHP_EOL)
       ->save()->flush();

How to use:

Installation: (eg. in a Laravel 5+ project)

composer require jetwaves/edit-array-in-file

API:

  • $editor->where($arrayName, $options = [], $type = self::TYPE_VARIABLE) : Find the code snippet(an array)
  • $editor->find($keyword, $comp = null, $type = self::FIND_TYPE_KEY_ONLY) : Find a line in the array above
  • $editor->insert($data, $insertType = self::INSERT_TYPE_RAW ) : Insert a new line into the array
  • $editor->delete() : Delete the line found by find()
  • $editor->save() : Reconstruct the file lines in memory
  • $editor->flush() : Write the edited file content into file

Example:

include the lib:

<?php
    use Jetwaves\EditArrayInFile\Editor;

or, (*3)

<?php
    require('EditArrayInFileEditor.php');

When the target is an array in the value of a Key-Value pair)

<?php
    $editor = new Editor('testSource/app.php');

1.1.CRUD->R : Find target key in a php source code file:, (*4)

<?php
    $targetArray = editor->where('aliases',[], Editor::TYPE_KV_PAIR)->get();
    echo '     '.method.'() line:'.line.'   targetArray  = '.print_r(targetArray, true);

the result is:, (*5)

     () line:10   $targetArray  = Array
(
    [0] =>     'aliases' => [
// ATTENTION:  there is PHP_EOL in the end of these lines who are invisible.
    [1] =>

    [2] =>         'App' => Illuminate\Support\Facades\App::class,

    [3] =>         'Artisan' => Illuminate\Support\Facades\Artisan::class,

    [4] =>

    [5] =>     ],

)

1.2.CRUD->C : Insert into target key's value in a php source code file:, (*6)

<?php
    $editor->where('aliases',[], Editor::TYPE_KV_PAIR);
    $editor->insert("'JWTAuth' => Tymon\\JWTAuth\\Facades\\JWTAuth::class".PHP_EOL);
    $editor->save()->flush();

the result is : (content of file app.php), (*7)

<?php
return [
    'log' => env('APP_LOG', 'single'),
    'log_level' => env('APP_LOG_LEVEL', 'debug'),
    'providers' => [
        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
    ],
    'aliases' => [
        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class     // <====== HERE IS THE INSERTED LINE
    ],
];

1.3. CRUD->R : Search in target variable for a key :, (*8)

    <?php
    $val = $editor->where('aliases',[], Editor::TYPE_KV_PAIR)
                  ->find('App', Editor::FIND_TYPE_KEY_ONLY);

the result is :, (*9)

    target key `s val  = Illuminate\Support\Facades\App::class

1.4. CRUD->D : delete one line in target Key-Value pair, (*10)

<?php
$editor->where('aliases',[], Editor::TYPE_KV_PAIR)
       ->find('JWTAuth', Editor::FIND_TYPE_ALL);
$editor->delete()->save()->flush();

It will delete the line 'JWTAuth' from file Kernel.php, (*11)

When the target is an array in a variable)

<?php
    $editor = new Editor('testSource/Kernel.php');

2.1.CRUD->R : Find target variable in a php source code file:, (*12)

<?php
    $targetArray = $editor->where('$routeMiddleware',[], Editor::TYPE_VARIABLE)->get();
    echo '     '.__method__.'() line:'.__line__.'   $targetArray  = '.print_r($targetArray, true);

the result is:, (*13)

     () line:26   $targetArray  = Array
(
    [0] =>     protected $routeMiddleware = [
// ATTENTION:  there is PHP_EOL in the end of these lines who are invisible.
    [1] =>         'auth' => \Illuminate\Auth\Middleware\Authenticate::class,

    [2] =>         'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,

    [3] =>         'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,

    [4] =>         'can' => \Illuminate\Auth\Middleware\Authorize::class,

    [5] =>         'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

    [6] =>         'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

    [7] =>         'jwt.auth' => \App\Http\Middleware\VerifyJWTToken::class

    [8] =>     ];

)

2.2.CRUD->C : Insert into target key's value in a php source code file:, (*14)

<?php
    $editor->where('$routeMiddleware',[], Editor::TYPE_VARIABLE);
    $editor->insert("'jetwaves.auth' => \\App\\Ssq\\TestMiddleware\\VerifyJWTToken::class".PHP_EOL);
    $editor->save()->flush();

the result is : (content of file Kernel.php), (*15)

<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     * These middleware may be assigned to groups or used individually.
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'jwt.auth' => \App\Http\Middleware\VerifyJWTToken::class,
        'jetwaves.auth' => \App\Ssq\TestMiddleware\VerifyJWTToken::class  // <====== HERE IS THE INSERTED LINE
    ];
}

2.3. CRUD->R : Search in target variable for a key :, (*16)

<?php
$editor->where('$routeMiddleware',[], Editor::TYPE_VARIABLE)
       ->find('bindings', Editor::FIND_TYPE_KEY_ONLY);

the result is :, (*17)

target key `s val  = \Illuminate\Routing\Middleware\SubstituteBindings::class

2.4. CRUD->D : delete one line in target variable, (*18)

<?php
    $editor->where('$routeMiddleware',[], Editor::TYPE_VARIABLE)
            ->find('auth.basic', Editor::FIND_TYPE_ALL)
            ->delete()->save()->flush();

It will delete the line 'auth.basic' from file Kernel.php, (*19)

The Versions

17/02 2018

dev-master

9999999-dev

CRUD (Add, modify, list, delete) of elements from arrays in php file.

  Sources   Download

MIT

by Avatar jetwaves

17/02 2018

v1.1.4

1.1.4.0

CRUD (Add, modify, list, delete) of elements from arrays in php file.

  Sources   Download

MIT

by Avatar jetwaves

15/02 2018

v1.1.3

1.1.3.0

CRUD (Add, modify, list, delete) of elements from arrays in php file.

  Sources   Download

MIT

by Avatar jetwaves

14/02 2018

v1.1.2

1.1.2.0

CRUD (Add, modify, list, delete) of elements from arrays in php file.

  Sources   Download

MIT

by Avatar jetwaves

14/02 2018

v1.1.1

1.1.1.0

CRUD (Add, modify, list, delete) of elements from arrays in php file.

  Sources   Download

MIT

by Avatar jetwaves

13/02 2018

v1.1.0

1.1.0.0

CRUD (Add, modify, list, delete) of elements from arrays in php file.

  Sources   Download

MIT

by Avatar jetwaves

27/01 2018

1.0.0

1.0.0.0

CRUD (Add, modify, list, delete) of elements from arrays in php file.

  Sources   Download

MIT

by Avatar jetwaves