CRUD (Add, modify, list, delete) of elements from arrays in php 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)
<?php $editor->where('aliases',[], Editor::TYPE_KV_PAIR) ->insert("'JWTAuth' => Tymon\\JWTAuth\\Facades\\JWTAuth::class".PHP_EOL) ->save()->flush();
composer require jetwaves/edit-array-in-file
<?php use Jetwaves\EditArrayInFile\Editor;
or, (*3)
<?php require('EditArrayInFileEditor.php');
<?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)
<?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)