1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\Publisher; |
13
|
|
|
|
14
|
|
|
use RuntimeException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Remplacer le contenu du texte |
18
|
|
|
* |
19
|
|
|
* @credit <a href="http://codeigniter.com">CodeIgniter 4 - \CodeIgniter\Publisher\ContentReplacer</a> |
20
|
|
|
*/ |
21
|
|
|
class ContentReplacer |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Remplacer le contenu |
25
|
|
|
* |
26
|
|
|
* @param array $replaces [search => replace] |
27
|
|
|
*/ |
28
|
|
|
public function replace(string $content, array $replaces): string |
29
|
|
|
{ |
30
|
4 |
|
return strtr($content, $replaces); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Ajouter du texte |
35
|
|
|
* |
36
|
|
|
* @param string $text Texte à ajouter. |
37
|
|
|
* @param string $pattern Modèle de recherche d'expression régulière. |
38
|
|
|
* @param string $replace Remplacement de Regexp incluant le texte à ajouter. |
39
|
|
|
* |
40
|
|
|
* @return string|null Contenu mis à jour, ou null si non mis à jour. |
41
|
|
|
*/ |
42
|
|
|
private function add(string $content, string $text, string $pattern, string $replace): ?string |
43
|
|
|
{ |
44
|
4 |
|
$return = preg_match('/' . preg_quote($text, '/') . '/u', $content); |
45
|
|
|
|
46
|
|
|
if ($return === false) { |
47
|
|
|
// Erreur d'expression régulière. |
48
|
|
|
throw new RuntimeException('Erreur d\'expression régulière. Code d\'erreur PCRE: ' . preg_last_error()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($return === 1) { |
52
|
|
|
// Il a déjà été mis à jour. |
53
|
2 |
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
$return = preg_replace($pattern, $replace, $content); |
57
|
|
|
|
58
|
|
|
if ($return === null) { |
59
|
|
|
// Erreur d'expression régulière. |
60
|
|
|
throw new RuntimeException('Erreur d\'expression régulière. Code d\'erreur PCRE: ' . preg_last_error()); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
return $return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Ajouter une ligne après la ligne avec la chaîne |
68
|
|
|
* |
69
|
|
|
* @param string $content Contenu entier. |
70
|
|
|
* @param string $line Ligne à ajouter. |
71
|
|
|
* @param string $after Chaîne à rechercher. |
72
|
|
|
* |
73
|
|
|
* @return string|null Contenu mis à jour, ou null si non mis à jour. |
74
|
|
|
*/ |
75
|
|
|
public function addAfter(string $content, string $line, string $after): ?string |
76
|
|
|
{ |
77
|
4 |
|
$pattern = '/(.*)(\n[^\n]*?' . preg_quote($after, '/') . '[^\n]*?\n)/su'; |
78
|
4 |
|
$replace = '$1$2' . $line . "\n"; |
79
|
|
|
|
80
|
4 |
|
return $this->add($content, $line, $pattern, $replace); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Ajouter une ligne avant la ligne avec la chaîne |
85
|
|
|
* |
86
|
|
|
* @param string $content Contenu entier. |
87
|
|
|
* @param string $line Ligne à ajouter. |
88
|
|
|
* @param string $before String à rechercher. |
89
|
|
|
* |
90
|
|
|
* @return string|null Contenu mis à jour, ou null si non mis à jour. |
91
|
|
|
*/ |
92
|
|
|
public function addBefore(string $content, string $line, string $before): ?string |
93
|
|
|
{ |
94
|
4 |
|
$pattern = '/(\n)([^\n]*?' . preg_quote($before, '/') . ')(.*)/su'; |
95
|
4 |
|
$replace = '$1' . $line . "\n" . '$2$3'; |
96
|
|
|
|
97
|
4 |
|
return $this->add($content, $line, $pattern, $replace); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|