-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotobuf.php
52 lines (45 loc) · 1.48 KB
/
protobuf.php
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
<?php
$php_namespace = 'Waves\\\\Protobuf';
$php_metadata_namespace = 'Waves\\\\ProtobufMetadata';
if( !isset( $argv[1] ) )
$argv[1] = '*.proto';
$endl = "\n";
$trigger_prolog = 'package ';
$trigger_prolog_length = strlen( $trigger_prolog );
$files = rglob( '*.proto' );
foreach( $files as $file )
{
$content = file_get_contents( $file );
if( strpos( $content, 'php_namespace' ) !== false )
continue;
$strings = explode( $endl, $content );
$trigger = 0;
$content = '';
foreach( $strings as $string )
{
if( $trigger === 0 )
{
if( substr( $string, 0, $trigger_prolog_length ) === $trigger_prolog )
$trigger = 1;
}
else
if( $trigger === 1 )
{
$content .= 'option php_namespace = "' . $php_namespace . '";' . $endl;
$content .= 'option php_metadata_namespace = "' .$php_metadata_namespace. '";' . $endl;
$trigger = -1;
echo $file . ' file patched SUCCESS' . $endl;
}
$content .= $string . $endl;
}
file_put_contents( $file, $content );
}
function rglob( $pattern, $flags = 0 )
{
$files = glob( $pattern, $flags );
$dirs = glob( dirname( $pattern ).'/*', GLOB_ONLYDIR|GLOB_NOSORT );
$files = array_diff( $files, $dirs );
foreach( glob( dirname( $pattern ).'/*', GLOB_ONLYDIR|GLOB_NOSORT ) as $dir )
$files = array_merge( $files, rglob( $dir . '/' . basename( $pattern ), $flags ) );
return $files;
}