Skip to content

Commit 19e971e

Browse files
committed
add a function insertIntoFile() (locate with an anchor and an offset)
1 parent 0357c2c commit 19e971e

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/EditArrayInFileEditor.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,5 +343,41 @@ public static function endsWith($haystack, $needles)
343343
return false;
344344
}
345345

346+
/*
347+
* Insert content (array of string or one string) into the selected file located by an anchor string and an offset value
348+
*
349+
* ATTENTION: the EOL might be treated as a new line. So try avoid using offset value > 0
350+
*
351+
* */
352+
public static function insertIntoFile($filename, $anchorString, $offset=0, $content)
353+
{
354+
if(!file_exists($filename)){
355+
throw new \Exception('Target File '.$filename.' does not exists');
356+
}
357+
$fileLines = file($filename);
358+
$resLines = [];
359+
$foundAnchor = false;
360+
foreach ($fileLines as $line) {
361+
if(self::contains($line, $anchorString)){
362+
$foundAnchor = true;
363+
// decrease the offset value until 0 to locate the target line
364+
} else {
365+
if($foundAnchor){
366+
$offset = $offset - 1;
367+
}
368+
}
369+
if($offset == 0 && $foundAnchor ){
370+
if(is_array($content)){
371+
$resLines = array_merge($resLines, $content);
372+
} else {
373+
$resLines[] = $content;
374+
}
375+
}
376+
$resLines[] = $line;
377+
}
378+
$resLines = implode('',$resLines);
379+
file_put_contents($filename, $resLines);
380+
}
381+
346382

347383
}

src/Test/tt02_insertIntoFile.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace Jetwaves\EditArrayInFile\Test;
3+
4+
5+
require('../EditArrayInFileEditor.php');
6+
use Jetwaves\EditArrayInFile\Editor as Editor;
7+
8+
9+
//###When the target is an array in the value of a Key-Value pair)
10+
// ======= exemple of insert one line into [aliases] array in file [app.php]
11+
//$editor = new Editor('../testSource/app.php');
12+
13+
try {
14+
$dataToInsert = "use Illuminate\Support\Facades\Schema;".PHP_EOL;
15+
Editor::insertIntoFile('../testSource/AppServiceProvider.php',
16+
"use Illuminate\Support\ServiceProvider;", 1, $dataToInsert);
17+
18+
$dataToInsert = " Schema::defaultStringLength(191);".PHP_EOL;
19+
Editor::insertIntoFile('../testSource/AppServiceProvider.php',
20+
"public function register()", 3, $dataToInsert);
21+
22+
} catch (\Exception $e) {
23+
echo ''.__FILE__.'->'.__method__.'() line:'.__line__.PHP_EOL.' $e->getMessage() = '.print_r($e->getMessage(), true).PHP_EOL;
24+
echo ''.__FILE__.'->'.__method__.'() line:'.__line__.PHP_EOL.' $e->trace = '.print_r($e->getTraceAsString(), true).PHP_EOL;
25+
}
26+
27+
28+
29+
30+
31+

src/testSource/AppServiceProvider.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
//1
7+
//2
8+
//3
9+
10+
11+
class AppServiceProvider extends ServiceProvider
12+
{
13+
/**
14+
* Bootstrap any application services.
15+
*
16+
* @return void
17+
*/
18+
public function boot()
19+
{
20+
//
21+
}
22+
23+
/**
24+
* Register any application services.
25+
*
26+
* @return void
27+
*/
28+
public function register()
29+
{
30+
//a
31+
//b
32+
//1
33+
//2
34+
//3
35+
36+
}
37+
}

0 commit comments

Comments
 (0)