-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathBladeGettextExtractor.php
More file actions
86 lines (73 loc) · 2.72 KB
/
BladeGettextExtractor.php
File metadata and controls
86 lines (73 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace WP_CLI\I18n;
use eftec\bladeone\BladeOne;
// Modified Gettext Blade extractor that
// uses the up-to-date BladeOne standalone Blade engine,
// correctly supports fromStringMultiple.
/**
* Class to get gettext strings from blade.php files returning arrays.
*/
class BladeGettextExtractor extends \Gettext\Extractors\PhpCode {
/**
* Prepares a Blade compiler/engine and returns it.
*
* @return BladeOne
*/
protected static function getBladeCompiler() {
$cache_path = empty( $options['cachePath'] ) ? sys_get_temp_dir() : $options['cachePath'];
$blade_compiler = new BladeOne( null, $cache_path );
if ( method_exists( $blade_compiler, 'withoutComponentTags' ) ) {
$blade_compiler->withoutComponentTags();
}
return $blade_compiler;
}
/**
* Compiles the Blade template string into a PHP string in one step.
*
* @param string $text Blade string to be compiled to a PHP string
* @return string
*/
protected static function compileBladeToPhp( $text ) {
return static::getBladeCompiler()->compileString( $text );
}
/**
* Extracts PHP expressions from Blade component prop bindings.
*
* BladeOne does not compile <x-component> tags, so bound prop
* expressions like :prop="__('text', 'domain')" are left as-is
* and invisible to the PHP scanner. This method extracts those
* expressions and returns them as PHP code so that any gettext
* function calls within them can be detected.
*
* @param string $text Blade template string.
* @return string PHP code containing the extracted expressions.
*/
protected static function extractComponentPropExpressions( $text ) {
$php = '';
// Match opening (and self-closing) Blade component tags: <x-name ...> or <x-name ... />
// The attribute region handles quoted strings so that a '>' inside an
// attribute value does not end the match prematurely.
if ( ! preg_match_all( '/<x[-:\w]+\s+((?:[^>"\']*(?:"[^"]*"|\'[^\']*\'))*[^>"]*)\/?>/', $text, $tag_matches ) ) {
return $php;
}
foreach ( $tag_matches[1] as $attributes ) {
// Find :prop="expression" or :prop='expression' bound attributes.
if ( preg_match_all( '/(?<!\w):[\w.-]+=(["\'])(.*?)\1/s', $attributes, $attr_matches ) ) {
foreach ( $attr_matches[2] as $expression ) {
$php .= '<?php ' . $expression . '; ?>';
}
}
}
return $php;
}
/**
* {@inheritdoc}
*
* Note: In the parent PhpCode class fromString() uses fromStringMultiple() (overridden here)
*/
public static function fromStringMultiple( $text, array $translations, array $options = [] ) {
$php_string = static::compileBladeToPhp( $text );
$php_string .= static::extractComponentPropExpressions( $text );
return parent::fromStringMultiple( $php_string, $translations, $options );
}
}