CRUD (create/add/insert, modify/update, list/find/search, delete/remove) of elements from arrays in php file.
CREDIT TO dingo/api and Laravel framework
<?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
- $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
<?php
use Jetwaves\EditArrayInFile\Editor;
or
<?php
require('EditArrayInFileEditor.php');
<?php
$editor = new Editor('testSource/app.php');
1.1.CRUD->R : Find target key in a php source code file:
<?php
$targetArray = editor->where('aliases',[], Editor::TYPE_KV_PAIR)->get();
echo ' '.method.'() line:'.line.' targetArray = '.print_r(targetArray, true);
the result is:
() 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:
<?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)
<?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 :
<?php
$val = $editor->where('aliases',[], Editor::TYPE_KV_PAIR)
->find('App', Editor::FIND_TYPE_KEY_ONLY);
the result is :
target key `s val = Illuminate\Support\Facades\App::class
1.4. CRUD->D : delete one line in target Key-Value pair
<?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 ###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:
<?php
$targetArray = $editor->where('$routeMiddleware',[], Editor::TYPE_VARIABLE)->get();
echo ' '.__method__.'() line:'.__line__.' $targetArray = '.print_r($targetArray, true);
the result is:
() 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:
<?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)
<?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 :
<?php
$editor->where('$routeMiddleware',[], Editor::TYPE_VARIABLE)
->find('bindings', Editor::FIND_TYPE_KEY_ONLY);
the result is :
target key `s val = \Illuminate\Routing\Middleware\SubstituteBindings::class
2.4. CRUD->D : delete one line in target variable
<?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